Skip to content

Commit

Permalink
refactor: moved user presence logic from saga to matrix client
Browse files Browse the repository at this point in the history
  • Loading branch information
domw30 committed Oct 12, 2023
1 parent a2a7a04 commit adf6924
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/lib/chat/matrix-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/store/channels-list/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down

0 comments on commit adf6924

Please sign in to comment.