From 2e04e5c0914ec3ce99e896d8f0eb91622570e873 Mon Sep 17 00:00:00 2001 From: Kristina Date: Tue, 24 Jun 2025 17:24:57 +0400 Subject: [PATCH] Fix api (#75) Signed-off-by: Kristina Fefelova --- packages/rest-client/src/rest.ts | 270 +++++++++++++--------------- packages/rest-client/src/types.ts | 2 +- packages/sdk-types/src/serverApi.ts | 1 + packages/server/src/middlewares.ts | 2 +- 4 files changed, 131 insertions(+), 144 deletions(-) diff --git a/packages/rest-client/src/rest.ts b/packages/rest-client/src/rest.ts index 62e0f82297..47fc34e048 100644 --- a/packages/rest-client/src/rest.ts +++ b/packages/rest-client/src/rest.ts @@ -13,7 +13,7 @@ // limitations under the License. // -import { concatLink } from '@hcengineering/core' +import core, { concatLink, generateId, OperationDomain, TxDomainEvent } from '@hcengineering/core' import { type EventResult, type Event, @@ -79,15 +79,49 @@ class RestClientImpl implements RestClient { } } - async event (event: Event): Promise { - const response = await fetch(concatLink(this.endpoint, `/api/v1/event/${this.workspace}`), { + private wrapEvent (event: Event, modifiedBy: SocialID): TxDomainEvent { + return { + _id: generateId(), + _class: core.class.TxDomainEvent, + space: core.space.Tx, + objectSpace: core.space.Tx, + domain: 'communication' as OperationDomain, + event, + modifiedBy, + modifiedOn: Date.now() + } + } + + private async find(operation: string, params: Record): Promise { + const searchParams = new URLSearchParams() + if (Object.keys(params).length > 0) { + searchParams.append('params', JSON.stringify(params)) + } + const requestUrl = concatLink( + this.endpoint, + `/api/v1/request/communication/${operation}/${this.workspace}?${searchParams.toString()}` + ) + return await retry( + async () => { + const response = await fetch(requestUrl, this.requestInit()) + if (!response.ok) { + throw new Error(response.statusText) + } + return await extractJson(response) + }, + { retries } + ) + } + + async event (event: Event, socialId: SocialID): Promise { + const response = await fetch(concatLink(this.endpoint, `/api/v1/tx/${this.workspace}`), { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + this.token }, keepalive: true, - body: JSON.stringify(event) + body: JSON.stringify(this.wrapEvent(event, socialId)) }) if (!response.ok) { throw new Error(response.statusText) @@ -106,18 +140,21 @@ class RestClientImpl implements RestClient { messageId?: MessageID, options?: CreateMessageOptions ): Promise { - const result = await this.event({ - type: MessageEventType.CreateMessage, - messageType: type, - cardId, - cardType, - content, - extra, - socialId, - date, - messageId, - options - }) + const result = await this.event( + { + type: MessageEventType.CreateMessage, + messageType: type, + cardId, + cardType, + content, + extra, + socialId, + date, + messageId, + options + }, + socialId + ) return result as CreateMessageResult } @@ -130,26 +167,32 @@ class RestClientImpl implements RestClient { date?: Date, options?: UpdatePatchOptions ): Promise { - await this.event({ - type: MessageEventType.UpdatePatch, - cardId, - messageId, - content, - extra, - socialId, - date, - options - }) + await this.event( + { + type: MessageEventType.UpdatePatch, + cardId, + messageId, + content, + extra, + socialId, + date, + options + }, + socialId + ) } async removeMessage (cardId: CardID, messageId: MessageID, socialId: SocialID, date?: Date): Promise { - await this.event({ - type: MessageEventType.RemovePatch, - cardId, - messageId, - socialId, - date - }) + await this.event( + { + type: MessageEventType.RemovePatch, + cardId, + messageId, + socialId, + date + }, + socialId + ) } async attachBlobs ( @@ -159,19 +202,22 @@ class RestClientImpl implements RestClient { socialId: SocialID, date?: Date ): Promise { - await this.event({ - type: MessageEventType.BlobPatch, - cardId, - messageId, - operations: [ - { - opcode: 'attach', - blobs - } - ], - socialId, - date - }) + await this.event( + { + type: MessageEventType.BlobPatch, + cardId, + messageId, + operations: [ + { + opcode: 'attach', + blobs + } + ], + socialId, + date + }, + socialId + ) } async detachBlobs ( @@ -181,19 +227,22 @@ class RestClientImpl implements RestClient { socialId: SocialID, date?: Date ): Promise { - await this.event({ - type: MessageEventType.BlobPatch, - cardId, - messageId, - operations: [ - { - opcode: 'detach', - blobIds - } - ], - socialId, - date - }) + await this.event( + { + type: MessageEventType.BlobPatch, + cardId, + messageId, + operations: [ + { + opcode: 'detach', + blobIds + } + ], + socialId, + date + }, + socialId + ) } async setBlobs ( @@ -203,100 +252,37 @@ class RestClientImpl implements RestClient { socialId: SocialID, date?: Date ): Promise { - await this.event({ - type: MessageEventType.BlobPatch, - cardId, - messageId, - operations: [ - { - opcode: 'set', - blobs - } - ], - socialId, - date - }) + await this.event( + { + type: MessageEventType.BlobPatch, + cardId, + messageId, + operations: [ + { + opcode: 'set', + blobs + } + ], + socialId, + date + }, + socialId + ) } async findMessages (params: FindMessagesParams): Promise { - const searchParams = new URLSearchParams() - if (Object.keys(params).length > 0) { - searchParams.append('params', JSON.stringify(params)) - } - const requestUrl = concatLink(this.endpoint, `/api/v1/find-messages/${this.workspace}?${searchParams.toString()}`) - - return await retry( - async () => { - const response = await fetch(requestUrl, this.requestInit()) - if (!response.ok) { - throw new Error(response.statusText) - } - return await extractJson(response) - }, - { retries } - ) + return await this.find('findMessages', params) } async findMessagesGroups (params: FindMessagesGroupsParams): Promise { - const searchParams = new URLSearchParams() - if (Object.keys(params).length > 0) { - searchParams.append('params', JSON.stringify(params)) - } - const requestUrl = concatLink( - this.endpoint, - `/api/v1/find-messages-groups/${this.workspace}?${searchParams.toString()}` - ) - return await retry( - async () => { - const response = await fetch(requestUrl, this.requestInit()) - if (!response.ok) { - throw new Error(response.statusText) - } - return await extractJson(response) - }, - { retries } - ) + return await this.find('findMessagesGroups', params) } async findNotificationContexts (params: FindNotificationContextParams): Promise { - const searchParams = new URLSearchParams() - if (Object.keys(params).length > 0) { - searchParams.append('params', JSON.stringify(params)) - } - const requestUrl = concatLink( - this.endpoint, - `/api/v1/find-notification-contexts/${this.workspace}?${searchParams.toString()}` - ) - return await retry( - async () => { - const response = await fetch(requestUrl, this.requestInit()) - if (!response.ok) { - throw new Error(response.statusText) - } - return await extractJson(response) - }, - { retries } - ) + return await this.find('findNotificationContexts', params) } async findNotifications (params: FindNotificationsParams): Promise { - const searchParams = new URLSearchParams() - if (Object.keys(params).length > 0) { - searchParams.append('params', JSON.stringify(params)) - } - const requestUrl = concatLink( - this.endpoint, - `/api/v1/find-notifications/${this.workspace}?${searchParams.toString()}` - ) - return await retry( - async () => { - const response = await fetch(requestUrl, this.requestInit()) - if (!response.ok) { - throw new Error(response.statusText) - } - return await extractJson(response) - }, - { retries } - ) + return await this.find('findNotifications', params) } } diff --git a/packages/rest-client/src/types.ts b/packages/rest-client/src/types.ts index 3b5c1c675b..cf92ff888b 100644 --- a/packages/rest-client/src/types.ts +++ b/packages/rest-client/src/types.ts @@ -46,7 +46,7 @@ export interface RestClient { findNotificationContexts: (params: FindNotificationContextParams) => Promise findNotifications: (params: FindNotificationsParams) => Promise - event: (event: Event) => Promise + event: (event: Event, socialId: SocialID) => Promise createMessage: ( cardId: CardID, diff --git a/packages/sdk-types/src/serverApi.ts b/packages/sdk-types/src/serverApi.ts index 2be491af85..93ab109319 100644 --- a/packages/sdk-types/src/serverApi.ts +++ b/packages/sdk-types/src/serverApi.ts @@ -34,6 +34,7 @@ import type { EventResult, Event } from './events/event' export interface SessionData { sessionId?: string account: Account + derived?: boolean contextData?: any } diff --git a/packages/server/src/middlewares.ts b/packages/server/src/middlewares.ts index 0c7f3bf481..aefd96dbed 100644 --- a/packages/server/src/middlewares.ts +++ b/packages/server/src/middlewares.ts @@ -175,7 +175,7 @@ export class Middlewares { async event (session: SessionData, event: Event): Promise { if (this.head === undefined) return {} - return (await this.head?.event(session, event as Enriched, false)) ?? {} + return (await this.head?.event(session, event as Enriched, session.derived ?? false)) ?? {} } async closeSession (sessionId: string): Promise {