2022-01-20 09:27:41 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021, 2022 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
2022-09-21 08:08:25 +00:00
|
|
|
import contact, { Contact, contactId, formatName, Organization, Person } from '@hcengineering/contact'
|
2023-02-07 04:46:28 +00:00
|
|
|
import core, { concatLink, Doc, Tx, TxRemoveDoc } from '@hcengineering/core'
|
2022-09-21 08:08:25 +00:00
|
|
|
import login from '@hcengineering/login'
|
2022-11-16 15:03:03 +00:00
|
|
|
import { getMetadata } from '@hcengineering/platform'
|
|
|
|
import type { TriggerControl } from '@hcengineering/server-core'
|
2022-09-21 08:08:25 +00:00
|
|
|
import view from '@hcengineering/view'
|
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,
|
|
|
|
{ findAll, hierarchy, storageFx, removedMap }: TriggerControl
|
|
|
|
): Promise<Tx[]> {
|
2022-01-20 09:27:41 +00:00
|
|
|
if (tx._class !== core.class.TxRemoveDoc) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
const rmTx = tx as TxRemoveDoc<Contact>
|
|
|
|
|
|
|
|
if (!hierarchy.isDerived(rmTx.objectClass, contact.class.Contact)) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
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 []
|
|
|
|
}
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:09:13 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-10-26 10:24:39 +00:00
|
|
|
export function personHTMLPresenter (doc: Doc, control: TriggerControl): string {
|
2022-02-22 09:09:13 +00:00
|
|
|
const person = doc as Person
|
|
|
|
const front = getMetadata(login.metadata.FrontUrl) ?? ''
|
2023-02-01 11:27:15 +00:00
|
|
|
const path = `${workbenchId}/${control.workspace.name}/${contactId}#${view.component.EditDoc}|${person._id}|${person._class}|content`
|
|
|
|
const link = concatLink(front, path)
|
|
|
|
return `<a href="${link}">${formatName(person.name)}</a>`
|
2022-02-22 09:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function personTextPresenter (doc: Doc): string {
|
|
|
|
const person = doc as Person
|
|
|
|
return `${formatName(person.name)}`
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-10-26 10:24:39 +00:00
|
|
|
export function organizationHTMLPresenter (doc: Doc, control: TriggerControl): string {
|
2022-02-22 09:09:13 +00:00
|
|
|
const organization = doc as Organization
|
|
|
|
const front = getMetadata(login.metadata.FrontUrl) ?? ''
|
2023-02-01 11:27:15 +00:00
|
|
|
const path = `${workbenchId}/${control.workspace.name}/${contactId}#${view.component.EditDoc}|${organization._id}|${organization._class}|content`
|
|
|
|
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}`
|
|
|
|
}
|
|
|
|
|
2022-01-20 09:27:41 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
|
|
export default async () => ({
|
|
|
|
trigger: {
|
|
|
|
OnContactDelete
|
2022-02-22 09:09:13 +00:00
|
|
|
},
|
|
|
|
function: {
|
|
|
|
PersonHTMLPresenter: personHTMLPresenter,
|
|
|
|
PersonTextPresenter: personTextPresenter,
|
|
|
|
OrganizationHTMLPresenter: organizationHTMLPresenter,
|
|
|
|
OrganizationTextPresenter: organizationTextPresenter
|
2022-01-20 09:27:41 +00:00
|
|
|
}
|
|
|
|
})
|