Skip to content

Commit 9669b4e

Browse files
fix: Update profile avatar immediately after saving
- Fixed useAvatarETag hook to always observe avatarETag changes instead of only when empty - Added key prop to Avatar Image component with avatarETag for cache-busting - Updated updateAvatar stream handler to also update logged-in user's avatarETag in servers database - Avatar now updates immediately when navigating back from ChangeAvatarView without requiring manual refresh
1 parent 3532447 commit 9669b4e

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-35
lines changed

app/containers/Avatar/Avatar.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,14 @@ const Avatar = React.memo(
8484

8585
image = (
8686
<Image
87+
key={`${uri}-${avatarETag || ''}`}
8788
style={avatarStyle}
8889
source={{
8990
uri,
9091
headers: RocketChatSettings.customHeaders
9192
}}
9293
priority='high'
94+
cachePolicy='memory-disk'
9395
/>
9496
);
9597
}

app/containers/Avatar/useAvatarETag.ts

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,42 @@ export const useAvatarETag = ({
2424

2525
useEffect(() => {
2626
let subscription: Subscription;
27-
if (!avatarETag) {
28-
const observeAvatarETag = async () => {
29-
const db = database.active;
30-
const usersCollection = db.get('users');
31-
const subsCollection = db.get('subscriptions');
27+
const observeAvatarETag = async () => {
28+
const db = database.active;
29+
const usersCollection = db.get('users');
30+
const subsCollection = db.get('subscriptions');
3231

33-
let record;
34-
try {
35-
if (username === text) {
36-
const serversDB = database.servers;
37-
const userCollections = serversDB.get('users');
38-
const user = await userCollections.find(id);
39-
record = user;
40-
} else if (isDirect()) {
41-
const [user] = await usersCollection.query(Q.where('username', text)).fetch();
42-
record = user;
43-
} else if (rid) {
44-
record = await subsCollection.find(rid);
45-
}
46-
} catch {
47-
// Record not found
32+
let record;
33+
try {
34+
if (username === text) {
35+
const serversDB = database.servers;
36+
const userCollections = serversDB.get('users');
37+
const user = await userCollections.find(id);
38+
record = user;
39+
} else if (isDirect()) {
40+
const [user] = await usersCollection.query(Q.where('username', text)).fetch();
41+
record = user;
42+
} else if (rid) {
43+
record = await subsCollection.find(rid);
4844
}
45+
} catch {
46+
// Record not found
47+
}
4948

50-
if (record) {
51-
const observable = record.observe() as Observable<TSubscriptionModel | TUserModel | TLoggedUserModel>;
52-
subscription = observable.subscribe(r => {
53-
setAvatarETag(r.avatarETag);
54-
});
55-
}
56-
};
57-
observeAvatarETag();
58-
return () => {
59-
if (subscription?.unsubscribe) {
60-
subscription.unsubscribe();
61-
}
62-
};
63-
}
64-
}, [text]);
49+
if (record) {
50+
const observable = record.observe() as Observable<TSubscriptionModel | TUserModel | TLoggedUserModel>;
51+
subscription = observable.subscribe(r => {
52+
setAvatarETag(r.avatarETag);
53+
});
54+
}
55+
};
56+
observeAvatarETag();
57+
return () => {
58+
if (subscription?.unsubscribe) {
59+
subscription.unsubscribe();
60+
}
61+
};
62+
}, [username, text, id, rid, type]);
6563

6664
return { avatarETag };
6765
};

app/lib/services/connect.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,22 @@ function connect({ server, logoutOnError = false }: { server: string; logoutOnEr
230230
} catch {
231231
// We can't create a new record since we don't receive the user._id
232232
}
233+
// Also update logged-in user's avatarETag in servers database if it's their avatar
234+
const { user: loggedUser } = store.getState().login;
235+
if (loggedUser && loggedUser.username === username) {
236+
const serversDB = database.servers;
237+
const serversUserCollection = serversDB.get('users');
238+
try {
239+
const loggedUserRecord = await serversUserCollection.find(loggedUser.id);
240+
await serversDB.write(async () => {
241+
await loggedUserRecord.update(u => {
242+
u.avatarETag = etag;
243+
});
244+
});
245+
} catch {
246+
// User record not found in servers database
247+
}
248+
}
233249
} else if (/permissions-changed/.test(eventName)) {
234250
const { _id, roles } = ddpMessage.fields.args[1];
235251
const db = database.active;

0 commit comments

Comments
 (0)