MarseyWorld/files/classes/submission.py

416 lines
11 KiB
Python
Raw Normal View History

2021-10-05 19:15:48 +00:00
from flask import render_template, g
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-07-21 01:12:26 +00:00
import re, random
from urllib.parse import urlparse
2021-08-04 15:35:10 +00:00
from files.helpers.lazy import lazy
2021-10-06 00:04:35 +00:00
from files.helpers.const import SLURS, AUTOPOLLER_ACCOUNT
2021-10-05 19:15:48 +00:00
from files.__main__ import Base
2021-10-05 23:11:17 +00:00
from .flags import Flag
2021-08-02 14:27:20 +00:00
from os import environ
2021-09-19 18:28:31 +00:00
import time
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)
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-06 00:04:35 +00:00
return self.comments.filter_by(author_id = AUTOPOLLER_ACCOUNT)
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)
# compute number of months
months = now.tm_mon - ctd.tm_mon + 12 * (now.tm_year - ctd.tm_year)
# remove a month count if current day of month < creation day of month
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):
return f"https://{site}/post/{self.id}"
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}"
2021-09-19 20:06:52 +00:00
@lazy
2021-09-29 16:50:52 +00:00
def rendered_page(self, sort=None, comment=None, comment_info=None, v=None):
2021-07-21 01:12:26 +00:00
2021-09-22 18:36:03 +00:00
if self.is_banned and not (v and (v.admin_level >= 3 or self.author_id == v.id)): template = "submission_banned.html"
else: template = "submission.html"
2021-07-21 01:12:26 +00:00
2021-09-22 18:36:03 +00:00
self.tree_comments(comment=comment)
2021-07-21 01:12:26 +00:00
return render_template(template,
v=v,
p=self,
2021-09-05 13:51:16 +00:00
sort=sort,
2021-07-21 01:12:26 +00:00
linked_comment=comment,
comment_info=comment_info,
2021-09-22 22:54:13 +00:00
render_replies=True
2021-07-21 01:12:26 +00:00
)
@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")
def tree_comments(self, comment=None, v=None):
2021-09-22 18:36:03 +00:00
comments = self.__dict__.get('preloaded_comments', [])
if not comments: return
2021-07-21 01:12:26 +00:00
pinned_comment=[]
index = {}
for c in comments:
if c.is_pinned and c.parent_fullname==self.fullname:
2021-09-22 18:36:03 +00:00
pinned_comment += [c]
2021-07-21 01:12:26 +00:00
continue
2021-09-22 18:36:03 +00:00
if c.parent_fullname in index: index[c.parent_fullname].append(c)
else: index[c.parent_fullname] = [c]
2021-07-21 01:12:26 +00:00
2021-09-22 18:36:03 +00:00
for c in comments: c.__dict__["replies"] = index.get(c.fullname, [])
2021-07-21 01:12:26 +00:00
2021-09-22 18:36:03 +00:00
if comment: self.__dict__["replies"] = [comment]
else: self.__dict__["replies"] = pinned_comment + index.get(self.fullname, [])
2021-07-21 01:12:26 +00:00
@property
2021-08-01 04:27:10 +00:00
@lazy
2021-07-21 01:12:26 +00:00
def thumb_url(self):
2021-09-13 15:58:52 +00:00
if self.over_18: return f"https://{site}/assets/images/nsfw.webp"
elif not self.url: return f"https://{site}/assets/images/{site_name}/default_thumb_text.webp"
2021-07-28 02:27:01 +00:00
elif self.thumburl: return self.thumburl
2021-09-13 15:58:52 +00:00
elif "youtu.be" in self.domain or "youtube.com" in self.domain: return f"https://{site}/assets/images/default_thumb_yt.webp"
else: return f"https://{site}/assets/images/default_thumb_link.webp"
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-07-28 03:29:06 +00:00
'distinguish_level': self.distinguish_level,
2021-07-25 01:18:20 +00:00
#'award_count': self.award_count,
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-09-17 14:07:08 +00:00
if self.club and not (v and v.paid_dues): return "COUNTRY CLUB ONLY"
2021-09-24 02:00:11 +00:00
body = self.body_html
2021-08-21 11:06:28 +00:00
if not v or v.slurreplacer:
for s,r in SLURS.items():
2021-09-28 03:59:33 +00:00
body = body.replace(s, r)
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):
if self.club and not (v and v.paid_dues): return "COUNTRY CLUB ONLY"
body = self.body
if not v or v.slurreplacer:
for s,r in SLURS.items():
body = body.replace(s, r)
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-09-17 14:07:08 +00:00
if self.club and not (v and v.paid_dues) and not (v and v.admin_level == 6): 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-09-11 23:40:58 +00:00
if not v or v.slurreplacer:
2021-09-28 03:59:33 +00:00
for s,r in SLURS.items(): title = title.replace(s, r)
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):
if self.club and not (v and v.paid_dues) and not (v and v.admin_level == 6): return 'COUNTRY CLUB MEMBERS ONLY'
else: title = self.title
if not v or v.slurreplacer:
for s,r in SLURS.items(): title = title.replace(s, r)
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"
id=Column(Integer, primary_key=true)
user_id=Column(Integer, ForeignKey("users.id"))
submission_id=Column(Integer, ForeignKey("submissions.id"))
2021-09-20 21:19:33 +00:00
type=Column(Integer)