Skip to content

Commit

Permalink
chore: delete user working without relying on msg db watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosSpessatto committed Sep 18, 2023
1 parent 5e61b10 commit 6d516c9
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
23 changes: 16 additions & 7 deletions apps/meteor/app/lib/server/functions/deleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ export async function deleteUser(userId: string, confirmRelinquish = false, dele

// Users without username can't do anything, so there is nothing to remove
if (user.username != null) {
let userToReplaceWhenUnlinking: IUser | null = null;
const nameAlias = i18n.t('Removed_User');
await relinquishRoomOwnerships(userId, subscribedRooms);

const messageErasureType = settings.get('Message_ErasureType');
const messageErasureType = settings.get<'Delete' | 'Unlink' | 'Keep'>('Message_ErasureType');
switch (messageErasureType) {
case 'Delete':
const store = FileUpload.getStore('Uploads');
Expand All @@ -68,12 +70,11 @@ export async function deleteUser(userId: string, confirmRelinquish = false, dele

break;
case 'Unlink':
const rocketCat = await Users.findOneById('rocket.cat');
const nameAlias = i18n.t('Removed_User');
if (!rocketCat?._id || !rocketCat?.username) {
userToReplaceWhenUnlinking = await Users.findOneById('rocket.cat');
if (!userToReplaceWhenUnlinking?._id || !userToReplaceWhenUnlinking?.username) {
break;
}
await Messages.unlinkUserId(userId, rocketCat?._id, rocketCat?.username, nameAlias);
await Messages.unlinkUserId(userId, userToReplaceWhenUnlinking?._id, userToReplaceWhenUnlinking?.username, nameAlias);
break;
}

Expand Down Expand Up @@ -104,8 +105,16 @@ export async function deleteUser(userId: string, confirmRelinquish = false, dele
await Integrations.disableByUserId(userId); // Disables all the integrations which rely on the user being deleted.

// Don't broadcast user.deleted for Erasure Type of 'Keep' so that messages don't disappear from logged in sessions
if (messageErasureType !== 'Keep') {
void api.broadcast('user.deleted', user);
if (messageErasureType === 'Delete') {
void api.broadcast('user.deleted', user, {
messageErasureType,
});
}
if (messageErasureType === 'Unlink' && userToReplaceWhenUnlinking) {
void api.broadcast('user.deleted', user, {
messageErasureType,
replaceByUser: { _id: userToReplaceWhenUnlinking._id, username: userToReplaceWhenUnlinking?.username, alias: nameAlias },
});
}
}

Expand Down
18 changes: 17 additions & 1 deletion apps/meteor/client/startup/UserDeleted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@ import { ChatMessage } from '../../app/models/client';
import { Notifications } from '../../app/notifications/client';

Meteor.startup(() => {
Notifications.onLogged('Users:Deleted', ({ userId }) => {
Notifications.onLogged('Users:Deleted', ({ userId, messageErasureType, replaceByUser }) => {
if (messageErasureType === 'Unlink' && replaceByUser) {
return ChatMessage.update(
{
'u._id': userId,
},
{
$set: {
'alias': replaceByUser.alias,
'u._id': replaceByUser._id,
'u.username': replaceByUser.username,
'u.name': undefined,
},
},
{ multi: true },
);
}
ChatMessage.remove({
'u._id': userId,
});
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/server/modules/listeners/listeners.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ export class ListenersModule {
});
});

service.onEvent('user.deleted', ({ _id: userId }) => {
service.onEvent('user.deleted', ({ _id: userId }, data) => {
notifications.notifyLoggedInThisInstance('Users:Deleted', {
userId,
...data,
});
});

Expand Down
13 changes: 10 additions & 3 deletions ee/packages/ddp-client/src/types/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,16 @@ export interface StreamerEvents {
{
key: 'Users:Deleted';
args: [
{
userId: IUser['_id'];
},
| {
userId: IUser['_id'];
messageErasureType: 'Delete';
replaceByUser?: never;
}
| {
userId: IUser['_id'];
messageErasureType: 'Unlink';
replaceByUser?: { _id: IUser['_id']; username: IUser['username']; alias: string };
},
];
},
{
Expand Down
12 changes: 11 additions & 1 deletion packages/core-services/src/events/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,17 @@ export type EventSignatures = {
'stream'([streamer, eventName, payload]: [string, string, any[]]): void;
'subscription'(data: { action: string; subscription: Partial<ISubscription> }): void;
'user.avatarUpdate'(user: Partial<IUser>): void;
'user.deleted'(user: Pick<IUser, '_id'>): void;
'user.deleted'(
user: Pick<IUser, '_id'>,
data:
| {
messageErasureType: 'Delete';
}
| {
messageErasureType: 'Unlink';
replaceByUser: { _id: IUser['_id']; username: IUser['username']; alias: string };
},
): void;
'user.deleteCustomStatus'(userStatus: IUserStatus): void;
'user.nameChanged'(user: Pick<IUser, '_id' | 'name' | 'username'>): void;
'user.realNameChanged'(user: Partial<IUser>): void;
Expand Down

0 comments on commit 6d516c9

Please sign in to comment.