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
121 lines
4.1 KiB
TypeScript
121 lines
4.1 KiB
TypeScript
import { Apps, AppEvents } from '@rocket.chat/apps';
|
|
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
|
|
import { Message, Team, Room } from '@rocket.chat/core-services';
|
|
import type { IRoom, IUser, MessageTypesValues } from '@rocket.chat/core-typings';
|
|
import { Subscriptions, Rooms } from '@rocket.chat/models';
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { settings } from '../../settings';
|
|
import { afterLeaveRoomCallback } from '../callbacks/afterLeaveRoomCallback';
|
|
import { beforeLeaveRoomCallback } from '../callbacks/beforeLeaveRoomCallback';
|
|
import { notifyOnRoomChangedById, notifyOnSubscriptionChanged } from '../notifyListener';
|
|
|
|
/**
|
|
* Removes a user from a room when triggered by federation or other external events.
|
|
* Executes only the necessary database operations, with no callbacks, to prevent
|
|
* propagation loops during external event processing.
|
|
*/
|
|
export const performUserRemoval = async function (
|
|
room: IRoom,
|
|
user: IUser,
|
|
options?: { byUser?: IUser; skipAppPreEvents?: boolean; customSystemMessage?: MessageTypesValues },
|
|
): Promise<void> {
|
|
const subscription = await Subscriptions.findOneByRoomIdAndUserId(room._id, user._id, {
|
|
projection: { _id: 1, status: 1 },
|
|
});
|
|
if (!subscription) {
|
|
return;
|
|
}
|
|
|
|
// make TS happy, this should never happen
|
|
if (!user.username) {
|
|
throw new Error('User must have a username to be removed from the room');
|
|
}
|
|
|
|
// TODO: move before callbacks to service
|
|
await beforeLeaveRoomCallback.run(user, room);
|
|
|
|
if (subscription) {
|
|
if (options?.customSystemMessage) {
|
|
await Message.saveSystemMessage(options?.customSystemMessage, room._id, user.username || '', user);
|
|
} else if (options?.byUser) {
|
|
const extraData = {
|
|
u: options.byUser,
|
|
};
|
|
|
|
if (room.teamMain) {
|
|
await Message.saveSystemMessage('removed-user-from-team', room._id, user.username, user, extraData);
|
|
} else {
|
|
await Message.saveSystemMessage('ru', room._id, user.username, user, extraData);
|
|
}
|
|
} else if (subscription.status === 'INVITED') {
|
|
await Message.saveSystemMessage('uir', room._id, user.username, user);
|
|
} else if (room.teamMain) {
|
|
await Message.saveSystemMessage('ult', room._id, user.username, user);
|
|
} else {
|
|
await Message.saveSystemMessage('ul', room._id, user.username, user);
|
|
}
|
|
}
|
|
|
|
if (room.t === 'l') {
|
|
await Message.saveSystemMessage('command', room._id, 'survey', user);
|
|
}
|
|
|
|
const deletedSubscription = await Subscriptions.removeByRoomIdAndUserId(room._id, user._id);
|
|
if (deletedSubscription) {
|
|
void notifyOnSubscriptionChanged(deletedSubscription, 'removed');
|
|
}
|
|
|
|
if (room.teamId && room.teamMain) {
|
|
await Team.removeMember(room.teamId, user._id);
|
|
}
|
|
|
|
if (room.encrypted && settings.get('E2E_Enable')) {
|
|
await Rooms.removeUsersFromE2EEQueueByRoomId(room._id, [user._id]);
|
|
}
|
|
|
|
// remove references to the user in direct message rooms
|
|
if (room.t === 'd') {
|
|
await Rooms.removeUserReferenceFromDMsById(room._id, user.username, user._id);
|
|
}
|
|
|
|
void notifyOnRoomChangedById(room._id);
|
|
};
|
|
|
|
/**
|
|
* Removes a user from the given room by performing the required database updates
|
|
* and triggering all standard callbacks. Used for local actions (UI or API)
|
|
* that should propagate normally to federation and other subscribers.
|
|
*/
|
|
export const removeUserFromRoom = async function (
|
|
rid: string,
|
|
user: IUser,
|
|
options?: { byUser?: IUser; skipAppPreEvents?: boolean; customSystemMessage?: MessageTypesValues },
|
|
): Promise<void> {
|
|
const room = await Rooms.findOneById(rid);
|
|
if (!room) {
|
|
return;
|
|
}
|
|
|
|
// Rationale: for an abac room, we don't want apps to be able to prevent a user from leaving
|
|
if (!options?.skipAppPreEvents) {
|
|
try {
|
|
await Apps.self?.triggerEvent(AppEvents.IPreRoomUserLeave, room, user, options?.byUser);
|
|
} catch (error: any) {
|
|
if (error.name === AppsEngineException.name) {
|
|
throw new Meteor.Error('error-app-prevented', error.message);
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
await Room.beforeLeave(room);
|
|
|
|
await performUserRemoval(room, user, options);
|
|
|
|
await afterLeaveRoomCallback.run({ user, kicker: options?.byUser }, room);
|
|
|
|
await Apps.self?.triggerEvent(AppEvents.IPostRoomUserLeave, room, user, options?.byUser);
|
|
};
|