2021-10-18 20:46:57 +00:00
|
|
|
from os import environ
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
from flask import render_template
|
2021-07-21 01:12:26 +00:00
|
|
|
from sqlalchemy import *
|
2021-10-05 19:12:31 +00:00
|
|
|
from sqlalchemy.orm import relationship, deferred
|
2021-10-18 20:46:57 +00:00
|
|
|
|
2021-10-18 01:26:30 +00:00
|
|
|
from files.__main__ import Base
|
2021-10-20 14:37:46 +00:00
|
|
|
from files.helpers.const import AUTOPOLLER_ACCOUNT, censor_slurs
|
2021-10-18 20:46:57 +00:00
|
|
|
from files.helpers.lazy import lazy
|
2021-10-05 23:11:17 +00:00
|
|
|
from .flags import Flag
|
2021-08-02 14:27:20 +00:00
|
|
|
|
2021-08-05 14:41:32 +00:00
|
|
|
site = environ.get("DOMAIN").strip()
|
2021-08-19 05:40:23 +00:00
|
|
|
site_name = environ.get("SITE_NAME").strip()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-10-05 19:15:48 +00:00
|
|
|
|
2021-09-19 18:22:57 +00:00
|
|
|
class Submission(Base):
|
2021-07-21 01:12:26 +00:00
|
|
|
__tablename__ = "submissions"
|
|
|
|
|
|
|
|
id = Column(BigInteger, primary_key=True)
|
|
|
|
author_id = Column(BigInteger, ForeignKey("users.id"))
|
|
|
|
edited_utc = Column(BigInteger, default=0)
|
|
|
|
created_utc = Column(BigInteger, default=0)
|
2021-10-06 22:38:15 +00:00
|
|
|
thumburl = Column(String)
|
2021-07-21 01:12:26 +00:00
|
|
|
is_banned = Column(Boolean, default=False)
|
2021-09-10 05:36:31 +00:00
|
|
|
removed_by = Column(Integer)
|
2021-08-11 22:28:11 +00:00
|
|
|
bannedfor = Column(Boolean)
|
2021-07-21 01:12:26 +00:00
|
|
|
views = Column(Integer, default=0)
|
|
|
|
deleted_utc = Column(Integer, default=0)
|
|
|
|
distinguish_level = Column(Integer, default=0)
|
2021-10-06 22:38:15 +00:00
|
|
|
created_str = Column(String)
|
|
|
|
stickied = Column(String)
|
2021-07-21 01:12:26 +00:00
|
|
|
is_pinned = Column(Boolean, default=False)
|
|
|
|
private = Column(Boolean, default=False)
|
2021-09-11 23:40:58 +00:00
|
|
|
club = Column(Boolean, default=False)
|
2021-08-22 11:29:00 +00:00
|
|
|
comment_count = Column(Integer, default=0)
|
2021-07-21 01:12:26 +00:00
|
|
|
is_approved = Column(Integer, ForeignKey("users.id"), default=0)
|
|
|
|
over_18 = Column(Boolean, default=False)
|
|
|
|
is_bot = Column(Boolean, default=False)
|
|
|
|
upvotes = Column(Integer, default=1)
|
|
|
|
downvotes = Column(Integer, default=0)
|
2021-07-25 23:49:53 +00:00
|
|
|
app_id=Column(Integer, ForeignKey("oauth_apps.id"))
|
2021-10-06 22:38:15 +00:00
|
|
|
title = Column(String)
|
|
|
|
title_html = Column(String)
|
|
|
|
url = Column(String)
|
|
|
|
body = deferred(Column(String))
|
|
|
|
body_html = deferred(Column(String))
|
|
|
|
ban_reason = Column(String)
|
|
|
|
embed_url = Column(String)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-10-05 23:11:17 +00:00
|
|
|
comments = relationship("Comment", lazy="dynamic", primaryjoin="Comment.parent_submission==Submission.id", viewonly=True)
|
|
|
|
flags = relationship("Flag", lazy="dynamic", viewonly=True)
|
|
|
|
author = relationship("User", primaryjoin="Submission.author_id==User.id")
|
2021-09-24 02:00:11 +00:00
|
|
|
oauth_app = relationship("OauthApp", viewonly=True)
|
2021-09-22 14:51:36 +00:00
|
|
|
approved_by = relationship("User", uselist=False, primaryjoin="Submission.is_approved==User.id", viewonly=True)
|
|
|
|
awards = relationship("AwardRelationship", viewonly=True)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
if "created_utc" not in kwargs:
|
|
|
|
kwargs["created_utc"] = int(time.time())
|
|
|
|
kwargs["created_str"] = time.strftime(
|
|
|
|
"%I:%M %p on %d %b %Y", time.gmtime(
|
|
|
|
kwargs["created_utc"]))
|
|
|
|
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"<Submission(id={self.id})>"
|
2021-08-06 12:22:29 +00:00
|
|
|
|
2021-09-19 18:22:57 +00:00
|
|
|
|
2021-10-05 23:11:17 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def options(self):
|
2021-10-07 04:17:50 +00:00
|
|
|
return self.comments.filter_by(author_id = AUTOPOLLER_ACCOUNT, level=1)
|
2021-10-05 23:11:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def created_datetime(self):
|
|
|
|
return str(time.strftime("%d/%B/%Y %H:%M:%S UTC", time.gmtime(self.created_utc)))
|
|
|
|
|
2021-09-19 18:22:57 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def created_datetime(self):
|
|
|
|
return str(time.strftime("%d/%B/%Y %H:%M:%S UTC", time.gmtime(self.created_utc)))
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def age_string(self):
|
|
|
|
|
|
|
|
age = int(time.time()) - self.created_utc
|
|
|
|
|
|
|
|
if age < 60:
|
|
|
|
return "just now"
|
|
|
|
elif age < 3600:
|
|
|
|
minutes = int(age / 60)
|
|
|
|
return f"{minutes}m ago"
|
|
|
|
elif age < 86400:
|
|
|
|
hours = int(age / 3600)
|
|
|
|
return f"{hours}hr ago"
|
|
|
|
elif age < 2678400:
|
|
|
|
days = int(age / 86400)
|
|
|
|
return f"{days}d ago"
|
|
|
|
|
|
|
|
now = time.gmtime()
|
|
|
|
ctd = time.gmtime(self.created_utc)
|
|
|
|
|
|
|
|
months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year)
|
|
|
|
if now.tm_mday < ctd.tm_mday:
|
|
|
|
months -= 1
|
|
|
|
|
|
|
|
if months < 12:
|
|
|
|
return f"{months}mo ago"
|
|
|
|
else:
|
|
|
|
years = int(months / 12)
|
|
|
|
return f"{years}yr ago"
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def edited_string(self):
|
|
|
|
|
|
|
|
if not self.edited_utc: return "never"
|
|
|
|
|
|
|
|
age = int(time.time()) - self.edited_utc
|
|
|
|
|
|
|
|
if age < 60:
|
|
|
|
return "just now"
|
|
|
|
elif age < 3600:
|
|
|
|
minutes = int(age / 60)
|
|
|
|
return f"{minutes}m ago"
|
|
|
|
elif age < 86400:
|
|
|
|
hours = int(age / 3600)
|
|
|
|
return f"{hours}hr ago"
|
|
|
|
elif age < 2678400:
|
|
|
|
days = int(age / 86400)
|
|
|
|
return f"{days}d ago"
|
|
|
|
|
|
|
|
now = time.gmtime()
|
|
|
|
ctd = time.gmtime(self.edited_utc)
|
|
|
|
months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year)
|
|
|
|
|
|
|
|
if months < 12:
|
|
|
|
return f"{months}mo ago"
|
|
|
|
else:
|
|
|
|
years = now.tm_year - ctd.tm_year
|
|
|
|
return f"{years}yr ago"
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def edited_datetime(self):
|
|
|
|
return str(time.strftime("%d/%B/%Y %H:%M:%S UTC", time.gmtime(self.edited_utc)))
|
|
|
|
|
|
|
|
|
2021-09-23 19:22:18 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def score(self):
|
|
|
|
return self.upvotes - self.downvotes
|
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def fullname(self):
|
2021-07-30 05:31:38 +00:00
|
|
|
return f"t2_{self.id}"
|
2021-09-12 06:12:44 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def shortlink(self):
|
2021-10-19 23:43:11 +00:00
|
|
|
return f"http://{site}/post/{self.id}"
|
2021-09-12 06:12:44 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def permalink(self):
|
2021-09-12 08:02:04 +00:00
|
|
|
if self.club: return f"/post/{self.id}"
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
output = self.title.lower()
|
|
|
|
|
|
|
|
output = re.sub('&\w{2,3};', '', output)
|
|
|
|
|
|
|
|
output = [re.sub('\W', '', word) for word in output.split()]
|
2021-08-02 07:37:46 +00:00
|
|
|
output = [x for x in output if x][:6]
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
output = '-'.join(output)
|
|
|
|
|
2021-09-03 13:52:37 +00:00
|
|
|
if not output: output = '-'
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
return f"/post/{self.id}/{output}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def domain(self):
|
|
|
|
|
2021-07-27 21:58:35 +00:00
|
|
|
if not self.url: return "text post"
|
2021-07-21 01:12:26 +00:00
|
|
|
domain = urlparse(self.url).netloc
|
2021-07-27 21:58:35 +00:00
|
|
|
if domain.startswith("www."): domain = domain.split("www.")[1]
|
2021-07-21 01:12:26 +00:00
|
|
|
return domain.replace("old.reddit.com", "reddit.com")
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
2021-08-01 04:27:10 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def thumb_url(self):
|
2021-10-19 23:43:11 +00:00
|
|
|
if self.over_18: return f"http://{site}/assets/images/nsfw.gif"
|
2021-10-25 14:47:19 +00:00
|
|
|
elif not self.url: return f"http://{site}/assets/images/{site_name}/default_text.gif"
|
2021-07-28 02:27:01 +00:00
|
|
|
elif self.thumburl: return self.thumburl
|
2021-10-19 23:43:11 +00:00
|
|
|
elif "youtu.be" in self.domain or "youtube.com" in self.domain: return f"http://{site}/assets/images/default_thumb_yt.gif"
|
|
|
|
else: return f"http://{site}/assets/images/default_thumb_link.gif"
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
@property
|
2021-09-19 20:06:52 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def json_raw(self):
|
2021-07-28 03:55:47 +00:00
|
|
|
flags = {}
|
|
|
|
for f in self.flags: flags[f.user.username] = f.reason
|
|
|
|
|
2021-07-25 22:11:26 +00:00
|
|
|
data = {'author_name': self.author.username,
|
2021-07-21 01:12:26 +00:00
|
|
|
'permalink': self.permalink,
|
|
|
|
'is_banned': bool(self.is_banned),
|
2021-07-25 21:59:20 +00:00
|
|
|
'deleted_utc': self.deleted_utc,
|
2021-07-21 01:12:26 +00:00
|
|
|
'created_utc': self.created_utc,
|
2021-07-29 05:31:57 +00:00
|
|
|
'id': self.id,
|
2021-07-21 01:12:26 +00:00
|
|
|
'title': self.title,
|
|
|
|
'is_nsfw': self.over_18,
|
|
|
|
'is_bot': self.is_bot,
|
|
|
|
'thumb_url': self.thumb_url,
|
|
|
|
'domain': self.domain,
|
|
|
|
'url': self.url,
|
|
|
|
'body': self.body,
|
|
|
|
'body_html': self.body_html,
|
|
|
|
'created_utc': self.created_utc,
|
|
|
|
'edited_utc': self.edited_utc or 0,
|
|
|
|
'comment_count': self.comment_count,
|
2021-09-18 19:45:17 +00:00
|
|
|
'score': self.score,
|
|
|
|
'upvotes': self.upvotes,
|
|
|
|
'downvotes': self.downvotes,
|
2021-07-28 02:27:01 +00:00
|
|
|
'stickied': self.stickied,
|
2021-10-16 19:29:36 +00:00
|
|
|
'private' : self.private,
|
2021-07-28 03:29:06 +00:00
|
|
|
'distinguish_level': self.distinguish_level,
|
2021-08-08 22:24:22 +00:00
|
|
|
'voted': self.voted if hasattr(self, 'voted') else 0,
|
2021-07-28 03:55:47 +00:00
|
|
|
'flags': flags,
|
2021-07-21 01:12:26 +00:00
|
|
|
}
|
2021-07-28 03:55:47 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
if self.ban_reason:
|
|
|
|
data["ban_reason"]=self.ban_reason
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
@property
|
2021-09-19 18:25:40 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def json_core(self):
|
|
|
|
|
|
|
|
if self.is_banned:
|
|
|
|
return {'is_banned': True,
|
2021-07-25 21:59:20 +00:00
|
|
|
'deleted_utc': self.deleted_utc,
|
2021-07-21 01:12:26 +00:00
|
|
|
'ban_reason': self.ban_reason,
|
2021-07-30 05:31:38 +00:00
|
|
|
'id': self.id,
|
2021-07-21 01:12:26 +00:00
|
|
|
'title': self.title,
|
|
|
|
'permalink': self.permalink,
|
|
|
|
}
|
2021-07-25 21:59:20 +00:00
|
|
|
elif self.deleted_utc:
|
2021-07-21 01:12:26 +00:00
|
|
|
return {'is_banned': bool(self.is_banned),
|
2021-07-25 21:59:20 +00:00
|
|
|
'deleted_utc': True,
|
2021-07-30 05:31:38 +00:00
|
|
|
'id': self.id,
|
2021-07-21 01:12:26 +00:00
|
|
|
'title': self.title,
|
|
|
|
'permalink': self.permalink,
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.json_raw
|
|
|
|
|
|
|
|
@property
|
2021-09-19 18:25:40 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def json(self):
|
|
|
|
|
|
|
|
data=self.json_core
|
|
|
|
|
|
|
|
if self.deleted_utc > 0 or self.is_banned:
|
|
|
|
return data
|
|
|
|
|
|
|
|
data["author"]=self.author.json_core
|
|
|
|
data["comment_count"]=self.comment_count
|
|
|
|
|
|
|
|
|
|
|
|
if "replies" in self.__dict__:
|
|
|
|
data["replies"]=[x.json_core for x in self.replies]
|
|
|
|
|
2021-08-07 23:01:07 +00:00
|
|
|
if "voted" in self.__dict__:
|
2021-08-07 23:03:15 +00:00
|
|
|
data["voted"] = self.voted
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
2021-08-11 20:20:37 +00:00
|
|
|
def award_count(self, kind) -> int:
|
|
|
|
return len([x for x in self.awards if x.kind == kind])
|
2021-07-27 23:16:41 +00:00
|
|
|
|
2021-09-19 20:06:52 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def realurl(self, v):
|
|
|
|
if v and v.agendaposter and random.randint(1, 10) < 4:
|
|
|
|
return 'https://secure.actblue.com/donate/ms_blm_homepage_2019'
|
2021-08-11 19:17:43 +00:00
|
|
|
elif v and self.url and self.url.startswith("https://old.reddit.com/"):
|
2021-08-11 18:57:12 +00:00
|
|
|
url = self.url
|
2021-08-11 18:59:09 +00:00
|
|
|
if not v.oldreddit: url = self.url.replace("old.reddit.com", "reddit.com")
|
2021-08-14 23:43:31 +00:00
|
|
|
if v.controversial and '/comments/' in url and "sort=" not in url:
|
2021-08-11 18:57:12 +00:00
|
|
|
if "?" in url: url += "&sort=controversial"
|
2021-08-11 19:05:38 +00:00
|
|
|
else: url += "?sort=controversial"
|
2021-08-11 18:59:09 +00:00
|
|
|
return url
|
2021-09-13 12:06:22 +00:00
|
|
|
elif self.url:
|
|
|
|
if v and v.nitter: return self.url.replace("www.twitter.com", "nitter.net").replace("twitter.com", "nitter.net")
|
|
|
|
return self.url
|
2021-08-11 19:17:43 +00:00
|
|
|
else: return ""
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
def realbody(self, v):
|
2021-10-23 14:35:35 +00:00
|
|
|
if self.club and not (v and v.paid_dues): return "<p>COUNTRY CLUB ONLY</p>"
|
2021-08-21 11:06:28 +00:00
|
|
|
|
2021-10-20 14:37:46 +00:00
|
|
|
body = self.body_html
|
2021-10-18 20:46:57 +00:00
|
|
|
body = censor_slurs(body, v)
|
2021-08-21 11:06:28 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
if v and not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com")
|
2021-09-13 12:06:22 +00:00
|
|
|
if v and v.nitter: body = body.replace("www.twitter.com", "nitter.net").replace("twitter.com", "nitter.net")
|
2021-07-21 01:12:26 +00:00
|
|
|
return body
|
|
|
|
|
2021-10-02 20:58:14 +00:00
|
|
|
def plainbody(self, v):
|
2021-10-23 14:35:35 +00:00
|
|
|
if self.club and not (v and v.paid_dues): return "<p>COUNTRY CLUB ONLY</p>"
|
2021-10-02 20:58:14 +00:00
|
|
|
|
2021-10-20 14:37:46 +00:00
|
|
|
body = self.body
|
2021-10-18 20:46:57 +00:00
|
|
|
body = censor_slurs(body, v)
|
2021-10-02 20:58:14 +00:00
|
|
|
|
|
|
|
if v and not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com")
|
|
|
|
if v and v.nitter: body = body.replace("www.twitter.com", "nitter.net").replace("twitter.com", "nitter.net")
|
|
|
|
return body
|
|
|
|
|
2021-09-19 20:06:52 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def realtitle(self, v):
|
2021-10-23 14:35:35 +00:00
|
|
|
if self.club and not (v and v.paid_dues) and not (v and v.admin_level == 6):
|
|
|
|
if v: return f'{v.username} dox'
|
|
|
|
else: return 'COUNTRY CLUB MEMBERS ONLY'
|
2021-09-11 23:40:58 +00:00
|
|
|
elif self.title_html: title = self.title_html
|
2021-07-21 01:12:26 +00:00
|
|
|
else: title = self.title
|
2021-08-21 11:06:28 +00:00
|
|
|
|
2021-10-19 23:54:23 +00:00
|
|
|
title = censor_slurs(title, v)
|
2021-08-21 11:06:28 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
return title
|
|
|
|
|
2021-10-02 20:58:14 +00:00
|
|
|
@lazy
|
|
|
|
def plaintitle(self, v):
|
2021-10-23 14:35:35 +00:00
|
|
|
if self.club and not (v and v.paid_dues) and not (v and v.admin_level == 6):
|
|
|
|
if v: return f'{v.username} dox'
|
|
|
|
else: return 'COUNTRY CLUB MEMBERS ONLY'
|
2021-10-02 20:58:14 +00:00
|
|
|
else: title = self.title
|
|
|
|
|
2021-10-19 23:54:23 +00:00
|
|
|
title = censor_slurs(title, v)
|
2021-10-02 20:58:14 +00:00
|
|
|
|
|
|
|
return title
|
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
@property
|
2021-09-19 18:25:40 +00:00
|
|
|
@lazy
|
2021-07-21 01:12:26 +00:00
|
|
|
def is_image(self):
|
2021-09-13 15:58:52 +00:00
|
|
|
if self.url: return self.url.lower().endswith('.webp') or self.url.lower().endswith('.jpg') or self.url.lower().endswith('.png') or self.url.lower().endswith('.gif') or self.url.lower().endswith('.jpeg') or self.url.lower().endswith('?maxwidth=9999')
|
2021-07-21 01:12:26 +00:00
|
|
|
else: return False
|
2021-07-31 13:41:00 +00:00
|
|
|
|
2021-07-27 00:16:30 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
2021-10-05 19:15:48 +00:00
|
|
|
def active_flags(self): return self.flags.count()
|
2021-07-31 13:54:04 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
2021-10-05 19:15:48 +00:00
|
|
|
def ordered_flags(self): return self.flags.order_by(Flag.id).all()
|
2021-07-27 00:16:30 +00:00
|
|
|
|
2021-07-27 00:05:58 +00:00
|
|
|
|
2021-09-19 18:22:57 +00:00
|
|
|
class SaveRelationship(Base):
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
__tablename__="save_relationship"
|
|
|
|
|
2021-10-16 19:29:36 +00:00
|
|
|
id=Column(Integer, primary_key=True)
|
|
|
|
user_id=Column(Integer)
|
|
|
|
submission_id=Column(Integer)
|
|
|
|
comment_id=Column(Integer)
|
2021-09-20 21:19:33 +00:00
|
|
|
type=Column(Integer)
|