rework reddit notifs - testing on pcm lol

pull/34/head
Aevann1 2022-12-01 00:29:13 +02:00
parent d160a1686f
commit e1ed46fa36
3 changed files with 16 additions and 37 deletions

View File

@ -129,6 +129,7 @@ class User(Base):
total_lottery_winnings = Column(Integer, default=0) total_lottery_winnings = Column(Integer, default=0)
last_viewed_post_notifs = Column(Integer, default=0) last_viewed_post_notifs = Column(Integer, default=0)
last_viewed_log_notifs = Column(Integer, default=0) last_viewed_log_notifs = Column(Integer, default=0)
last_viewed_reddit_notifs = Column(Integer, default=0)
pronouns = Column(String, default='they/them') pronouns = Column(String, default='they/them')
bite = Column(Integer) bite = Column(Integer)
earlylife = Column(Integer) earlylife = Column(Integer)
@ -701,9 +702,9 @@ class User(Base):
@property @property
@lazy @lazy
def reddit_notifications_count(self): def reddit_notifications_count(self):
if not self.can_view_offsitementions: return 0 if not self.can_view_offsitementions or self.id == AEVANN_ID: return 0
return g.db.query(Notification).join(Comment).filter( return g.db.query(Comment).filter(
Notification.user_id == self.id, Notification.read == False, Comment.created_utc > self.last_viewed_reddit_notifs,
Comment.is_banned == False, Comment.deleted_utc == 0, Comment.is_banned == False, Comment.deleted_utc == 0,
Comment.body_html.like('%<p>New site mention%<a href="https://old.reddit.com/r/%'), Comment.body_html.like('%<p>New site mention%<a href="https://old.reddit.com/r/%'),
Comment.parent_submission == None, Comment.author_id == AUTOJANNY_ID).count() Comment.parent_submission == None, Comment.author_id == AUTOJANNY_ID).count()

View File

@ -10,7 +10,6 @@ from sqlalchemy import or_
import files.helpers.const as const import files.helpers.const as const
from files.classes.badges import Badge from files.classes.badges import Badge
from files.classes.comment import Comment from files.classes.comment import Comment
from files.classes.notifications import Notification
from files.classes.user import User from files.classes.user import User
from files.helpers.sanitize import sanitize from files.helpers.sanitize import sanitize
@ -21,18 +20,8 @@ from files.helpers.sanitize import sanitize
# value from /meta (or just guessing) and doing a random selection of keywords. # value from /meta (or just guessing) and doing a random selection of keywords.
def offsite_mentions_task(cache:Cache): def offsite_mentions_task(cache:Cache):
if const.REDDIT_NOTIFS_SITE: site_mentions = get_mentions(cache, const.REDDIT_NOTIFS_SITE)
row_send_to = g.db.query(Badge.user_id).filter_by(badge_id=140).all() notify_mentions(send_to, site_mentions)
row_send_to += g.db.query(User.id).filter(
User.admin_level >= const.PERMS['NOTIFICATIONS_REDDIT'],
User.id != const.AEVANN_ID,
).all()
send_to = [x[0] for x in row_send_to]
send_to = set(send_to)
site_mentions = get_mentions(cache, const.REDDIT_NOTIFS_SITE)
notify_mentions(send_to, site_mentions)
if const.REDDIT_NOTIFS_USERS: if const.REDDIT_NOTIFS_USERS:
for query, send_user in const.REDDIT_NOTIFS_USERS.items(): for query, send_user in const.REDDIT_NOTIFS_USERS.items():
@ -119,6 +108,7 @@ def notify_mentions(send_to, mentions, mention_str='site mention'):
g.db.flush() g.db.flush()
new_comment.top_comment_id = new_comment.id new_comment.top_comment_id = new_comment.id
for user_id in send_to: if mention_str == 'mention of you':
notif = Notification(comment_id=new_comment.id, user_id=user_id) for user_id in send_to:
g.db.add(notif) notif = Notification(comment_id=new_comment.id, user_id=user_id)
g.db.add(notif)

View File

@ -224,28 +224,17 @@ def notifications_reddit(v:User):
if not v.can_view_offsitementions: abort(403) if not v.can_view_offsitementions: abort(403)
notifications = g.db.query(Notification, Comment).join(Notification.comment).filter( listing = g.db.query(Comment).filter(
Notification.user_id == v.id, Comment.created_utc > v.last_viewed_reddit_notifs,
Comment.body_html.like('%<p>New site mention%<a href="https://old.reddit.com/r/%'), Comment.body_html.like('%<p>New site mention%<a href="https://old.reddit.com/r/%'),
Comment.parent_submission == None, Comment.parent_submission == None,
Comment.author_id == AUTOJANNY_ID Comment.author_id == AUTOJANNY_ID
).order_by(Notification.created_utc.desc()).offset(25 * (page - 1)).limit(101).all() ).order_by(Comment.created_utc.desc()).offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE+1).all()
listing = [] next_exists = len(listing) > PAGE_SIZE
for index, x in enumerate(notifications[:100]): v.last_viewed_reddit_notifs = int(time.time())
n, c = x g.db.add(v)
if n.read and index > 24: break
elif not n.read:
n.read = True
c.unread = True
g.db.add(n)
if n.created_utc > 1620391248: c.notif_utc = n.created_utc
listing.append(c)
next_exists = (len(notifications) > len(listing))
g.db.commit()
if v.client: return {"data":[x.json(g.db) for x in listing]} if v.client: return {"data":[x.json(g.db) for x in listing]}
@ -271,7 +260,6 @@ def notifications(v:User):
Notification.user_id == v.id, Notification.user_id == v.id,
Comment.is_banned == False, Comment.is_banned == False,
Comment.deleted_utc == 0, Comment.deleted_utc == 0,
Comment.body_html.notlike('%<p>New site mention%<a href="https://old.reddit.com/r/%'),
or_(Comment.sentto == None, Comment.sentto == MODMAIL_ID), or_(Comment.sentto == None, Comment.sentto == MODMAIL_ID),
not_(and_(Comment.sentto != None, Comment.sentto == MODMAIL_ID, User.is_muted)), not_(and_(Comment.sentto != None, Comment.sentto == MODMAIL_ID, User.is_muted)),
) )