2021-11-07 20:34:12 +00:00
|
|
|
from sqlalchemy.orm import deferred, aliased
|
2021-10-15 14:08:27 +00:00
|
|
|
from secrets import token_hex
|
|
|
|
import pyotp
|
|
|
|
from files.helpers.discord import remove_user
|
2022-05-22 16:13:19 +00:00
|
|
|
from files.helpers.media import *
|
2021-10-15 14:08:27 +00:00
|
|
|
from files.helpers.const import *
|
|
|
|
from .alts import Alt
|
2022-02-14 19:02:05 +00:00
|
|
|
from .saves import *
|
2022-02-26 13:31:49 +00:00
|
|
|
from .notifications import Notification
|
2021-10-15 14:08:27 +00:00
|
|
|
from .award import AwardRelationship
|
2022-02-26 13:31:49 +00:00
|
|
|
from .follows import *
|
2021-10-15 14:08:27 +00:00
|
|
|
from .subscriptions import *
|
|
|
|
from .userblock import *
|
|
|
|
from .badges import *
|
|
|
|
from .clients import *
|
2021-12-26 03:18:28 +00:00
|
|
|
from .mod_logs import *
|
2022-02-05 21:09:17 +00:00
|
|
|
from .mod import *
|
2022-02-16 04:33:13 +00:00
|
|
|
from .exiles import *
|
2022-02-14 01:22:26 +00:00
|
|
|
from .sub_block import *
|
2022-06-22 20:03:10 +00:00
|
|
|
from .submission import sort_posts
|
2021-10-15 14:08:27 +00:00
|
|
|
from files.__main__ import Base, cache
|
|
|
|
from files.helpers.security import *
|
2022-06-26 00:22:56 +00:00
|
|
|
from copy import deepcopy
|
2021-10-15 14:08:27 +00:00
|
|
|
import random
|
2021-12-03 19:20:52 +00:00
|
|
|
from os import environ, remove, path
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-04-25 21:59:12 +00:00
|
|
|
defaulttheme = environ.get("DEFAULT_THEME", "midnight").strip()
|
|
|
|
defaulttimefilter = environ.get("DEFAULT_TIME_FILTER", "all").strip()
|
2021-10-15 14:08:27 +00:00
|
|
|
cardview = bool(int(environ.get("CARD_VIEW", 1)))
|
|
|
|
|
2022-05-19 21:59:35 +00:00
|
|
|
if SITE_NAME in ('Cringetopia', 'WPD'): patron_default = 7
|
|
|
|
else: patron_default = 0
|
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
class User(Base):
|
|
|
|
__tablename__ = "users"
|
|
|
|
|
2022-01-19 09:07:16 +00:00
|
|
|
if SITE == "pcmemes.net":
|
2021-10-15 14:08:27 +00:00
|
|
|
basedcount = Column(Integer, default=0)
|
|
|
|
pills = deferred(Column(String, default=""))
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
username = Column(String)
|
2022-02-05 11:31:05 +00:00
|
|
|
namecolor = Column(String, default=DEFAULT_COLOR)
|
2021-10-15 14:08:27 +00:00
|
|
|
background = Column(String)
|
|
|
|
customtitle = Column(String)
|
2021-11-06 17:30:39 +00:00
|
|
|
customtitleplain = deferred(Column(String))
|
2022-02-05 11:31:05 +00:00
|
|
|
titlecolor = Column(String, default=DEFAULT_COLOR)
|
2021-10-15 14:08:27 +00:00
|
|
|
theme = Column(String, default=defaulttheme)
|
2022-02-05 11:31:05 +00:00
|
|
|
themecolor = Column(String, default=DEFAULT_COLOR)
|
2021-10-15 14:08:27 +00:00
|
|
|
cardview = Column(Boolean, default=cardview)
|
|
|
|
song = Column(String)
|
|
|
|
highres = Column(String)
|
|
|
|
profileurl = Column(String)
|
|
|
|
bannerurl = Column(String)
|
2022-02-04 11:13:59 +00:00
|
|
|
house = Column(String)
|
2022-05-19 21:59:35 +00:00
|
|
|
patron = Column(Integer, default=patron_default)
|
2022-01-29 02:48:34 +00:00
|
|
|
patron_utc = Column(Integer, default=0)
|
2021-10-15 14:08:27 +00:00
|
|
|
verified = Column(String)
|
2021-10-27 00:37:34 +00:00
|
|
|
verifiedcolor = Column(String)
|
2021-10-27 20:12:16 +00:00
|
|
|
marseyawarded = Column(Integer)
|
2022-01-31 01:41:04 +00:00
|
|
|
rehab = Column(Integer)
|
2021-11-18 20:50:03 +00:00
|
|
|
longpost = Column(Integer)
|
2022-01-25 01:59:58 +00:00
|
|
|
winnings = Column(Integer, default=0)
|
2021-11-26 21:24:47 +00:00
|
|
|
unblockable = Column(Boolean)
|
2021-11-23 22:36:38 +00:00
|
|
|
bird = Column(Integer)
|
2021-11-06 17:30:39 +00:00
|
|
|
email = deferred(Column(String))
|
2022-06-26 05:39:46 +00:00
|
|
|
css = Column(String)
|
2021-10-15 14:08:27 +00:00
|
|
|
profilecss = deferred(Column(String))
|
|
|
|
passhash = deferred(Column(String))
|
|
|
|
post_count = Column(Integer, default=0)
|
|
|
|
comment_count = Column(Integer, default=0)
|
|
|
|
received_award_count = Column(Integer, default=0)
|
2022-02-13 01:08:12 +00:00
|
|
|
created_utc = Column(Integer)
|
2021-10-15 14:08:27 +00:00
|
|
|
admin_level = Column(Integer, default=0)
|
2022-06-20 20:25:03 +00:00
|
|
|
last_active = Column(Integer, default=0, nullable=False)
|
2021-10-21 19:25:58 +00:00
|
|
|
coins_spent = Column(Integer, default=0)
|
2021-12-14 22:06:54 +00:00
|
|
|
lootboxes_bought = Column(Integer, default=0)
|
2022-02-12 23:10:29 +00:00
|
|
|
agendaposter = Column(Integer, default=0)
|
2021-10-15 14:08:27 +00:00
|
|
|
changelogsub = Column(Boolean, default=False)
|
|
|
|
is_activated = Column(Boolean, default=False)
|
|
|
|
shadowbanned = Column(String)
|
|
|
|
over_18 = Column(Boolean, default=False)
|
|
|
|
hidevotedon = Column(Boolean, default=False)
|
|
|
|
highlightcomments = Column(Boolean, default=True)
|
2022-06-27 02:14:53 +00:00
|
|
|
poorcel = Column(Boolean, default=False)
|
2021-10-15 14:08:27 +00:00
|
|
|
slurreplacer = Column(Boolean, default=True)
|
2021-10-21 20:50:00 +00:00
|
|
|
flairchanged = Column(Integer)
|
2021-10-15 14:08:27 +00:00
|
|
|
newtab = Column(Boolean, default=False)
|
|
|
|
newtabexternal = Column(Boolean, default=True)
|
2022-04-11 10:23:52 +00:00
|
|
|
reddit = Column(String, default='old.reddit.com')
|
2021-11-26 21:24:47 +00:00
|
|
|
nitter = Column(Boolean)
|
|
|
|
mute = Column(Boolean)
|
|
|
|
unmutable = Column(Boolean)
|
|
|
|
eye = Column(Boolean)
|
|
|
|
alt = Column(Boolean)
|
2022-06-10 11:12:19 +00:00
|
|
|
offsitementions = Column(Boolean, default=False, nullable=False)
|
2021-10-15 14:08:27 +00:00
|
|
|
frontsize = Column(Integer, default=25)
|
|
|
|
controversial = Column(Boolean, default=False)
|
2021-11-06 17:30:39 +00:00
|
|
|
bio = deferred(Column(String))
|
2021-11-04 17:02:21 +00:00
|
|
|
bio_html = Column(String)
|
2021-11-06 17:30:39 +00:00
|
|
|
sig = deferred(Column(String))
|
2021-11-04 15:12:17 +00:00
|
|
|
sig_html = Column(String)
|
2021-11-18 16:04:52 +00:00
|
|
|
fp = Column(String)
|
2021-11-26 21:24:47 +00:00
|
|
|
sigs_disabled = Column(Boolean)
|
2021-12-10 04:58:55 +00:00
|
|
|
fish = Column(Boolean)
|
2021-12-31 23:45:27 +00:00
|
|
|
progressivestack = Column(Integer)
|
2022-03-23 22:42:33 +00:00
|
|
|
deflector = Column(Integer)
|
2021-11-06 17:30:39 +00:00
|
|
|
friends = deferred(Column(String))
|
|
|
|
friends_html = deferred(Column(String))
|
|
|
|
enemies = deferred(Column(String))
|
|
|
|
enemies_html = deferred(Column(String))
|
2021-10-15 14:08:27 +00:00
|
|
|
is_banned = Column(Integer, default=0)
|
|
|
|
unban_utc = Column(Integer, default=0)
|
2021-11-06 17:30:39 +00:00
|
|
|
ban_reason = deferred(Column(String))
|
2022-02-13 01:08:12 +00:00
|
|
|
club_allowed = Column(Boolean)
|
2021-10-15 14:08:27 +00:00
|
|
|
login_nonce = Column(Integer, default=0)
|
2021-11-06 17:30:39 +00:00
|
|
|
reserved = deferred(Column(String))
|
2021-10-15 14:08:27 +00:00
|
|
|
coins = Column(Integer, default=0)
|
|
|
|
truecoins = Column(Integer, default=0)
|
2021-10-21 22:55:48 +00:00
|
|
|
procoins = Column(Integer, default=0)
|
2021-10-15 14:08:27 +00:00
|
|
|
mfa_secret = deferred(Column(String))
|
|
|
|
is_private = Column(Boolean, default=False)
|
|
|
|
stored_subscriber_count = Column(Integer, default=0)
|
|
|
|
defaultsortingcomments = Column(String, default="top")
|
|
|
|
defaultsorting = Column(String, default="hot")
|
|
|
|
defaulttime = Column(String, default=defaulttimefilter)
|
|
|
|
is_nofollow = Column(Boolean, default=False)
|
|
|
|
custom_filter_list = Column(String)
|
|
|
|
discord_id = Column(String)
|
|
|
|
ban_evade = Column(Integer, default=0)
|
2022-07-03 11:56:40 +00:00
|
|
|
original_username = Column(String)
|
2021-10-15 14:08:27 +00:00
|
|
|
referred_by = Column(Integer, ForeignKey("users.id"))
|
2022-05-30 09:32:45 +00:00
|
|
|
can_gamble = Column(Boolean, default=True)
|
2022-05-29 03:33:44 +00:00
|
|
|
currently_held_lottery_tickets = Column(Integer, default=0)
|
|
|
|
total_held_lottery_tickets = Column(Integer, default=0)
|
|
|
|
total_lottery_winnings = Column(Integer, default=0)
|
2022-02-10 20:35:16 +00:00
|
|
|
|
2022-07-02 06:48:04 +00:00
|
|
|
badges = relationship("Badge", order_by="Badge.created_utc", back_populates="user")
|
|
|
|
subscriptions = relationship("Subscription", back_populates="user")
|
|
|
|
following = relationship("Follow", primaryjoin="Follow.user_id==User.id", back_populates="user")
|
|
|
|
followers = relationship("Follow", primaryjoin="Follow.target_id==User.id", back_populates="target")
|
|
|
|
viewers = relationship("ViewerRelationship", primaryjoin="User.id == ViewerRelationship.user_id")
|
|
|
|
blocking = relationship("UserBlock", lazy="dynamic", primaryjoin="User.id==UserBlock.user_id", back_populates="user")
|
|
|
|
blocked = relationship("UserBlock", lazy="dynamic", primaryjoin="User.id==UserBlock.target_id", back_populates="target")
|
|
|
|
authorizations = relationship("ClientAuth", back_populates="user")
|
|
|
|
awards = relationship("AwardRelationship", primaryjoin="User.id==AwardRelationship.user_id", back_populates="user")
|
|
|
|
referrals = relationship("User")
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
|
|
|
if "password" in kwargs:
|
|
|
|
kwargs["passhash"] = self.hash_password(kwargs["password"])
|
|
|
|
kwargs.pop("password")
|
|
|
|
|
2022-02-14 21:07:31 +00:00
|
|
|
if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time())
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
2022-07-02 06:48:04 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f"<User(id={self.id})>"
|
|
|
|
|
|
|
|
|
2022-02-05 21:09:17 +00:00
|
|
|
@lazy
|
|
|
|
def mods(self, sub):
|
2022-03-05 19:46:47 +00:00
|
|
|
return self.admin_level == 3 or bool(g.db.query(Mod.user_id).filter_by(user_id=self.id, sub=sub).one_or_none())
|
2022-02-16 04:33:13 +00:00
|
|
|
|
|
|
|
@lazy
|
|
|
|
def exiled_from(self, sub):
|
|
|
|
return self.admin_level < 2 and bool(g.db.query(Exile.user_id).filter_by(user_id=self.id, sub=sub).one_or_none())
|
2022-02-11 23:32:14 +00:00
|
|
|
|
2022-02-14 01:22:26 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def all_blocks(self):
|
2022-02-24 08:28:13 +00:00
|
|
|
return [x[0] for x in g.db.query(SubBlock.sub).filter_by(user_id=self.id).all()]
|
2022-02-14 01:22:26 +00:00
|
|
|
|
|
|
|
@lazy
|
|
|
|
def blocks(self, sub):
|
|
|
|
return g.db.query(SubBlock).filter_by(user_id=self.id, sub=sub).one_or_none()
|
|
|
|
|
2022-06-09 20:24:24 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def all_follows(self):
|
|
|
|
return [x[0] for x in g.db.query(SubSubscription.sub).filter_by(user_id=self.id).all()]
|
|
|
|
|
|
|
|
@lazy
|
|
|
|
def follows(self, sub):
|
|
|
|
return g.db.query(SubSubscription).filter_by(user_id=self.id, sub=sub).one_or_none()
|
|
|
|
|
2022-02-11 23:32:14 +00:00
|
|
|
@lazy
|
|
|
|
def mod_date(self, sub):
|
|
|
|
if self.id == AEVANN_ID: return 1
|
|
|
|
mod = g.db.query(Mod).filter_by(user_id=self.id, sub=sub).one_or_none()
|
|
|
|
if not mod: return None
|
|
|
|
return mod.created_utc
|
2022-02-05 21:09:17 +00:00
|
|
|
|
2021-11-06 18:20:34 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def csslazy(self):
|
|
|
|
return self.css
|
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def created_date(self):
|
|
|
|
|
|
|
|
return time.strftime("%d %b %Y", time.gmtime(self.created_utc))
|
|
|
|
|
2022-06-20 20:25:03 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def last_active_date(self):
|
|
|
|
if self.last_active == 0:
|
|
|
|
return "never"
|
|
|
|
return str(time.strftime("%d %b %Y", time.gmtime(self.last_active)))
|
2022-05-19 21:59:35 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def is_cakeday(self):
|
|
|
|
if time.time() - self.created_utc > 363 * 86400:
|
|
|
|
date = time.strftime("%d %b", time.gmtime(self.created_utc))
|
|
|
|
now = time.strftime("%d %b", time.gmtime())
|
2022-05-26 01:30:25 +00:00
|
|
|
if date == now:
|
|
|
|
if not self.has_badge(134):
|
|
|
|
new_badge = Badge(badge_id=134, user_id=self.id)
|
|
|
|
g.db.add(new_badge)
|
|
|
|
return True
|
2022-05-19 21:59:35 +00:00
|
|
|
return False
|
|
|
|
|
2022-04-05 19:08:06 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def discount(self):
|
|
|
|
if self.patron == 1: discount = 0.90
|
|
|
|
elif self.patron == 2: discount = 0.85
|
|
|
|
elif self.patron == 3: discount = 0.80
|
|
|
|
elif self.patron == 4: discount = 0.75
|
|
|
|
elif self.patron == 5: discount = 0.70
|
|
|
|
elif self.patron == 6: discount = 0.65
|
2022-05-19 21:59:35 +00:00
|
|
|
elif self.patron == 7: discount = 0.60
|
2022-04-05 19:08:06 +00:00
|
|
|
else: discount = 1
|
|
|
|
|
2022-06-26 05:11:25 +00:00
|
|
|
owned_badges = [x.badge_id for x in self.badges]
|
|
|
|
|
2022-05-19 17:41:04 +00:00
|
|
|
for badge in discounts:
|
2022-06-26 05:11:25 +00:00
|
|
|
if badge in owned_badges: discount -= discounts[badge]
|
2022-04-05 19:08:06 +00:00
|
|
|
|
|
|
|
return discount
|
2022-06-10 11:12:19 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def can_view_offsitementions(self):
|
|
|
|
return self.offsitementions or self.admin_level >= REDDIT_NOTIFS_JL_MIN
|
2022-04-05 19:08:06 +00:00
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
2021-11-05 21:30:54 +00:00
|
|
|
def user_awards(self):
|
2021-11-02 21:29:53 +00:00
|
|
|
return_value = list(AWARDS2.values())
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-06-26 00:22:56 +00:00
|
|
|
awards_owned = g.db.query(AwardRelationship.kind, func.count()) \
|
|
|
|
.filter_by(user_id=self.id, submission_id=None, comment_id=None) \
|
|
|
|
.group_by(AwardRelationship.kind).all()
|
|
|
|
awards_owned = dict(awards_owned)
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-06-26 00:22:56 +00:00
|
|
|
for val in return_value:
|
|
|
|
if val['kind'] in awards_owned:
|
|
|
|
val['owned'] = awards_owned[val['kind']]
|
|
|
|
else:
|
|
|
|
val['owned'] = 0
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
return return_value
|
|
|
|
|
|
|
|
@property
|
2022-06-23 16:36:39 +00:00
|
|
|
@lazy
|
2021-10-15 14:08:27 +00:00
|
|
|
def referral_count(self):
|
|
|
|
return len(self.referrals)
|
|
|
|
|
2022-06-23 16:36:39 +00:00
|
|
|
@lazy
|
2022-07-03 17:55:25 +00:00
|
|
|
def has_blocked(self, target):
|
2022-01-02 00:06:46 +00:00
|
|
|
return g.db.query(UserBlock).filter_by(user_id=self.id, target_id=target.id).one_or_none()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def paid_dues(self):
|
2022-04-03 17:37:20 +00:00
|
|
|
return not self.shadowbanned and not (self.is_banned and not self.unban_utc) and (self.admin_level or self.club_allowed or (self.club_allowed != False and self.truecoins > dues))
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-02-23 05:19:57 +00:00
|
|
|
@lazy
|
2021-10-15 14:08:27 +00:00
|
|
|
def any_block_exists(self, other):
|
|
|
|
|
2021-11-06 15:52:48 +00:00
|
|
|
return g.db.query(UserBlock).filter(
|
2021-10-15 14:08:27 +00:00
|
|
|
or_(and_(UserBlock.user_id == self.id, UserBlock.target_id == other.id), and_(
|
2022-02-14 16:27:54 +00:00
|
|
|
UserBlock.user_id == other.id, UserBlock.target_id == self.id))).first()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
def validate_2fa(self, token):
|
|
|
|
|
|
|
|
x = pyotp.TOTP(self.mfa_secret)
|
|
|
|
return x.verify(token, valid_window=1)
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def age(self):
|
|
|
|
return int(time.time()) - self.created_utc
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def alts_unique(self):
|
|
|
|
alts = []
|
|
|
|
for u in self.alts:
|
|
|
|
if u not in alts: alts.append(u)
|
|
|
|
return alts
|
|
|
|
|
2022-01-02 18:03:51 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def alts_patron(self):
|
|
|
|
for u in self.alts_unique:
|
|
|
|
if u.patron: return True
|
|
|
|
return False
|
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
@cache.memoize(timeout=86400)
|
2022-05-01 21:44:38 +00:00
|
|
|
def userpagelisting(self, site=None, v=None, page=1, sort="new", t="all"):
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-12-29 06:43:20 +00:00
|
|
|
if self.shadowbanned and not (v and (v.admin_level > 1 or v.id == self.id)): return []
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-11-06 15:52:48 +00:00
|
|
|
posts = g.db.query(Submission.id).filter_by(author_id=self.id, is_pinned=False)
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-11-15 22:19:59 +00:00
|
|
|
if not (v and (v.admin_level > 1 or v.id == self.id)):
|
2022-06-18 23:55:45 +00:00
|
|
|
posts = posts.filter_by(is_banned=False, private=False, ghost=False)
|
|
|
|
|
|
|
|
if not (v and v.admin_level > 1):
|
|
|
|
posts = posts.filter_by(deleted_utc=0)
|
2021-10-15 14:08:27 +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
|
|
|
|
posts = posts.filter(Submission.created_utc >= cutoff)
|
|
|
|
|
2022-06-22 19:57:57 +00:00
|
|
|
posts = sort_posts(sort, posts)
|
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
posts = posts.offset(25 * (page - 1)).limit(26).all()
|
|
|
|
|
|
|
|
return [x[0] for x in posts]
|
|
|
|
|
2022-01-02 00:05:22 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def follow_count(self):
|
2022-05-19 21:59:35 +00:00
|
|
|
return g.db.query(Follow).filter_by(user_id=self.id).count()
|
2022-01-02 00:05:22 +00:00
|
|
|
|
2021-10-25 02:44:54 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def bio_html_eager(self):
|
2021-12-10 22:49:39 +00:00
|
|
|
if self.bio_html == None: return ''
|
2022-06-20 02:15:33 +00:00
|
|
|
return self.bio_html.replace('data-src', 'src') \
|
2022-06-22 15:59:47 +00:00
|
|
|
.replace('src="/i/loading.webp?v=2000"', '') \
|
2022-06-22 15:51:19 +00:00
|
|
|
.replace('src="/i/loading.webp"', '') \
|
|
|
|
.replace('src="/i/l.webp"', '')
|
2021-10-25 02:44:54 +00:00
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def fullname(self):
|
|
|
|
return f"t1_{self.id}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def banned_by(self):
|
|
|
|
if not self.is_suspended: return None
|
2022-06-18 00:57:23 +00:00
|
|
|
return g.db.get(User, self.is_banned)
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-06-23 16:36:39 +00:00
|
|
|
@lazy
|
2021-11-21 17:40:47 +00:00
|
|
|
def has_badge(self, badge_id):
|
2022-01-02 00:06:46 +00:00
|
|
|
return g.db.query(Badge).filter_by(user_id=self.id, badge_id=badge_id).one_or_none()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
@lazy
|
|
|
|
def formkey(self):
|
|
|
|
|
|
|
|
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
|
|
|
|
@lazy
|
|
|
|
def url(self):
|
2022-04-02 17:11:35 +00:00
|
|
|
return f"/@{self.username}"
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2022-01-21 11:55:53 +00:00
|
|
|
return f"<User(id={self.id})>"
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def unban_string(self):
|
|
|
|
if self.unban_utc == 0:
|
|
|
|
return "permanently banned"
|
|
|
|
|
|
|
|
wait = self.unban_utc - int(time.time())
|
|
|
|
|
|
|
|
if wait < 60:
|
|
|
|
text = f"{wait}s"
|
|
|
|
else:
|
|
|
|
days = wait//(24*60*60)
|
|
|
|
wait -= days*24*60*60
|
|
|
|
|
|
|
|
hours = wait//(60*60)
|
|
|
|
wait -= hours*60*60
|
|
|
|
|
|
|
|
mins = wait//60
|
|
|
|
|
|
|
|
text = f"{days}d {hours:02d}h {mins:02d}m"
|
|
|
|
|
|
|
|
return f"Unban in {text}"
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def received_awards(self):
|
|
|
|
|
|
|
|
awards = {}
|
|
|
|
|
2022-07-03 11:56:40 +00:00
|
|
|
post_awards = g.db.query(AwardRelationship).join(AwardRelationship.post).filter(Submission.author_id == self.id).all()
|
|
|
|
comment_awards = g.db.query(AwardRelationship).join(AwardRelationship.comment).filter(Comment.author_id == self.id).all()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
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-12-26 03:18:28 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def modaction_num(self):
|
|
|
|
if self.admin_level < 2: return 0
|
2022-05-19 21:59:35 +00:00
|
|
|
return g.db.query(ModAction).filter_by(user_id=self.id).count()
|
2021-12-26 03:18:28 +00:00
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def notifications_count(self):
|
2022-06-15 05:43:34 +00:00
|
|
|
notifs = g.db.query(Notification.user_id).join(Comment).filter(
|
|
|
|
Notification.user_id == self.id, Notification.read == False,
|
|
|
|
Comment.is_banned == False, Comment.deleted_utc == 0)
|
2022-04-15 16:28:08 +00:00
|
|
|
|
|
|
|
if not self.shadowbanned and self.admin_level < 3:
|
2022-07-03 06:12:53 +00:00
|
|
|
notifs = notifs.join(Notification.user).filter(User.shadowbanned == None)
|
2022-04-15 16:28:08 +00:00
|
|
|
|
|
|
|
return notifs.count()
|
2021-11-05 17:55:23 +00:00
|
|
|
|
2022-06-15 05:43:34 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def normal_notifications_count(self):
|
|
|
|
return self.notifications_count \
|
|
|
|
- self.post_notifications_count \
|
|
|
|
- self.modaction_notifications_count \
|
|
|
|
- self.reddit_notifications_count
|
|
|
|
|
2021-11-05 17:55:23 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def post_notifications_count(self):
|
2022-06-10 12:28:46 +00:00
|
|
|
return g.db.query(Notification).join(Comment).filter(
|
|
|
|
Notification.user_id == self.id, Notification.read == False,
|
|
|
|
Comment.author_id == AUTOJANNY_ID).count()
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def modaction_notifications_count(self):
|
2022-06-26 01:22:05 +00:00
|
|
|
if not self.admin_level: return 0
|
2022-06-10 12:28:46 +00:00
|
|
|
return g.db.query(Notification).join(Comment).filter(
|
|
|
|
Notification.user_id == self.id, Notification.read == False,
|
|
|
|
Comment.is_banned == False, Comment.deleted_utc == 0,
|
|
|
|
Comment.body_html.like(f'%<p>{NOTIF_MODACTION_PREFIX}%'),
|
|
|
|
Comment.parent_submission == None, Comment.author_id == NOTIFICATIONS_ID).count()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-04-03 18:01:11 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def reddit_notifications_count(self):
|
2022-06-26 01:22:05 +00:00
|
|
|
if not self.can_view_offsitementions: return 0
|
2022-06-10 12:28:46 +00:00
|
|
|
return g.db.query(Notification).join(Comment).filter(
|
|
|
|
Notification.user_id == self.id, Notification.read == False,
|
|
|
|
Comment.is_banned == False, Comment.deleted_utc == 0,
|
|
|
|
Comment.body_html.like('%<p>New site mention: <a href="https://old.reddit.com/r/%'),
|
|
|
|
Comment.parent_submission == None, Comment.author_id == NOTIFICATIONS_ID).count()
|
2022-04-03 18:01:11 +00:00
|
|
|
|
2022-04-06 21:53:14 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
2022-06-15 05:43:34 +00:00
|
|
|
def notifications_do(self):
|
|
|
|
# only meaningful when notifications_count > 0; otherwise falsely '' ~ normal
|
|
|
|
if self.normal_notifications_count > 0:
|
|
|
|
return ''
|
|
|
|
elif self.post_notifications_count > 0:
|
|
|
|
return 'posts'
|
|
|
|
elif self.modaction_notifications_count > 0:
|
|
|
|
return 'modactions'
|
|
|
|
elif self.reddit_notifications_count > 0:
|
|
|
|
return 'reddit'
|
|
|
|
return ''
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def notifications_color(self):
|
|
|
|
colors = {
|
|
|
|
'': '#dc3545',
|
|
|
|
'posts': '#0000ff',
|
|
|
|
'modactions': '#e5990d',
|
|
|
|
'reddit': '#805ad5',
|
|
|
|
}
|
|
|
|
return colors[self.notifications_do] if self.notifications_do \
|
|
|
|
else colors['']
|
2022-04-06 21:53:14 +00:00
|
|
|
|
2021-12-20 23:50:02 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
2022-04-03 18:12:37 +00:00
|
|
|
def do_posts(self):
|
2022-06-10 12:28:46 +00:00
|
|
|
return self.post_notifications_count and \
|
|
|
|
self.post_notifications_count == (
|
|
|
|
self.notifications_count
|
|
|
|
- self.modaction_notifications_count
|
|
|
|
- self.reddit_notifications_count)
|
2022-04-03 18:12:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def do_reddit(self):
|
|
|
|
return self.notifications_count == self.reddit_notifications_count
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def alts(self):
|
|
|
|
|
2021-11-06 15:52:48 +00:00
|
|
|
subq = g.db.query(Alt).filter(
|
2021-10-15 14:08:27 +00:00
|
|
|
or_(
|
|
|
|
Alt.user1 == self.id,
|
|
|
|
Alt.user2 == self.id
|
|
|
|
)
|
|
|
|
).subquery()
|
|
|
|
|
|
|
|
data = g.db.query(
|
|
|
|
User,
|
2021-11-07 20:34:12 +00:00
|
|
|
aliased(Alt, alias=subq)
|
2021-11-06 15:52:48 +00:00
|
|
|
).join(
|
2021-10-15 14:08:27 +00:00
|
|
|
subq,
|
|
|
|
or_(
|
2021-11-07 20:34:12 +00:00
|
|
|
subq.c.user1 == User.id,
|
|
|
|
subq.c.user2 == User.id
|
2021-10-15 14:08:27 +00:00
|
|
|
)
|
|
|
|
).filter(
|
|
|
|
User.id != self.id
|
2022-03-21 21:46:10 +00:00
|
|
|
).order_by(User.username).all()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
output = []
|
|
|
|
for x in data:
|
|
|
|
user = x[0]
|
|
|
|
user._is_manual = x[1].is_manual
|
|
|
|
output.append(user)
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
2022-02-11 23:33:25 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def moderated_subs(self):
|
|
|
|
modded_subs = g.db.query(Mod.sub).filter_by(user_id=self.id).all()
|
|
|
|
return modded_subs
|
|
|
|
|
2022-06-23 16:36:39 +00:00
|
|
|
@lazy
|
2021-10-15 14:08:27 +00:00
|
|
|
def has_follower(self, user):
|
|
|
|
|
2022-01-02 00:06:46 +00:00
|
|
|
return g.db.query(Follow).filter_by(target_id=self.id, user_id=user.id).one_or_none()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def banner_url(self):
|
|
|
|
if self.bannerurl: return self.bannerurl
|
2022-06-22 15:59:47 +00:00
|
|
|
else: return f"/i/{SITE_NAME}/site_preview.webp?v=2000"
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def profile_url(self):
|
2022-05-25 04:41:23 +00:00
|
|
|
if self.agendaposter: return f"{SITE_FULL}/assets/images/pfps/agendaposter/{random.randint(1, 57)}.webp?v=1"
|
2022-01-24 21:38:03 +00:00
|
|
|
if self.profileurl:
|
|
|
|
if self.profileurl.startswith('/'): return SITE_FULL + self.profileurl
|
|
|
|
return self.profileurl
|
2022-04-26 13:33:39 +00:00
|
|
|
return f"{SITE_FULL}/assets/images/default-profile-pic.webp?v=1008"
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-12-10 23:29:35 +00:00
|
|
|
@lazy
|
2021-12-17 05:08:40 +00:00
|
|
|
def json_popover(self, v):
|
2021-12-10 23:29:35 +00:00
|
|
|
data = {'username': self.username,
|
|
|
|
'url': self.url,
|
2022-01-03 01:54:13 +00:00
|
|
|
'id': self.id,
|
2021-12-10 23:29:35 +00:00
|
|
|
'profile_url': self.profile_url,
|
|
|
|
'bannerurl': self.banner_url,
|
|
|
|
'bio_html': self.bio_html_eager,
|
|
|
|
'coins': self.coins,
|
2022-06-12 06:11:59 +00:00
|
|
|
'post_count': 0 if self.shadowbanned and not (v and (v.shadowbanned or v.admin_level >= 2)) else self.post_count,
|
|
|
|
'comment_count': 0 if self.shadowbanned and not (v and (v.shadowbanned or v.admin_level >= 2)) else self.comment_count,
|
2021-12-11 00:53:16 +00:00
|
|
|
'badges': [x.path for x in self.badges],
|
2021-12-10 23:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def json_raw(self):
|
|
|
|
data = {'username': self.username,
|
|
|
|
'url': self.url,
|
|
|
|
'is_banned': bool(self.is_banned),
|
|
|
|
'created_utc': self.created_utc,
|
|
|
|
'id': self.id,
|
|
|
|
'is_private': self.is_private,
|
2022-01-28 21:42:09 +00:00
|
|
|
'profile_url': self.profile_url,
|
2021-12-10 22:52:13 +00:00
|
|
|
'bannerurl': self.banner_url,
|
2021-10-15 14:08:27 +00:00
|
|
|
'bio': self.bio,
|
2021-12-10 22:49:39 +00:00
|
|
|
'bio_html': self.bio_html_eager,
|
2021-10-15 14:08:27 +00:00
|
|
|
'flair': self.customtitle
|
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def json_core(self):
|
|
|
|
|
|
|
|
now = int(time.time())
|
2022-01-06 16:46:09 +00:00
|
|
|
if self.is_suspended:
|
2021-10-15 14:08:27 +00:00
|
|
|
return {'username': self.username,
|
|
|
|
'url': self.url,
|
|
|
|
'is_banned': True,
|
|
|
|
'is_permanent_ban': not bool(self.unban_utc),
|
|
|
|
'ban_reason': self.ban_reason,
|
|
|
|
'id': self.id
|
|
|
|
}
|
|
|
|
return self.json_raw
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def json(self):
|
|
|
|
data = self.json_core
|
|
|
|
|
2021-12-12 19:20:57 +00:00
|
|
|
data["badges"] = [x.json for x in self.badges]
|
2021-12-10 23:29:35 +00:00
|
|
|
data['coins'] = self.coins
|
2021-10-15 14:08:27 +00:00
|
|
|
data['post_count'] = self.post_count
|
|
|
|
data['comment_count'] = self.comment_count
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2021-12-03 19:20:52 +00:00
|
|
|
def ban(self, admin=None, reason=None, days=0):
|
2022-01-16 01:04:31 +00:00
|
|
|
if days:
|
|
|
|
self.unban_utc = int(time.time()) + (days * 86400)
|
|
|
|
g.db.add(self)
|
2022-01-16 00:54:04 +00:00
|
|
|
elif self.discord_id: remove_user(self)
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-11-18 14:21:19 +00:00
|
|
|
self.is_banned = admin.id if admin else AUTOJANNY_ID
|
2021-10-15 14:08:27 +00:00
|
|
|
if reason: self.ban_reason = reason
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
2022-06-23 16:36:39 +00:00
|
|
|
@lazy
|
2021-10-15 14:08:27 +00:00
|
|
|
def is_suspended(self):
|
2022-01-06 16:46:09 +00:00
|
|
|
return (self.is_banned and (self.unban_utc == 0 or self.unban_utc > time.time()))
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def applications(self):
|
2022-05-25 17:01:29 +00:00
|
|
|
return g.db.query(OauthApp).filter_by(author_id=self.id).order_by(OauthApp.id).all()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
|
2022-03-05 21:00:01 +00:00
|
|
|
@property
|
2022-03-05 20:53:39 +00:00
|
|
|
@lazy
|
|
|
|
def userblocks(self):
|
2022-06-29 03:07:00 +00:00
|
|
|
return [x[0] for x in g.db.query(UserBlock.target_id).filter_by(user_id=self.id).all()]
|
2022-03-05 20:53:39 +00:00
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
@lazy
|
|
|
|
def saved_idlist(self, page=1):
|
2022-07-03 02:43:49 +00:00
|
|
|
posts = g.db.query(SaveRelationship.submission_id).filter_by(user_id=self.id).offset(25 * (page - 1)).all()
|
|
|
|
return [x[0] for x in posts]
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
@lazy
|
2022-01-29 13:57:55 +00:00
|
|
|
def saved_comment_idlist(self, page=1):
|
2022-07-03 02:43:49 +00:00
|
|
|
comments = g.db.query(CommentSaveRelationship.comment_id).filter_by(user_id=self.id).offset(25 * (page - 1)).all()
|
|
|
|
return [x[0] for x in comments]
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2022-07-03 02:43:49 +00:00
|
|
|
@lazy
|
|
|
|
def subscribed_idlist(self, page=1):
|
|
|
|
posts = g.db.query(Subscription.submission_id).filter_by(user_id=self.id)
|
|
|
|
return [x[0] for x in posts]
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
|
2022-01-29 13:53:45 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def saved_count(self):
|
2022-07-03 02:43:49 +00:00
|
|
|
return g.db.query(SaveRelationship).filter_by(user_id=self.id).count()
|
2022-01-29 13:53:45 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def saved_comment_count(self):
|
2022-07-03 02:43:49 +00:00
|
|
|
return g.db.query(CommentSaveRelationship).filter_by(user_id=self.id).count()
|
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def subscribed_count(self):
|
|
|
|
return g.db.query(Subscription).filter_by(user_id=self.id).count()
|
2022-01-29 13:53:45 +00:00
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
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]
|
2022-05-19 21:59:35 +00:00
|
|
|
return l
|
2022-05-29 03:33:44 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def lottery_stats(self):
|
2022-06-10 21:40:43 +00:00
|
|
|
return { "winnings": self.total_lottery_winnings, "ticketsHeld": { "current": self.currently_held_lottery_tickets , "total": self.total_held_lottery_tickets } }
|
2022-06-21 05:20:21 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def can_create_hole(self):
|
|
|
|
return self.admin_level >= HOLE_CREATE_JL_MIN
|