Skip to content

Commit

Permalink
cleanup отправки уведомления об удалении
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Nov 24, 2024
1 parent bb229d7 commit f7110c5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
24 changes: 4 additions & 20 deletions sql/updates/2024-11-24-delete-events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,10 @@
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<changeSet id="2024112401" author="Maxim Valyanskiy">
<sql splitStatements="false">
CREATE OR REPLACE FUNCTION public.event_delete()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
DECLARE
thecomment comments%ROWTYPE;
BEGIN
SELECT * INTO thecomment FROM comments WHERE id = NEW.msgid;

IF FOUND THEN
IF thecomment.userid != NEW.delby THEN
INSERT INTO user_events (userid, type, private, message_id, comment_id, message) VALUES (thecomment.userid, 'DEL', 't', thecomment.topic, NEW.msgid, NEW.reason);
END IF;
END IF;

RETURN NULL;
END;
$function$
<changeSet id="2024112402" author="Maxim Valyanskiy">
<sql>
DROP TRIGGER event_delete_t on del_info;
DROP FUNCTION event_delete();
</sql>
</changeSet>
</databaseChangeLog>
37 changes: 23 additions & 14 deletions src/main/scala/ru/org/linux/comment/DeleteService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DeleteService(commentDao: CommentDao, userDao: UserDao, userEventService:
deleteInfoDao.insert(info)

userEventService.processTopicDeleted(Seq(topic.id))
userEventService.insertTopicDeleteNotifications(topic, info)
userEventService.insertTopicDeleteNotification(topic, info)
}
}

Expand All @@ -74,6 +74,7 @@ class DeleteService(commentDao: CommentDao, userDao: UserDao, userEventService:

commentDao.updateStatsAfterDelete(comment.id, 1)
userEventService.processCommentsDeleted(Seq(comment.id))
userEventService.insertCommentDeleteNotification(comment, info)
}

deleted.isDefined
Expand All @@ -94,11 +95,7 @@ class DeleteService(commentDao: CommentDao, userDao: UserDao, userEventService:

val replys = DeleteService.getAllReplys(node, 0)

val deleted = deleteReplys(comment, reason, replys, user, -scoreBonus)

userEventService.processCommentsDeleted(deleted)

deleted
deleteReplys(comment, reason, replys, user, -scoreBonus, notifyReplys = !topic.isExpired)
}

