MarseyWorld/files/classes/submission.py

413 lines
11 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
2021-12-11 02:27:46 +00:00
from files.helpers.const import AUTOPOLLER_ID, AUTOBETTER_ID, censor_slurs, TROLLTITLES
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-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-11-16 21:21:53 +00:00
if site == 'pcmemes.net': cc = "SPLASH MOUNTAIN"
else: cc = "COUNTRY CLUB"
2021-07-21 01:12:26 +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-11-26 21:24:47 +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
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-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)
awards = relationship("AwardRelationship", viewonly=True)
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
2021-09-19 18:22:57 +00:00
2021-11-05 21:30:54 +00:00
@property
@lazy
def flags(self):
2021-11-06 15:52:48 +00:00
return g.db.query(Flag).filter_by(post_id=self.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):
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-12-05 19:45:08 +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_text.webp"
2021-07-28 02:27:01 +00:00
elif self.thumburl: return self.thumburl
2021-12-05 19:45:08 +00:00
elif "youtu.be" in self.domain or "youtube.com" == 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,
'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
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):
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")
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-12-02 18:45:03 +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
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-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()
2021-07-21 01:12:26 +00:00
return body
2021-10-02 20:58:14 +00:00
def plainbody(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)): return f"<p>{cc} 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
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)
2021-11-16 21:21:53 +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)
2021-11-16 21:21:53 +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-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)
submission_id=Column(Integer)
comment_id=Column(Integer)
2021-09-20 21:19:33 +00:00
type=Column(Integer)