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
129 lines
4.0 KiB
TypeScript
129 lines
4.0 KiB
TypeScript
/*
|
|
* Invite is a named function that will replace /invite commands
|
|
* @param {Object} message - The message object
|
|
*/
|
|
|
|
import { api } from '@rocket.chat/core-services';
|
|
import type { ISubscription, SlashCommand, SlashCommandCallbackParams } from '@rocket.chat/core-typings';
|
|
import { Rooms, Subscriptions, Users } from '@rocket.chat/models';
|
|
import { isTruthy } from '@rocket.chat/tools';
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { canAccessRoomAsync } from '../../lib/authorization';
|
|
import { i18n } from '../../lib/i18n';
|
|
import { slashCommands } from '../../lib/utils/slashCommand';
|
|
import { addUsersToRoomMethod } from '../../meteor-methods/rooms/addUsersToRoom';
|
|
import { createChannelMethod } from '../../meteor-methods/rooms/createChannel';
|
|
import { createPrivateGroupMethod } from '../../meteor-methods/rooms/createPrivateGroup';
|
|
import { settings } from '../../settings';
|
|
|
|
function inviteAll<T extends string>(type: T): SlashCommand<T>['callback'] {
|
|
return async function inviteAll({ command, params, message, userId }: SlashCommandCallbackParams<T>): Promise<void> {
|
|
if (!/invite\-all-(to|from)/.test(command)) {
|
|
return;
|
|
}
|
|
|
|
let channel = params.trim();
|
|
if (channel === '') {
|
|
return;
|
|
}
|
|
|
|
channel = channel.replace('#', '');
|
|
|
|
if (!channel) {
|
|
return;
|
|
}
|
|
if (!userId) {
|
|
return;
|
|
}
|
|
|
|
const user = await Users.findOneById(userId);
|
|
if (!user) {
|
|
return;
|
|
}
|
|
const lng = user?.language || settings.get('Language') || 'en';
|
|
|
|
const baseChannel = type === 'to' ? await Rooms.findOneById(message.rid) : await Rooms.findOneByName(channel);
|
|
const targetChannel = type === 'from' ? await Rooms.findOneById(message.rid) : await Rooms.findOneByName(channel);
|
|
|
|
if (!baseChannel) {
|
|
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
|
|
msg: i18n.t('Channel_doesnt_exist', {
|
|
interpolation: { escapeValue: false },
|
|
channelName: channel,
|
|
lng,
|
|
}),
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (!(await canAccessRoomAsync(baseChannel, user))) {
|
|
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
|
|
msg: i18n.t('Room_not_exist_or_not_permission', { lng }),
|
|
});
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const APIsettings = settings.get<number>('API_User_Limit');
|
|
if (!APIsettings) {
|
|
return;
|
|
}
|
|
if ((await Subscriptions.countByRoomIdWhenUsernameExists(baseChannel._id)) > APIsettings) {
|
|
throw new Meteor.Error('error-user-limit-exceeded', 'User Limit Exceeded', {
|
|
method: 'addAllToRoom',
|
|
});
|
|
}
|
|
|
|
const cursor = Subscriptions.findByRoomIdWhenUsernameExists(baseChannel._id, {
|
|
projection: { 'u.username': 1 },
|
|
});
|
|
const users = (await cursor.toArray()).map((s: ISubscription) => s.u.username).filter(isTruthy);
|
|
|
|
if (!targetChannel && ['c', 'p'].indexOf(baseChannel.t) > -1) {
|
|
baseChannel.t === 'c' ? await createChannelMethod(userId, channel, users) : await createPrivateGroupMethod(user, channel, users);
|
|
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
|
|
msg: i18n.t('Channel_created', {
|
|
interpolation: { escapeValue: false },
|
|
channelName: channel,
|
|
lng,
|
|
}),
|
|
});
|
|
} else {
|
|
await addUsersToRoomMethod(userId, {
|
|
rid: targetChannel?._id ?? '',
|
|
users,
|
|
});
|
|
}
|
|
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
|
|
msg: i18n.t('Users_added', { lng }),
|
|
});
|
|
} catch (e: any) {
|
|
const msg = e.error === 'cant-invite-for-direct-room' ? 'Cannot_invite_users_to_direct_rooms' : e.error;
|
|
void api.broadcast('notify.ephemeralMessage', userId, message.rid, {
|
|
msg: i18n.t(msg, { lng }),
|
|
});
|
|
}
|
|
};
|
|
}
|
|
|
|
slashCommands.add({
|
|
command: 'invite-all-to',
|
|
callback: inviteAll('to'),
|
|
options: {
|
|
description: 'Invite_user_to_join_channel_all_to',
|
|
params: '#room',
|
|
permission: ['add-user-to-joined-room', 'add-user-to-any-c-room', 'add-user-to-any-p-room'],
|
|
},
|
|
});
|
|
slashCommands.add({
|
|
command: 'invite-all-from',
|
|
callback: inviteAll('from'),
|
|
options: {
|
|
description: 'Invite_user_to_join_channel_all_from',
|
|
params: '#room',
|
|
permission: 'add-user-to-joined-room',
|
|
},
|
|
});
|
|
module.exports = inviteAll;
|