The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/server/methods/toggleFavorite.ts

33 lines
1.1 KiB

import type { ServerMethods } from '@rocket.chat/ui-contexts';
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import type { IRoom } from '@rocket.chat/core-typings';
import { Subscriptions } from '@rocket.chat/models';
declare module '@rocket.chat/ui-contexts' {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface ServerMethods {
toggleFavorite(rid: IRoom['_id'], f?: boolean): Promise<number>;
}
}
Meteor.methods<ServerMethods>({
async toggleFavorite(rid, f) {
check(rid, String);
check(f, Match.Optional(Boolean));
const userId = Meteor.userId();
if (!userId) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'toggleFavorite',
});
}
const userSubscription = await Subscriptions.findOneByRoomIdAndUserId(rid, userId);
if (!userSubscription) {
throw new Meteor.Error('error-invalid-subscription', 'You must be part of a room to favorite it', { method: 'toggleFavorite' });
}
return (await Subscriptions.setFavoriteByRoomIdAndUserId(rid, userId, f)).modifiedCount;
},
});