From e205f313912dc90e408027d51084c6a95f051708 Mon Sep 17 00:00:00 2001 From: Denis Bunakalya Date: Wed, 13 Apr 2022 13:30:54 +0300 Subject: [PATCH] Chunter: Update channel last message and close thread on deletion from other user (#1389) * 1300 Update channel last message on deletion Signed-off-by: Denis Bunakalya * 1300 Close thread on deletion even from other user Signed-off-by: Denis Bunakalya --- .../src/components/Message.svelte | 14 +---- .../src/components/ThreadView.svelte | 12 ++++- server-plugins/chunter-resources/src/index.ts | 54 ++++++++++++++++++- 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/plugins/chunter-resources/src/components/Message.svelte b/plugins/chunter-resources/src/components/Message.svelte index 9f13f9871f..09cf44db81 100644 --- a/plugins/chunter-resources/src/components/Message.svelte +++ b/plugins/chunter-resources/src/components/Message.svelte @@ -21,7 +21,7 @@ import { NotificationClientImpl } from '@anticrm/notification-resources' import { getResource } from '@anticrm/platform' import { Avatar, getClient, MessageViewer } from '@anticrm/presentation' - import { ActionIcon, IconMoreH, Menu, showPopup, getCurrentLocation, navigate } from '@anticrm/ui' + import { ActionIcon, IconMoreH, Menu, showPopup } from '@anticrm/ui' import { Action } from '@anticrm/view' import { getActions } from '@anticrm/view-resources' import { createEventDispatcher } from 'svelte' @@ -59,19 +59,9 @@ action: chunter.actionImpl.SubscribeMessage } as Action) - async function deleteMessage () { - await client.remove(message) - const loc = getCurrentLocation() - - if (loc.path[3] === message._id) { - loc.path.length = 3 - navigate(loc) - } - } - const deleteAction = { label: chunter.string.DeleteMessage, - action: deleteMessage + action: async () => await client.remove(message) } const showMenu = async (ev: Event): Promise => { diff --git a/plugins/chunter-resources/src/components/ThreadView.svelte b/plugins/chunter-resources/src/components/ThreadView.svelte index b3de9b44fd..cb0bf625f2 100644 --- a/plugins/chunter-resources/src/components/ThreadView.svelte +++ b/plugins/chunter-resources/src/components/ThreadView.svelte @@ -20,7 +20,7 @@ import core, { Doc, generateId, getCurrentAccount, Ref, Space, TxFactory } from '@anticrm/core' import { NotificationClientImpl } from '@anticrm/notification-resources' import { createQuery, getClient } from '@anticrm/presentation' - import { IconClose, Label } from '@anticrm/ui' + import { IconClose, Label, getCurrentLocation, navigate } from '@anticrm/ui' import { afterUpdate, beforeUpdate, createEventDispatcher } from 'svelte' import { createBacklinks } from '../backlinks' import chunter from '../plugin' @@ -65,7 +65,15 @@ { _id: id }, - (res) => (message = res[0]), + (res) => { + message = res[0] + + if (!message) { + const loc = getCurrentLocation() + loc.path.length = 3 + navigate(loc) + } + }, { lookup: { _id: { attachments: attachment.class.Attachment }, diff --git a/server-plugins/chunter-resources/src/index.ts b/server-plugins/chunter-resources/src/index.ts index fbc79374e6..afe1de7110 100644 --- a/server-plugins/chunter-resources/src/index.ts +++ b/server-plugins/chunter-resources/src/index.ts @@ -15,7 +15,21 @@ import chunter, { Channel, Comment, Message, ThreadMessage } from '@anticrm/chunter' import { EmployeeAccount } from '@anticrm/contact' -import core, { Class, Doc, DocumentQuery, FindOptions, FindResult, Hierarchy, Ref, Tx, TxCreateDoc, TxProcessor, TxUpdateDoc, TxRemoveDoc } from '@anticrm/core' +import core, { + Class, + Doc, + DocumentQuery, + FindOptions, + FindResult, + Hierarchy, + Ref, + Tx, + TxCreateDoc, + TxProcessor, + TxUpdateDoc, + TxRemoveDoc, + TxCollectionCUD +} from '@anticrm/core' import login from '@anticrm/login' import { getMetadata } from '@anticrm/platform' import { TriggerControl } from '@anticrm/server-core' @@ -144,12 +158,50 @@ export async function MessageCreate (tx: Tx, control: TriggerControl): Promise { + const hierarchy = control.hierarchy + if (tx._class !== core.class.TxCollectionCUD) return [] + + const rmTx = (tx as TxCollectionCUD).tx + if (!hierarchy.isDerived(rmTx.objectClass, chunter.class.Message)) { + return [] + } + const createTx = (await control.findAll(core.class.TxCreateDoc, { + objectId: rmTx.objectId + }, { limit: 1 }))[0] + + const message = TxProcessor.createDoc2Doc(createTx as TxCreateDoc) + + const channel = (await control.findAll(chunter.class.Channel, { + _id: message.space + }, { limit: 1 }))[0] + + if (channel.lastMessage === message.createOn) { + const messages = await control.findAll(chunter.class.Message, { + attachedTo: channel._id + }) + const lastMessageDate = messages.reduce((maxDate, mess) => mess.createOn > maxDate ? mess.createOn : maxDate, 0) + + const updateTx = control.txFactory.createTxUpdateDoc(channel._class, channel.space, channel._id, { + lastMessage: lastMessageDate > 0 ? lastMessageDate : undefined + }) + + return [updateTx] + } + + return [] +} + /** * @public */ export async function ChunterTrigger (tx: Tx, control: TriggerControl): Promise { const promises = [ MessageCreate(tx, control), + MessageDelete(tx, control), CommentCreate(tx, control), CommentDelete(tx, control) ]