Skip to content

Commit

Permalink
fix: avatar is reset in the UI when username is changed (RocketChat#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusbsilva137 authored Jul 4, 2023
1 parent ca8d94b commit 3e2d700
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-icons-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

fix: Avatar is reset in the UI when username is changed
20 changes: 10 additions & 10 deletions apps/meteor/app/lib/server/functions/saveUserIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ export async function saveUserIdentity({ _id, name: rawName, username: rawUserna
// if coming from old username, update all references
if (previousUsername) {
if (usernameChanged && typeof rawUsername !== 'undefined') {
const fileStore = FileUpload.getStore('Avatars');
const previousFile = await fileStore.model.findOneByName(previousUsername);
const file = await fileStore.model.findOneByName(username);
if (file) {
await fileStore.model.deleteFile(file._id);
}
if (previousFile) {
await fileStore.model.updateFileNameById(previousFile._id, username);
}

await Messages.updateAllUsernamesByUserId(user._id, username);
await Messages.updateUsernameOfEditByUserId(user._id, username);

Expand All @@ -64,16 +74,6 @@ export async function saveUserIdentity({ _id, name: rawName, username: rawUserna
await Subscriptions.setUserUsernameByUserId(user._id, username);

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

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

// update other references if either the name or username has changed
Expand Down

0 comments on commit 3e2d700

Please sign in to comment.