2022-01-20 09:27:41 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
2023-03-03 17:02:11 +00:00
|
|
|
// Copyright © 2021, 2022, 2023 Hardcore Engineering Inc.
|
2022-01-20 09:27:41 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2023-11-17 07:35:09 +00:00
|
|
|
import contact, {
|
|
|
|
Channel,
|
|
|
|
Contact,
|
|
|
|
Organization,
|
|
|
|
Person,
|
|
|
|
contactId,
|
|
|
|
getName,
|
|
|
|
formatContactName
|
|
|
|
} from '@hcengineering/contact'
|
|
|
|
import { Ref, Class, Doc, Tx, TxRemoveDoc, TxUpdateDoc, concatLink, Hierarchy } from '@hcengineering/core'
|
2023-04-10 09:15:00 +00:00
|
|
|
import notification, { Collaborators } from '@hcengineering/notification'
|
2022-11-16 15:03:03 +00:00
|
|
|
import { getMetadata } from '@hcengineering/platform'
|
2023-04-14 17:18:23 +00:00
|
|
|
import serverCore, { TriggerControl } from '@hcengineering/server-core'
|
2022-11-16 15:03:03 +00:00
|
|
|
import { workbenchId } from '@hcengineering/workbench'
|
2022-01-20 09:27:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-02-07 04:46:28 +00:00
|
|
|
export async function OnContactDelete (
|
|
|
|
tx: Tx,
|
2023-02-21 13:45:05 +00:00
|
|
|
{ findAll, hierarchy, storageFx, removedMap, txFactory }: TriggerControl
|
2023-02-07 04:46:28 +00:00
|
|
|
): Promise<Tx[]> {
|
2022-01-20 09:27:41 +00:00
|
|
|
const rmTx = tx as TxRemoveDoc<Contact>
|
|
|
|
|
2023-02-07 04:46:28 +00:00
|
|
|
const removeContact = removedMap.get(rmTx.objectId) as Contact
|
|
|
|
if (removeContact === undefined) {
|
2022-01-20 09:27:41 +00:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2023-02-07 04:46:28 +00:00
|
|
|
const avatar: string | undefined = [removeContact.avatar].filter((x): x is string => x !== undefined).slice(-1)[0]
|
2022-01-20 09:27:41 +00:00
|
|
|
|
|
|
|
if (avatar === undefined) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2023-01-31 05:48:40 +00:00
|
|
|
if (avatar?.includes('://') && !avatar?.startsWith('image://')) {
|
2023-01-25 13:42:55 +00:00
|
|
|
return []
|
|
|
|
}
|
2023-04-07 15:39:53 +00:00
|
|
|
if (avatar === '') {
|
|
|
|
return []
|
|
|
|
}
|
2023-01-25 13:42:55 +00:00
|
|
|
|
2022-01-20 09:27:41 +00:00
|
|
|
storageFx(async (adapter, bucket) => {
|
2022-11-16 15:03:03 +00:00
|
|
|
await adapter.remove(bucket, [avatar])
|
|
|
|
|
2023-05-19 11:46:05 +00:00
|
|
|
if (avatar != null) {
|
|
|
|
const extra = await adapter.list(bucket, avatar)
|
|
|
|
if (extra.length > 0) {
|
|
|
|
await adapter.remove(
|
|
|
|
bucket,
|
|
|
|
Array.from(extra.entries()).map((it) => it[1].name)
|
|
|
|
)
|
|
|
|
}
|
2022-07-02 09:26:23 +00:00
|
|
|
}
|
2022-01-20 09:27:41 +00:00
|
|
|
})
|
|
|
|
|
2023-02-21 13:45:05 +00:00
|
|
|
const result: Tx[] = []
|
|
|
|
|
|
|
|
const members = await findAll(contact.class.Member, { contact: removeContact._id })
|
|
|
|
for (const member of members) {
|
|
|
|
const removeTx = txFactory.createTxRemoveDoc(member._class, member.space, member._id)
|
|
|
|
const tx = txFactory.createTxCollectionCUD(
|
|
|
|
member.attachedToClass,
|
|
|
|
member.attachedTo,
|
|
|
|
member.space,
|
|
|
|
member.collection,
|
|
|
|
removeTx
|
|
|
|
)
|
|
|
|
result.push(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2022-01-20 09:27:41 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 09:15:00 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export async function OnChannelUpdate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
|
|
|
|
const uTx = tx as TxUpdateDoc<Channel>
|
|
|
|
|
|
|
|
const result: Tx[] = []
|
|
|
|
|
|
|
|
if (uTx.operations.$inc?.items !== undefined) {
|
|
|
|
const doc = (await control.findAll(uTx.objectClass, { _id: uTx.objectId }, { limit: 1 }))[0]
|
|
|
|
if (doc !== undefined) {
|
|
|
|
if (control.hierarchy.hasMixin(doc, notification.mixin.Collaborators)) {
|
|
|
|
const collab = control.hierarchy.as(doc, notification.mixin.Collaborators) as Doc as Collaborators
|
|
|
|
if (collab.collaborators.includes(tx.modifiedBy)) {
|
|
|
|
result.push(
|
|
|
|
control.txFactory.createTxMixin(doc._id, doc._class, doc.space, notification.mixin.Collaborators, {
|
|
|
|
$push: {
|
|
|
|
collaborators: tx.modifiedBy
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-19 02:21:42 +00:00
|
|
|
const res = control.txFactory.createTxMixin<Doc, Collaborators>(
|
2023-04-10 09:15:00 +00:00
|
|
|
doc._id,
|
|
|
|
doc._class,
|
|
|
|
doc.space,
|
|
|
|
notification.mixin.Collaborators,
|
|
|
|
{
|
|
|
|
collaborators: [tx.modifiedBy]
|
|
|
|
}
|
|
|
|
)
|
2023-05-19 02:21:42 +00:00
|
|
|
result.push(res)
|
2023-04-10 09:15:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:09:13 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-03-15 14:06:03 +00:00
|
|
|
export async function personHTMLPresenter (doc: Doc, control: TriggerControl): Promise<string> {
|
2022-02-22 09:09:13 +00:00
|
|
|
const person = doc as Person
|
2023-03-22 02:48:57 +00:00
|
|
|
const front = getMetadata(serverCore.metadata.FrontUrl) ?? ''
|
2023-03-20 08:45:52 +00:00
|
|
|
const path = `${workbenchId}/${control.workspace.name}/${contactId}/${doc._id}`
|
2023-02-01 11:27:15 +00:00
|
|
|
const link = concatLink(front, path)
|
2023-08-04 18:06:21 +00:00
|
|
|
return `<a href="${link}">${getName(control.hierarchy, person)}</a>`
|
2022-02-22 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-08-04 18:06:21 +00:00
|
|
|
export function personTextPresenter (doc: Doc, control: TriggerControl): string {
|
2022-02-22 09:09:13 +00:00
|
|
|
const person = doc as Person
|
2023-08-04 18:06:21 +00:00
|
|
|
return `${getName(control.hierarchy, person)}`
|
2022-02-22 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-03-15 14:06:03 +00:00
|
|
|
export async function organizationHTMLPresenter (doc: Doc, control: TriggerControl): Promise<string> {
|
2022-02-22 09:09:13 +00:00
|
|
|
const organization = doc as Organization
|
2023-03-22 02:48:57 +00:00
|
|
|
const front = getMetadata(serverCore.metadata.FrontUrl) ?? ''
|
2023-03-20 08:45:52 +00:00
|
|
|
const path = `${workbenchId}/${control.workspace.name}/${contactId}/${doc._id}`
|
2023-02-01 11:27:15 +00:00
|
|
|
const link = concatLink(front, path)
|
|
|
|
return `<a href="${link}">${organization.name}</a>`
|
2022-02-22 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function organizationTextPresenter (doc: Doc): string {
|
|
|
|
const organization = doc as Organization
|
|
|
|
return `${organization.name}`
|
|
|
|
}
|
|
|
|
|
2023-11-17 07:35:09 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2023-11-20 10:01:43 +00:00
|
|
|
export function contactNameProvider (hierarchy: Hierarchy, props: Record<string, string>): string {
|
2023-11-17 07:35:09 +00:00
|
|
|
const _class = props._class !== undefined ? (props._class as Ref<Class<Doc>>) : contact.class.Contact
|
|
|
|
return formatContactName(hierarchy, _class, props.name ?? '')
|
|
|
|
}
|
|
|
|
|
2022-01-20 09:27:41 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
|
|
export default async () => ({
|
|
|
|
trigger: {
|
2023-03-03 17:02:11 +00:00
|
|
|
OnContactDelete,
|
2023-04-10 09:15:00 +00:00
|
|
|
OnChannelUpdate
|
2022-02-22 09:09:13 +00:00
|
|
|
},
|
|
|
|
function: {
|
|
|
|
PersonHTMLPresenter: personHTMLPresenter,
|
|
|
|
PersonTextPresenter: personTextPresenter,
|
|
|
|
OrganizationHTMLPresenter: organizationHTMLPresenter,
|
2023-11-17 07:35:09 +00:00
|
|
|
OrganizationTextPresenter: organizationTextPresenter,
|
|
|
|
ContactNameProvider: contactNameProvider
|
2022-01-20 09:27:41 +00:00
|
|
|
}
|
|
|
|
})
|