Skip to content

Commit 4f19f87

Browse files
committed
Add room reaction use to CLI room display
Add a line such as: Reactions: 375; top 5: 😈 (35), ❤️ (27), 😮 (26), 🤖 (22), 🐛 (21) showing the total and top 5 reactions used in the room.
1 parent 061e504 commit 4f19f87

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

sogs/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ def close_conn():
165165
def print_room(room: Room):
166166
msgs, msgs_size = room.messages_size()
167167
files, files_size = room.attachments_size()
168+
reactions = room.reactions_counts()
169+
r_total = sum(x[1] for x in reactions)
170+
reactions.sort(key=lambda x: x[1], reverse=True)
168171

169172
msgs_size /= 1_000_000
170173
files_size /= 1_000_000
@@ -183,6 +186,7 @@ def print_room(room: Room):
183186
URL: {config.URL_BASE}/{room.token}?public_key={crypto.server_pubkey_hex}
184187
Messages: {msgs} ({msgs_size:.1f} MB)
185188
Attachments: {files} ({files_size:.1f} MB)
189+
Reactions: {r_total}; top 5: {', '.join(f"{r} ({c})" for r, c in reactions[0:5])}
186190
Active users: {active[0]} (1d), {active[1]} (7d), {active[2]} (14d), {active[3]} (30d)
187191
Moderators: {admins} admins ({len(ha)} hidden), {mods} moderators ({len(hm)} hidden)""",
188192
end='',

sogs/model/room.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,22 @@ def attachments_size(self):
10441044
"SELECT COUNT(*), COALESCE(SUM(size), 0) FROM files WHERE room = :r", r=self.id
10451045
).first()[0:2]
10461046

1047+
def reactions_counts(self):
1048+
"""Returns a list of [reaction, count] pairs where count is the aggregate count of that
1049+
reaction use in this room"""
1050+
return [
1051+
(r, c)
1052+
for r, c in query(
1053+
"""
1054+
SELECT reaction, COUNT(*)
1055+
FROM message_reactions JOIN messages ON messages.id = message
1056+
WHERE room = :r
1057+
GROUP BY reaction
1058+
""",
1059+
r=self.id,
1060+
)
1061+
]
1062+
10471063
def get_reactions(
10481064
self,
10491065
message_ids: List[int],

0 commit comments

Comments
 (0)