2023-08-24 15:53:18 +00:00
|
|
|
import { Backlink } from '@hcengineering/chunter'
|
|
|
|
import { Data, DocumentQuery, TxOperations } from '@hcengineering/core'
|
2022-01-19 09:04:30 +00:00
|
|
|
import chunter from './plugin'
|
|
|
|
|
2022-09-09 03:44:33 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export async function updateBacklinksList (
|
|
|
|
client: TxOperations,
|
|
|
|
q: DocumentQuery<Backlink>,
|
|
|
|
backlinks: Array<Data<Backlink>>
|
|
|
|
): Promise<void> {
|
|
|
|
const current = await client.findAll(chunter.class.Backlink, q)
|
|
|
|
|
2022-01-19 09:04:30 +00:00
|
|
|
// We need to find ones we need to remove, and ones we need to update.
|
|
|
|
for (const c of current) {
|
|
|
|
// Find existing and check if we need to update message.
|
2023-05-23 10:42:39 +00:00
|
|
|
const pos = backlinks.findIndex(
|
|
|
|
(b) => b.backlinkId === c.backlinkId && b.backlinkClass === c.backlinkClass && b.attachedTo === c.attachedTo
|
|
|
|
)
|
2022-01-19 09:04:30 +00:00
|
|
|
if (pos !== -1) {
|
|
|
|
// We need to check and update if required.
|
|
|
|
const data = backlinks[pos]
|
|
|
|
if (c.message !== data.message) {
|
2022-04-12 05:59:38 +00:00
|
|
|
await client.updateCollection(c._class, c.space, c._id, c.attachedTo, c.attachedToClass, c.collection, {
|
|
|
|
message: data.message
|
|
|
|
})
|
2022-01-19 09:04:30 +00:00
|
|
|
}
|
|
|
|
backlinks.splice(pos, 1)
|
|
|
|
} else {
|
|
|
|
// We need to remove backlink.
|
|
|
|
await client.removeCollection(c._class, c.space, c._id, c.attachedTo, c.attachedToClass, c.collection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add missing backlinks
|
|
|
|
for (const backlink of backlinks) {
|
|
|
|
const { attachedTo, attachedToClass, collection, ...adata } = backlink
|
2022-04-12 05:59:38 +00:00
|
|
|
await client.addCollection(
|
|
|
|
chunter.class.Backlink,
|
|
|
|
chunter.space.Backlinks,
|
|
|
|
attachedTo,
|
|
|
|
attachedToClass,
|
|
|
|
collection,
|
|
|
|
adata
|
|
|
|
)
|
2022-01-19 09:04:30 +00:00
|
|
|
}
|
|
|
|
}
|