rDrama/files/classes/submission.py

411 lines
12 KiB
Python
Raw Normal View History

2021-10-18 20:46:57 +00:00
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 *
2022-03-05 22:46:56 +00:00
from sqlalchemy.orm import relationship, deferred
2021-10-18 01:26:30 +00:00
from files.__main__ import Base
2022-01-19 09:07:16 +00:00
from files.helpers.const import *
from files.helpers.regex import *
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
from .comment import Comment, normalize_urls_runtime
from .saves import SaveRelationship
2022-02-05 21:09:17 +00:00
from .sub import *
from .subscriptions import *
2022-02-07 11:39:26 +00:00
from .votes import CommentVote
from .polls import *
from flask import g
2022-06-22 19:57:57 +00:00
2021-09-19 18:22:57 +00:00
class Submission(Base):
2021-07-21 01:12:26 +00:00
__tablename__ = "submissions"
2022-02-12 22:23:41 +00:00
id = Column(Integer, primary_key=True)
author_id = Column(Integer, ForeignKey("users.id"))
edited_utc = Column(Integer, default=0)
created_utc = Column(Integer)
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)
2022-06-25 00:11:00 +00:00
bannedfor = Column(String)
2022-02-15 22:54:17 +00:00
ghost = Column(Boolean, default=False)
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
stickied = Column(String)
2021-12-26 01:03:21 +00:00
stickied_utc = Column(Integer)
2022-07-01 23:11:48 +00:00
hole_pinned = Column(String)
2022-02-13 02:45:09 +00:00
sub = Column(String, ForeignKey("subs.name"))
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)
2022-02-13 02:45:09 +00:00
is_approved = Column(Integer, ForeignKey("users.id"))
2021-07-21 01:12:26 +00:00
over_18 = Column(Boolean, default=False)
is_bot = Column(Boolean, default=False)
upvotes = Column(Integer, default=1)
downvotes = Column(Integer, default=0)
2021-11-30 14:18:16 +00:00
realupvotes = Column(Integer, default=1)
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)
2021-11-06 17:30:39 +00:00
body = Column(String)
body_html = Column(String)
2021-12-05 20:22:57 +00:00
flair = Column(String)
category_id = Column(Integer, ForeignKey("category.id"))
2021-10-06 22:38:15 +00:00
ban_reason = Column(String)
embed_url = Column(String)
2022-02-27 22:49:34 +00:00
new = Column(Boolean)
2021-07-21 01:12:26 +00:00
author = relationship("User", primaryjoin="Submission.author_id==User.id")
oauth_app = relationship("OauthApp")
approved_by = relationship("User", uselist=False, primaryjoin="Submission.is_approved==User.id")
awards = relationship("AwardRelationship", order_by="AwardRelationship.awarded_utc.desc()", back_populates="post")
flags = relationship("Flag", order_by="Flag.created_utc")
comments = relationship("Comment", primaryjoin="Comment.parent_submission==Submission.id", back_populates="post")
subr = relationship("Sub", primaryjoin="foreign(Submission.sub)==remote(Sub.name)")
options = relationship("SubmissionOption", order_by="SubmissionOption.id")
category = relationship("Category", primaryjoin="Submission.category_id==Category.id")
2021-07-21 01:12:26 +00:00
2022-03-05 22:46:56 +00:00
bump_utc = deferred(Column(Integer, server_default=FetchedValue()))
2022-02-21 05:55:37 +00:00
2021-07-21 01:12:26 +00:00
def __init__(self, *args, **kwargs):
2022-02-14 21:07:31 +00:00
if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time())
2021-07-21 01:12:26 +00:00
super().__init__(*args, **kwargs)
def __repr__(self):
return f"<Submission(id={self.id})>"
2021-08-06 12:22:29 +00:00
2022-01-06 20:30:08 +00:00
@property
2021-12-27 02:09:06 +00:00
@lazy
def controversial(self):
if self.downvotes > 5 and 0.25 < self.upvotes / self.downvotes < 4: return True
return False
2021-09-19 18:22:57 +00:00
2021-11-05 20:10:51 +00:00
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 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):
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)
2022-05-16 20:57:47 +00:00
if now.tm_mday < ctd.tm_mday:
months -= 1
2021-09-19 18:22:57 +00:00
if months < 12:
return f"{months}mo ago"
else:
2022-05-16 20:57:47 +00:00
years = int(months / 12)
2021-09-19 18:22:57 +00:00
return f"{years}yr ago"
2022-02-05 17:51:42 +00:00
@property
@lazy
def score(self):
return self.upvotes - self.downvotes
2021-09-23 19:22:18 +00:00
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
2022-02-16 04:33:13 +00:00
@property
@lazy
2022-02-24 13:20:48 +00:00
def shortlink(self):
2022-02-17 07:12:38 +00:00
link = f"/post/{self.id}"
2022-03-09 02:04:37 +00:00
if self.sub: link = f"/h/{self.sub}{link}"
2022-02-16 04:33:13 +00:00
2022-02-26 22:13:02 +00:00
if self.club: return link + '/-'
2021-07-21 01:12:26 +00:00
2022-02-27 21:57:44 +00:00
output = title_regex.sub('', self.title.lower())
output = output.split()[:6]
2021-07-21 01:12:26 +00:00
output = '-'.join(output)
2022-02-27 21:57:44 +00:00
2021-09-03 13:52:37 +00:00
if not output: output = '-'
2021-07-21 01:12:26 +00:00
2022-02-04 18:35:39 +00:00
return f"{link}/{output}"
2022-02-04 03:06:49 +00:00
@property
@lazy
def permalink(self):
return SITE_FULL + self.shortlink
2021-07-21 01:12:26 +00:00
@property
@lazy
def domain(self):
2021-12-30 05:27:22 +00:00
if not self.url: return None
2022-01-19 09:21:20 +00:00
if self.url.startswith('/'): return SITE
2022-02-25 18:16:11 +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")
2022-01-21 20:56:56 +00:00
@property
@lazy
def author_name(self):
2022-01-22 10:14:15 +00:00
if self.ghost: return '👻'
2022-01-21 20:56:56 +00:00
else: return self.author.username
2021-12-28 14:52:57 +00:00
@property
@lazy
def is_youtube(self):
return self.domain == "youtube.com" and self.embed_url and self.embed_url.startswith('<lite-youtube')
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):
2022-04-26 00:36:03 +00:00
if self.over_18: return f"{SITE_FULL}/assets/images/nsfw.webp?v=1"
2022-07-10 20:27:56 +00:00
elif not self.url: return f"{SITE_FULL}/assets/images/{SITE_NAME}/default_text.webp?v=2"
2022-01-28 21:42:09 +00:00
elif self.thumburl:
if self.thumburl.startswith('/'): return SITE_FULL + self.thumburl
return self.thumburl
2022-05-24 16:28:12 +00:00
elif self.is_youtube or self.is_video: return f"{SITE_FULL}/assets/images/default_thumb_video.webp?v=1"
elif self.is_audio: return f"{SITE_FULL}/assets/images/default_thumb_audio.webp?v=1"
2022-07-12 00:35:40 +00:00
elif self.domain == SITE: return f"{SITE_FULL}/assets/images/{SITE_NAME}/site_preview.webp?v=3001"
2022-04-26 00:36:03 +00:00
else: return f"{SITE_FULL}/assets/images/default_thumb_link.webp?v=1"
2021-07-21 01:12:26 +00:00
@property
2021-09-19 20:06:52 +00:00
@lazy
2022-07-11 17:33:26 +00:00
def json(self):
if self.is_banned:
return {'is_banned': True,
'deleted_utc': self.deleted_utc,
'ban_reason': self.ban_reason,
'id': self.id,
'title': self.title,
'permalink': self.permalink,
}
if self.deleted_utc:
return {'is_banned': bool(self.is_banned),
'deleted_utc': True,
'id': self.id,
'title': self.title,
'permalink': self.permalink,
}
2021-07-28 03:55:47 +00:00
flags = {}
2022-06-29 07:22:18 +00:00
for f in self.flags: flags[f.user.username] = f.reason
2021-07-28 03:55:47 +00:00
2022-07-11 17:33:26 +00:00
data = {'author_name': self.author_name if self.author else '',
2021-07-21 01:12:26 +00:00
'permalink': self.permalink,
2022-04-06 21:01:32 +00:00
'shortlink': self.shortlink,
2021-07-21 01:12:26 +00:00
'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,
2022-01-28 21:42:09 +00:00
'thumb_url': self.thumb_url,
2021-07-21 01:12:26 +00:00
'domain': self.domain,
2022-01-28 21:42:09 +00:00
'url': self.realurl(None),
2021-07-21 01:12:26 +00:00
'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,
'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-12-07 23:09:43 +00:00
'club': self.club,
2022-07-11 17:33:26 +00:00
'author': '👻' if self.ghost else self.author.json,
2022-07-12 18:05:56 +00:00
'comment_count': self.comment_count
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 "replies" in self.__dict__:
2022-07-11 17:19:48 +00:00
data["replies"]=[x.json for x in self.replies]
2021-07-21 01:12:26 +00:00
return data
2022-06-23 16:36:39 +00:00
@lazy
2022-06-27 02:14:53 +00:00
def award_count(self, kind, v):
2022-06-27 03:46:32 +00:00
if v and v.poor: return 0
2021-08-11 20:20:37 +00:00
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):
url = self.url
if not url: return ''
if url.startswith('/'): return SITE_FULL + url
url = normalize_urls_runtime(url, v)
if url.startswith("https://old.reddit.com/r/") and '/comments/' in url and "sort=" not in url:
if "?" in url: url += "&context=9"
else: url += "?context=8"
if v and v.controversial: url += "&sort=controversial"
return url
2022-06-23 16:36:39 +00:00
@lazy
2022-06-26 06:10:02 +00:00
def realbody(self, v, listing=False):
2022-01-19 09:07:16 +00:00
if self.club and not (v and (v.paid_dues or v.id == self.author_id)): return f"<p>{CC} ONLY</p>"
2021-08-21 11:06:28 +00:00
2022-01-23 16:54:57 +00:00
body = self.body_html or ""
2022-01-06 01:25:04 +00:00
2021-10-18 20:46:57 +00:00
body = censor_slurs(body, v)
2021-08-21 11:06:28 +00:00
body = normalize_urls_runtime(body, v)
2021-11-13 21:07:59 +00:00
2021-11-22 14:31:07 +00:00
if v and v.shadowbanned and v.id == self.author_id and 86400 > time.time() - self.created_utc > 20:
2021-11-23 19:28:29 +00:00
ti = max(int((time.time() - self.created_utc)/60), 1)
2022-02-26 22:44:42 +00:00
maxupvotes = min(ti, 11)
2021-11-23 19:28:29 +00:00
rand = random.randint(0, maxupvotes)
2021-11-13 21:07:59 +00:00
if self.upvotes < rand:
2022-02-26 22:44:42 +00:00
amount = random.randint(0, 3)
2022-02-14 02:47:28 +00:00
if amount == 1:
self.views += amount*random.randint(3, 5)
self.upvotes += amount
g.db.add(self)
2021-11-13 21:07:59 +00:00
if self.options:
curr = [x for x in self.options if x.exclusive and x.voted(v)]
if curr: curr = " value=" + str(curr[0].id)
else: curr = ''
body += f'<input class="d-none" id="current-{self.id}"{curr}>'
for c in self.options:
if c.exclusive:
body += f'''<div class="custom-control"><input name="choice-{self.id}" autocomplete="off" class="custom-control-input" type="radio" id="{c.id}" onchange="choice_vote('{c.id}','{self.id}','post')"'''
if c.voted(v): body += " checked "
body += f'''><label class="custom-control-label" for="{c.id}">{c.body_html}<span class="presult-{self.id}'''
body += f'"> - <a href="/votes/post/option/{c.id}"><span id="choice-{c.id}">{c.upvotes}</span> votes</a></span></label></div>'
else:
2022-06-26 06:10:02 +00:00
body += f'<div class="custom-control"><input type="checkbox" class="custom-control-input" id="{c.id}" name="option"'
if c.voted(v): body += " checked"
if v: body += f''' onchange="poll_vote('{c.id}', 'post')"'''
2022-06-26 06:10:02 +00:00
else: body += f''' onchange="poll_vote_no_v('{c.id}', '{self.id}')"'''
body += f'''><label class="custom-control-label" for="{c.id}">{c.body_html}<span class="presult-{self.id}'''
body += f'"> - <a href="/votes/post/option/{c.id}"><span id="poll-{c.id}">{c.upvotes}</span> votes</a></span></label></div>'
if not listing and self.author.sig_html and (self.author_id == MOOSE_ID or (not self.ghost and not (v and (v.sigs_disabled or v.poor)))):
body += f"<hr>{self.author.sig_html}"
2022-01-22 16:22:31 +00:00
2021-07-21 01:12:26 +00:00
return body
2022-06-23 16:36:39 +00:00
@lazy
2021-10-02 20:58:14 +00:00
def plainbody(self, v):
2022-01-19 09:07:16 +00:00
if self.club and not (v and (v.paid_dues or v.id == self.author_id)): return f"<p>{CC} ONLY</p>"
2021-10-02 20:58:14 +00:00
2022-01-06 01:25:04 +00:00
body = self.body
2022-01-06 01:24:03 +00:00
if not body: return ""
2021-10-18 20:46:57 +00:00
body = censor_slurs(body, v)
2021-10-02 20:58:14 +00:00
body = normalize_urls_runtime(body, v)
2021-10-02 20:58:14 +00:00
return body
2021-09-19 20:06:52 +00:00
@lazy
2021-07-21 01:12:26 +00:00
def realtitle(self, v):
2021-12-02 18:45:03 +00:00
if self.club and not (v and (v.paid_dues or v.id == self.author_id)):
2021-10-26 21:31:39 +00:00
if v: return random.choice(TROLLTITLES).format(username=v.username)
2022-05-07 04:13:19 +00:00
elif dues == -2: return f'Please make an account to see this post'
2022-01-19 09:07:16 +00:00
else: return f'{CC} 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-12-02 18:45:03 +00:00
if self.club and not (v and (v.paid_dues or v.id == self.author_id)):
2021-10-26 21:31:39 +00:00
if v: return random.choice(TROLLTITLES).format(username=v.username)
2022-01-19 09:07:16 +00:00
else: return f'{CC} 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-12-18 03:13:46 +00:00
@property
@lazy
def is_video(self):
2022-05-25 18:29:22 +00:00
return self.url and any((self.url.lower().endswith(x) for x in ('.mp4','.webm','.mov'))) and is_safe_url(self.url)
2021-12-18 03:13:46 +00:00
2022-05-24 16:28:12 +00:00
@property
@lazy
def is_audio(self):
2022-05-25 18:29:22 +00:00
return self.url and any((self.url.lower().endswith(x) for x in ('.mp3','.wav','.ogg','.aac','.m4a','.flac'))) and is_safe_url(self.url)
2022-05-24 16:28:12 +00:00
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):
2022-05-25 18:29:22 +00:00
if self.url and (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') or self.url.lower().endswith('&fidelity=high')) and is_safe_url(self.url):
2022-04-17 20:20:40 +00:00
return True
return False
2021-07-31 13:41:00 +00:00
2021-07-27 00:16:30 +00:00
@lazy
2022-07-11 18:13:00 +00:00
def filtered_flags(self, v):
return [f for f in self.flags if (v and v.shadowbanned) or not f.user.shadowbanned]
@lazy
def active_flags(self, v):
return len(self.filtered_flags(v))