mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-08 02:37:44 +02:00
TSK-1341 Remove last view (#3115)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
@@ -26,7 +26,6 @@ import core, {
|
||||
Hierarchy,
|
||||
Ref,
|
||||
Tx,
|
||||
TxCollectionCUD,
|
||||
TxCreateDoc,
|
||||
TxCUD,
|
||||
TxProcessor,
|
||||
@@ -192,84 +191,11 @@ async function ThreadMessageDelete (tx: Tx, control: TriggerControl): Promise<Tx
|
||||
return [updateTx]
|
||||
}
|
||||
|
||||
async function MessageCreate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
|
||||
const hierarchy = control.hierarchy
|
||||
const actualTx = TxProcessor.extractTx(tx)
|
||||
if (actualTx._class !== core.class.TxCreateDoc) return []
|
||||
const doc = TxProcessor.createDoc2Doc(actualTx as TxCreateDoc<Doc>)
|
||||
if (!hierarchy.isDerived(doc._class, chunter.class.Message)) {
|
||||
return []
|
||||
}
|
||||
|
||||
const message = doc as Message
|
||||
|
||||
const channel = (
|
||||
await control.findAll(
|
||||
chunter.class.ChunterSpace,
|
||||
{
|
||||
_id: message.space
|
||||
},
|
||||
{ limit: 1 }
|
||||
)
|
||||
)[0]
|
||||
|
||||
if (channel?.lastMessage === undefined || channel.lastMessage < message.createOn) {
|
||||
const res = control.txFactory.createTxUpdateDoc<ChunterSpace>(channel._class, channel.space, channel._id, {
|
||||
lastMessage: message.createOn
|
||||
})
|
||||
return [res]
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
async function MessageDelete (tx: Tx, control: TriggerControl): Promise<Tx[]> {
|
||||
const hierarchy = control.hierarchy
|
||||
|
||||
const rmTx = TxProcessor.extractTx(tx) as TxCollectionCUD<ChunterSpace, Message>
|
||||
if (rmTx._class !== core.class.TxRemoveDoc) return []
|
||||
if (!hierarchy.isDerived(rmTx.objectClass, chunter.class.Message)) {
|
||||
return []
|
||||
}
|
||||
|
||||
const message = control.removedMap.get(rmTx.objectId) as Message
|
||||
|
||||
if (message === undefined) {
|
||||
return []
|
||||
}
|
||||
|
||||
const channel = (
|
||||
await control.findAll(
|
||||
chunter.class.ChunterSpace,
|
||||
{
|
||||
_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<ChunterSpace>(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),
|
||||
ThreadMessageCreate(tx, control),
|
||||
ThreadMessageDelete(tx, control),
|
||||
CommentCreate(tx as TxCUD<Doc>, control)
|
||||
|
||||
@@ -100,7 +100,7 @@ export async function OnMessageCreate (tx: Tx, control: TriggerControl): Promise
|
||||
}
|
||||
if (docs.length === 0) {
|
||||
res.push(
|
||||
control.txFactory.createTxCreateDoc(notification.class.DocUpdates, notification.space.Notifications, {
|
||||
control.txFactory.createTxCreateDoc(notification.class.DocUpdates, channel.space, {
|
||||
user: tx.modifiedBy,
|
||||
attachedTo: channel._id,
|
||||
attachedToClass: channel._class,
|
||||
|
||||
@@ -45,7 +45,6 @@ import notification, {
|
||||
Collaborators,
|
||||
DocUpdates,
|
||||
EmailNotification,
|
||||
LastView,
|
||||
NotificationProvider,
|
||||
NotificationType
|
||||
} from '@hcengineering/notification'
|
||||
@@ -54,7 +53,6 @@ import type { TriggerControl } from '@hcengineering/server-core'
|
||||
import serverNotification, {
|
||||
HTMLPresenter,
|
||||
TextPresenter,
|
||||
createLastViewTx,
|
||||
getEmployeeAccount,
|
||||
getEmployeeAccountById
|
||||
} from '@hcengineering/server-notification'
|
||||
@@ -254,62 +252,6 @@ async function getEmailNotificationTx (
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function UpdateLastView (tx: Tx, control: TriggerControl): Promise<Tx[]> {
|
||||
const actualTx = TxProcessor.extractTx(tx)
|
||||
if (actualTx._class !== core.class.TxRemoveDoc) {
|
||||
return []
|
||||
}
|
||||
|
||||
if ((actualTx as TxCUD<Doc>).objectClass === notification.class.LastView) {
|
||||
return []
|
||||
}
|
||||
|
||||
const result: Tx[] = []
|
||||
|
||||
const removeTx = actualTx as TxRemoveDoc<Doc>
|
||||
const lastViews = await control.findAll(notification.class.LastView, { [removeTx.objectId]: { $exists: true } })
|
||||
for (const lastView of lastViews) {
|
||||
const clearTx = control.txFactory.createTxUpdateDoc(lastView._class, lastView.space, lastView._id, {
|
||||
$unset: {
|
||||
[removeTx.objectId]: ''
|
||||
}
|
||||
})
|
||||
result.push(clearTx)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function OnUpdateLastView (tx: Tx, control: TriggerControl): Promise<Tx[]> {
|
||||
const actualTx = TxProcessor.extractTx(tx) as TxUpdateDoc<LastView>
|
||||
if (actualTx._class !== core.class.TxUpdateDoc) return []
|
||||
if (actualTx.objectClass !== notification.class.LastView) return []
|
||||
const result: Tx[] = []
|
||||
const lastView = (await control.findAll(notification.class.LastView, { _id: actualTx.objectId }))[0]
|
||||
if (lastView === undefined) return result
|
||||
for (const key in actualTx.operations) {
|
||||
const docs = await control.findAll(notification.class.DocUpdates, {
|
||||
attachedTo: key as Ref<Doc>,
|
||||
user: lastView.user
|
||||
})
|
||||
for (const doc of docs) {
|
||||
const txes = doc.txes.filter((p) => p[1] > actualTx.operations[key])
|
||||
result.push(
|
||||
control.txFactory.createTxUpdateDoc(doc._class, doc.space, doc._id, {
|
||||
txes
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
function getBacklink (ptx: TxCollectionCUD<Doc, Backlink>): Backlink {
|
||||
return TxProcessor.createDoc2Doc(ptx.tx as TxCreateDoc<Backlink>)
|
||||
}
|
||||
@@ -493,7 +435,7 @@ async function getNotificationTxes (
|
||||
const current = docUpdates.find((p) => p.user === target)
|
||||
if (current === undefined) {
|
||||
res.push(
|
||||
control.txFactory.createTxCreateDoc(notification.class.DocUpdates, notification.space.Notifications, {
|
||||
control.txFactory.createTxCreateDoc(notification.class.DocUpdates, object.space, {
|
||||
user: target,
|
||||
attachedTo: object._id,
|
||||
attachedToClass: object._class,
|
||||
@@ -709,6 +651,17 @@ function isMixinTx (tx: TxUpdateDoc<Doc> | TxMixin<Doc, Doc>): tx is TxMixin<Doc
|
||||
return tx._class === core.class.TxMixin
|
||||
}
|
||||
|
||||
async function changeSpaceTxes (control: TriggerControl, tx: TxUpdateDoc<Doc> | TxMixin<Doc, Doc>): Promise<Tx[]> {
|
||||
if (tx._class !== core.class.TxUpdateDoc) return []
|
||||
const ctx = tx as TxUpdateDoc<Doc>
|
||||
if (ctx.operations.space === undefined) return []
|
||||
const docUpdates = await control.findAll(notification.class.DocUpdates, { attachedTo: tx.objectId })
|
||||
|
||||
return docUpdates.map((value) =>
|
||||
control.txFactory.createTxUpdateDoc(value._class, value.space, value._id, { space: ctx.operations.space })
|
||||
)
|
||||
}
|
||||
|
||||
async function updateCollaboratorDoc (
|
||||
tx: TxUpdateDoc<Doc> | TxMixin<Doc, Doc>,
|
||||
control: TriggerControl,
|
||||
@@ -750,47 +703,11 @@ async function updateCollaboratorDoc (
|
||||
|
||||
res = res.concat(await getSpaceCollabTxes(control, doc, tx, originTx))
|
||||
|
||||
res = res.concat(await changeSpaceTxes(control, tx))
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function OnAddCollborator (tx: Tx, control: TriggerControl): Promise<Tx[]> {
|
||||
const result: Tx[] = []
|
||||
const actualTx = TxProcessor.extractTx(tx) as TxMixin<Doc, Collaborators>
|
||||
|
||||
if (actualTx._class !== core.class.TxMixin) return []
|
||||
if (actualTx.mixin !== notification.mixin.Collaborators) return []
|
||||
if (actualTx.attributes.collaborators !== undefined) {
|
||||
for (const collab of actualTx.attributes.collaborators) {
|
||||
const resTx = await createLastViewTx(control.findAll, actualTx.objectId, collab)
|
||||
if (resTx !== undefined) {
|
||||
result.push(resTx)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (actualTx.attributes.$push?.collaborators !== undefined) {
|
||||
const collab = actualTx.attributes.$push?.collaborators
|
||||
if (typeof collab === 'object') {
|
||||
if ('$each' in collab) {
|
||||
for (const collaborator of collab.$each) {
|
||||
const resTx = await createLastViewTx(control.findAll, actualTx.objectId, collaborator)
|
||||
if (resTx !== undefined) {
|
||||
result.push(resTx)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const resTx = await createLastViewTx(control.findAll, actualTx.objectId, collab)
|
||||
if (resTx !== undefined) {
|
||||
result.push(resTx)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
@@ -897,9 +814,6 @@ export default async () => ({
|
||||
trigger: {
|
||||
OnBacklinkCreate,
|
||||
CollaboratorDocHandler: collaboratorDocHandler,
|
||||
OnUpdateLastView,
|
||||
UpdateLastView,
|
||||
OnAddCollborator,
|
||||
OnAttributeCreate,
|
||||
OnAttributeUpdate
|
||||
},
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
//
|
||||
|
||||
import contact, { Employee, EmployeeAccount } from '@hcengineering/contact'
|
||||
import { Account, Class, Doc, Mixin, Ref, Tx, TxCreateDoc, TxFactory, TxUpdateDoc } from '@hcengineering/core'
|
||||
import notification, { LastView, NotificationType } from '@hcengineering/notification'
|
||||
import { Account, Class, Doc, Mixin, Ref, Tx } from '@hcengineering/core'
|
||||
import { NotificationType } from '@hcengineering/notification'
|
||||
import { Plugin, Resource, plugin } from '@hcengineering/platform'
|
||||
import type { TriggerControl, TriggerFunc } from '@hcengineering/server-core'
|
||||
|
||||
@@ -25,42 +25,6 @@ import type { TriggerControl, TriggerFunc } from '@hcengineering/server-core'
|
||||
*/
|
||||
export const serverNotificationId = 'server-notification' as Plugin
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function getUpdateLastViewTx (
|
||||
findAll: TriggerControl['findAll'],
|
||||
attachedTo: Ref<Doc>,
|
||||
lastView: number,
|
||||
user: Ref<Account>
|
||||
): Promise<TxUpdateDoc<LastView> | TxCreateDoc<LastView> | undefined> {
|
||||
const current = (
|
||||
await findAll(
|
||||
notification.class.LastView,
|
||||
{
|
||||
user
|
||||
},
|
||||
{ limit: 1 }
|
||||
)
|
||||
)[0]
|
||||
const factory = new TxFactory(user, true)
|
||||
if (current !== undefined) {
|
||||
if (current[attachedTo] === -1 || current[attachedTo] >= lastView) {
|
||||
return
|
||||
}
|
||||
const u = factory.createTxUpdateDoc(current._class, current.space, current._id, {
|
||||
[attachedTo]: lastView
|
||||
})
|
||||
return u
|
||||
} else {
|
||||
const u = factory.createTxCreateDoc(notification.class.LastView, notification.space.Notifications, {
|
||||
user,
|
||||
[attachedTo]: lastView
|
||||
})
|
||||
return u
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
@@ -115,38 +79,6 @@ export async function getEmployee (employee: Ref<Employee>, control: TriggerCont
|
||||
return account
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function createLastViewTx (
|
||||
findAll: TriggerControl['findAll'],
|
||||
attachedTo: Ref<Doc>,
|
||||
user: Ref<Account>
|
||||
): Promise<TxCreateDoc<LastView> | TxUpdateDoc<LastView> | undefined> {
|
||||
const current = (
|
||||
await findAll(
|
||||
notification.class.LastView,
|
||||
{
|
||||
user
|
||||
},
|
||||
{ limit: 1 }
|
||||
)
|
||||
)[0]
|
||||
const factory = new TxFactory(user, true)
|
||||
if (current === undefined) {
|
||||
const u = factory.createTxCreateDoc(notification.class.LastView, notification.space.Notifications, {
|
||||
user,
|
||||
[attachedTo]: 1
|
||||
})
|
||||
return u
|
||||
} else if (current[attachedTo] === undefined) {
|
||||
const u = factory.createTxUpdateDoc(current._class, current.space, current._id, {
|
||||
[attachedTo]: 1
|
||||
})
|
||||
return u
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
@@ -191,10 +123,7 @@ export default plugin(serverNotificationId, {
|
||||
},
|
||||
trigger: {
|
||||
OnBacklinkCreate: '' as Resource<TriggerFunc>,
|
||||
UpdateLastView: '' as Resource<TriggerFunc>,
|
||||
OnUpdateLastView: '' as Resource<TriggerFunc>,
|
||||
CollaboratorDocHandler: '' as Resource<TriggerFunc>,
|
||||
OnAddCollborator: '' as Resource<TriggerFunc>,
|
||||
OnAttributeCreate: '' as Resource<TriggerFunc>,
|
||||
OnAttributeUpdate: '' as Resource<TriggerFunc>
|
||||
},
|
||||
|
||||
@@ -101,7 +101,7 @@ export async function OnMessageCreate (tx: Tx, control: TriggerControl): Promise
|
||||
}
|
||||
if (docs.length === 0) {
|
||||
res.push(
|
||||
control.txFactory.createTxCreateDoc(notification.class.DocUpdates, notification.space.Notifications, {
|
||||
control.txFactory.createTxCreateDoc(notification.class.DocUpdates, channel.space, {
|
||||
user: tx.modifiedBy,
|
||||
attachedTo: channel._id,
|
||||
attachedToClass: channel._class,
|
||||
|
||||
Reference in New Issue
Block a user