From b2b2989e460aafa57555b67a5e4d68298da441b9 Mon Sep 17 00:00:00 2001 From: Reinaldo Neto Date: Tue, 7 Feb 2023 13:47:06 -0300 Subject: [PATCH] fix the visual bug when the user change the avatar to new then clear cache --- app/containers/Avatar/AvatarContainer.tsx | 2 +- app/containers/Avatar/useAvatarETag.ts | 15 +++++++++++---- app/lib/methods/subscriptions/rooms.ts | 8 +++++++- app/sagas/login.js | 16 ++++++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/app/containers/Avatar/AvatarContainer.tsx b/app/containers/Avatar/AvatarContainer.tsx index 9262e09a8..d529c7481 100644 --- a/app/containers/Avatar/AvatarContainer.tsx +++ b/app/containers/Avatar/AvatarContainer.tsx @@ -42,7 +42,7 @@ const AvatarContainer = ({ true ); - const { avatarETag } = useAvatarETag({ username, text, type, rid }); + const { avatarETag } = useAvatarETag({ username, text, type, rid, id }); return ( { const [avatarETag, setAvatarETag] = useState(''); @@ -30,7 +32,12 @@ export const useAvatarETag = ({ let record; try { - if (isDirect() || username === text) { + if (username === text) { + const serversDB = database.servers; + const userCollections = serversDB.get('users'); + const user = await userCollections.find(id); + record = user; + } else if (isDirect()) { const [user] = await usersCollection.query(Q.where('username', text)).fetch(); record = user; } else if (rid) { @@ -41,7 +48,7 @@ export const useAvatarETag = ({ } if (record) { - const observable = record.observe() as Observable; + const observable = record.observe() as Observable; subscription = observable.subscribe(r => { setAvatarETag(r.avatarETag); }); diff --git a/app/lib/methods/subscriptions/rooms.ts b/app/lib/methods/subscriptions/rooms.ts index 443b681f4..9c0fe267f 100644 --- a/app/lib/methods/subscriptions/rooms.ts +++ b/app/lib/methods/subscriptions/rooms.ts @@ -291,7 +291,7 @@ export default function subscribeRooms() { const [type, data] = ddpMessage.fields.args; const [, ev] = ddpMessage.fields.eventName.split('/'); if (/userData/.test(ev)) { - const [{ diff }] = ddpMessage.fields.args; + const [{ diff, unset }] = ddpMessage.fields.args; if (diff?.statusLivechat) { store.dispatch(setUser({ statusLivechat: diff.statusLivechat })); } @@ -301,6 +301,12 @@ export default function subscribeRooms() { if ((['settings.preferences.alsoSendThreadToChannel'] as any) in diff) { store.dispatch(setUser({ alsoSendThreadToChannel: diff['settings.preferences.alsoSendThreadToChannel'] })); } + if (diff?.avatarETag) { + store.dispatch(setUser({ avatarETag: diff.avatarETag })); + } + if (unset?.avatarETag) { + store.dispatch(setUser({ avatarETag: '' })); + } } if (/subscriptions/.test(ev)) { if (type === 'removed') { diff --git a/app/sagas/login.js b/app/sagas/login.js index af2ad9b03..6e5b2a437 100644 --- a/app/sagas/login.js +++ b/app/sagas/login.js @@ -247,6 +247,22 @@ const handleLogout = function* handleLogout({ forcedByServer, message }) { }; const handleSetUser = function* handleSetUser({ user }) { + if ('avatarETag' in user) { + const userId = yield select(state => state.login.user.id); + const serversDB = database.servers; + const userCollections = serversDB.get('users'); + yield serversDB.write(async () => { + try { + const userRecord = await userCollections.find(userId); + await userRecord.update(record => { + record.avatarETag = user.avatarETag; + }); + } catch { + // + } + }); + } + setLanguage(user?.language); if (user?.statusLivechat && isOmnichannelModuleAvailable()) {