Skip to content

Commit

Permalink
Close #2708 Remove user's avatar from filesystem when deleting the us…
Browse files Browse the repository at this point in the history
…er (#2853)
  • Loading branch information
sampaiodiego authored and engelgabriel committed Apr 12, 2016
1 parent 5809607 commit 472304e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Package.onUse(function(api) {
// SERVER FUNCTIONS
api.addFiles('server/functions/checkUsernameAvailability.coffee', 'server');
api.addFiles('server/functions/checkEmailAvailability.js', 'server');
api.addFiles('server/functions/deleteUser.js', 'server');
api.addFiles('server/functions/sendMessage.coffee', 'server');
api.addFiles('server/functions/settings.coffee', 'server');
api.addFiles('server/functions/setUsername.coffee', 'server');
Expand Down
29 changes: 29 additions & 0 deletions packages/rocketchat-lib/server/functions/deleteUser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* globals RocketChat */
RocketChat.deleteUser = function(userId) {
const user = RocketChat.models.Users.findOneById(userId);

RocketChat.models.Messages.removeByUserId(userId); // Remove user messages
RocketChat.models.Subscriptions.findByUserId(userId).forEach((subscription) => {
let 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);
}
}
});

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') {
RocketChatFileAvatarInstance.deleteFile(encodeURIComponent(user.username + '.jpg'));
}

RocketChat.models.Users.removeById(userId); // Remove user from users database
};
19 changes: 1 addition & 18 deletions packages/rocketchat-lib/server/methods/deleteUserOwnAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,7 @@ Meteor.methods({
}

Meteor.defer(function() {
RocketChat.models.Messages.removeByUserId(userId); // Remove user messages
RocketChat.models.Subscriptions.findByUserId(userId).forEach((subscription) => {
let 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);
}
}
});

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.Users.removeById(userId); // Remove user from users database
RocketChat.deleteUser(userId);
});

return true;
Expand Down
16 changes: 1 addition & 15 deletions server/methods/deleteUser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@ Meteor.methods
unless user?
throw new Meteor.Error 'not-found', '[methods] deleteUser -> User not found'

RocketChat.models.Messages.removeByUserId userId # Remove user messages

RocketChat.models.Subscriptions.findByUserId(userId).forEach (subscription) ->
room = RocketChat.models.Rooms.findOneById subscription.rid
if room.t isnt 'c' and room.usernames.length is 1
RocketChat.models.Rooms.removeById subscription.rid # Remove non-channel rooms with only 1 user (the one being deleted)
if room.t is '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.Users.removeById userId # Remove user from users database
RocketChat.deleteUser(userId)

return true

0 comments on commit 472304e

Please sign in to comment.