add group dms

pull/225/head
Aevann 2024-03-08 05:20:37 +02:00
parent 5afb92b767
commit 6b3afb9899
6 changed files with 29 additions and 9 deletions

View File

@ -4,10 +4,11 @@ from urllib.parse import parse_qs, urlencode, urlparse, urlunparse, ParseResult
from flask import g
from sqlalchemy import Column, ForeignKey
from sqlalchemy.dialects.postgresql import TSVECTOR
from sqlalchemy.dialects.postgresql import TSVECTOR, ARRAY
from sqlalchemy.orm import relationship
from sqlalchemy.schema import FetchedValue
from sqlalchemy.sql.sqltypes import *
from sqlalchemy.ext.mutable import MutableList
from files.classes import Base
from files.helpers.config.const import *
@ -199,6 +200,7 @@ class Comment(Base):
pinned_utc = Column(Integer)
num_of_pinned_children = Column(Integer, default=0)
sentto = Column(Integer, ForeignKey("users.id"))
group_dm_ids = Column(MutableList.as_mutable(ARRAY(Integer)))
app_id = Column(Integer, ForeignKey("oauth_apps.id"))
upvotes = Column(Integer, default=1)
downvotes = Column(Integer, default=0)
@ -226,7 +228,8 @@ class Comment(Base):
post = relationship("Post", back_populates="comments")
author = relationship("User", primaryjoin="User.id==Comment.author_id")
senttouser = relationship("User", primaryjoin="User.id==Comment.sentto")
parent_comment = relationship("Comment", remote_side=[id])
parent_comment = relationship("Comment", remote_side=[id], back_populates="child_comments")
child_comments = relationship("Comment", remote_side=[parent_comment_id], back_populates="parent_comment")
awards = relationship("AwardRelationship", order_by="AwardRelationship.awarded_utc.desc()", back_populates="comment")
reports = relationship("CommentReport", order_by="CommentReport.created_utc")
options = relationship("CommentOption", order_by="CommentOption.id")
@ -237,6 +240,10 @@ class Comment(Base):
def __init__(self, *args, **kwargs):
if "created_utc" not in kwargs:
kwargs["created_utc"] = int(time.time())
if "sentto" in kwargs and "group_dm_ids" not in kwargs:
kwargs["group_dm_ids"] = [kwargs["author_id"], kwargs["sentto"]]
super().__init__(*args, **kwargs)
def __repr__(self):

View File

@ -832,7 +832,7 @@ class User(Base):
Notification.user_id == self.id,
Notification.read == False,
Comment.sentto != None,
or_(Comment.author_id==self.id, Comment.sentto==self.id),
Comment.group_dm_ids.any(self.id),
Comment.parent_post == None,
)

View File

@ -42,7 +42,7 @@ def can_see(user, obj):
if obj.sentto == MODMAIL_ID:
if obj.top_comment.author_id == user.id: return True
return user.admin_level >= PERMS['VIEW_MODMAIL']
if obj.sentto != user.id:
if user.id not in obj.group_dm_ids:
return user.admin_level >= PERMS['BLACKJACK_NOTIFICATIONS']
elif isinstance(obj, Hole):
if obj.name == 'chudrama': return bool(user) and user.can_see_chudrama

View File

@ -313,6 +313,7 @@ def messagereply(v):
top_comment_id=parent.top_comment_id,
level=parent.level + 1,
sentto=sentto,
group_dm_ids=parent.group_dm_ids,
body=body,
body_html=body_html,
)
@ -321,6 +322,14 @@ def messagereply(v):
execute_blackjack(v, c, c.body_html, 'chat')
execute_under_siege(v, c, c.body_html, 'chat')
if mention_regex.fullmatch(c.body):
uid = get_user(c.body[1:], attributes=[User.id]).id
if uid not in parent.group_dm_ids:
parent.group_dm_ids.append(uid)
g.db.add(parent)
for child in parent.child_comments:
child.group_dm_ids = parent.group_dm_ids
if user_id and user_id not in {v.id, MODMAIL_ID} | BOT_IDs:
if can_see(user, v):
notif = g.db.query(Notification).filter_by(comment_id=c.id, user_id=user_id).one_or_none()
@ -340,7 +349,8 @@ def messagereply(v):
g.db.add(notif)
elif user_id and user_id not in {v.id, MODMAIL_ID} | BOT_IDs:
c.unread = True
rendered = render_template("comments.html", v=get_account(user_id), comments=[c])
emit('insert_reply', [parent.id, rendered], namespace='/', to=user_id)
for user_id in c.group_dm_ids[1:]:
rendered = render_template("comments.html", v=get_account(user_id), comments=[c])
emit('insert_reply', [parent.id, rendered], namespace='/', to=user_id)
return {"comment": render_template("comments.html", v=v, comments=[c])}

View File

@ -68,7 +68,7 @@ def notifications_messages(v):
# Notifications & Comments. It's worth it. Save yourself.
message_threads = g.db.query(Comment).filter(
Comment.sentto != None,
or_(Comment.author_id == v.id, Comment.sentto == v.id),
Comment.group_dm_ids.any(v.id),
Comment.parent_post == None,
Comment.level == 1,
)
@ -78,7 +78,7 @@ def notifications_messages(v):
.distinct(Comment.top_comment_id) \
.filter(
Comment.sentto != None,
or_(Comment.author_id == v.id, Comment.sentto == v.id),
Comment.group_dm_ids.any(v.id),
).order_by(
Comment.top_comment_id.desc(),
Comment.created_utc.desc()
@ -93,7 +93,8 @@ def notifications_messages(v):
notifs_unread_row = g.db.query(Notification.comment_id).join(Comment).filter(
Notification.user_id == v.id,
Notification.read == False,
or_(Comment.author_id == v.id, Comment.sentto == v.id),
Comment.sentto != None,
Comment.group_dm_ids.any(v.id),
).all()
notifs_unread = [n.comment_id for n in notifs_unread_row]

View File

@ -0,0 +1,2 @@
ALTER TABLE comments add COLUMN group_dm_ids int[];
update comments set group_dm_ids=ARRAY[author_id, sentto] where sentto is not null;