From adf6924dc0aad27b21a8d13a7f0fe7e5a17fbd81 Mon Sep 17 00:00:00 2001 From: dominic Date: Thu, 12 Oct 2023 19:26:09 +0100 Subject: [PATCH] refactor: moved user presence logic from saga to matrix client --- src/lib/chat/matrix-client.ts | 14 +++++++++++++- src/store/channels-list/saga.ts | 6 +++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/lib/chat/matrix-client.ts b/src/lib/chat/matrix-client.ts index cbfe9c722..649199ac4 100644 --- a/src/lib/chat/matrix-client.ts +++ b/src/lib/chat/matrix-client.ts @@ -72,7 +72,19 @@ export class MatrixClient implements IChatClient { async getUserPresence(userId: string) { await this.waitForConnection(); - return await this.matrix.getPresence(userId); + + try { + const userPresenceData = await this.matrix.getPresence(userId); + const isOnline = userPresenceData?.currently_active || false; + const lastSeenAt = userPresenceData?.last_active_ago + ? new Date(Date.now() - userPresenceData.last_active_ago).toISOString() + : null; + + return { lastSeenAt, isOnline }; + } catch (error) { + console.error(error); + return { lastSeenAt: null, isOnline: false }; + } } async getChannels(_id: string) { diff --git a/src/store/channels-list/saga.ts b/src/store/channels-list/saga.ts index e82a0a473..da097a425 100644 --- a/src/store/channels-list/saga.ts +++ b/src/store/channels-list/saga.ts @@ -73,9 +73,9 @@ export function* updateOtherMembersLastSeenAt(conversations) { const matrixId = conversation?.otherMembers?.[0]?.matrixId; if (conversation.isOneOnOne && matrixId) { - const userPresenceData = yield call([chatClient, chatClient.getUserPresence], matrixId); - if (userPresenceData?.last_active_ago) { - conversation.otherMembers[0].lastSeenAt = new Date(Date.now() - userPresenceData.last_active_ago).toISOString(); + const presenceData = yield call([chatClient, chatClient.getUserPresence], matrixId); + if (presenceData && presenceData.lastSeenAt) { + conversation.otherMembers[0].lastSeenAt = presenceData.lastSeenAt; } } }