|
|
|
|
@ -1,7 +1,7 @@ |
|
|
|
|
import type { IUser, ISubscription } from '@rocket.chat/core-typings'; |
|
|
|
|
import type { IUser } from '@rocket.chat/core-typings'; |
|
|
|
|
import { Subscriptions, Users } from '@rocket.chat/models'; |
|
|
|
|
|
|
|
|
|
import { subscriptionHasRole } from '../../../authorization/server'; |
|
|
|
|
import { Users, Subscriptions } from '../../../models/server'; |
|
|
|
|
|
|
|
|
|
export type SubscribedRoomsForUserWithDetails = { |
|
|
|
|
rid: string; |
|
|
|
|
@ -16,18 +16,18 @@ export function shouldRemoveOrChangeOwner(subscribedRooms: SubscribedRoomsForUse |
|
|
|
|
return subscribedRooms.some(({ shouldBeRemoved, shouldChangeOwner }) => shouldBeRemoved || shouldChangeOwner); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export function getSubscribedRoomsForUserWithDetails( |
|
|
|
|
export async function getSubscribedRoomsForUserWithDetails( |
|
|
|
|
userId: string, |
|
|
|
|
assignNewOwner = true, |
|
|
|
|
roomIds: string[] = [], |
|
|
|
|
): SubscribedRoomsForUserWithDetails[] { |
|
|
|
|
): 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.
|
|
|
|
|
cursor.forEach((subscription: ISubscription) => { |
|
|
|
|
for await (const subscription of cursor) { |
|
|
|
|
const roomData: SubscribedRoomsForUserWithDetails = { |
|
|
|
|
rid: subscription.rid, |
|
|
|
|
t: subscription.t, |
|
|
|
|
@ -39,27 +39,29 @@ export function getSubscribedRoomsForUserWithDetails( |
|
|
|
|
|
|
|
|
|
if (subscriptionHasRole(subscription, 'owner')) { |
|
|
|
|
// Fetch the number of owners
|
|
|
|
|
const numOwners = Subscriptions.findByRoomIdAndRoles(subscription.rid, ['owner']).count(); |
|
|
|
|
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 } }; |
|
|
|
|
const options = { projection: { 'u._id': 1 }, sort: { ts: 1 as const } }; |
|
|
|
|
const subscribersCursor = Subscriptions.findByRoomId(subscription.rid, options); |
|
|
|
|
|
|
|
|
|
subscribersCursor.forEach(({ u: { _id: uid } }: ISubscription) => { |
|
|
|
|
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 (roomData.shouldChangeOwner || uid === userId) { |
|
|
|
|
return; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
const newOwner = Users.findOneActiveById(uid, { fields: { _id: 1 } }); |
|
|
|
|
const newOwner = await Users.findOneActiveById(uid, { projection: { _id: 1 } }); |
|
|
|
|
if (!newOwner) { |
|
|
|
|
return; |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
roomData.newOwner = uid; |
|
|
|
|
roomData.shouldChangeOwner = true; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 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') { |
|
|
|
|
@ -68,11 +70,11 @@ export function getSubscribedRoomsForUserWithDetails( |
|
|
|
|
} |
|
|
|
|
} else if (roomData.t !== 'c') { |
|
|
|
|
// If the user is not an owner, remove the room if the user is the only subscriber
|
|
|
|
|
roomData.shouldBeRemoved = Subscriptions.findByRoomId(roomData.rid).count() === 1; |
|
|
|
|
roomData.shouldBeRemoved = (await Subscriptions.countByRoomId(roomData.rid)) === 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
subscribedRooms.push(roomData); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return subscribedRooms; |
|
|
|
|
} |
|
|
|
|
|