/**
Expand Down Expand Up @@ -207,7 +204,7 @@ class DeleteService(commentDao: CommentDao, userDao: UserDao, userEventService:
* @return список идентификационных номеров удалённых комментариев
*/
private def deleteReplys(root: Comment, rootReason: String, replys: Seq[DeleteService.CommentAndDepth],
user: User, rootBonus: Int): Seq[Int] = {
user: User, rootBonus: Int, notifyReplys: Boolean): Seq[Int] = {
val score = rootBonus < -2

val deleteInfos = new ArrayBuffer[InsertDeleteInfo](initialSize = replys.size)
Expand All @@ -220,13 +217,18 @@ class DeleteService(commentDao: CommentDao, userDao: UserDao, userEventService:

del.foreach { info =>
deleteInfos.addOne(info)

if (notifyReplys) {
userEventService.insertCommentDeleteNotification(child, info)
}
}
}

val deletedMain = doDeleteCommentWithScore(root, rootBonus, rootReason, user)

deletedMain.foreach { info =>
deleteInfos.addOne(info)
userEventService.insertCommentDeleteNotification(root, info)
}

if (deleteInfos.nonEmpty) {
Expand All @@ -235,7 +237,11 @@ class DeleteService(commentDao: CommentDao, userDao: UserDao, userEventService:
commentDao.updateStatsAfterDelete(root.id, deleteInfos.size)
}

deleteInfos.map(_.msgid).toVector
val deletedComments = deleteInfos.map(_.msgid).toVector

userEventService.processCommentsDeleted(deletedComments)

deletedComments
}

private def doDeleteTopic(topic: Topic, moderator: User, reason: String, bonus: Int): Option[InsertDeleteInfo] = {
Expand Down Expand Up @@ -270,10 +276,6 @@ class DeleteService(commentDao: CommentDao, userDao: UserDao, userEventService:

userEventService.processTopicDeleted(deletedTopics.map(_.msgid))

if (notifyUser) {
userEventService.insertTopicMassDeleteNotifications(deletedTopics.map(_.msgid), reason, moderator)
}

// delete comments
val skippedComments = new ArrayBuffer[Int]
val deletedComments = new ArrayBuffer[InsertDeleteInfo]
Expand All @@ -291,18 +293,25 @@ class DeleteService(commentDao: CommentDao, userDao: UserDao, userEventService:
}
}

val deletedCommentIds = deletedComments.map(_.msgid).toVector

for (info <- deletedComments) {
commentDao.updateStatsAfterDelete(info.msgid, 1)
}

userEventService.processCommentsDeleted(deletedComments.view.map(_.msgid).toVector)
userEventService.processCommentsDeleted(deletedCommentIds)

// common
deleteInfoDao.insert((deletedComments ++ deletedTopics).asJava)

if (notifyUser) {
userEventService.insertTopicMassDeleteNotifications(deletedTopics.map(_.msgid), reason, moderator)
userEventService.insertCommentMassDeleteNotifications(deletedCommentIds, reason, moderator)
}

new DeleteCommentResult(
deletedTopics.view.map(_.msgid).map(Integer.valueOf).toVector.asJava,
deletedComments.view.map(_.msgid).map(Integer.valueOf).toVector.asJava,
deletedCommentIds.map(Integer.valueOf).asJava,
skippedComments.map(Integer.valueOf).asJava)
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/scala/ru/org/linux/user/UserEventDao.scala
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,13 @@ class UserEventDao(ds: DataSource, val transactionManager: PlatformTransactionMa
and topics.userid != :deletedBy and topics.userid != ${User.ANONYMOUS_ID})
""", Map("message" -> reason, "topics" -> topicsIds.map(Integer.valueOf).asJava, "deletedBy" -> deletedBy).asJava)
}

def insertCommentMassDeleteNotifications(commentIds: Seq[Int], reason: String, deletedBy: Int): Unit = {
namedJdbcTemplate.update(s"""
insert into user_events (userid, type, private, message_id, comment_id, message)
(select comments.userid, '${DELETED.getType}', true, comments.topic, comments.id,
:message from comments where comments.id in (:comments)
and comments.userid != :deletedBy and comments.userid != ${User.ANONYMOUS_ID})
""", Map("message" -> reason, "comments" -> commentIds.map(Integer.valueOf).asJava, "deletedBy" -> deletedBy).asJava)
}
}
22 changes: 21 additions & 1 deletion src/main/scala/ru/org/linux/user/UserEventService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class UserEventService(userEventDao: UserEventDao, val transactionManager: Platf
}
}

def insertTopicDeleteNotifications(topic: Topic, info: InsertDeleteInfo): Unit = {
def insertTopicDeleteNotification(topic: Topic, info: InsertDeleteInfo): Unit = {
assert(topic.id == info.msgid())

if (info.deleteUser().getId != topic.authorUserId && topic.authorUserId != User.ANONYMOUS_ID) {
Expand All @@ -191,9 +191,29 @@ class UserEventService(userEventDao: UserEventDao, val transactionManager: Platf
}
}

def insertCommentDeleteNotification(comment: Comment, info: InsertDeleteInfo): Unit = {
assert(comment.id == info.msgid())

if (info.deleteUser().getId != comment.userid && comment.userid != User.ANONYMOUS_ID) {
userEventDao.addEvent(
eventType = DELETED.getType,
userId = comment.userid,
isPrivate = true,
topicId = Some(comment.topicId),
commentId = Some(comment.id),
message = Some(info.reason()))
}
}

def insertTopicMassDeleteNotifications(topicsIds: Seq[Int], reason: String, deletedBy: User): Unit = {
if (topicsIds.nonEmpty) {
userEventDao.insertTopicMassDeleteNotifications(topicsIds, reason, deletedBy.getId)
}
}

def insertCommentMassDeleteNotifications(commentIds: Seq[Int], reason: String, deletedBy: User): Unit = {
if (commentIds.nonEmpty) {
userEventDao.insertCommentMassDeleteNotifications(commentIds, reason, deletedBy.getId)
}
}
}

0 comments on commit f7110c5

Please sign in to comment.