From e4563135bf863d0a7af5490a682babbe8e53cc7c Mon Sep 17 00:00:00 2001
From: Bruno Windels <brunow@matrix.org>
Date: Mon, 30 Mar 2020 21:59:44 +0200
Subject: [PATCH] look at prev_content when handling member events

---
 .../room/timeline/tiles/RoomMemberTile.js     | 43 +++++++++++++------
 .../room/timeline/entries/EventEntry.js       |  5 +++
 2 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/src/domain/session/room/timeline/tiles/RoomMemberTile.js b/src/domain/session/room/timeline/tiles/RoomMemberTile.js
index 58e722c9..f841a2ed 100644
--- a/src/domain/session/room/timeline/tiles/RoomMemberTile.js
+++ b/src/domain/session/room/timeline/tiles/RoomMemberTile.js
@@ -7,20 +7,37 @@ export default class RoomNameTile extends SimpleTile {
     }
 
     get announcement() {
-        const {sender, content, stateKey} = this._entry;
-        switch (content.membership) {
-            case "invite": return `${stateKey} was invited to the room by ${sender}`;
-            case "join": return `${stateKey} joined the room`;
-            case "leave": {
-                if (stateKey === sender) {
-                    return `${stateKey} left the room`;
-                } else {
-                    const reason = content.reason;
-                    return `${stateKey} was kicked from the room by ${sender}${reason ? `: ${reason}` : ""}`;
-                }
+        const {sender, content, prevContent, stateKey} = this._entry;
+        const membership = content && content.membership;
+        const prevMembership = prevContent && prevContent.membership;
+
+        if (prevMembership === "join" && membership === "join") {
+            if (content.avatar_url !== prevContent.avatar_url) {
+                return `${stateKey} changed their avatar`; 
+            } else if (content.displayname !== prevContent.displayname) {
+                return `${stateKey} changed their name to ${content.displayname}`; 
             }
-            case "ban": return `${stateKey} was banned from the room by ${sender}`;
-            default: return `${sender} membership changed to ${content.membership}`;
+        } else if (membership === "join") {
+            return `${stateKey} joined the room`;
+        } else if (membership === "invite") {
+            return `${stateKey} was invited to the room by ${sender}`;
+        } else if (prevMembership === "invite") {
+            if (membership === "join") {
+                return `${stateKey} accepted the invitation to join the room`;
+            } else if (membership === "leave") {
+                return `${stateKey} declined the invitation to join the room`;
+            }
+        } else if (membership === "leave") {
+            if (stateKey === sender) {
+                return `${stateKey} left the room`;
+            } else {
+                const reason = content.reason;
+                return `${stateKey} was kicked from the room by ${sender}${reason ? `: ${reason}` : ""}`;
+            }
+        } else if (membership === "ban") {
+            return `${stateKey} was banned from the room by ${sender}`;
         }
+        
+        return `${sender} membership changed to ${content.membership}`;
     }
 }
diff --git a/src/matrix/room/timeline/entries/EventEntry.js b/src/matrix/room/timeline/entries/EventEntry.js
index ea2143f1..9156d466 100644
--- a/src/matrix/room/timeline/entries/EventEntry.js
+++ b/src/matrix/room/timeline/entries/EventEntry.js
@@ -18,6 +18,11 @@ export default class EventEntry extends BaseEntry {
         return this._eventEntry.event.content;
     }
 
+    get prevContent() {
+        const unsigned = this._eventEntry.event.unsigned;
+        return unsigned && unsigned.prev_content;
+    }
+
     get eventType() {
         return this._eventEntry.event.type;
     }