MarseyWorld/files/classes/submission.py

413 lines
13 KiB
Python
Raw Normal View History

2021-07-26 18:47:42 +00:00
from flask import render_template, g
2021-07-21 01:12:26 +00:00
from sqlalchemy import *
from sqlalchemy.orm import relationship, deferred
import re, random
from urllib.parse import urlparse
2021-08-04 15:35:10 +00:00
from files.helpers.lazy import lazy
from files.__main__ import Base
2021-07-31 13:55:49 +00:00
from .mix_ins import *
2021-07-31 13:41:00 +00:00
from .flags import *
2021-08-02 14:27:20 +00:00
from os import environ
2021-08-05 14:41:32 +00:00
site = environ.get("DOMAIN").strip()
2021-07-21 01:12:26 +00:00
class SubmissionAux(Base):
__tablename__ = "submissions_aux"
key_id = Column(BigInteger, primary_key=True)
id = Column(BigInteger, ForeignKey("submissions.id"))
2021-07-25 23:49:53 +00:00
title = Column(String(500))
title_html = Column(String(500))
url = Column(String(500))
2021-07-21 01:12:26 +00:00
body = Column(String(10000), default="")
body_html = Column(String(20000), default="")
ban_reason = Column(String(128), default="")
embed_url = Column(String(256), default="")
class Submission(Base, Stndrd, Age_times, Scores, Fuzzing):
__tablename__ = "submissions"
id = Column(BigInteger, primary_key=True)
submission_aux = relationship(
"SubmissionAux",
lazy="joined",
uselist=False,
innerjoin=True,
primaryjoin="Submission.id==SubmissionAux.id")
author_id = Column(BigInteger, ForeignKey("users.id"))
edited_utc = Column(BigInteger, default=0)
created_utc = Column(BigInteger, default=0)
2021-07-25 23:49:53 +00:00
thumburl = Column(String)
2021-07-21 01:12:26 +00:00
is_banned = Column(Boolean, default=False)
views = Column(Integer, default=0)
deleted_utc = Column(Integer, default=0)
distinguish_level = Column(Integer, default=0)
2021-07-25 23:49:53 +00:00
created_str = Column(String(255))
2021-07-21 01:12:26 +00:00
stickied = Column(Boolean, default=False)
is_pinned = Column(Boolean, default=False)
private = Column(Boolean, default=False)
2021-08-06 12:31:20 +00:00
comments = relationship(
2021-07-21 01:12:26 +00:00
"Comment",
2021-08-06 12:31:20 +00:00
lazy="joined",
2021-07-21 01:12:26 +00:00
primaryjoin="Comment.parent_submission==Submission.id",
2021-08-06 12:44:24 +00:00
)
2021-08-06 12:57:37 +00:00
flags = relationship("Flag", lazy="dynamic")
2021-07-21 01:12:26 +00:00
is_approved = Column(Integer, ForeignKey("users.id"), default=0)
over_18 = Column(Boolean, default=False)
author = relationship(
"User",
lazy="joined",
innerjoin=True,
primaryjoin="Submission.author_id==User.id")
is_pinned = 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-07-21 01:12:26 +00:00
oauth_app=relationship("OauthApp")
approved_by = relationship(
"User",
uselist=False,
primaryjoin="Submission.is_approved==User.id")
2021-07-27 11:07:36 +00:00
awards = relationship("AwardRelationship", lazy="joined")
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
@property
@lazy
def comment_count(self):
return len(self.comments)
@property
@lazy
def score(self):
return self.upvotes - self.downvotes
2021-07-21 01:12:26 +00:00
@property
@lazy
def hotscore(self):
return 10000000*(self.upvotes - self.downvotes + 1)/(((self.age+3600)/1000)**(1.35))
@property
@lazy
def score_disputed(self):
return (self.upvotes+1) * (self.downvotes+1)
@property
@lazy
def fullname(self):
2021-07-30 05:31:38 +00:00
return f"t2_{self.id}"
2021-07-21 01:12:26 +00:00
@property
@lazy
def permalink(self):
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)
if not output:
output = '-'
return f"/post/{self.id}/{output}"
def rendered_page(self, sort=None, comment=None, comment_info=None, v=None):
# check for banned
2021-07-26 15:53:04 +00:00
if v and (v.admin_level >= 3 or self.author_id == v.id):
2021-07-21 01:12:26 +00:00
template = "submission.html"
elif self.is_banned:
template = "submission_banned.html"
else:
template = "submission.html"
# load and tree comments
# calling this function with a comment object will do a comment
# permalink thing
2021-08-07 23:01:07 +00:00
if "replies" not in self.__dict__ and "preloaded_comments" in self.__dict__:
2021-07-21 01:12:26 +00:00
self.tree_comments(comment=comment)
return render_template(template,
v=v,
p=self,
sort=sort,
linked_comment=comment,
comment_info=comment_info,
render_replies=True,
)
@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-08-07 23:01:07 +00:00
comments = self.__dict__.get('preloaded_comments',[])
2021-07-21 01:12:26 +00:00
if not comments:
return
pinned_comment=[]
index = {}
for c in comments:
if c.is_pinned and c.parent_fullname==self.fullname:
pinned_comment+=[c]
continue
if c.parent_fullname in index:
index[c.parent_fullname].append(c)
else:
index[c.parent_fullname] = [c]
for c in comments:
c.__dict__["replies"] = index.get(c.fullname, [])
if comment:
self.__dict__["replies"] = [comment]
else:
self.__dict__["replies"] = pinned_comment + index.get(self.fullname, [])
@property
2021-08-01 04:27:10 +00:00
@lazy
2021-07-21 01:12:26 +00:00
def thumb_url(self):
2021-08-02 15:05:46 +00:00
if self.over_18: return f"https://{site}/assets/images/nsfw.png"
elif not self.url: return f"https://{site}/assets/images/default_thumb_text.png"
2021-07-28 02:27:01 +00:00
elif self.thumburl: return self.thumburl
2021-08-02 15:05:46 +00:00
elif "youtu.be" in self.domain or "youtube.com" in self.domain: return f"https://{site}/assets/images/default_thumb_yt.png"
else: return f"https://{site}/assets/images/default_thumb_link.png"
2021-07-21 01:12:26 +00:00
@property
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,
'score': self.score_fuzzed,
'upvotes': self.upvotes_fuzzed,
'downvotes': self.downvotes_fuzzed,
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
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
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-07-27 23:16:41 +00:00
def has_award(self, kind):
return bool(len([x for x in self.awards if x.kind == kind]))
2021-07-21 01:12:26 +00:00
@property
def title(self):
return self.submission_aux.title
@title.setter
def title(self, x):
self.submission_aux.title = x
g.db.add(self.submission_aux)
@property
def url(self):
return self.submission_aux.url
@url.setter
def url(self, x):
self.submission_aux.url = x
g.db.add(self.submission_aux)
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")
if v.controversial and '/comments/' 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-08-11 19:17:43 +00:00
elif self.url: return self.url
else: return ""
2021-07-21 01:12:26 +00:00
@property
def body(self):
return self.submission_aux.body
@body.setter
def body(self, x):
self.submission_aux.body = x
g.db.add(self.submission_aux)
@property
def body_html(self):
return self.submission_aux.body_html
@body_html.setter
def body_html(self, x):
self.submission_aux.body_html = x
g.db.add(self.submission_aux)
def realbody(self, v):
body = self.submission_aux.body_html
2021-07-29 05:06:04 +00:00
if not v or v.slurreplacer: body = body.replace(" nigger"," 🏀").replace(" Nigger"," 🏀").replace(" NIGGER"," 🏀").replace(" pedo"," libertarian").replace(" Pedo"," Libertarian ").replace(" PEDO"," LIBERTARIAN ").replace(" tranny"," 🚄").replace(" Tranny"," 🚄").replace(" TRANNY"," 🚄").replace(" fag"," cute twink").replace(" Fag"," Cute twink").replace(" FAG"," CUTE TWINK").replace(" faggot"," cute twink").replace(" Faggot"," Cute twink").replace(" FAGGOT"," CUTE TWINK").replace(" trump"," DDR").replace(" Trump"," DDR").replace(" TRUMP"," DDR").replace(" biden"," DDD").replace(" Biden"," DDD").replace(" BIDEN"," DDD").replace(" steve akins"," penny verity oaken").replace(" Steve Akins"," Penny Verity Oaken").replace(" STEVE AKINS"," PENNY VERITY OAKEN").replace(" RETARD"," RSLUR").replace(" rapist"," male feminist").replace(" Rapist"," Male feminist").replace(" RAPIST"," MALE FEMINIST").replace(" RETARD"," RSLUR").replace(" rapist"," male feminist").replace(" Rapist"," Male feminist").replace(" RAPIST"," MALE FEMINIST").replace(" RETARD"," RSLUR").replace(" rapist"," male feminist").replace(" Rapist"," Male feminist").replace(" RAPIST"," MALE FEMINIST").replace(" kill yourself"," keep yourself safe").replace(" KILL YOURSELF"," KEEP YOURSELF SAFE").replace(" trannie"," 🚄").replace(" Trannie"," 🚄").replace(" TRANNIE"," 🚄").replace(" troon"," 🚄").replace(" Troon"," 🚄").replace(" TROON"," 🚄")
2021-07-21 01:12:26 +00:00
if v and not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com")
return body
@property
def title_html(self):
return self.submission_aux.title_html
@title_html.setter
def title_html(self, x):
self.submission_aux.title_html = x
g.db.add(self.submission_aux)
def realtitle(self, v):
if self.title_html: title = self.title_html
else: title = self.title
2021-07-29 05:06:04 +00:00
if not v or v.slurreplacer: title = title.replace(" nigger"," 🏀").replace(" Nigger"," 🏀").replace(" NIGGER"," 🏀").replace(" pedo"," libertarian").replace(" Pedo"," Libertarian ").replace(" PEDO"," LIBERTARIAN ").replace(" tranny"," 🚄").replace(" Tranny"," 🚄").replace(" TRANNY"," 🚄").replace(" fag"," cute twink").replace(" Fag"," Cute twink").replace(" FAG"," CUTE TWINK").replace(" faggot"," cute twink").replace(" Faggot"," Cute twink").replace(" FAGGOT"," CUTE TWINK").replace(" trump"," DDR").replace(" Trump"," DDR").replace(" TRUMP"," DDR").replace(" biden"," DDD").replace(" Biden"," DDD").replace(" BIDEN"," DDD").replace(" steve akins"," penny verity oaken").replace(" Steve Akins"," Penny Verity Oaken").replace(" STEVE AKINS"," PENNY VERITY OAKEN").replace(" RETARD"," RSLUR").replace(" rapist"," male feminist").replace(" Rapist"," Male feminist").replace(" RAPIST"," MALE FEMINIST").replace(" RETARD"," RSLUR").replace(" rapist"," male feminist").replace(" Rapist"," Male feminist").replace(" RAPIST"," MALE FEMINIST").replace(" RETARD"," RSLUR").replace(" rapist"," male feminist").replace(" Rapist"," Male feminist").replace(" RAPIST"," MALE FEMINIST").replace(" kill yourself"," keep yourself safe").replace(" KILL YOURSELF"," KEEP YOURSELF SAFE").replace(" trannie"," 🚄").replace(" Trannie"," 🚄").replace(" TRANNIE"," 🚄").replace(" troon"," 🚄").replace(" Troon"," 🚄").replace(" TROON"," 🚄")
2021-07-21 01:12:26 +00:00
return title
@property
def ban_reason(self):
return self.submission_aux.ban_reason
@ban_reason.setter
def ban_reason(self, x):
self.submission_aux.ban_reason = x
g.db.add(self.submission_aux)
@property
def embed_url(self):
return self.submission_aux.embed_url
@embed_url.setter
def embed_url(self, x):
self.submission_aux.embed_url = x
g.db.add(self.submission_aux)
@property
def is_blocked(self):
return self.__dict__.get('_is_blocked', False)
@property
def is_blocking(self):
return self.__dict__.get('_is_blocking', False)
2021-07-25 01:18:20 +00:00
#@property
#def award_count(self):
#return len(self.awards)
2021-07-21 01:12:26 +00:00
@property
def is_image(self):
2021-07-27 21:58:35 +00:00
if self.url: return 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') or self.url.lower().endswith('?maxwidth=8888')
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-08-06 12:57:37 +00:00
def active_flags(self): return self.flags.count()
2021-07-31 13:54:04 +00:00
@property
@lazy
2021-07-31 14:11:11 +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-07-21 01:12:26 +00:00
class SaveRelationship(Base, Stndrd):
__tablename__="save_relationship"
id=Column(Integer, primary_key=true)
user_id=Column(Integer, ForeignKey("users.id"))
submission_id=Column(Integer, ForeignKey("submissions.id"))
2021-07-28 22:38:58 +00:00
type=Column(Integer)