From d3e6e7e47d100563d18830b0f6bdd6bee5bddfce Mon Sep 17 00:00:00 2001
From: Nico Alt <nicoalt@posteo.org>
Date: Sat, 24 Oct 2020 12:34:21 +0200
Subject: [PATCH] Show unread messages counter in contact list

Fixes #29
---
 briar-gtk/briar_gtk/containers/private_chat.py | 10 ++++++++--
 briar-gtk/briar_gtk/widgets/contact_row.py     | 16 ++++++++++++++++
 briar-gtk/data/ui/application.css              | 14 ++++++++++++++
 3 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/briar-gtk/briar_gtk/containers/private_chat.py b/briar-gtk/briar_gtk/containers/private_chat.py
index dc5109f..0600ca0 100644
--- a/briar-gtk/briar_gtk/containers/private_chat.py
+++ b/briar-gtk/briar_gtk/containers/private_chat.py
@@ -99,6 +99,8 @@ class PrivateChatContainer(Container):
             # Abusing idle_add function here because otherwise the message box
             # is too small and scrolling cuts out messages
             GLib.idle_add(self._add_message, message)
+            if message.get("read", True) is False:
+                GLib.idle_add(private_chat.mark_read, message["id"])
         socket_listener = APP().api.socket_listener
         signal_id = socket_listener.connect("ConversationMessageReceivedEvent",
                                             self._add_message_async)
@@ -120,8 +122,12 @@ class PrivateChatContainer(Container):
         return "text" not in message
 
     def _add_message_async(self, message):
-        if message["data"]["contactId"] == self._contact_id:
-            GLib.idle_add(self._add_message_and_scroll, message["data"])
+        if message["data"]["contactId"] != self._contact_id:
+            return
+        GLib.idle_add(self._add_message_and_scroll, message["data"])
+        if message["data"].get("read", True) is False:
+            private_chat = PrivateChat(APP().api, self._contact_id)
+            GLib.idle_add(private_chat.mark_read, message["data"]["id"])
 
     def _add_message_and_scroll(self, message):
         self._add_message(message)
diff --git a/briar-gtk/briar_gtk/widgets/contact_row.py b/briar-gtk/briar_gtk/widgets/contact_row.py
index 41b4784..bd0d7fe 100644
--- a/briar-gtk/briar_gtk/widgets/contact_row.py
+++ b/briar-gtk/briar_gtk/widgets/contact_row.py
@@ -17,6 +17,10 @@ class ContactRowWidget(Gtk.ListBoxRow):
     def _setup_view(self, contact):
         name = ContactRowWidget._get_contact_name(contact)
         contact_label = ContactRowWidget._create_contact_label(name)
+        unread_count = str(contact['unreadCount'])
+        contact_unread_count = ContactRowWidget._create_unread_count(
+            unread_count
+        )
         connected = contact["connected"]
         contact_state = ContactRowWidget._create_contact_state(connected)
         contact_box = ContactRowWidget._create_contact_box()
@@ -24,6 +28,7 @@ class ContactRowWidget(Gtk.ListBoxRow):
 
         contact_box.pack_start(contact_label, True, True, 0)
         contact_box.pack_end(contact_state, False, False, 0)
+        contact_box.pack_end(contact_unread_count, False, False, 0)
         contact_event_box.add(contact_box)
         self.add(contact_event_box)
 
@@ -45,6 +50,17 @@ class ContactRowWidget(Gtk.ListBoxRow):
         contact_label.set_ellipsize(Pango.EllipsizeMode.END)
         return contact_label
 
+    @staticmethod
+    def _create_unread_count(unread_count):
+        if unread_count == "0":
+            return Gtk.EventBox()
+        unread_count_label = Gtk.Label.new(unread_count)
+        unread_count_label.set_valign(Gtk.Align.CENTER)
+        unread_count_label.set_halign(Gtk.Align.START)
+        style = unread_count_label.get_style_context()
+        style.add_class("notify-badge")
+        return unread_count_label
+
     @staticmethod
     def _create_contact_state(connected):
         file_name = "contact_disconnected"
diff --git a/briar-gtk/data/ui/application.css b/briar-gtk/data/ui/application.css
index fc3c7b3..15fe115 100644
--- a/briar-gtk/data/ui/application.css
+++ b/briar-gtk/data/ui/application.css
@@ -91,3 +91,17 @@ row .timestamp {
   border: none;
 }
 
+/**
+  From GNOME Fractal
+  https://gitlab.gnome.org/GNOME/fractal/-/blob/4.4.0/fractal-gtk/res/app.css#L5
+**/
+.notify-badge {
+  background-color: #555;
+  color: white;
+  font-weight: bold;
+  font-size: 0.7em;
+  border-radius: 8px;
+  min-width: 0.7em;
+  padding: 2px 5px;
+}
+
-- 
GitLab