Skip to content

Commit

Permalink
[FIX] User registration updating wrong subscriptions (#20128)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Jan 9, 2021
1 parent 1fa6c26 commit c36bc62
Showing 1 changed file with 25 additions and 23 deletions.
48 changes: 25 additions & 23 deletions app/lib/server/functions/saveUserIdentity.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,36 @@ export function saveUserIdentity(userId, { _id, name: rawName, username: rawUser
}

// if coming from old username, update all references
if (previousUsername && usernameChanged && typeof rawUsername !== 'undefined') {
Messages.updateAllUsernamesByUserId(user._id, username);
Messages.updateUsernameOfEditByUserId(user._id, username);
Messages.findByMention(previousUsername).forEach(function(msg) {
const updatedMsg = msg.msg.replace(new RegExp(`@${ previousUsername }`, 'ig'), `@${ username }`);
return Messages.updateUsernameAndMessageOfMentionByIdAndOldUsername(msg._id, previousUsername, username, updatedMsg);
});
Rooms.replaceUsername(previousUsername, username);
Rooms.replaceMutedUsername(previousUsername, username);
Rooms.replaceUsernameOfUserByUserId(user._id, username);
Subscriptions.setUserUsernameByUserId(user._id, username);
if (previousUsername) {
if (usernameChanged && typeof rawUsername !== 'undefined') {
Messages.updateAllUsernamesByUserId(user._id, username);
Messages.updateUsernameOfEditByUserId(user._id, username);
Messages.findByMention(previousUsername).forEach(function(msg) {
const updatedMsg = msg.msg.replace(new RegExp(`@${ previousUsername }`, 'ig'), `@${ username }`);
return Messages.updateUsernameAndMessageOfMentionByIdAndOldUsername(msg._id, previousUsername, username, updatedMsg);
});
Rooms.replaceUsername(previousUsername, username);
Rooms.replaceMutedUsername(previousUsername, username);
Rooms.replaceUsernameOfUserByUserId(user._id, username);
Subscriptions.setUserUsernameByUserId(user._id, username);

LivechatDepartmentAgents.replaceUsernameOfAgentByUserId(user._id, username);
LivechatDepartmentAgents.replaceUsernameOfAgentByUserId(user._id, username);

const fileStore = FileUpload.getStore('Avatars');
const file = fileStore.model.findOneByName(previousUsername);
if (file) {
fileStore.model.updateFileNameById(file._id, username);
const fileStore = FileUpload.getStore('Avatars');
const file = fileStore.model.findOneByName(previousUsername);
if (file) {
fileStore.model.updateFileNameById(file._id, username);
}
}
}

// update other references if either the name or username has changed
if (usernameChanged || nameChanged) {
// update name and fname of 1-on-1 direct messages
Subscriptions.updateDirectNameAndFnameByName(previousUsername, rawUsername && username, rawName && name);
// update other references if either the name or username has changed
if (usernameChanged || nameChanged) {
// update name and fname of 1-on-1 direct messages
Subscriptions.updateDirectNameAndFnameByName(previousUsername, rawUsername && username, rawName && name);

// update name and fname of group direct messages
updateGroupDMsName(user);
// update name and fname of group direct messages
updateGroupDMsName(user);
}
}

return true;
Expand Down

0 comments on commit c36bc62

Please sign in to comment.