From 909e1c336345d823ec58557fb8f6b07ec95d9f80 Mon Sep 17 00:00:00 2001 From: miruka Date: Sun, 21 Apr 2019 12:25:55 -0400 Subject: [PATCH] Fix and improve room members event texts --- harmonyqml/backend/signal_manager.py | 7 +++- harmonyqml/components/chat/EventContent.qml | 4 +- harmonyqml/components/chat/utils.js | 42 ++++++++++++++++----- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/harmonyqml/backend/signal_manager.py b/harmonyqml/backend/signal_manager.py index 76ad0ea0..06d11c67 100644 --- a/harmonyqml/backend/signal_manager.py +++ b/harmonyqml/backend/signal_manager.py @@ -93,7 +93,12 @@ class SignalManager(QObject): def onRoomLeft(self, client: Client, room_id: str) -> None: rooms = self.backend.models.rooms[client.userId] - del rooms[rooms.indexWhere("roomId", room_id)] + try: + index = rooms.indexWhere("roomId", room_id) + except ValueError: + pass + else: + del rooms[index] def onRoomSyncPrevBatchTokenReceived( diff --git a/harmonyqml/components/chat/EventContent.qml b/harmonyqml/components/chat/EventContent.qml index 9b609957..ea035424 100644 --- a/harmonyqml/components/chat/EventContent.qml +++ b/harmonyqml/components/chat/EventContent.qml @@ -24,7 +24,9 @@ RowLayout { id: contentLabel text: "" + - (displayName.value || dict.sender) + " " + contentText + + (displayName.value || dict.sender) + + (contentText.startsWith("'s ") ? "" : " ") + + contentText + "  " + Qt.formatDateTime(dateTime, "hh:mm:ss") + "" diff --git a/harmonyqml/components/chat/utils.js b/harmonyqml/components/chat/utils.js index 23b846b4..81e65bc7 100644 --- a/harmonyqml/components/chat/utils.js +++ b/harmonyqml/components/chat/utils.js @@ -69,11 +69,11 @@ function getHistoryVisibilityEventText(dict) { break case "joined": - var end = "all room members since they joined." + var end = "all room members, since the point they joined." break case "invited": - var end = "all room members since they were invited." + var end = "all room members, since the point they were invited." break } @@ -81,28 +81,52 @@ function getHistoryVisibilityEventText(dict) { } +function getStateDisplayName(dict) { + // The dict.content.displayname may be outdated, prefer + // retrieving it fresh + var name = Backend.getUserDisplayName(dict.state_key, false) + return name === dict.state_key ? + dict.content.displayname : name.result() +} + + function getMemberEventText(dict) { var info = dict.content, prev = dict.prev_content if (! prev || (info.membership != prev.membership)) { + var reason = info.reason ? (" Reason: " + info.reason) : "" + switch (info.membership) { case "join": - return "joined the room." + return prev && prev.membership === "invite" ? + "accepted the invitation." : "joined the room." break case "invite": - var name = Backend.getUserDisplayName(dict.state_key, false) - var name = name === dict.state_key ? - info.displayname : name.result() - return "invited " + name + " to the room." + return "invited " + getStateDisplayName(dict) + " to the room." break case "leave": - return "left the room." + if (dict.state_key === dict.sender) { + return (prev && prev.membership === "invite" ? + "declined the invitation." : "left the room.") + + reason + } + + var name = getStateDisplayName(dict) + return (prev && prev.membership === "invite" ? + "withdrew " + name + "'s invitation." : + + prev && prev.membership == "ban" ? + "unbanned " + name + " from the room." : + + "kicked out " + name + " from the room.") + + reason break case "ban": - return "was banned from the room." + var name = getStateDisplayName(dict) + return "banned " + name + " from the room." + reason break } }