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
99 lines
2.9 KiB
TypeScript
99 lines
2.9 KiB
TypeScript
import { Room } from '@rocket.chat/core-services';
|
|
import type { IRoom, IUser, RoomType } from '@rocket.chat/core-typings';
|
|
import { Rooms, Subscriptions, Users } from '@rocket.chat/models';
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { isObject } from '../../../lib/utils/isObject';
|
|
import { createDirectMessage } from '../../meteor-methods/messages/createDirectMessage';
|
|
|
|
export const getRoomByNameOrIdWithOptionToJoin = async ({
|
|
user,
|
|
nameOrId = '',
|
|
type,
|
|
tryDirectByUserIdOnly = false,
|
|
joinChannel = true,
|
|
errorOnEmpty = true,
|
|
}: {
|
|
user: Pick<IUser, '_id' | 'username' | 'federated' | 'federation'>;
|
|
nameOrId: string;
|
|
type?: RoomType;
|
|
tryDirectByUserIdOnly?: boolean;
|
|
joinChannel?: boolean;
|
|
errorOnEmpty?: boolean;
|
|
}): Promise<IRoom | null> => {
|
|
let room: IRoom | null;
|
|
|
|
// If the nameOrId starts with #, then let's try to find a channel or group
|
|
if (nameOrId.startsWith('#')) {
|
|
nameOrId = nameOrId.substring(1);
|
|
room = await Rooms.findOneByIdOrName(nameOrId);
|
|
} else if (nameOrId.startsWith('@') || type === 'd') {
|
|
// If the nameOrId starts with @ OR type is 'd', then let's try just a direct message
|
|
nameOrId = nameOrId.replace('@', '');
|
|
|
|
let roomUser;
|
|
if (tryDirectByUserIdOnly) {
|
|
roomUser = await Users.findOneById(nameOrId);
|
|
} else {
|
|
roomUser = await Users.findOne({
|
|
$or: [{ _id: nameOrId }, { username: nameOrId }],
|
|
});
|
|
}
|
|
|
|
room = isObject(roomUser)
|
|
? await Rooms.findOneDirectRoomContainingAllUserIDs([...new Set([user._id, roomUser._id])])
|
|
: await Rooms.findOneById(nameOrId);
|
|
|
|
// If the room hasn't been found yet, let's try some more
|
|
if (!isObject(room)) {
|
|
// If the roomUser wasn't found, then there's no destination to point towards
|
|
// so return out based upon errorOnEmpty
|
|
if (!isObject(roomUser)) {
|
|
if (errorOnEmpty) {
|
|
throw new Meteor.Error('invalid-channel');
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const { rid } = await createDirectMessage([roomUser.username], user._id);
|
|
|
|
return Rooms.findOneById(rid);
|
|
}
|
|
} else {
|
|
// Otherwise, we'll treat this as a channel or group.
|
|
room = await Rooms.findOneByIdOrName(nameOrId);
|
|
}
|
|
|
|
// If no room was found, handle the room return based upon errorOnEmpty
|
|
if (!room && errorOnEmpty) {
|
|
throw new Meteor.Error('invalid-channel');
|
|
}
|
|
|
|
if (room === null) {
|
|
return null;
|
|
}
|
|
|
|
// If a room was found and they provided a type to search, then check
|
|
// and if the type found isn't what we're looking for then handle
|
|
// the return based upon errorOnEmpty
|
|
if (type && room.t !== type) {
|
|
if (errorOnEmpty) {
|
|
throw new Meteor.Error('invalid-channel');
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// If the room type is channel and joinChannel has been passed, try to join them
|
|
// if they can't join the room, this will error out!
|
|
if (room.t === 'c' && joinChannel) {
|
|
const sub = await Subscriptions.findOneByRoomIdAndUserId(room._id, user._id, { projection: { _id: 1 } });
|
|
if (!sub) {
|
|
await Room.join({ room, user });
|
|
}
|
|
}
|
|
|
|
return room;
|
|
};
|