Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 45 additions & 20 deletions apps/meteor/app/api/server/v1/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,29 +306,53 @@ API.v1.addRoute(
},
);

API.v1.addRoute(
'rooms.saveNotification',
{ authRequired: true },
{
async post() {
const { roomId, notifications } = this.bodyParams;
const saveNotificationBodySchema = ajv.compile<{
roomId: string;
notifications: Record<string, string>;
}>({
type: 'object',
properties: {
roomId: { type: 'string', minLength: 1 },
notifications: {
type: 'object',
minProperties: 1,
additionalProperties: { type: 'string' },
},
},
required: ['roomId', 'notifications'],
additionalProperties: false,
});

if (!roomId) {
return API.v1.failure("The 'roomId' param is required");
}
const saveNotificationResponseSchema = ajv.compile({
type: 'object',
properties: {
success: { type: 'boolean', enum: [true] },
},
required: ['success'],
additionalProperties: false,
});

if (!notifications || Object.keys(notifications).length === 0) {
return API.v1.failure("The 'notifications' param is required");
}
const roomsSaveNotificationEndpoint = API.v1.post(
'rooms.saveNotification',
{
authRequired: true,
body: saveNotificationBodySchema,
response: {
200: saveNotificationResponseSchema,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const { roomId, notifications } = this.bodyParams;

await Promise.all(
Object.entries(notifications as Notifications).map(async ([notificationKey, notificationValue]) =>
saveNotificationSettingsMethod(this.userId, roomId, notificationKey as NotificationFieldType, notificationValue),
),
);
await Promise.all(
Object.entries(notifications as Notifications).map(async ([notificationKey, notificationValue]) =>
saveNotificationSettingsMethod(this.userId, roomId, notificationKey as NotificationFieldType, notificationValue),
),
);

return API.v1.success();
},
return API.v1.success({ success: true });
},
);

Expand Down Expand Up @@ -1213,7 +1237,8 @@ export const roomEndpoints = API.v1

type RoomEndpoints = ExtractRoutesFromAPI<typeof roomEndpoints> &
ExtractRoutesFromAPI<typeof roomEndpoints> &
ExtractRoutesFromAPI<typeof roomDeleteEndpoint>;
ExtractRoutesFromAPI<typeof roomDeleteEndpoint> &
ExtractRoutesFromAPI<typeof roomsSaveNotificationEndpoint>;

declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
Expand Down
4 changes: 0 additions & 4 deletions packages/rest-typings/src/v1/rooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,10 +800,6 @@ export type RoomsEndpoints = {
}) => { message: IMessage | null };
};

'/v1/rooms.saveNotification': {
POST: (params: { roomId: string; notifications: Notifications }) => void;
};

'/v1/rooms.nameExists': {
GET: (params: { roomName: string }) => {
exists: boolean;
Expand Down
Loading