2023-08-22 04:52:10 +00:00
|
|
|
import { Backlink, getBacklinks } from '@hcengineering/chunter'
|
2023-08-04 18:06:21 +00:00
|
|
|
import contact, { PersonAccount } from '@hcengineering/contact'
|
2022-09-21 08:08:25 +00:00
|
|
|
import { Account, Class, Client, Data, Doc, DocumentQuery, Ref, TxOperations } from '@hcengineering/core'
|
2022-01-19 09:04:30 +00:00
|
|
|
import chunter from './plugin'
|
|
|
|
|
2022-04-12 05:59:38 +00:00
|
|
|
export async function getUser (
|
|
|
|
client: Client,
|
2023-08-04 18:06:21 +00:00
|
|
|
user: Ref<PersonAccount> | Ref<Account>
|
|
|
|
): Promise<PersonAccount | undefined> {
|
|
|
|
return await client.findOne(contact.class.PersonAccount, { _id: user as Ref<PersonAccount> })
|
2022-01-19 09:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getTime (time: number): string {
|
|
|
|
let options: Intl.DateTimeFormatOptions = { hour: 'numeric', minute: 'numeric' }
|
|
|
|
if (!isToday(time)) {
|
|
|
|
options = {
|
|
|
|
month: 'numeric',
|
|
|
|
day: 'numeric',
|
|
|
|
...options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Date(time).toLocaleString('default', options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isToday (time: number): boolean {
|
|
|
|
const current = new Date()
|
|
|
|
const target = new Date(time)
|
2022-04-12 05:59:38 +00:00
|
|
|
return (
|
|
|
|
current.getDate() === target.getDate() &&
|
|
|
|
current.getMonth() === target.getMonth() &&
|
|
|
|
current.getFullYear() === target.getFullYear()
|
|
|
|
)
|
2022-01-19 09:04:30 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 10:42:39 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-04-12 05:59:38 +00:00
|
|
|
export async function updateBacklinks (
|
|
|
|
client: TxOperations,
|
|
|
|
backlinkId: Ref<Doc>,
|
|
|
|
backlinkClass: Ref<Class<Doc>>,
|
|
|
|
attachedDocId: Ref<Doc> | undefined,
|
|
|
|
content: string
|
|
|
|
): Promise<void> {
|
2022-09-09 03:44:33 +00:00
|
|
|
const q: DocumentQuery<Backlink> = { backlinkId, backlinkClass, collection: 'backlinks' }
|
2022-01-19 09:04:30 +00:00
|
|
|
if (attachedDocId !== undefined) {
|
|
|
|
q.attachedDocId = attachedDocId
|
|
|
|
}
|
|
|
|
const backlinks = getBacklinks(backlinkId, backlinkClass, attachedDocId, content)
|
|
|
|
|
2022-09-09 03:44:33 +00:00
|
|
|
await updateBacklinksList(client, q, backlinks)
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
}
|
|
|
|
}
|