Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove user's avatar from filesystem when deleting the user #2853

Merged
merged 1 commit into from
Apr 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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