Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
Denis Bykhov
2022-05-07 00:45:59 +07:00
committed by GitHub
parent 50ad39d037
commit 03cb718c72
20 changed files with 212 additions and 23 deletions
+66 -4
View File
@@ -14,8 +14,38 @@
//
import contact, { Channel } from '@anticrm/contact'
import gmail from '@anticrm/gmail'
import { Class, Doc, DocumentQuery, FindOptions, FindResult, Hierarchy, Ref } from '@anticrm/core'
import core, {
AttachedDoc,
Class,
Doc,
DocumentQuery,
FindOptions,
FindResult,
Hierarchy,
Ref,
Tx,
TxCollectionCUD,
TxCreateDoc,
TxProcessor
} from '@anticrm/core'
import gmail, { Message } from '@anticrm/gmail'
import { TriggerControl } from '@anticrm/server-core'
const extractTx = (tx: Tx): Tx => {
if (tx._class === core.class.TxCollectionCUD) {
const ctx = tx as TxCollectionCUD<Doc, AttachedDoc>
if (ctx.tx._class === core.class.TxCreateDoc) {
const create = ctx.tx as TxCreateDoc<AttachedDoc>
create.attributes.attachedTo = ctx.objectId
create.attributes.attachedToClass = ctx.objectClass
create.attributes.collection = ctx.collection
return create
}
return ctx.tx
}
return tx
}
/**
* @public
@@ -33,13 +63,45 @@ export async function FindMessages (
if (channel.provider !== contact.channelProvider.Email) {
return []
}
const messages = await findAll(gmail.class.Message, { attachedTo: doc._id })
const newMessages = await findAll(gmail.class.NewMessage, { attachedTo: doc._id })
const messages = await findAll(gmail.class.Message, { attachedTo: channel._id })
const newMessages = await findAll(gmail.class.NewMessage, { attachedTo: channel._id })
return [...messages, ...newMessages]
}
/**
* @public
*/
export async function OnMessageCreate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
const actualTx = extractTx(tx)
if (actualTx._class !== core.class.TxCreateDoc) {
return []
}
const createTx = tx as TxCreateDoc<Message>
if (!control.hierarchy.isDerived(createTx.objectClass, gmail.class.Message)) {
return []
}
const message = TxProcessor.createDoc2Doc<Message>(createTx)
const channel = (await control.findAll(contact.class.Channel, { _id: message.attachedTo }, { limit: 1 }))[0]
if (channel === undefined) {
return []
}
if (channel.lastMessage === undefined || channel.lastMessage < message.sendOn) {
const tx = control.txFactory.createTxUpdateDoc(channel._class, channel.space, channel._id, {
lastMessage: message.sendOn
})
return [tx]
}
return []
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default async () => ({
trigger: {
OnMessageCreate
},
function: {
FindMessages
}