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
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
82 lines
2.8 KiB
TypeScript
82 lines
2.8 KiB
TypeScript
import type { IUser } from '@rocket.chat/core-typings';
|
|
import { Subscriptions, Users } from '@rocket.chat/models';
|
|
|
|
import { subscriptionHasRole } from '../authorization';
|
|
|
|
export type SubscribedRoomsForUserWithDetails = {
|
|
rid: string;
|
|
t: string;
|
|
shouldBeRemoved: boolean;
|
|
shouldChangeOwner: boolean;
|
|
userIsLastOwner: boolean;
|
|
newOwner: IUser['_id'] | null;
|
|
};
|
|
|
|
export function shouldRemoveOrChangeOwner(subscribedRooms: SubscribedRoomsForUserWithDetails[]): boolean {
|
|
return subscribedRooms.some(({ shouldBeRemoved, shouldChangeOwner }) => shouldBeRemoved || shouldChangeOwner);
|
|
}
|
|
|
|
export async function getSubscribedRoomsForUserWithDetails(
|
|
userId: string,
|
|
assignNewOwner = true,
|
|
roomIds: string[] = [],
|
|
): Promise<SubscribedRoomsForUserWithDetails[]> {
|
|
const subscribedRooms: SubscribedRoomsForUserWithDetails[] = [];
|
|
|
|
const cursor =
|
|
roomIds.length > 0 ? Subscriptions.findByUserIdAndRoomIds(userId, roomIds) : Subscriptions.findByUserIdExceptType(userId, 'd');
|
|
|
|
// Iterate through all the rooms the user is subscribed to, to check if he is the last owner of any of them.
|
|
for await (const subscription of cursor) {
|
|
const roomData: SubscribedRoomsForUserWithDetails = {
|
|
rid: subscription.rid,
|
|
t: subscription.t,
|
|
shouldBeRemoved: false,
|
|
shouldChangeOwner: false,
|
|
userIsLastOwner: false,
|
|
newOwner: null,
|
|
};
|
|
|
|
if (subscriptionHasRole(subscription, 'owner')) {
|
|
// Fetch the number of owners
|
|
const numOwners = await Subscriptions.countByRoomIdAndRoles(subscription.rid, ['owner']);
|
|
// If it's only one, then this user is the only owner.
|
|
roomData.userIsLastOwner = numOwners === 1;
|
|
if (numOwners === 1 && assignNewOwner) {
|
|
// Let's check how many subscribers the room has.
|
|
const options = { projection: { 'u._id': 1 }, sort: { ts: 1 as const } };
|
|
const subscribersCursor = Subscriptions.findByRoomId(subscription.rid, options);
|
|
|
|
for await (const {
|
|
u: { _id: uid },
|
|
} of subscribersCursor) {
|
|
// If we already changed the owner or this subscription is for the user we are removing, then don't try to give it ownership
|
|
if (uid === userId) {
|
|
continue;
|
|
}
|
|
const newOwner = await Users.findOneActiveById(uid, { projection: { _id: 1 } });
|
|
if (!newOwner) {
|
|
continue;
|
|
}
|
|
|
|
roomData.newOwner = uid;
|
|
roomData.shouldChangeOwner = true;
|
|
break;
|
|
}
|
|
|
|
// If there's no subscriber available to be the new owner and it's not a public room, we can remove it.
|
|
if (!roomData.shouldChangeOwner && roomData.t !== 'c') {
|
|
roomData.shouldBeRemoved = true;
|
|
}
|
|
}
|
|
} else if (roomData.t !== 'c') {
|
|
// If the user is not an owner, remove the room if the user is the only subscriber
|
|
roomData.shouldBeRemoved = (await Subscriptions.countByRoomId(roomData.rid)) === 1;
|
|
}
|
|
|
|
subscribedRooms.push(roomData);
|
|
}
|
|
|
|
return subscribedRooms;
|
|
}
|