2021-07-21 01:12:26 +00:00
|
|
|
from sqlalchemy.orm import deferred, contains_eager, aliased
|
|
|
|
from secrets import token_hex
|
|
|
|
import pyotp
|
|
|
|
|
2021-09-04 00:52:13 +00:00
|
|
|
from files.helpers.discord import remove_user
|
2021-08-04 15:35:10 +00:00
|
|
|
from files.helpers.images import *
|
2021-08-21 11:06:28 +00:00
|
|
|
from files.helpers.const import *
|
2021-07-21 01:12:26 +00:00
|
|
|
from .alts import Alt
|
|
|
|
from .submission import SaveRelationship
|
|
|
|
from .comment import Notification
|
2021-08-11 15:15:51 +00:00
|
|
|
from .award import AwardRelationship
|
2021-07-21 01:12:26 +00:00
|
|
|
from .subscriptions import *
|
|
|
|
from .userblock import *
|
|
|
|
from .badges import *
|
|
|
|
from .clients import *
|
2021-08-04 15:35:10 +00:00
|
|
|
from files.__main__ import Base, cache
|
|
|
|
from files.helpers.security import *
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-08-05 14:41:32 +00:00
|
|
|
site = environ.get("DOMAIN").strip()
|
2021-09-09 04:39:54 +00:00
|
|
|
site_name = environ.get("SITE_NAME").strip()
|
2021-08-22 20:03:01 +00:00
|
|
|
defaulttheme = environ.get("DEFAULT_THEME", "light").strip()
|
|
|
|
defaultcolor = environ.get("DEFAULT_COLOR", "fff").strip()
|
2021-09-04 16:30:39 +00:00
|
|
|
defaulttimefilter = environ.get("DEFAULT_TIME_FILTER", "all").strip()
|
2021-09-03 20:52:40 +00:00
|
|
|
cardview = bool(int(environ.get("CARD_VIEW", 1)))
|
2021-08-27 22:10:03 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
class User(Base, Stndrd, Age_times):
|
|
|
|
__tablename__ = "users"
|
|
|
|
id = Column(Integer, primary_key=True)
|
2021-07-25 23:49:53 +00:00
|
|
|
username = Column(String)
|
2021-08-19 05:14:52 +00:00
|
|
|
namecolor = Column(String, default=defaultcolor)
|
2021-08-11 20:11:55 +00:00
|
|
|
background = Column(String)
|
2021-07-25 23:49:53 +00:00
|
|
|
customtitle = Column(String)
|
|
|
|
customtitleplain = Column(String)
|
2021-09-03 21:04:04 +00:00
|
|
|
|
2021-09-06 22:40:39 +00:00
|
|
|
if "pcmemes.net" in site:
|
2021-09-03 21:04:04 +00:00
|
|
|
quadrant = Column(String)
|
|
|
|
basedcount = Column(Integer, default=0)
|
2021-09-03 22:26:18 +00:00
|
|
|
pills = Column(String, default="")
|
2021-09-03 21:04:04 +00:00
|
|
|
|
2021-08-19 05:14:52 +00:00
|
|
|
titlecolor = Column(String, default=defaultcolor)
|
|
|
|
theme = Column(String, default=defaulttheme)
|
|
|
|
themecolor = Column(String, default=defaultcolor)
|
2021-09-03 20:52:40 +00:00
|
|
|
cardview = Column(Boolean, default=cardview)
|
2021-07-25 23:49:53 +00:00
|
|
|
song = Column(String)
|
2021-07-28 10:29:14 +00:00
|
|
|
highres = Column(String)
|
2021-07-25 23:49:53 +00:00
|
|
|
profileurl = Column(String)
|
|
|
|
bannerurl = Column(String)
|
2021-07-30 15:00:27 +00:00
|
|
|
patron = Column(Integer, default=0)
|
2021-08-26 21:12:38 +00:00
|
|
|
verified = Column(String)
|
2021-07-25 23:49:53 +00:00
|
|
|
email = Column(String)
|
|
|
|
css = deferred(Column(String))
|
|
|
|
profilecss = deferred(Column(String))
|
|
|
|
passhash = deferred(Column(String))
|
2021-07-28 03:55:47 +00:00
|
|
|
post_count = Column(Integer, default=0)
|
|
|
|
comment_count = Column(Integer, default=0)
|
2021-09-05 17:42:39 +00:00
|
|
|
received_award_count = Column(Integer, default=0)
|
2021-07-21 01:12:26 +00:00
|
|
|
created_utc = Column(Integer, default=0)
|
2021-07-25 13:53:26 +00:00
|
|
|
suicide_utc = Column(Integer, default=0)
|
2021-07-29 06:29:13 +00:00
|
|
|
rent_utc = Column(Integer, default=0)
|
2021-08-31 21:13:34 +00:00
|
|
|
steal_utc = Column(Integer, default=0)
|
|
|
|
fail_utc = Column(Integer, default=0)
|
2021-08-31 23:09:00 +00:00
|
|
|
fail2_utc = Column(Integer, default=0)
|
2021-07-21 01:12:26 +00:00
|
|
|
admin_level = Column(Integer, default=0)
|
|
|
|
agendaposter = Column(Boolean, default=False)
|
|
|
|
agendaposter_expires_utc = Column(Integer, default=0)
|
|
|
|
changelogsub = Column(Boolean, default=False)
|
|
|
|
is_activated = Column(Boolean, default=False)
|
|
|
|
shadowbanned = Column(Boolean, default=False)
|
|
|
|
over_18 = Column(Boolean, default=False)
|
|
|
|
hidevotedon = Column(Boolean, default=False)
|
2021-09-05 19:20:32 +00:00
|
|
|
highlightcomments = Column(Boolean, default=True)
|
2021-07-21 01:12:26 +00:00
|
|
|
slurreplacer = Column(Boolean, default=True)
|
|
|
|
flairchanged = Column(Boolean, default=False)
|
|
|
|
newtab = Column(Boolean, default=False)
|
|
|
|
newtabexternal = Column(Boolean, default=True)
|
2021-09-13 12:06:22 +00:00
|
|
|
oldreddit = Column(Boolean)
|
|
|
|
nitter = Column(Boolean)
|
2021-08-11 18:57:12 +00:00
|
|
|
controversial = Column(Boolean, default=False)
|
2021-08-07 15:50:15 +00:00
|
|
|
submissions = relationship(
|
|
|
|
"Submission",
|
|
|
|
lazy="dynamic",
|
|
|
|
primaryjoin="Submission.author_id==User.id")
|
2021-08-07 15:24:24 +00:00
|
|
|
comments = relationship(
|
|
|
|
"Comment",
|
|
|
|
lazy="dynamic",
|
|
|
|
primaryjoin="Comment.author_id==User.id")
|
2021-08-15 04:18:50 +00:00
|
|
|
bio = Column(String)
|
|
|
|
bio_html = Column(String)
|
2021-08-06 12:44:22 +00:00
|
|
|
badges = relationship("Badge", lazy="dynamic")
|
2021-07-21 01:12:26 +00:00
|
|
|
notifications = relationship(
|
|
|
|
"Notification",
|
|
|
|
lazy="dynamic")
|
|
|
|
|
2021-08-07 11:21:16 +00:00
|
|
|
is_banned = Column(Integer, default=0)
|
|
|
|
unban_utc = Column(Integer, default=0)
|
2021-08-15 04:18:50 +00:00
|
|
|
ban_reason = Column(String)
|
2021-09-12 18:25:59 +00:00
|
|
|
club_banned = Column(Boolean, default=False)
|
2021-07-21 01:12:26 +00:00
|
|
|
login_nonce = Column(Integer, default=0)
|
2021-07-25 23:49:53 +00:00
|
|
|
reserved = Column(String(256))
|
2021-08-04 16:00:57 +00:00
|
|
|
coins = Column(Integer, default=0)
|
2021-07-25 23:49:53 +00:00
|
|
|
mfa_secret = deferred(Column(String(16)))
|
2021-07-21 01:12:26 +00:00
|
|
|
is_private = Column(Boolean, default=False)
|
|
|
|
stored_subscriber_count = Column(Integer, default=0)
|
|
|
|
defaultsortingcomments = Column(String, default="top")
|
|
|
|
defaultsorting = Column(String, default="hot")
|
2021-08-27 22:10:03 +00:00
|
|
|
defaulttime = Column(String, default=defaulttimefilter)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
is_nofollow = Column(Boolean, default=False)
|
2021-08-15 04:18:50 +00:00
|
|
|
custom_filter_list = Column(String(1000))
|
2021-07-25 23:49:53 +00:00
|
|
|
discord_id = Column(String(64))
|
2021-07-21 01:12:26 +00:00
|
|
|
ban_evade = Column(Integer, default=0)
|
|
|
|
original_username = deferred(Column(String(255)))
|
|
|
|
subscriptions = relationship("Subscription")
|
|
|
|
|
|
|
|
following = relationship("Follow", primaryjoin="Follow.user_id==User.id")
|
|
|
|
followers = relationship("Follow", primaryjoin="Follow.target_id==User.id")
|
|
|
|
|
2021-07-23 15:51:07 +00:00
|
|
|
viewers = relationship("ViewerRelationship", primaryjoin="User.id == ViewerRelationship.user_id")
|
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
blocking = relationship("UserBlock", lazy="dynamic", primaryjoin="User.id==UserBlock.user_id")
|
|
|
|
blocked = relationship("UserBlock", lazy="dynamic", primaryjoin="User.id==UserBlock.target_id")
|
|
|
|
|
|
|
|
_applications = relationship("OauthApp", lazy="dynamic")
|
|
|
|
authorizations = relationship("ClientAuth", lazy="dynamic")
|
|
|
|
|
2021-07-27 11:07:57 +00:00
|
|
|
awards = relationship(
|
|
|
|
"AwardRelationship",
|
|
|
|
lazy="dynamic",
|
|
|
|
primaryjoin="User.id==AwardRelationship.user_id"
|
|
|
|
)
|
|
|
|
|
2021-08-06 12:22:29 +00:00
|
|
|
referred_by = Column(Integer, ForeignKey("users.id"))
|
|
|
|
|
|
|
|
referrals = relationship(
|
|
|
|
"User",
|
|
|
|
lazy="joined"
|
|
|
|
)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
|
|
|
if "password" in kwargs:
|
|
|
|
kwargs["passhash"] = self.hash_password(kwargs["password"])
|
|
|
|
kwargs.pop("password")
|
|
|
|
|
|
|
|
kwargs["created_utc"] = int(time.time())
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
2021-08-06 12:22:29 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def referral_count(self):
|
2021-08-06 12:47:25 +00:00
|
|
|
return len(self.referrals)
|
2021-08-06 12:22:29 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
def has_block(self, target):
|
|
|
|
|
|
|
|
return g.db.query(UserBlock).filter_by(
|
|
|
|
user_id=self.id, target_id=target.id).first()
|
|
|
|
|
|
|
|
def any_block_exists(self, other):
|
|
|
|
|
|
|
|
return g.db.query(UserBlock).filter(
|
|
|
|
or_(and_(UserBlock.user_id == self.id, UserBlock.target_id == other.id), and_(
|
|
|
|
UserBlock.user_id == other.id, UserBlock.target_id == self.id))).first()
|
|
|
|
|
|
|
|
def validate_2fa(self, token):
|
|
|
|
|
|
|
|
x = pyotp.TOTP(self.mfa_secret)
|
|
|
|
return x.verify(token, valid_window=1)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def age(self):
|
|
|
|
return int(time.time()) - self.created_utc
|
|
|
|
|
|
|
|
@property
|
|
|
|
def strid(self):
|
|
|
|
return str(self.id)
|
|
|
|
|
2021-08-31 16:32:04 +00:00
|
|
|
@cache.memoize(timeout=86400)
|
2021-07-21 01:12:26 +00:00
|
|
|
def userpagelisting(self, v=None, page=1, sort="new", t="all"):
|
|
|
|
|
|
|
|
submissions = g.db.query(Submission).options(lazyload('*')).filter_by(author_id=self.id, is_pinned=False)
|
|
|
|
|
|
|
|
if not (v and (v.admin_level >= 3 or v.id == self.id)):
|
2021-09-01 22:47:13 +00:00
|
|
|
submissions = submissions.filter_by(deleted_utc=0, is_banned=False, private=False)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
now = int(time.time())
|
|
|
|
if t == 'hour':
|
|
|
|
cutoff = now - 3600
|
|
|
|
elif t == 'day':
|
|
|
|
cutoff = now - 86400
|
|
|
|
elif t == 'week':
|
|
|
|
cutoff = now - 604800
|
|
|
|
elif t == 'month':
|
|
|
|
cutoff = now - 2592000
|
|
|
|
elif t == 'year':
|
|
|
|
cutoff = now - 31536000
|
|
|
|
else:
|
|
|
|
cutoff = 0
|
|
|
|
submissions = submissions.filter(Submission.created_utc >= cutoff)
|
|
|
|
|
|
|
|
if sort == "new":
|
|
|
|
submissions = submissions.order_by(Submission.created_utc.desc()).all()
|
|
|
|
elif sort == "old":
|
|
|
|
submissions = submissions.order_by(Submission.created_utc.asc()).all()
|
|
|
|
elif sort == "controversial":
|
|
|
|
submissions = sorted(submissions.all(), key=lambda x: x.score_disputed, reverse=True)
|
|
|
|
elif sort == "top":
|
2021-08-06 12:22:29 +00:00
|
|
|
submissions = sorted(submissions.all(), key=lambda x: x.score, reverse=True)
|
2021-07-21 01:12:26 +00:00
|
|
|
elif sort == "bottom":
|
2021-08-06 12:22:29 +00:00
|
|
|
submissions = sorted(submissions.all(), key=lambda x: x.score)
|
2021-07-21 01:12:26 +00:00
|
|
|
elif sort == "comments":
|
2021-08-22 13:15:13 +00:00
|
|
|
submissions = submissions.order_by(Submission.comment_count.desc()).all()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
firstrange = 25 * (page - 1)
|
|
|
|
secondrange = firstrange + 26
|
|
|
|
listing = [x.id for x in submissions[firstrange:secondrange]]
|
|
|
|
return listing
|
|
|
|
|
2021-08-31 16:32:04 +00:00
|
|
|
@cache.memoize(timeout=86400)
|
2021-07-21 01:12:26 +00:00
|
|
|
def commentlisting(self, v=None, page=1, sort="new", t="all"):
|
2021-07-28 04:17:23 +00:00
|
|
|
comments = self.comments.options(lazyload('*')).filter(Comment.parent_submission != None).join(Comment.post)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
if (not v) or (v.id != self.id and v.admin_level == 0):
|
|
|
|
comments = comments.filter(Comment.deleted_utc == 0)
|
|
|
|
comments = comments.filter(Comment.is_banned == False)
|
|
|
|
|
|
|
|
comments = comments.options(contains_eager(Comment.post))
|
|
|
|
|
2021-09-12 19:20:57 +00:00
|
|
|
if not v:
|
2021-09-12 19:29:36 +00:00
|
|
|
comments = comments.filter(Submission.club == False)
|
2021-09-12 19:20:57 +00:00
|
|
|
elif v.admin_level < 3 and (v.coins < 750 or v.club_banned):
|
2021-09-12 19:29:36 +00:00
|
|
|
comments = comments.filter(Submission.club == False)
|
2021-09-12 19:20:57 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
now = int(time.time())
|
|
|
|
if t == 'hour':
|
|
|
|
cutoff = now - 3600
|
|
|
|
elif t == 'day':
|
|
|
|
cutoff = now - 86400
|
|
|
|
elif t == 'week':
|
|
|
|
cutoff = now - 604800
|
|
|
|
elif t == 'month':
|
|
|
|
cutoff = now - 2592000
|
|
|
|
elif t == 'year':
|
|
|
|
cutoff = now - 31536000
|
|
|
|
else:
|
|
|
|
cutoff = 0
|
|
|
|
comments = comments.filter(Comment.created_utc >= cutoff)
|
|
|
|
|
|
|
|
if sort == "new":
|
|
|
|
comments = comments.order_by(Comment.created_utc.desc()).all()
|
|
|
|
elif sort == "old":
|
|
|
|
comments = comments.order_by(Comment.created_utc.asc()).all()
|
|
|
|
elif sort == "controversial":
|
|
|
|
comments = sorted(comments.all(), key=lambda x: x.score_disputed, reverse=True)
|
|
|
|
elif sort == "top":
|
2021-08-06 12:22:29 +00:00
|
|
|
comments = sorted(comments.all(), key=lambda x: x.score, reverse=True)
|
2021-07-21 01:12:26 +00:00
|
|
|
elif sort == "bottom":
|
2021-08-06 12:22:29 +00:00
|
|
|
comments = sorted(comments.all(), key=lambda x: x.score)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
firstrange = 25 * (page - 1)
|
|
|
|
secondrange = firstrange + 26
|
|
|
|
return [x.id for x in comments[firstrange:secondrange]]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fullname(self):
|
2021-07-30 05:31:38 +00:00
|
|
|
return f"t1_{self.id}"
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def banned_by(self):
|
|
|
|
|
2021-09-07 02:41:21 +00:00
|
|
|
if not self.is_suspended: return None
|
2021-07-21 01:12:26 +00:00
|
|
|
return g.db.query(User).filter_by(id=self.is_banned).first()
|
|
|
|
|
|
|
|
def has_badge(self, badgedef_id):
|
|
|
|
return self.badges.filter_by(badge_id=badgedef_id).first()
|
|
|
|
|
|
|
|
def hash_password(self, password):
|
|
|
|
return generate_password_hash(
|
|
|
|
password, method='pbkdf2:sha512', salt_length=8)
|
|
|
|
|
|
|
|
def verifyPass(self, password):
|
|
|
|
return check_password_hash(self.passhash, password)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def formkey(self):
|
|
|
|
|
|
|
|
if "session_id" not in session:
|
|
|
|
session["session_id"] = token_hex(16)
|
|
|
|
|
|
|
|
msg = f"{session['session_id']}+{self.id}+{self.login_nonce}"
|
|
|
|
|
|
|
|
return generate_hash(msg)
|
|
|
|
|
|
|
|
def validate_formkey(self, formkey):
|
|
|
|
|
|
|
|
return validate_hash(f"{session['session_id']}+{self.id}+{self.login_nonce}", formkey)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self):
|
|
|
|
return f"/@{self.username}"
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"<User(username={self.username})>"
|
|
|
|
|
2021-07-28 15:37:38 +00:00
|
|
|
@property
|
|
|
|
def unban_string(self):
|
|
|
|
if self.unban_utc == 0:
|
|
|
|
return "permanently banned"
|
|
|
|
|
|
|
|
wait = self.unban_utc - int(time.time())
|
|
|
|
|
|
|
|
if wait < 60:
|
2021-07-28 20:51:29 +00:00
|
|
|
text = f"{wait}s"
|
2021-07-28 15:37:38 +00:00
|
|
|
else:
|
|
|
|
days = wait//(24*60*60)
|
|
|
|
wait -= days*24*60*60
|
|
|
|
|
|
|
|
hours = wait//(60*60)
|
|
|
|
wait -= hours*60*60
|
|
|
|
|
|
|
|
mins = wait//60
|
|
|
|
|
2021-07-28 20:51:29 +00:00
|
|
|
text = f"{days}d {hours:02d}h {mins:02d}m"
|
2021-07-28 15:37:38 +00:00
|
|
|
|
|
|
|
return f"Unban in {text}"
|
|
|
|
|
2021-07-30 18:40:15 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def display_awards(self):
|
|
|
|
|
2021-07-31 09:18:59 +00:00
|
|
|
awards = {}
|
2021-07-30 18:51:29 +00:00
|
|
|
active_awards = [x for x in self.awards if not x.given]
|
2021-07-30 18:40:15 +00:00
|
|
|
|
2021-07-30 18:51:29 +00:00
|
|
|
for a in active_awards:
|
2021-07-31 09:18:59 +00:00
|
|
|
if a.kind in awards:
|
|
|
|
awards[a.kind]['count'] += 1
|
2021-07-30 18:40:15 +00:00
|
|
|
else:
|
2021-07-31 09:18:59 +00:00
|
|
|
awards[a.kind] = a.type
|
|
|
|
awards[a.kind]['count'] = 1
|
2021-07-30 18:40:15 +00:00
|
|
|
|
2021-07-31 09:22:57 +00:00
|
|
|
return sorted(list(awards.values()), key=lambda x: x['kind'], reverse=True)
|
2021-07-30 18:40:15 +00:00
|
|
|
|
2021-08-11 15:15:51 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def received_awards(self):
|
|
|
|
|
|
|
|
awards = {}
|
|
|
|
|
|
|
|
posts_idlist = g.db.query(Submission.id).filter_by(author_id=self.id).subquery()
|
|
|
|
comments_idlist = g.db.query(Comment.id).filter_by(author_id=self.id).subquery()
|
|
|
|
|
|
|
|
post_awards = g.db.query(AwardRelationship).filter(AwardRelationship.submission_id.in_(posts_idlist)).all()
|
|
|
|
comment_awards = g.db.query(AwardRelationship).filter(AwardRelationship.comment_id.in_(comments_idlist)).all()
|
|
|
|
|
|
|
|
total_awards = post_awards + comment_awards
|
|
|
|
|
|
|
|
for a in total_awards:
|
|
|
|
if a.kind in awards:
|
|
|
|
awards[a.kind]['count'] += 1
|
|
|
|
else:
|
|
|
|
awards[a.kind] = a.type
|
|
|
|
awards[a.kind]['count'] = 1
|
|
|
|
|
|
|
|
return sorted(list(awards.values()), key=lambda x: x['kind'], reverse=True)
|
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def post_notifications_count(self):
|
|
|
|
return self.notifications.filter(Notification.read == False).join(Notification.comment).filter(
|
2021-08-21 11:06:28 +00:00
|
|
|
Comment.author_id == AUTOJANNY_ACCOUNT).count()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def notifications_count(self):
|
|
|
|
|
|
|
|
return self.notifications.join(Notification.comment).filter(Notification.read == False,
|
|
|
|
Comment.is_banned == False,
|
|
|
|
Comment.deleted_utc == 0).count()
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def alts(self):
|
|
|
|
|
|
|
|
subq = g.db.query(Alt).filter(
|
|
|
|
or_(
|
|
|
|
Alt.user1 == self.id,
|
|
|
|
Alt.user2 == self.id
|
|
|
|
)
|
|
|
|
).subquery()
|
|
|
|
|
|
|
|
data = g.db.query(
|
|
|
|
User,
|
|
|
|
aliased(Alt, alias=subq)
|
|
|
|
).join(
|
|
|
|
subq,
|
|
|
|
or_(
|
|
|
|
subq.c.user1 == User.id,
|
|
|
|
subq.c.user2 == User.id
|
|
|
|
)
|
|
|
|
).filter(
|
|
|
|
User.id != self.id
|
|
|
|
).order_by(User.username.asc()).all()
|
|
|
|
|
2021-08-23 19:10:49 +00:00
|
|
|
data = [x for x in data]
|
2021-07-21 01:12:26 +00:00
|
|
|
output = []
|
|
|
|
for x in data:
|
|
|
|
user = x[0]
|
|
|
|
user._is_manual = x[1].is_manual
|
|
|
|
output.append(user)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
def alts_threaded(self, db):
|
|
|
|
|
|
|
|
subq = db.query(Alt).filter(
|
|
|
|
or_(
|
|
|
|
Alt.user1 == self.id,
|
|
|
|
Alt.user2 == self.id
|
|
|
|
)
|
|
|
|
).subquery()
|
|
|
|
|
|
|
|
data = db.query(
|
|
|
|
User,
|
|
|
|
aliased(Alt, alias=subq)
|
|
|
|
).join(
|
|
|
|
subq,
|
|
|
|
or_(
|
|
|
|
subq.c.user1 == User.id,
|
|
|
|
subq.c.user2 == User.id
|
|
|
|
)
|
|
|
|
).filter(
|
|
|
|
User.id != self.id
|
|
|
|
).order_by(User.username.asc()).all()
|
|
|
|
|
|
|
|
data = [x for x in data]
|
|
|
|
output = []
|
|
|
|
for x in data:
|
|
|
|
user = x[0]
|
|
|
|
user._is_manual = x[1].is_manual
|
|
|
|
output.append(user)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
|
|
|
def has_follower(self, user):
|
|
|
|
|
2021-07-29 00:11:37 +00:00
|
|
|
return g.db.query(Follow).filter_by(target_id=self.id, user_id=user.id).first()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-09-09 04:39:54 +00:00
|
|
|
@property
|
|
|
|
def banner_url(self):
|
|
|
|
if self.bannerurl: return self.bannerurl
|
2021-09-13 15:58:52 +00:00
|
|
|
else: return f"https://{site}/assets/images/{site_name}/preview.webp"
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-08-31 16:32:04 +00:00
|
|
|
@cache.memoize(timeout=86400)
|
2021-07-21 01:12:26 +00:00
|
|
|
def defaultpicture(self):
|
2021-08-21 07:01:02 +00:00
|
|
|
pic = random.randint(1, 150)
|
2021-09-13 15:58:52 +00:00
|
|
|
return f"https://{site}/assets/images/defaultpictures/{pic}.webp"
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def profile_url(self):
|
2021-07-27 00:05:58 +00:00
|
|
|
if self.profileurl: return self.profileurl
|
2021-08-20 05:52:52 +00:00
|
|
|
elif "rdrama" in site: return self.defaultpicture()
|
2021-09-13 15:58:52 +00:00
|
|
|
else: return f"https://{site}/assets/images/default-profile-pic.webp"
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def json_raw(self):
|
|
|
|
data = {'username': self.username,
|
2021-07-27 00:05:58 +00:00
|
|
|
'url': self.url,
|
2021-07-21 01:12:26 +00:00
|
|
|
'is_banned': bool(self.is_banned),
|
|
|
|
'created_utc': self.created_utc,
|
2021-07-30 05:31:38 +00:00
|
|
|
'id': self.id,
|
2021-07-21 01:12:26 +00:00
|
|
|
'is_private': self.is_private,
|
|
|
|
'profile_url': self.profile_url,
|
2021-09-08 17:58:35 +00:00
|
|
|
'bannerurl': self.bannerurl,
|
2021-07-21 01:12:26 +00:00
|
|
|
'bio': self.bio,
|
|
|
|
'bio_html': self.bio_html,
|
|
|
|
'flair': self.customtitle
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def json_core(self):
|
|
|
|
|
|
|
|
now = int(time.time())
|
2021-07-26 01:34:58 +00:00
|
|
|
if self.is_banned and (not self.unban_utc or now < self.unban_utc):
|
2021-07-21 01:12:26 +00:00
|
|
|
return {'username': self.username,
|
2021-07-27 00:05:58 +00:00
|
|
|
'url': self.url,
|
2021-07-21 01:12:26 +00:00
|
|
|
'is_banned': True,
|
|
|
|
'is_permanent_ban': not bool(self.unban_utc),
|
|
|
|
'ban_reason': self.ban_reason,
|
2021-07-30 05:31:38 +00:00
|
|
|
'id': self.id
|
2021-07-21 01:12:26 +00:00
|
|
|
}
|
|
|
|
return self.json_raw
|
|
|
|
|
|
|
|
@property
|
|
|
|
def json(self):
|
|
|
|
data = self.json_core
|
|
|
|
|
|
|
|
data["badges"] = [x.json_core for x in self.badges]
|
2021-08-04 16:00:57 +00:00
|
|
|
data['coins'] = int(self.coins)
|
2021-07-21 01:12:26 +00:00
|
|
|
data['post_count'] = self.post_count
|
|
|
|
data['comment_count'] = self.comment_count
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def ban(self, admin=None, reason=None, days=0):
|
|
|
|
|
|
|
|
if days > 0:
|
|
|
|
ban_time = int(time.time()) + (days * 86400)
|
|
|
|
self.unban_utc = ban_time
|
|
|
|
|
|
|
|
else:
|
2021-07-27 00:05:58 +00:00
|
|
|
self.bannerurl = None
|
|
|
|
self.profileurl = None
|
2021-09-04 00:52:13 +00:00
|
|
|
if self.discord_id: remove_user(self)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-08-21 11:06:28 +00:00
|
|
|
self.is_banned = admin.id if admin else AUTOJANNY_ACCOUNT
|
2021-07-27 00:05:58 +00:00
|
|
|
if reason: self.ban_reason = reason
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-27 00:05:58 +00:00
|
|
|
g.db.add(self)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
def unban(self):
|
|
|
|
|
2021-07-26 02:06:05 +00:00
|
|
|
self.is_banned = 0
|
|
|
|
self.unban_utc = 0
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
g.db.add(self)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_suspended(self):
|
2021-07-26 01:34:58 +00:00
|
|
|
return (self.is_banned and (not self.unban_utc or self.unban_utc > time.time()))
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_blocking(self):
|
2021-09-09 14:15:27 +00:00
|
|
|
return self.__dict__.get('_is_blocking', 0)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_blocked(self):
|
2021-09-09 14:15:27 +00:00
|
|
|
return self.__dict__.get('_is_blocked', 0)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
def applications(self):
|
|
|
|
return [x for x in self._applications.order_by(
|
|
|
|
OauthApp.id.asc()).all()]
|
|
|
|
|
|
|
|
def subscribed_idlist(self, page=1):
|
|
|
|
posts = g.db.query(Subscription.submission_id).filter_by(user_id=self.id).all()
|
|
|
|
return [x[0] for x in posts]
|
|
|
|
|
|
|
|
def saved_idlist(self, page=1):
|
|
|
|
|
|
|
|
posts = g.db.query(Submission.id).options(lazyload('*')).filter_by(is_banned=False,
|
|
|
|
deleted_utc=0
|
|
|
|
)
|
|
|
|
|
|
|
|
saved = g.db.query(SaveRelationship.submission_id).filter(SaveRelationship.user_id == self.id).subquery()
|
|
|
|
posts = posts.filter(Submission.id.in_(saved))
|
|
|
|
|
|
|
|
if self.admin_level == 0:
|
|
|
|
blocking = g.db.query(
|
|
|
|
UserBlock.target_id).filter_by(
|
|
|
|
user_id=self.id).subquery()
|
|
|
|
blocked = g.db.query(
|
|
|
|
UserBlock.user_id).filter_by(
|
|
|
|
target_id=self.id).subquery()
|
|
|
|
|
|
|
|
posts = posts.filter(
|
|
|
|
Submission.author_id.notin_(blocking),
|
|
|
|
Submission.author_id.notin_(blocked)
|
|
|
|
)
|
|
|
|
|
|
|
|
posts = posts.order_by(Submission.created_utc.desc())
|
|
|
|
|
|
|
|
return [x[0] for x in posts.offset(25 * (page - 1)).limit(26).all()]
|
|
|
|
|
|
|
|
def saved_comment_idlist(self, page=1):
|
|
|
|
|
|
|
|
comments = g.db.query(Comment.id).options(lazyload('*')).filter_by(is_banned=False, deleted_utc=0)
|
|
|
|
|
|
|
|
saved = g.db.query(SaveRelationship.submission_id).filter(SaveRelationship.user_id == self.id).subquery()
|
|
|
|
comments = comments.filter(Comment.id.in_(saved))
|
|
|
|
|
|
|
|
if self.admin_level == 0:
|
|
|
|
blocking = g.db.query(
|
|
|
|
UserBlock.target_id).filter_by(
|
|
|
|
user_id=self.id).subquery()
|
|
|
|
blocked = g.db.query(
|
|
|
|
UserBlock.user_id).filter_by(
|
|
|
|
target_id=self.id).subquery()
|
|
|
|
|
|
|
|
comments = comments.filter(
|
|
|
|
Comment.author_id.notin_(blocking),
|
|
|
|
Comment.author_id.notin_(blocked)
|
|
|
|
)
|
|
|
|
|
|
|
|
comments = comments.order_by(Comment.created_utc.desc())
|
|
|
|
|
|
|
|
return [x[0] for x in comments.offset(25 * (page - 1)).limit(26).all()]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def filter_words(self):
|
|
|
|
l = [i.strip() for i in self.custom_filter_list.split('\n')] if self.custom_filter_list else []
|
|
|
|
l = [i for i in l if i]
|
|
|
|
return l
|
|
|
|
|
2021-07-23 15:51:07 +00:00
|
|
|
|
|
|
|
class ViewerRelationship(Base):
|
|
|
|
|
|
|
|
__tablename__ = "viewers"
|
|
|
|
|
|
|
|
id = Column(Integer, Sequence('viewers_id_seq'), primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id'))
|
|
|
|
viewer_id = Column(Integer, ForeignKey('users.id'))
|
|
|
|
last_view_utc = Column(Integer)
|
|
|
|
|
|
|
|
user = relationship("User", lazy="joined", primaryjoin="ViewerRelationship.user_id == User.id")
|
|
|
|
viewer = relationship("User", lazy="joined", primaryjoin="ViewerRelationship.viewer_id == User.id")
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
|
|
|
if 'last_view_utc' not in kwargs:
|
|
|
|
kwargs['last_view_utc'] = int(time.time())
|
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def last_view_since(self):
|
|
|
|
|
|
|
|
return int(time.time()) - self.last_view_utc
|
|
|
|
|
|
|
|
@property
|
|
|
|
def last_view_string(self):
|
|
|
|
|
|
|
|
age = self.last_view_since
|
|
|
|
|
|
|
|
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)
|
2021-08-21 11:06:28 +00:00
|
|
|
return f"{years}yr ago"
|