From 677affc0c9b9c0d016b94cfa62bf7839b5f67e7d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tadeusz=20So=C5=9Bnierz?= <tadeusz@sosnierz.com>
Date: Wed, 13 Oct 2021 13:30:07 +0200
Subject: [PATCH] Record user activity only when they visibly communicate with
 a room

---
 src/bot.ts | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/src/bot.ts b/src/bot.ts
index 6cfa859..287e3b1 100644
--- a/src/bot.ts
+++ b/src/bot.ts
@@ -178,8 +178,6 @@ export class DiscordBot {
 
     public GetIntentFromDiscordMember(member: Discord.GuildMember | Discord.PartialUser | Discord.User,
                                       webhookID: string|null = null): Intent {
-        let intent: Intent;
-
         if (webhookID) {
             // webhookID and user IDs are the same, they are unique, so no need to prefix _webhook_
             const name = member instanceof Discord.GuildMember ? member.user.username : member.username;
@@ -188,13 +186,9 @@ export class DiscordBot {
                 throw Error("Couldn't get intent for Discord member, name was null");
             }
             // TODO: We need to sanitize name
-            intent = this.bridge.getIntentForSuffix(`${webhookID}_${Util.EscapeStringForUserId(name)}`);
-        } else {
-            intent = this.bridge.getIntentForSuffix(member.id);
+            return this.bridge.getIntentForSuffix(`${webhookID}_${Util.EscapeStringForUserId(name)}`);
         }
-
-        this.userActivity.updateUserActivity(intent.userId);
-        return intent;
+        return this.bridge.getIntentForSuffix(member.id);
     }
 
     public async init(): Promise<void> {
@@ -912,6 +906,7 @@ export class DiscordBot {
                                     msgID: string): Promise<boolean> {
         const rooms = await this.channelSync.GetRoomIdsFromChannel(chan);
         const intent = this.GetIntentFromDiscordMember(author);
+        this.userActivity.updateUserActivity(intent.userId);
 
         await Util.AsyncForEach(rooms, async (roomId) => {
             const eventId = await intent.sendEvent(roomId, {
@@ -938,6 +933,7 @@ export class DiscordBot {
         try {
             const intent = this.GetIntentFromDiscordMember(user);
             await intent.ensureRegistered();
+            this.userActivity.updateUserActivity(intent.userId);
             await Promise.all(rooms.map( async (roomId) => {
                 return intent.underlyingClient.setTyping(roomId, isTyping);
             }));
@@ -1008,6 +1004,7 @@ export class DiscordBot {
         }
         try {
             const intent = this.GetIntentFromDiscordMember(msg.author, msg.webhookID);
+            this.userActivity.updateUserActivity(intent.userId);
             // Check Attachements
             if (!editEventId) {
                 // on discord you can't edit in images, you can only edit text
@@ -1153,6 +1150,7 @@ export class DiscordBot {
             log.info(`Deleting discord msg ${storeEvent.DiscordId}`);
             const intent = this.GetIntentFromDiscordMember(msg.author, msg.webhookID);
             await intent.ensureRegistered();
+            this.userActivity.updateUserActivity(intent.userId);
             const matrixIds = storeEvent.MatrixId.split(";");
             try {
                 await intent.underlyingClient.redactEvent(matrixIds[1], matrixIds[0]);
-- 
GitLab