MarseyWorld/files/classes/submission.py

465 lines
13 KiB
Python
Raw Normal View History

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-11-07 13:37:26 +00:00
from sqlalchemy.orm import relationship
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 *
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-11-05 21:30:54 +00:00
from .comment import Comment
from flask import g
2021-08-02 14:27:20 +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-11-26 21:24:47 +00:00
bannedfor = Column(Boolean)
2022-01-22 10:14:15 +00:00
ghost = 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
stickied = Column(String)
2021-12-26 01:03:21 +00:00
stickied_utc = Column(Integer)
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-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)
2021-10-06 22:38:15 +00:00
ban_reason = Column(String)
embed_url = Column(String)
2021-07-21 01:12:26 +00:00
2021-10-05 23:11:17 +00:00
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)
2022-01-10 22:53:48 +00:00
awards = relationship("AwardRelationship", viewonly=True)
2021-12-17 17:55:11 +00:00
reports = relationship("Flag", viewonly=True)
2022-01-23 16:54:57 +00:00
comments = relationship("Comment", primaryjoin="Comment.parent_submission==Submission.id")
2021-07-21 01:12:26 +00:00
def __init__(self, *args, **kwargs):
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
@lazy
2022-01-22 10:14:15 +00:00
def comments2(self):
2022-01-21 14:40:01 +00:00
return g.db.query(Comment.author_id, Comment.created_utc, Comment.id).filter(Comment.parent_submission == self.id, Comment.author_id.notin_((AUTOPOLLER_ID,AUTOBETTER_ID))).all()
2022-01-06 20:30:08 +00:00
2021-12-27 02:09:06 +00:00
@property
@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 21:30:54 +00:00
@property
@lazy
def flags(self):
2022-01-06 19:05:13 +00:00
return g.db.query(Flag).filter_by(post_id=self.id).order_by(Flag.id)
2021-11-05 21:30:54 +00:00
2021-10-05 23:11:17 +00:00
@property
@lazy
def options(self):
2021-11-18 14:21:19 +00:00
return g.db.query(Comment).filter_by(parent_submission = self.id, author_id = AUTOPOLLER_ID, level=1)
2021-10-05 23:11:17 +00:00
2021-12-11 02:27:46 +00:00
@property
@lazy
def bet_options(self):
2021-12-11 02:41:14 +00:00
return g.db.query(Comment).filter_by(parent_submission = self.id, author_id = AUTOBETTER_ID, level=1)
2021-12-11 02:27:46 +00:00
2021-11-05 20:10:51 +00:00
def total_poll_voted(self, v):
if v:
2021-12-11 02:58:04 +00:00
for option in self.options:
2021-12-11 02:27:46 +00:00
if option.poll_voted(v): return True
return False
def total_bet_voted(self, v):
2021-12-11 03:32:43 +00:00
if "closed" in self.body.lower(): return True
2021-12-11 02:27:46 +00:00
if v:
for option in self.bet_options:
2021-11-05 20:10:51 +00:00
if option.poll_voted(v): return True
return False
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-11-21 12:32:49 +00:00
return f"/post/{self.id}"
2021-09-12 06:12:44 +00:00
2021-07-21 01:12:26 +00:00
@property
@lazy
def permalink(self):
2022-01-28 20:02:35 +00:00
if self.club: return f"{SITE_FULL}/post/{self.id}"
2021-07-21 01:12:26 +00:00
output = self.title.lower()
2022-01-27 17:04:02 +00:00
output = re.sub('&\w{2,3};', '', output, flags=re.A)
2021-07-21 01:12:26 +00:00
2022-01-27 17:04:02 +00:00
output = [re.sub('\W', '', word, flags=re.A) 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
2022-01-28 20:02:35 +00:00
return f"{SITE_FULL}/post/{self.id}/{output}"
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
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")
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-01-19 09:07:16 +00:00
if self.over_18: return f"{SITE_FULL}/static/assets/images/nsfw.webp"
elif not self.url: return f"{SITE_FULL}/static/assets/images/{SITE_NAME}/default_text.webp"
2022-01-28 21:42:09 +00:00
elif self.thumburl:
if self.thumburl.startswith('/'): return SITE_FULL + self.thumburl
return self.thumburl
2022-01-19 09:07:16 +00:00
elif self.is_youtube or self.is_video: return f"{SITE_FULL}/static/assets/images/default_thumb_yt.webp"
else: return f"{SITE_FULL}/static/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
2022-01-21 20:56:56 +00:00
data = {'author_name': self.author_name if self.author else '',
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,
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,
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
2022-01-12 01:19:13 +00:00
if self.deleted_utc or self.is_banned:
2021-07-21 01:12:26 +00:00
return data
2022-01-22 10:14:15 +00:00
data["author"]='👻' if self.ghost else self.author.json_core
2021-07-21 01:12:26 +00:00
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
2022-01-23 23:06:34 +00:00
def award_count(self, kind):
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):
2021-11-23 13:48:10 +00:00
if v and self.url and self.url.startswith("https://old.reddit.com/"):
2021-08-11 18:57:12 +00:00
url = self.url
2021-11-19 23:05:28 +00:00
if v.teddit: url = self.url.replace("old.reddit.com", "teddit.net")
elif not v.oldreddit: url = self.url.replace("old.reddit.com", "reddit.com")
2022-01-12 03:41:23 +00:00
if '/comments/' in url and "sort=" not in url:
2022-01-13 23:29:15 +00:00
if "?" in url: url += "&context=9"
else: url += "?context=9"
2022-01-12 03:41:23 +00:00
if v.controversial: 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")
2022-01-28 21:42:09 +00:00
if self.url.startswith('/'): return SITE_FULL + self.url
2021-09-13 12:06:22 +00:00
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):
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
2021-11-19 23:05:28 +00:00
if v:
if v.teddit: body = body.replace("old.reddit.com", "teddit.net")
elif not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com")
if v.nitter: body = body.replace("www.twitter.com", "nitter.net").replace("twitter.com", "nitter.net")
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)
2021-11-22 14:31:07 +00:00
maxupvotes = min(ti, 27)
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:
2021-12-04 20:48:10 +00:00
amount = random.randint(0, 3)
2021-12-05 18:58:46 +00:00
self.views += amount*random.randint(3, 5)
2021-12-04 20:48:10 +00:00
self.upvotes += amount
2021-11-13 21:07:59 +00:00
g.db.add(self)
2021-12-04 20:48:10 +00:00
self.author.coins += amount
g.db.add(self.author)
2021-11-13 21:07:59 +00:00
g.db.commit()
2022-01-23 16:54:57 +00:00
for o in self.options:
body += f'<div class="custom-control"><input type="checkbox" class="custom-control-input" id="{o.id}" name="option"'
if o.poll_voted(v): body += " checked"
if v: body += f''' onchange="poll_vote('{o.id}', '{self.id}')"'''
else: body += f''' onchange="poll_vote_no_v('{o.id}', '{self.id}')"'''
body += f'''><label class="custom-control-label" for="{o.id}">{o.body_html}<span class="presult-{self.id}'''
if not self.total_poll_voted(v): body += ' d-none'
body += f'"> - <a href="/votes?link=t3_{o.id}"><span id="poll-{o.id}">{o.upvotes}</span> votes</a></span></label></div>'
for c in self.bet_options:
body += f'''<div class="custom-control mt-3"><input autocomplete="off" class="custom-control-input bet" type="radio" id="{c.id}" onchange="bet_vote('{c.id}')"'''
if c.poll_voted(v): body += " checked "
if not (v and v.coins > 200) or self.total_bet_voted(v): body += " disabled "
body += f'''><label class="custom-control-label" for="{c.id}">{c.body_html} - <a href="/votes?link=t3_{c.id}"><span id="bet-{c.id}">{c.upvotes}</span> bets</a>'''
if not self.total_bet_voted(v):
body += '''<span class="cost"> (cost of entry: 200 coins)</span>'''
body += "</label>"
if v and v.admin_level > 2:
body += f'''<button class="btn btn-primary px-2 mx-2" style="font-size:10px;padding:2px;margin-top:-5px" onclick="post_toast('/distribute/{c.id}')">Declare winner</button>'''
body += "</div>"
2022-01-22 16:22:31 +00:00
if self.author.sig_html and not self.ghost and (self.author_id == MOOSE_ID or not (v and v.sigs_disabled)):
2022-01-22 16:41:48 +00:00
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
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
2021-11-19 23:05:28 +00:00
if v:
if v.teddit: body = body.replace("old.reddit.com", "teddit.net")
elif not v.oldreddit: body = body.replace("old.reddit.com", "reddit.com")
if v.nitter: body = body.replace("www.twitter.com", "nitter.net").replace("twitter.com", "nitter.net")
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-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-01-14 04:38:27 +00:00
return self.url and any((self.url.lower().endswith(x) for x in ('.mp4','.webm','.mov')))
2021-12-18 03:13:46 +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-01-14 02:33:27 +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') or self.url.lower().endswith('&fidelity=high')
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
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)
submission_id=Column(Integer)
2022-01-29 13:43:29 +00:00
comment_id=Column(Integer)