-
Notifications
You must be signed in to change notification settings - Fork 13.1k
chore: add the new endpoints to sync with cloud #30546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
apps/meteor/app/cloud/server/functions/syncWorkspace/announcementSync.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { type Cloud, type Serialized } from '@rocket.chat/core-typings'; | ||
| import { serverFetch as fetch } from '@rocket.chat/server-fetch'; | ||
| import { v, compile } from 'suretype'; | ||
|
|
||
| import { CloudWorkspaceAccessError } from '../../../../../lib/errors/CloudWorkspaceAccessError'; | ||
| import { CloudWorkspaceConnectionError } from '../../../../../lib/errors/CloudWorkspaceConnectionError'; | ||
| import { CloudWorkspaceRegistrationError } from '../../../../../lib/errors/CloudWorkspaceRegistrationError'; | ||
| import { SystemLogger } from '../../../../../server/lib/logger/system'; | ||
| import { settings } from '../../../../settings/server'; | ||
| import { buildWorkspaceRegistrationData } from '../buildRegistrationData'; | ||
| import { getWorkspaceAccessToken } from '../getWorkspaceAccessToken'; | ||
| import { retrieveRegistrationStatus } from '../retrieveRegistrationStatus'; | ||
| import { handleAnnouncementsOnWorkspaceSync, handleNpsOnWorkspaceSync } from './handleCommsSync'; | ||
| import { legacySyncWorkspace } from './legacySyncWorkspace'; | ||
|
|
||
| const workspaceCommPayloadSchema = v.object({ | ||
| workspaceId: v.string().required(), | ||
| publicKey: v.string(), | ||
| nps: v.object({ | ||
| id: v.string().required(), | ||
| startAt: v.string().format('date-time').required(), | ||
| expireAt: v.string().format('date-time').required(), | ||
| }), | ||
| announcements: v.object({ | ||
| create: v.array( | ||
| v.object({ | ||
| _id: v.string().required(), | ||
| _updatedAt: v.string().format('date-time').required(), | ||
| selector: v.object({ | ||
| roles: v.array(v.string()), | ||
| }), | ||
| platform: v.array(v.string().enum('web', 'mobile')).required(), | ||
| expireAt: v.string().format('date-time').required(), | ||
| startAt: v.string().format('date-time').required(), | ||
| createdBy: v.string().enum('cloud', 'system').required(), | ||
| createdAt: v.string().format('date-time').required(), | ||
| dictionary: v.object({}).additional(v.object({}).additional(v.string())), | ||
| view: v.any(), | ||
| surface: v.string().enum('banner', 'modal').required(), | ||
| }), | ||
| ), | ||
| delete: v.array(v.string()), | ||
| }), | ||
| }); | ||
|
|
||
| const assertWorkspaceCommPayload = compile(workspaceCommPayloadSchema); | ||
|
|
||
| const fetchCloudAnnouncementsSync = async ({ | ||
| token, | ||
| data, | ||
| }: { | ||
| token: string; | ||
| data: Cloud.WorkspaceSyncRequestPayload; | ||
| }): Promise<Serialized<Cloud.WorkspaceCommsResponsePayload>> => { | ||
| const cloudUrl = settings.get<string>('Cloud_Url'); | ||
| const response = await fetch(`${cloudUrl}/api/v3/comms/workspace`, { | ||
| method: 'POST', | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| }, | ||
| body: data, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| try { | ||
| const { error } = await response.json(); | ||
| throw new CloudWorkspaceConnectionError(`Failed to connect to Rocket.Chat Cloud: ${error}`); | ||
| } catch (error) { | ||
| throw new CloudWorkspaceConnectionError(`Failed to connect to Rocket.Chat Cloud: ${response.statusText}`); | ||
| } | ||
| } | ||
|
|
||
| const payload = await response.json(); | ||
|
|
||
| assertWorkspaceCommPayload(payload); | ||
| return payload; | ||
| }; | ||
|
|
||
| export async function announcementSync() { | ||
| try { | ||
| const { workspaceRegistered } = await retrieveRegistrationStatus(); | ||
| if (!workspaceRegistered) { | ||
| throw new CloudWorkspaceRegistrationError('Workspace is not registered'); | ||
| } | ||
|
|
||
| const token = await getWorkspaceAccessToken(true); | ||
| if (!token) { | ||
| throw new CloudWorkspaceAccessError('Workspace does not have a valid access token'); | ||
| } | ||
|
|
||
| const workspaceRegistrationData = await buildWorkspaceRegistrationData(undefined); | ||
|
|
||
| const { nps, announcements } = await fetchCloudAnnouncementsSync({ | ||
| token, | ||
| data: workspaceRegistrationData, | ||
| }); | ||
|
|
||
| if (nps) { | ||
| await handleNpsOnWorkspaceSync(nps); | ||
| } | ||
|
|
||
| if (announcements) { | ||
| await handleAnnouncementsOnWorkspaceSync(announcements); | ||
| } | ||
|
|
||
| return true; | ||
| } catch (err) { | ||
| SystemLogger.error({ | ||
| msg: 'Failed to sync with Rocket.Chat Cloud', | ||
| url: '/sync', | ||
| err, | ||
| }); | ||
| } | ||
|
|
||
| await legacySyncWorkspace(); | ||
| } |
65 changes: 65 additions & 0 deletions
65
apps/meteor/app/cloud/server/functions/syncWorkspace/handleCommsSync.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { NPS, Banner } from '@rocket.chat/core-services'; | ||
| import { type Cloud, type Serialized } from '@rocket.chat/core-typings'; | ||
| import { CloudAnnouncements } from '@rocket.chat/models'; | ||
|
|
||
| import { getAndCreateNpsSurvey } from '../../../../../server/services/nps/getAndCreateNpsSurvey'; | ||
|
|
||
| export const handleNpsOnWorkspaceSync = async (nps: Exclude<Serialized<Cloud.WorkspaceSyncPayload>['nps'], undefined>) => { | ||
| const { id: npsId, expireAt } = nps; | ||
|
|
||
| const startAt = new Date(nps.startAt); | ||
|
|
||
| await NPS.create({ | ||
| npsId, | ||
| startAt, | ||
| expireAt: new Date(expireAt), | ||
| createdBy: { | ||
| _id: 'rocket.cat', | ||
| username: 'rocket.cat', | ||
| }, | ||
| }); | ||
|
|
||
| const now = new Date(); | ||
|
|
||
| if (startAt.getFullYear() === now.getFullYear() && startAt.getMonth() === now.getMonth() && startAt.getDate() === now.getDate()) { | ||
| await getAndCreateNpsSurvey(npsId); | ||
| } | ||
| }; | ||
|
|
||
| export const handleBannerOnWorkspaceSync = async (banners: Exclude<Serialized<Cloud.WorkspaceSyncPayload>['banners'], undefined>) => { | ||
| for await (const banner of banners) { | ||
| const { createdAt, expireAt, startAt, inactivedAt, _updatedAt, ...rest } = banner; | ||
|
|
||
| await Banner.create({ | ||
| ...rest, | ||
| createdAt: new Date(createdAt), | ||
| expireAt: new Date(expireAt), | ||
| startAt: new Date(startAt), | ||
| ...(inactivedAt && { inactivedAt: new Date(inactivedAt) }), | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const deserializeAnnouncement = (announcement: Serialized<Cloud.Announcement>): Cloud.Announcement => ({ | ||
| ...announcement, | ||
| _updatedAt: new Date(announcement._updatedAt), | ||
| expireAt: new Date(announcement.expireAt), | ||
| startAt: new Date(announcement.startAt), | ||
| createdAt: new Date(announcement.createdAt), | ||
| }); | ||
|
|
||
| export const handleAnnouncementsOnWorkspaceSync = async ( | ||
| announcements: Exclude<Serialized<Cloud.WorkspaceCommsResponsePayload>['announcements'], undefined>, | ||
| ) => { | ||
| const { create, delete: deleteIds } = announcements; | ||
|
|
||
| if (deleteIds) { | ||
| await CloudAnnouncements.deleteMany({ _id: { $in: deleteIds } }); | ||
| } | ||
|
|
||
| for await (const announcement of create.map(deserializeAnnouncement)) { | ||
| const { _id, ...rest } = announcement; | ||
|
|
||
| await CloudAnnouncements.updateOne({ _id }, { $set: rest }, { upsert: true }); | ||
| } | ||
| }; |
4 changes: 2 additions & 2 deletions
4
apps/meteor/app/cloud/server/functions/syncWorkspace/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.