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
85 lines
2.8 KiB
TypeScript
85 lines
2.8 KiB
TypeScript
import type { IUser } from '@rocket.chat/core-typings';
|
|
import { isNotUndefined } from '@rocket.chat/core-typings';
|
|
import { Rooms, Subscriptions, Users } from '@rocket.chat/models';
|
|
import type { ClientSession } from 'mongodb';
|
|
|
|
import { notifyOnSubscriptionChangedByRoomId } from '../notifyListener';
|
|
|
|
type GroupDMMember = Pick<IUser, '_id' | 'username' | 'name'>;
|
|
|
|
const getFname = (members: GroupDMMember[]): string => members.map(({ name, username }) => name || username).join(', ');
|
|
const getName = (members: GroupDMMember[]): string => members.map(({ username }) => username).join(',');
|
|
|
|
async function getUsersWhoAreInTheSameGroupDMsAs(user: GroupDMMember) {
|
|
// add all users to single array so we can fetch details from them all at once
|
|
if ((await Rooms.countGroupDMsByUids([user._id])) === 0) {
|
|
return;
|
|
}
|
|
|
|
const userIds = new Set<string>();
|
|
const users = new Map<string, GroupDMMember>();
|
|
|
|
const rooms = Rooms.findGroupDMsByUids([user._id], { projection: { uids: 1 } });
|
|
await rooms.forEach((room) => {
|
|
if (!room.uids) {
|
|
return;
|
|
}
|
|
|
|
room.uids.forEach((uid) => uid !== user._id && userIds.add(uid));
|
|
});
|
|
|
|
(await Users.findByIds([...userIds], { projection: { username: 1, name: 1 } }).toArray()).forEach((user) => users.set(user._id, user));
|
|
|
|
return users;
|
|
}
|
|
|
|
function sortUsersAlphabetically(u1: GroupDMMember, u2: GroupDMMember): number {
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
return (u1.name! || u1.username!).localeCompare(u2.name! || u2.username!);
|
|
}
|
|
|
|
export const updateGroupDMsName = async (
|
|
userThatChangedName: GroupDMMember,
|
|
options?: {
|
|
session?: ClientSession;
|
|
},
|
|
): Promise<void> => {
|
|
if (!userThatChangedName.username) {
|
|
return;
|
|
}
|
|
|
|
const users = await getUsersWhoAreInTheSameGroupDMsAs(userThatChangedName);
|
|
if (!users) {
|
|
return;
|
|
}
|
|
|
|
users.set(userThatChangedName._id, userThatChangedName);
|
|
|
|
const { session } = options || {};
|
|
|
|
const rooms = Rooms.findGroupDMsByUids([userThatChangedName._id], { projection: { uids: 1 }, session });
|
|
|
|
const getMembers = (uids: string[]) => uids.map((uid) => users.get(uid)).filter(isNotUndefined);
|
|
|
|
// loop rooms to update the subscriptions from them all
|
|
for await (const room of rooms) {
|
|
if (!room.uids) {
|
|
return;
|
|
}
|
|
|
|
const members = getMembers(room.uids);
|
|
const sortedMembers = members.sort(sortUsersAlphabetically);
|
|
|
|
const subs = Subscriptions.findByRoomId(room._id, { projection: { '_id': 1, 'u._id': 1 }, session });
|
|
for await (const sub of subs) {
|
|
const otherMembers = sortedMembers.filter(({ _id }) => _id !== sub.u._id);
|
|
const updateNameRespose = await Subscriptions.updateNameAndFnameById(sub._id, getName(otherMembers), getFname(otherMembers), {
|
|
session,
|
|
});
|
|
if (updateNameRespose.modifiedCount) {
|
|
void notifyOnSubscriptionChangedByRoomId(room._id);
|
|
}
|
|
}
|
|
}
|
|
};
|