-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9986 from RocketChat/hotfix/user-delete-without-u…
…sername [FIX] Delete user without username was removing direct rooms of all users
- Loading branch information
Showing
3 changed files
with
32 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
RocketChat.deleteUser = function(userId) { | ||
const user = RocketChat.models.Users.findOneById(userId); | ||
|
||
RocketChat.models.Messages.removeByUserId(userId); // Remove user messages | ||
RocketChat.models.Subscriptions.db.findByUserId(userId).forEach((subscription) => { | ||
const room = RocketChat.models.Rooms.findOneById(subscription.rid); | ||
if (room) { | ||
if (room.t !== 'c' && room.usernames.length === 1) { | ||
RocketChat.models.Rooms.removeById(subscription.rid); // Remove non-channel rooms with only 1 user (the one being deleted) | ||
// Users without username can't do anything, so there is nothing to remove | ||
if (user.username != null) { | ||
RocketChat.models.Messages.removeByUserId(userId); // Remove user messages | ||
RocketChat.models.Subscriptions.db.findByUserId(userId).forEach((subscription) => { | ||
const room = RocketChat.models.Rooms.findOneById(subscription.rid); | ||
if (room) { | ||
if (room.t !== 'c' && room.usernames.length === 1) { | ||
RocketChat.models.Rooms.removeById(subscription.rid); // Remove non-channel rooms with only 1 user (the one being deleted) | ||
} | ||
if (room.t === 'd') { | ||
RocketChat.models.Subscriptions.removeByRoomId(subscription.rid); | ||
RocketChat.models.Messages.removeByRoomId(subscription.rid); | ||
} | ||
} | ||
if (room.t === 'd') { | ||
RocketChat.models.Subscriptions.removeByRoomId(subscription.rid); | ||
RocketChat.models.Messages.removeByRoomId(subscription.rid); | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
RocketChat.models.Subscriptions.removeByUserId(userId); // Remove user subscriptions | ||
RocketChat.models.Rooms.removeByTypeContainingUsername('d', user.username); // Remove direct rooms with the user | ||
RocketChat.models.Rooms.removeUsernameFromAll(user.username); // Remove user from all other rooms | ||
RocketChat.models.Subscriptions.removeByUserId(userId); // Remove user subscriptions | ||
RocketChat.models.Rooms.removeByTypeContainingUsername('d', user.username); // Remove direct rooms with the user | ||
RocketChat.models.Rooms.removeUsernameFromAll(user.username); // Remove user from all other rooms | ||
|
||
// removes user's avatar | ||
if (user.avatarOrigin === 'upload' || user.avatarOrigin === 'url') { | ||
FileUpload.getStore('Avatars').deleteByName(user.username); | ||
} | ||
// removes user's avatar | ||
if (user.avatarOrigin === 'upload' || user.avatarOrigin === 'url') { | ||
FileUpload.getStore('Avatars').deleteByName(user.username); | ||
} | ||
|
||
RocketChat.models.Integrations.disableByUserId(userId); // Disables all the integrations which rely on the user being deleted. | ||
RocketChat.models.Integrations.disableByUserId(userId); // Disables all the integrations which rely on the user being deleted. | ||
} | ||
|
||
RocketChat.models.Users.removeById(userId); // Remove user from users database | ||
}; |