Skip to content

Commit

Permalink
fix: ensure presence is fetched for multiple other members in a conve…
Browse files Browse the repository at this point in the history
…rsation
  • Loading branch information
domw30 committed Oct 17, 2023
1 parent cb025dd commit 04488f9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
13 changes: 9 additions & 4 deletions src/lib/chat/matrix-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,15 @@ export class MatrixClient implements IChatClient {

try {
const userPresenceData = await this.matrix.getPresence(userId);
const isOnline = userPresenceData?.presence === 'online';
const lastSeenAt = userPresenceData?.last_active_ago
? new Date(Date.now() - userPresenceData.last_active_ago).toISOString()
: null;

if (!userPresenceData) {
return { lastSeenAt: null, isOnline: false };
}

const { presence, last_active_ago } = userPresenceData;

const isOnline = presence === 'online';
const lastSeenAt = last_active_ago ? new Date(Date.now() - last_active_ago).toISOString() : null;

return { lastSeenAt, isOnline };
} catch (error) {
Expand Down
23 changes: 13 additions & 10 deletions src/store/channels-list/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,22 @@ export function* updateUserPresence(conversations) {

const chatClient = yield call(chat.get);
for (let conversation of conversations) {
const { isOneOnOne, otherMembers } = conversation;
const matrixId = otherMembers?.[0]?.matrixId;
const { otherMembers } = conversation;

for (let member of otherMembers) {
const matrixId = member?.matrixId;
if (!matrixId) continue;

if (isOneOnOne && matrixId) {
const presenceData = yield call([chatClient, chatClient.getUserPresence], matrixId);
if (!presenceData) continue;

const { lastSeenAt, isOnline } = presenceData;

if (presenceData) {
if (presenceData.lastSeenAt) {
conversation.otherMembers[0].lastSeenAt = presenceData.lastSeenAt;
}
if (presenceData.isOnline) {
conversation.otherMembers[0].isOnline = presenceData.isOnline;
}
if (lastSeenAt) {
member.lastSeenAt = lastSeenAt;
}
if (isOnline) {
member.isOnline = isOnline;
}
}
}
Expand Down

0 comments on commit 04488f9

Please sign in to comment.