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 <denis.bunakalya@xored.com>

* 1300 Close thread on deletion even from other user

Signed-off-by: Denis Bunakalya <denis.bunakalya@xored.com>
This commit is contained in:
Denis Bunakalya 2022-04-13 13:30:54 +03:00 committed by GitHub
parent ca91425d6f
commit e205f31391
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 15 deletions

View File

@ -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<void> => {

View File

@ -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 },

View File

@ -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<T
return []
}
/**
* @public
*/
export async function MessageDelete (tx: Tx, control: TriggerControl): Promise<Tx[]> {
const hierarchy = control.hierarchy
if (tx._class !== core.class.TxCollectionCUD) return []
const rmTx = (tx as TxCollectionCUD<Channel, Message>).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<Message>)
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>(channel._class, channel.space, channel._id, {
lastMessage: lastMessageDate > 0 ? lastMessageDate : undefined
})
return [updateTx]
}
return []
}
/**
* @public
*/
export async function ChunterTrigger (tx: Tx, control: TriggerControl): Promise<Tx[]> {
const promises = [
MessageCreate(tx, control),
MessageDelete(tx, control),
CommentCreate(tx, control),
CommentDelete(tx, control)
]