// // Copyright © 2020, 2021 Anticrm Platform Contributors. // Copyright © 2021, 2022, 2023 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. // import { ActivityMessage } from '@hcengineering/activity' import contact, { Employee, Person, PersonAccount, PersonSpace } from '@hcengineering/contact' import { Account, Class, Doc, Mixin, Ref, Tx, TxCUD } from '@hcengineering/core' import { BaseNotificationType, InboxNotification, NotificationContent, NotificationProvider, NotificationType } from '@hcengineering/notification' import { Metadata, Plugin, Resource, plugin } from '@hcengineering/platform' import type { TriggerControl, TriggerFunc } from '@hcengineering/server-core' /** * @public */ export const serverNotificationId = 'server-notification' as Plugin /** * @public */ export function getPersonAccountById (_id: Ref, control: TriggerControl): PersonAccount | undefined { const account = control.modelDb.findAllSync( contact.class.PersonAccount, { _id: _id as Ref }, { limit: 1 } )[0] return account } /** * @public */ export async function getEmployee (employee: Ref, control: TriggerControl): Promise { const account = ( await control.findAll( contact.mixin.Employee, { _id: employee }, { limit: 1 } ) )[0] return account !== undefined ? control.hierarchy.as(account, contact.mixin.Employee) : undefined } /** * @public */ export type Presenter = (doc: T, control: TriggerControl) => Promise /** * @public */ export interface HTMLPresenter extends Class { presenter: Resource> } /** * @public */ export interface TextPresenter extends Class { presenter: Resource> } /** * @public */ export type TypeMatchFunc = Resource< (tx: Tx, doc: Doc, user: Ref[], type: NotificationType, control: TriggerControl) => boolean > /** * @public */ export interface TypeMatch extends NotificationType { func: TypeMatchFunc } /** * @public */ export type NotificationContentProvider = ( doc: Doc, tx: TxCUD, target: Ref, control: TriggerControl ) => Promise /** * @public */ export interface NotificationPresenter extends Class { presenter: Resource } export interface ReceiverInfo { _id: Ref account: PersonAccount person: Employee space: Ref } export interface SenderInfo { _id: Ref account?: PersonAccount person?: Person } export type NotificationProviderFunc = ( control: TriggerControl, types: BaseNotificationType[], object: Doc, data: InboxNotification, receiver: ReceiverInfo, sender: SenderInfo, message?: ActivityMessage ) => Promise export interface NotificationProviderResources extends Doc { provider: Ref fn: Resource } export const NOTIFICATION_BODY_SIZE = 50 export const PUSH_NOTIFICATION_TITLE_SIZE = 80 /** * @public */ export default plugin(serverNotificationId, { metadata: { SesUrl: '' as Metadata, PushPrivateKey: '' as Metadata, PushSubject: '' as Metadata }, class: { NotificationProviderResources: '' as Ref> }, mixin: { HTMLPresenter: '' as Ref>, TextPresenter: '' as Ref>, TypeMatch: '' as Ref>, NotificationPresenter: '' as Ref> }, trigger: { OnAttributeCreate: '' as Resource, OnAttributeUpdate: '' as Resource, OnReactionChanged: '' as Resource, OnDocRemove: '' as Resource }, function: { IsUserInFieldValueTypeMatch: '' as TypeMatchFunc, IsUserEmployeeInFieldValueTypeMatch: '' as TypeMatchFunc } })