Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
117 lines
3.8 KiB
TypeScript
117 lines
3.8 KiB
TypeScript
import { api, Message, Team } from '@rocket.chat/core-services';
|
|
import type { IRoom, IUser } from '@rocket.chat/core-typings';
|
|
import { isRoomFederated } from '@rocket.chat/core-typings';
|
|
import type { ServerMethods } from '@rocket.chat/ddp-client';
|
|
import { Subscriptions, Rooms, Users } from '@rocket.chat/models';
|
|
import { check } from 'meteor/check';
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { hasPermissionAsync } from '../../lib/authorization/hasPermission';
|
|
import { beforeChangeRoomRole } from '../../lib/callbacks/beforeChangeRoomRole';
|
|
import { methodDeprecationLogger } from '../../lib/deprecationWarningLogger';
|
|
import { notifyOnSubscriptionChangedById } from '../../lib/notifyListener';
|
|
import { syncRoomRolePriorityForUserAndRoom } from '../../lib/roles/syncRoomRolePriority';
|
|
import { settings } from '../../settings';
|
|
|
|
declare module '@rocket.chat/ddp-client' {
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
interface ServerMethods {
|
|
removeRoomModerator(rid: IRoom['_id'], userId: IUser['_id']): boolean;
|
|
}
|
|
}
|
|
|
|
export const removeRoomModerator = async (fromUserId: IUser['_id'], rid: IRoom['_id'], userId: IUser['_id']): Promise<boolean> => {
|
|
check(rid, String);
|
|
check(userId, String);
|
|
|
|
const room = await Rooms.findOneById(rid, { projection: { t: 1, federated: 1, federation: 1 } });
|
|
if (!room) {
|
|
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
|
|
method: 'removeRoomModerator',
|
|
});
|
|
}
|
|
|
|
if (!(await hasPermissionAsync(fromUserId, 'set-moderator', rid)) && !isRoomFederated(room)) {
|
|
throw new Meteor.Error('error-not-allowed', 'Not allowed', {
|
|
method: 'removeRoomModerator',
|
|
});
|
|
}
|
|
|
|
const user = await Users.findOneById(userId);
|
|
|
|
if (!user?.username) {
|
|
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
|
|
method: 'removeRoomModerator',
|
|
});
|
|
}
|
|
|
|
const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, user._id);
|
|
|
|
if (!subscription) {
|
|
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
|
|
method: 'removeRoomModerator',
|
|
});
|
|
}
|
|
|
|
if (subscription.roles && (!Array.isArray(subscription.roles) || !subscription.roles.includes('moderator'))) {
|
|
throw new Meteor.Error('error-user-not-moderator', 'User is not a moderator', {
|
|
method: 'removeRoomModerator',
|
|
});
|
|
}
|
|
|
|
await beforeChangeRoomRole.run({ fromUserId, userId, room, role: 'user' });
|
|
|
|
const removeRoleResponse = await Subscriptions.removeRoleById(subscription._id, 'moderator');
|
|
await syncRoomRolePriorityForUserAndRoom(userId, rid, subscription.roles?.filter((r) => r !== 'moderator') || []);
|
|
|
|
if (removeRoleResponse.modifiedCount) {
|
|
void notifyOnSubscriptionChangedById(subscription._id);
|
|
}
|
|
|
|
const fromUser = await Users.findOneById(fromUserId);
|
|
if (!fromUser) {
|
|
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
|
|
method: 'removeRoomModerator',
|
|
});
|
|
}
|
|
|
|
await Message.saveSystemMessage('subscription-role-removed', rid, user.username, fromUser, { role: 'moderator' });
|
|
|
|
const team = await Team.getOneByMainRoomId(rid);
|
|
if (team) {
|
|
await Team.removeRolesFromMember(team._id, userId, ['moderator']);
|
|
}
|
|
|
|
const event = {
|
|
type: 'removed',
|
|
_id: 'moderator',
|
|
u: {
|
|
_id: user._id,
|
|
username: user.username,
|
|
name: user.name,
|
|
},
|
|
scope: rid,
|
|
} as const;
|
|
if (settings.get('UI_DisplayRoles')) {
|
|
void api.broadcast('user.roleUpdate', event);
|
|
}
|
|
void api.broadcast('federation.userRoleChanged', { ...event, givenByUserId: fromUserId });
|
|
|
|
return true;
|
|
};
|
|
|
|
Meteor.methods<ServerMethods>({
|
|
async removeRoomModerator(rid, userId) {
|
|
methodDeprecationLogger.method('removeRoomModerator', '9.0.0', ['/v1/channels.removeModerator', '/v1/groups.removeModerator']);
|
|
const uid = Meteor.userId();
|
|
|
|
if (!uid) {
|
|
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
|
|
method: 'removeRoomModerator',
|
|
});
|
|
}
|
|
|
|
return removeRoomModerator(uid, rid, userId);
|
|
},
|
|
});
|