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 1s
146 lines
4.7 KiB
TypeScript
146 lines
4.7 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 { isBannedSubscription, isRoomNativeFederated, type IUser } from '@rocket.chat/core-typings';
|
|
import { Subscriptions, Users, Rooms } from '@rocket.chat/models';
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { RoomMemberActions } from '../../../definition/IRoomTypeConfig';
|
|
import { callbacks } from '../callbacks';
|
|
import { roomCoordinator } from './roomCoordinator';
|
|
import { beforeAddUserToRoom as beforeAddUserToRoomPatch } from '../../hooks/rooms/beforeAddUserToRoom';
|
|
import { settings } from '../../settings';
|
|
import { beforeAddUserToRoom } from '../callbacks/beforeAddUserToRoom';
|
|
import { notifyOnRoomChangedById, notifyOnSubscriptionChanged } from '../notifyListener';
|
|
|
|
/**
|
|
* This function adds user to the given room.
|
|
* Caution - It does not validates if the user has permission to join room
|
|
*/
|
|
|
|
export const addUserToRoom = async (
|
|
rid: string,
|
|
user: Pick<IUser, '_id' | 'username'>,
|
|
inviter?: Pick<IUser, '_id' | 'username'>,
|
|
{
|
|
skipSystemMessage,
|
|
skipAlertSound,
|
|
createAsHidden = false,
|
|
}: {
|
|
skipSystemMessage?: boolean;
|
|
skipAlertSound?: boolean;
|
|
createAsHidden?: boolean;
|
|
} = {},
|
|
): Promise<boolean | undefined> => {
|
|
const now = new Date();
|
|
const room = await Rooms.findOneById(rid);
|
|
|
|
if (!room) {
|
|
throw new Meteor.Error('error-invalid-room', 'Invalid room', {
|
|
method: 'addUserToRoom',
|
|
});
|
|
}
|
|
|
|
const userToBeAdded = await Users.findOneById(user._id);
|
|
const roomDirectives = roomCoordinator.getRoomDirectives(room.t);
|
|
|
|
if (!userToBeAdded) {
|
|
throw new Meteor.Error('user-not-found');
|
|
}
|
|
|
|
if (
|
|
!(await roomDirectives.allowMemberAction(room, RoomMemberActions.JOIN, userToBeAdded._id)) &&
|
|
!(await roomDirectives.allowMemberAction(room, RoomMemberActions.INVITE, userToBeAdded._id))
|
|
) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const inviterUser = inviter && ((await Users.findOneById(inviter._id)) || undefined);
|
|
// Not "duplicated": we're moving away from callbacks so this is a patch function. We should migrate the next one to be a patch or use this same patch, instead of calling both
|
|
await beforeAddUserToRoomPatch([userToBeAdded.username!], room, inviterUser);
|
|
await beforeAddUserToRoom.run({ user: userToBeAdded, inviter: inviterUser }, room);
|
|
} catch (error) {
|
|
throw new Meteor.Error((error as any)?.message);
|
|
}
|
|
|
|
// TODO: are we calling this twice?
|
|
|
|
await callbacks.run('beforeAddedToRoom', { user: userToBeAdded, inviter });
|
|
|
|
try {
|
|
await Apps.self?.triggerEvent(AppEvents.IPreRoomUserJoined, room, userToBeAdded, inviter);
|
|
} catch (error: any) {
|
|
if (error.name === AppsEngineException.name) {
|
|
throw new Meteor.Error('error-app-prevented', error.message);
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
|
|
// for federation rooms we stop here since everything else will be handled by the federation invite flow
|
|
if (isRoomNativeFederated(room)) {
|
|
return;
|
|
}
|
|
|
|
// TODO: are we calling this twice?
|
|
if (room.t === 'c' || room.t === 'p' || room.t === 'l') {
|
|
// Add a new event, with an optional inviter
|
|
await callbacks.run('beforeAddedToRoom', { user: userToBeAdded, inviter }, room);
|
|
|
|
// Keep the current event
|
|
await callbacks.run('beforeJoinRoom', userToBeAdded, room);
|
|
}
|
|
|
|
const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, userToBeAdded._id);
|
|
if (subscription) {
|
|
if (!isBannedSubscription(subscription)) {
|
|
return true;
|
|
}
|
|
|
|
const deletedSub = await Subscriptions.removeByRoomIdAndUserId(rid, userToBeAdded._id);
|
|
if (!deletedSub) {
|
|
return true;
|
|
}
|
|
void notifyOnSubscriptionChanged(deletedSub, 'removed');
|
|
|
|
if (!skipSystemMessage && inviter) {
|
|
await Message.saveSystemMessage('user-unbanned', rid, userToBeAdded.username!, inviter, { ts: now });
|
|
}
|
|
}
|
|
|
|
await Room.createUserSubscription({
|
|
room,
|
|
ts: now,
|
|
inviter,
|
|
userToBeAdded,
|
|
createAsHidden,
|
|
skipAlertSound,
|
|
skipSystemMessage,
|
|
});
|
|
|
|
if (room.t === 'c' || room.t === 'p') {
|
|
process.nextTick(async () => {
|
|
// Add a new event, with an optional inviter
|
|
await callbacks.run('afterAddedToRoom', { user: userToBeAdded, inviter }, room);
|
|
|
|
// Keep the current event
|
|
await callbacks.run('afterJoinRoom', userToBeAdded, room);
|
|
|
|
void Apps.self?.triggerEvent(AppEvents.IPostRoomUserJoined, room, userToBeAdded, inviter);
|
|
});
|
|
}
|
|
|
|
if (room.teamMain && room.teamId) {
|
|
// if user is joining to main team channel, create a membership
|
|
await Team.addMember(inviter || userToBeAdded, userToBeAdded._id, room.teamId);
|
|
}
|
|
|
|
if (room.encrypted && settings.get('E2E_Enable') && userToBeAdded.e2e?.public_key) {
|
|
await Rooms.addUserIdToE2EEQueueByRoomIds([room._id], userToBeAdded._id);
|
|
}
|
|
|
|
void notifyOnRoomChangedById(rid);
|
|
return true;
|
|
};
|