2022-11-15 09:19:08 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
from sqlalchemy.sql.expression import not_, and_, or_
|
2023-05-05 00:35:56 +00:00
|
|
|
from sqlalchemy.orm import load_only
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
from files.classes.mod_logs import ModAction
|
2023-10-07 17:55:50 +00:00
|
|
|
from files.classes.hole_logs import HoleAction
|
2024-03-10 14:27:21 +00:00
|
|
|
from files.classes.private_chats import *
|
2022-12-11 23:44:34 +00:00
|
|
|
from files.helpers.config.const import *
|
2023-01-26 05:39:17 +00:00
|
|
|
from files.helpers.config.modaction_types import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.get import *
|
2023-10-05 10:09:58 +00:00
|
|
|
from files.helpers.can_see import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.routes.wrappers import *
|
2023-08-23 10:42:25 +00:00
|
|
|
from files.routes.comments import _mark_comment_as_read
|
2022-07-08 18:06:54 +00:00
|
|
|
from files.__main__ import app
|
|
|
|
|
|
|
|
@app.post("/clear")
|
2023-02-27 05:33:45 +00:00
|
|
|
@limiter.limit('1/second', scope=rpath)
|
2023-04-02 06:52:26 +00:00
|
|
|
@limiter.limit('1/second', scope=rpath, key_func=get_ID)
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
2022-07-08 18:06:54 +00:00
|
|
|
@auth_required
|
|
|
|
def clear(v):
|
2023-05-05 00:35:56 +00:00
|
|
|
notifs = g.db.query(Notification).join(Notification.comment).filter(
|
|
|
|
Notification.read == False,
|
|
|
|
Notification.user_id == v.id,
|
2023-05-05 00:46:32 +00:00
|
|
|
).options(load_only(Notification.comment_id)).all()
|
2022-07-08 18:06:54 +00:00
|
|
|
for n in notifs:
|
|
|
|
n.read = True
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(n)
|
2023-05-05 00:35:56 +00:00
|
|
|
|
2024-03-10 14:27:21 +00:00
|
|
|
chat_notifs = g.db.query(ChatNotification).filter_by(user_id=v.id)
|
|
|
|
for chat_notif in chat_notifs:
|
|
|
|
g.db.delete(chat_notif)
|
|
|
|
|
2024-02-01 00:30:12 +00:00
|
|
|
v.last_viewed_modmail_notifs = int(time.time())
|
2022-07-08 19:33:26 +00:00
|
|
|
v.last_viewed_post_notifs = int(time.time())
|
2022-08-05 21:50:30 +00:00
|
|
|
v.last_viewed_log_notifs = int(time.time())
|
2024-02-16 20:42:42 +00:00
|
|
|
v.last_viewed_offsite_notifs = int(time.time())
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(v)
|
2022-09-24 00:00:43 +00:00
|
|
|
return {"message": "Notifications marked as read!"}
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/unread")
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
2022-07-08 18:06:54 +00:00
|
|
|
@auth_required
|
|
|
|
def unread(v):
|
2023-03-16 06:27:58 +00:00
|
|
|
listing = g.db.query(Notification, Comment).join(Notification.comment).filter(
|
2022-07-08 18:06:54 +00:00
|
|
|
Notification.read == False,
|
|
|
|
Notification.user_id == v.id,
|
|
|
|
Comment.is_banned == False,
|
|
|
|
Comment.deleted_utc == 0,
|
|
|
|
).order_by(Notification.created_utc.desc()).all()
|
|
|
|
|
|
|
|
for n, c in listing:
|
|
|
|
n.read = True
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(n)
|
|
|
|
|
2023-06-08 00:49:37 +00:00
|
|
|
return {"data":[x[1].json for x in listing]}
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/notifications/messages")
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
2022-07-08 18:06:54 +00:00
|
|
|
@auth_required
|
2023-07-30 00:42:06 +00:00
|
|
|
def notifications_messages(v):
|
2023-05-05 05:23:59 +00:00
|
|
|
page = get_page()
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2022-09-10 00:30:15 +00:00
|
|
|
# All of these queries are horrible. For whomever comes here after me,
|
|
|
|
# PLEASE just turn DMs into their own table and get them out of
|
|
|
|
# Notifications & Comments. It's worth it. Save yourself.
|
2023-03-16 06:27:58 +00:00
|
|
|
message_threads = g.db.query(Comment).filter(
|
2022-09-10 00:30:15 +00:00
|
|
|
Comment.sentto != None,
|
2024-03-08 08:10:23 +00:00
|
|
|
or_(Comment.author_id == v.id, Comment.sentto == v.id),
|
2023-06-23 13:46:42 +00:00
|
|
|
Comment.parent_post == None,
|
2022-09-10 00:30:15 +00:00
|
|
|
Comment.level == 1,
|
|
|
|
)
|
2023-02-27 15:38:12 +00:00
|
|
|
|
2022-09-10 00:30:15 +00:00
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
thread_order = g.db.query(Comment.top_comment_id, Comment.created_utc) \
|
2022-09-10 00:30:15 +00:00
|
|
|
.distinct(Comment.top_comment_id) \
|
|
|
|
.filter(
|
|
|
|
Comment.sentto != None,
|
2024-03-08 08:10:23 +00:00
|
|
|
or_(Comment.author_id == v.id, Comment.sentto == v.id),
|
2022-09-10 00:30:15 +00:00
|
|
|
).order_by(
|
|
|
|
Comment.top_comment_id.desc(),
|
|
|
|
Comment.created_utc.desc()
|
|
|
|
).subquery()
|
|
|
|
|
|
|
|
message_threads = message_threads.join(thread_order,
|
|
|
|
thread_order.c.top_comment_id == Comment.top_comment_id)
|
|
|
|
|
|
|
|
# Clear notifications (used for unread indicator only) for all user messages.
|
2023-05-05 21:45:25 +00:00
|
|
|
|
2023-07-18 09:24:01 +00:00
|
|
|
if not session.get("GLOBAL") and not request.values.get('nr'):
|
2023-03-16 06:27:58 +00:00
|
|
|
notifs_unread_row = g.db.query(Notification.comment_id).join(Comment).filter(
|
2022-09-10 00:30:15 +00:00
|
|
|
Notification.user_id == v.id,
|
2023-03-09 23:41:57 +00:00
|
|
|
Notification.read == False,
|
2024-03-08 08:10:23 +00:00
|
|
|
or_(Comment.author_id == v.id, Comment.sentto == v.id),
|
2023-03-09 23:41:57 +00:00
|
|
|
).all()
|
|
|
|
|
|
|
|
notifs_unread = [n.comment_id for n in notifs_unread_row]
|
2023-07-02 21:42:34 +00:00
|
|
|
notif_list = g.db.query(Notification).filter(
|
2023-03-09 23:41:57 +00:00
|
|
|
Notification.user_id == v.id,
|
|
|
|
Notification.comment_id.in_(notifs_unread),
|
2023-07-02 21:42:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for n in notif_list:
|
|
|
|
n.read = True
|
|
|
|
g.db.add(n)
|
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.flush()
|
2022-07-08 19:42:40 +00:00
|
|
|
|
2023-03-24 23:59:42 +00:00
|
|
|
list_to_perserve_unread_attribute = []
|
|
|
|
comments_unread = g.db.query(Comment).filter(Comment.id.in_(notifs_unread))
|
|
|
|
for c in comments_unread:
|
|
|
|
c.unread = True
|
|
|
|
list_to_perserve_unread_attribute.append(c)
|
|
|
|
|
2023-05-05 00:40:06 +00:00
|
|
|
|
2023-05-05 21:44:24 +00:00
|
|
|
total = message_threads.count()
|
2023-05-05 00:40:06 +00:00
|
|
|
listing = message_threads.order_by(thread_order.c.created_utc.desc()) \
|
|
|
|
.offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE).all()
|
2022-09-10 00:30:15 +00:00
|
|
|
|
2023-06-08 00:49:37 +00:00
|
|
|
if v.client: return {"data":[x.json for x in listing]}
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
return render_template("notifications.html",
|
|
|
|
v=v,
|
|
|
|
notifications=listing,
|
2023-05-05 21:44:24 +00:00
|
|
|
total=total,
|
2022-07-08 18:06:54 +00:00
|
|
|
page=page,
|
|
|
|
standalone=True,
|
|
|
|
render_replies=True,
|
2022-09-04 23:15:37 +00:00
|
|
|
)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
|
2024-03-10 14:27:21 +00:00
|
|
|
@app.get("/notifications/chats")
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
|
|
|
@auth_required
|
|
|
|
def notifications_chats(v):
|
|
|
|
criteria1 = (Chat.id == ChatMembership.chat_id, ChatMembership.user_id == v.id)
|
|
|
|
criteria2 = (Chat.id == ChatNotification.chat_id, ChatNotification.user_id == v.id)
|
2024-03-10 19:30:04 +00:00
|
|
|
chats = g.db.query(Chat, func.count(ChatNotification.chat_id)).join(ChatMembership, and_(*criteria1)).outerjoin(ChatNotification, and_(*criteria2)).group_by(Chat).order_by(func.count(ChatNotification.chat_id).desc(), ChatMembership.created_utc.desc()).all()
|
2024-03-10 18:35:46 +00:00
|
|
|
return render_template("notifications.html", v=v, notifications=chats)
|
2024-03-10 14:27:21 +00:00
|
|
|
|
2024-02-01 16:00:51 +00:00
|
|
|
@app.get("/notifications/modmail")
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
|
|
|
@admin_level_required(PERMS['VIEW_MODMAIL'])
|
|
|
|
def notifications_modmail(v):
|
|
|
|
page = get_page()
|
|
|
|
|
2024-02-01 20:44:16 +00:00
|
|
|
sq = g.db.query(Comment.top_comment_id, Comment.created_utc).distinct(Comment.top_comment_id).filter_by(sentto=MODMAIL_ID).order_by(Comment.top_comment_id, Comment.created_utc.desc()).subquery()
|
2024-02-01 16:14:51 +00:00
|
|
|
|
|
|
|
comments = g.db.query(Comment).filter(Comment.id == sq.c.top_comment_id).order_by(sq.c.created_utc.desc())
|
2024-02-01 16:00:51 +00:00
|
|
|
|
|
|
|
total = comments.count()
|
|
|
|
listing = comments.order_by(Comment.id.desc()).offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE).all()
|
|
|
|
|
|
|
|
for c in listing:
|
|
|
|
c_and_children = [c] + c.replies('old')
|
|
|
|
for c in c_and_children:
|
|
|
|
if c.author_id == v.id: continue
|
|
|
|
c.unread = c.created_utc > v.last_viewed_modmail_notifs
|
|
|
|
|
|
|
|
if not session.get("GLOBAL") and not request.values.get('nr'):
|
|
|
|
v.last_viewed_modmail_notifs = int(time.time())
|
|
|
|
g.db.add(v)
|
|
|
|
|
|
|
|
if v.client: return {"data":[x.json for x in listing]}
|
|
|
|
|
|
|
|
return render_template("notifications.html",
|
|
|
|
v=v,
|
|
|
|
notifications=listing,
|
|
|
|
total=total,
|
|
|
|
page=page,
|
|
|
|
standalone=True,
|
|
|
|
render_replies=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-08 18:06:54 +00:00
|
|
|
@app.get("/notifications/posts")
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
2022-07-08 18:06:54 +00:00
|
|
|
@auth_required
|
2023-07-30 00:42:06 +00:00
|
|
|
def notifications_posts(v):
|
2023-05-05 05:23:59 +00:00
|
|
|
page = get_page()
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2024-02-11 14:16:09 +00:00
|
|
|
or_criteria = [
|
|
|
|
Post.hole.in_(v.followed_holes),
|
|
|
|
and_(
|
|
|
|
Post.author_id.in_(v.followed_users),
|
|
|
|
Post.notify == True,
|
|
|
|
Post.ghost == False,
|
|
|
|
)]
|
|
|
|
|
|
|
|
if v.effortpost_notifs:
|
|
|
|
or_criteria.append(Post.effortpost == True)
|
|
|
|
|
2023-06-07 23:26:32 +00:00
|
|
|
listing = g.db.query(Post).filter(
|
|
|
|
Post.deleted_utc == 0,
|
|
|
|
Post.is_banned == False,
|
2024-02-18 15:29:53 +00:00
|
|
|
Post.draft == False,
|
2023-06-07 23:26:32 +00:00
|
|
|
Post.author_id != v.id,
|
2023-08-11 12:43:58 +00:00
|
|
|
Post.author_id.notin_(v.userblocks),
|
2023-10-07 17:55:50 +00:00
|
|
|
or_(Post.hole == None, Post.hole.notin_(v.hole_blocks)),
|
2024-02-11 15:46:05 +00:00
|
|
|
or_(*or_criteria),
|
2023-06-07 23:26:32 +00:00
|
|
|
).options(load_only(Post.id))
|
2023-05-05 00:40:06 +00:00
|
|
|
|
2023-05-05 21:44:24 +00:00
|
|
|
total = listing.count()
|
2023-05-05 00:40:06 +00:00
|
|
|
|
2023-06-07 23:26:32 +00:00
|
|
|
listing = listing.order_by(Post.created_utc.desc()).offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all()
|
2023-05-05 00:40:06 +00:00
|
|
|
listing = [x.id for x in listing]
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2023-07-17 14:49:26 +00:00
|
|
|
listing = get_posts(listing, v=v)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
for p in listing:
|
|
|
|
p.unread = p.created_utc > v.last_viewed_post_notifs
|
|
|
|
|
2023-07-18 09:24:01 +00:00
|
|
|
if not session.get("GLOBAL") and not request.values.get('nr'):
|
2023-03-09 23:41:57 +00:00
|
|
|
v.last_viewed_post_notifs = int(time.time())
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(v)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2023-06-08 00:49:37 +00:00
|
|
|
if v.client: return {"data":[x.json for x in listing]}
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
return render_template("notifications.html",
|
|
|
|
v=v,
|
|
|
|
notifications=listing,
|
2023-05-05 21:44:24 +00:00
|
|
|
total=total,
|
2022-07-08 18:06:54 +00:00
|
|
|
page=page,
|
|
|
|
standalone=True,
|
|
|
|
render_replies=True,
|
2022-09-04 23:15:37 +00:00
|
|
|
)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/notifications/modactions")
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
2022-11-08 13:49:43 +00:00
|
|
|
@auth_required
|
2023-07-30 00:42:06 +00:00
|
|
|
def notifications_modactions(v):
|
2023-05-05 05:23:59 +00:00
|
|
|
page = get_page()
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2022-11-08 13:49:43 +00:00
|
|
|
if v.admin_level >= PERMS['NOTIFICATIONS_MODERATOR_ACTIONS']:
|
|
|
|
cls = ModAction
|
2023-10-07 17:55:50 +00:00
|
|
|
elif v.moderated_holes:
|
|
|
|
cls = HoleAction
|
2022-11-08 13:49:43 +00:00
|
|
|
else:
|
|
|
|
abort(403)
|
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
listing = g.db.query(cls).filter(cls.user_id != v.id)
|
2022-11-08 13:49:43 +00:00
|
|
|
|
2023-01-27 13:51:04 +00:00
|
|
|
if v.id == AEVANN_ID:
|
2023-01-27 08:18:05 +00:00
|
|
|
listing = listing.filter(cls.kind.in_(AEVANN_MODACTION_TYPES))
|
2022-12-16 19:34:01 +00:00
|
|
|
|
2023-01-26 05:31:47 +00:00
|
|
|
if v.admin_level < PERMS['PROGSTACK']:
|
2023-01-26 05:47:20 +00:00
|
|
|
listing = listing.filter(cls.kind.notin_(MODACTION_PRIVILEGED__TYPES))
|
2023-01-26 05:31:47 +00:00
|
|
|
|
2023-10-07 17:55:50 +00:00
|
|
|
if cls == HoleAction:
|
|
|
|
listing = listing.filter(cls.hole.in_(v.moderated_holes))
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2023-05-05 21:44:24 +00:00
|
|
|
total = listing.count()
|
2023-05-05 00:40:06 +00:00
|
|
|
listing = listing.order_by(cls.id.desc())
|
|
|
|
listing = listing.offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE).all()
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2022-08-05 21:50:30 +00:00
|
|
|
for ma in listing:
|
|
|
|
ma.unread = ma.created_utc > v.last_viewed_log_notifs
|
2022-07-08 19:42:40 +00:00
|
|
|
|
2023-07-18 09:24:01 +00:00
|
|
|
if not session.get("GLOBAL") and not request.values.get('nr'):
|
2023-03-09 23:41:57 +00:00
|
|
|
v.last_viewed_log_notifs = int(time.time())
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(v)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
return render_template("notifications.html",
|
|
|
|
v=v,
|
|
|
|
notifications=listing,
|
2023-05-05 21:44:24 +00:00
|
|
|
total=total,
|
2022-07-08 18:06:54 +00:00
|
|
|
page=page,
|
|
|
|
standalone=True,
|
|
|
|
render_replies=True,
|
2022-09-04 23:15:37 +00:00
|
|
|
)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-02-29 20:59:15 +00:00
|
|
|
@app.get("/notifications/offsite")
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
2022-07-08 18:06:54 +00:00
|
|
|
@auth_required
|
2024-02-16 20:42:42 +00:00
|
|
|
def notifications_offsite(v):
|
2023-05-05 05:23:59 +00:00
|
|
|
page = get_page()
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2024-02-28 23:56:52 +00:00
|
|
|
if v.offsite_mentions == None:
|
|
|
|
abort(403)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2023-03-16 06:27:58 +00:00
|
|
|
listing = g.db.query(Comment).filter(
|
2024-02-23 20:15:21 +00:00
|
|
|
Comment.body_html.like('<p>New site mention%'),
|
2023-06-23 13:46:42 +00:00
|
|
|
Comment.parent_post == None,
|
2022-07-10 14:09:41 +00:00
|
|
|
Comment.author_id == AUTOJANNY_ID
|
2023-05-05 00:40:06 +00:00
|
|
|
)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2023-05-05 21:44:24 +00:00
|
|
|
total = listing.count()
|
2023-05-05 00:40:06 +00:00
|
|
|
listing = listing.order_by(Comment.created_utc.desc()).offset(PAGE_SIZE*(page-1)).limit(PAGE_SIZE).all()
|
2022-11-30 22:33:41 +00:00
|
|
|
|
|
|
|
for ma in listing:
|
2024-02-16 20:42:42 +00:00
|
|
|
ma.unread = ma.created_utc > v.last_viewed_offsite_notifs
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2023-07-18 09:24:01 +00:00
|
|
|
if not session.get("GLOBAL") and not request.values.get('nr'):
|
2024-02-16 20:42:42 +00:00
|
|
|
v.last_viewed_offsite_notifs = int(time.time())
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(v)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2023-06-08 00:49:37 +00:00
|
|
|
if v.client: return {"data":[x.json for x in listing]}
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
return render_template("notifications.html",
|
|
|
|
v=v,
|
|
|
|
notifications=listing,
|
2023-05-05 21:44:24 +00:00
|
|
|
total=total,
|
2022-07-08 18:06:54 +00:00
|
|
|
page=page,
|
|
|
|
standalone=True,
|
|
|
|
render_replies=True,
|
2022-09-04 23:15:37 +00:00
|
|
|
)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/notifications")
|
2023-07-13 13:50:46 +00:00
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
2022-07-08 18:06:54 +00:00
|
|
|
@auth_required
|
2023-07-30 00:42:06 +00:00
|
|
|
def notifications(v):
|
2023-05-05 05:23:59 +00:00
|
|
|
page = get_page()
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2024-01-12 07:20:32 +00:00
|
|
|
if not session.get("GLOBAL") and v.admin_level < PERMS['USER_SHADOWBAN'] and not request.values.get('nr'):
|
2023-03-16 06:27:58 +00:00
|
|
|
unread_and_inaccessible = g.db.query(Notification).join(Notification.comment).join(Comment.author).filter(
|
2022-12-13 16:47:57 +00:00
|
|
|
Notification.user_id == v.id,
|
|
|
|
Notification.read == False,
|
|
|
|
or_(
|
2022-12-19 21:28:37 +00:00
|
|
|
User.shadowbanned != None,
|
2022-12-13 16:47:57 +00:00
|
|
|
Comment.is_banned != False,
|
|
|
|
Comment.deleted_utc != 0,
|
|
|
|
)
|
2023-05-05 00:46:53 +00:00
|
|
|
).options(load_only(Notification.comment_id)).all()
|
2023-05-04 23:05:56 +00:00
|
|
|
for n in unread_and_inaccessible:
|
|
|
|
n.read = True
|
|
|
|
g.db.add(n)
|
2022-12-13 16:47:57 +00:00
|
|
|
|
2023-05-05 00:35:56 +00:00
|
|
|
comments = g.db.query(Comment, Notification).options(load_only(Comment.id)).join(Notification.comment).filter(
|
2022-07-08 18:06:54 +00:00
|
|
|
Notification.user_id == v.id,
|
2024-03-08 08:10:23 +00:00
|
|
|
or_(Comment.sentto == None, Comment.sentto != v.id),
|
2022-11-13 22:13:14 +00:00
|
|
|
)
|
|
|
|
|
2024-01-12 07:20:32 +00:00
|
|
|
if v.admin_level < PERMS['USER_SHADOWBAN']:
|
2022-12-19 21:28:37 +00:00
|
|
|
comments = comments.filter(
|
|
|
|
Comment.is_banned == False,
|
|
|
|
Comment.deleted_utc == 0,
|
|
|
|
)
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2023-05-07 18:38:37 +00:00
|
|
|
total = comments.count()
|
2023-05-05 00:40:06 +00:00
|
|
|
|
2023-04-28 09:43:10 +00:00
|
|
|
comments = comments.order_by(Notification.created_utc.desc(), Comment.id.desc())
|
2023-05-05 00:40:06 +00:00
|
|
|
comments = comments.offset(PAGE_SIZE * (page - 1)).limit(PAGE_SIZE).all()
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
cids = [x[0].id for x in comments]
|
|
|
|
|
|
|
|
listing = []
|
2023-05-07 18:38:37 +00:00
|
|
|
all_items = [x[0] for x in comments]
|
2023-03-11 08:21:33 +00:00
|
|
|
|
|
|
|
for c, n in comments:
|
|
|
|
c.notified_utc = n.created_utc
|
2023-03-11 08:51:19 +00:00
|
|
|
c.collapse = n.read
|
2023-03-11 08:21:33 +00:00
|
|
|
|
2022-07-08 18:06:54 +00:00
|
|
|
for c, n in comments:
|
|
|
|
if n.created_utc > 1620391248: c.notif_utc = n.created_utc
|
|
|
|
|
2023-03-11 10:11:39 +00:00
|
|
|
if not n.read: c.unread = True
|
2023-09-06 18:02:47 +00:00
|
|
|
c.is_notif = True
|
2023-03-11 10:11:39 +00:00
|
|
|
|
2023-06-23 13:46:42 +00:00
|
|
|
if c.parent_post or c.wall_user_id:
|
2023-05-07 18:38:37 +00:00
|
|
|
all_items.append(c)
|
2023-02-25 23:44:31 +00:00
|
|
|
|
2022-07-08 18:06:54 +00:00
|
|
|
if c.replies2 == None:
|
2023-03-16 06:27:58 +00:00
|
|
|
c.replies2 = g.db.query(Comment).filter_by(parent_comment_id=c.id).filter(or_(Comment.author_id == v.id, Comment.id.in_(cids))).order_by(Comment.id.desc()).all()
|
2023-05-07 18:38:37 +00:00
|
|
|
all_items.extend(c.replies2)
|
2022-07-08 18:06:54 +00:00
|
|
|
for x in c.replies2:
|
|
|
|
if x.replies2 == None: x.replies2 = []
|
2023-05-05 21:45:25 +00:00
|
|
|
|
2022-07-08 18:06:54 +00:00
|
|
|
count = 0
|
2023-05-05 01:41:13 +00:00
|
|
|
while count < 30 and c.parent_comment and (c.parent_comment.author_id == v.id or c.parent_comment.id in cids):
|
2022-07-08 18:06:54 +00:00
|
|
|
count += 1
|
|
|
|
c = c.parent_comment
|
2023-03-11 08:21:33 +00:00
|
|
|
|
2023-03-11 09:11:17 +00:00
|
|
|
if c.replies2 == None:
|
2023-03-16 06:27:58 +00:00
|
|
|
c.replies2 = g.db.query(Comment).filter_by(parent_comment_id=c.id).filter(or_(Comment.author_id == v.id, Comment.id.in_(cids))).order_by(Comment.id.desc()).all()
|
2023-05-07 18:38:37 +00:00
|
|
|
all_items.extend(c.replies2)
|
2023-03-11 09:11:17 +00:00
|
|
|
for x in c.replies2:
|
|
|
|
if x.replies2 == None:
|
2023-03-16 06:27:58 +00:00
|
|
|
x.replies2 = g.db.query(Comment).filter_by(parent_comment_id=x.id).filter(or_(Comment.author_id == v.id, Comment.id.in_(cids))).order_by(Comment.id.desc()).all()
|
2023-05-07 18:38:37 +00:00
|
|
|
all_items.extend(x.replies2)
|
2023-03-11 08:21:33 +00:00
|
|
|
|
2023-03-11 09:12:16 +00:00
|
|
|
if not hasattr(c, "notified_utc") or n.created_utc > c.notified_utc:
|
|
|
|
c.notified_utc = n.created_utc
|
|
|
|
c.collapse = n.read
|
|
|
|
|
2023-03-11 08:21:33 +00:00
|
|
|
c.replies2 = sorted(c.replies2, key=lambda x: x.notified_utc if hasattr(x, "notified_utc") else x.id, reverse=True)
|
2022-07-08 18:06:54 +00:00
|
|
|
else:
|
2023-02-26 11:14:18 +00:00
|
|
|
while c.parent_comment_id:
|
2022-07-08 18:06:54 +00:00
|
|
|
c = c.parent_comment
|
2023-03-16 06:27:58 +00:00
|
|
|
c.replies2 = g.db.query(Comment).filter_by(parent_comment_id=c.id).order_by(Comment.id).all()
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
if c not in listing: listing.append(c)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2023-07-18 09:24:01 +00:00
|
|
|
if not n.read and not session.get("GLOBAL") and not request.values.get('nr'):
|
2023-03-11 08:51:19 +00:00
|
|
|
n.read = True
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(n)
|
2023-03-11 08:51:19 +00:00
|
|
|
|
2023-05-07 18:38:37 +00:00
|
|
|
all_items.extend(listing)
|
2022-12-23 22:29:04 +00:00
|
|
|
|
2023-02-28 18:51:11 +00:00
|
|
|
listing2 = {}
|
|
|
|
|
2023-02-26 11:14:18 +00:00
|
|
|
for x in listing:
|
|
|
|
if x.parent_comment_id:
|
2023-02-27 01:56:10 +00:00
|
|
|
parent = x.parent_comment
|
|
|
|
if parent.replies2 == None:
|
2023-03-16 06:27:58 +00:00
|
|
|
parent.replies2 = g.db.query(Comment).filter_by(parent_comment_id=parent.id).filter(or_(Comment.author_id == v.id, Comment.id.in_(cids+[x.id]))).order_by(Comment.id.desc()).all()
|
2023-05-07 18:38:37 +00:00
|
|
|
all_items.extend(parent.replies2)
|
2023-02-28 16:52:29 +00:00
|
|
|
for y in parent.replies2:
|
2023-02-28 17:27:36 +00:00
|
|
|
if y.replies2 == None:
|
|
|
|
y.replies2 = []
|
2023-02-28 18:51:11 +00:00
|
|
|
listing2[parent] = ''
|
2023-02-26 11:14:18 +00:00
|
|
|
else:
|
2023-02-28 18:51:11 +00:00
|
|
|
listing2[x] = ''
|
|
|
|
|
|
|
|
listing = listing2.keys()
|
2023-02-26 11:14:18 +00:00
|
|
|
|
2023-05-07 18:38:37 +00:00
|
|
|
all_items.extend(listing)
|
2023-02-26 11:14:18 +00:00
|
|
|
|
2023-05-07 18:38:37 +00:00
|
|
|
all_cids = [x.id for x in all_items]
|
|
|
|
all_cids.extend(cids)
|
|
|
|
all_cids = set(all_cids)
|
|
|
|
output = get_comments_v_properties(v, None, Comment.id.in_(all_cids))[1]
|
2022-07-08 18:06:54 +00:00
|
|
|
|
2023-08-20 03:06:46 +00:00
|
|
|
g.db.flush()
|
|
|
|
|
2023-06-08 00:49:37 +00:00
|
|
|
if v.client: return {"data":[x.json for x in listing]}
|
2022-07-08 18:06:54 +00:00
|
|
|
|
|
|
|
return render_template("notifications.html",
|
|
|
|
v=v,
|
|
|
|
notifications=listing,
|
2023-05-07 18:38:37 +00:00
|
|
|
total=total,
|
2022-07-08 18:06:54 +00:00
|
|
|
page=page,
|
|
|
|
standalone=True,
|
|
|
|
render_replies=True,
|
2022-09-29 05:43:29 +00:00
|
|
|
)
|
2023-08-23 10:42:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.get("/notification//<int:cid>")
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400)
|
|
|
|
@limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID)
|
|
|
|
@auth_required
|
|
|
|
def notification(v, cid):
|
|
|
|
comment = get_comment(cid, v=v)
|
|
|
|
|
2023-10-05 10:09:58 +00:00
|
|
|
if not can_see(v, comment): abort(403)
|
2023-08-23 10:42:25 +00:00
|
|
|
|
|
|
|
comment.unread = True
|
|
|
|
|
|
|
|
gevent.spawn(_mark_comment_as_read, comment.id, v.id)
|
|
|
|
|
2024-02-18 15:52:51 +00:00
|
|
|
if comment.level > 1:
|
|
|
|
shown_comment = comment.top_comment
|
|
|
|
else:
|
|
|
|
shown_comment = comment
|
|
|
|
|
2023-08-23 10:42:25 +00:00
|
|
|
return render_template("notifications.html",
|
|
|
|
v=v,
|
2024-02-18 15:52:51 +00:00
|
|
|
notifications=[shown_comment],
|
2023-08-23 10:42:25 +00:00
|
|
|
total=1,
|
|
|
|
page=1,
|
|
|
|
standalone=True,
|
|
|
|
render_replies=True,
|
2024-02-18 15:57:25 +00:00
|
|
|
focused_comment=comment,
|
2023-08-23 10:42:25 +00:00
|
|
|
)
|