changeset 1973:a9908e751c42

quick_frontend, primitivus (chat): mention detection in group chat + notification (mention and messages)
author Goffi <goffi@goffi.org>
date Mon, 27 Jun 2016 22:37:51 +0200
parents 02d21a589be2
children da6d1988dfcb
files frontends/src/primitivus/chat.py frontends/src/primitivus/constants.py frontends/src/primitivus/contact_list.py frontends/src/quick_frontend/quick_chat.py
diffstat 4 files changed, 37 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/chat.py	Mon Jun 27 22:36:22 2016 +0200
+++ b/frontends/src/primitivus/chat.py	Mon Jun 27 22:37:51 2016 +0200
@@ -133,7 +133,10 @@
             markup.append(("msg_lang", u"[{}] ".format(self.selected_lang)))
 
         # message body
-        markup.append(msg)
+        if d.mention:
+            markup.append(("msg_mention", msg))
+        else:
+            markup.append(msg)
 
         return markup
 
@@ -391,9 +394,23 @@
         if not message.message:
             log.error(u"Received an empty message for uid {}".format(message.uid))
         else:
-            self.mess_walker.append(MessageWidget(message))
+            wid = MessageWidget(message)
+            self.mess_walker.append(wid)
             self.mess_widgets.focus_position = len(self.mess_walker) - 1  # scroll down
             self.host.redraw()  # FIXME: should not be necessary
+            if wid.mess_data.mention:
+                from_jid = wid.mess_data.from_jid
+                msg = _(u'You have been mentioned by {nick} in {room}'.format(
+                    nick=wid.mess_data.nick,
+                    room=self.target,
+                    ))
+                self.host.notify(C.NOTIFY_MENTION, from_jid, msg, widget=self, profile=self.profile)
+            elif self.type == C.CHAT_ONE2ONE:
+                from_jid = wid.mess_data.from_jid
+                msg = _(u'{entity} is talking to you'.format(
+                    entity=from_jid,
+                    ))
+                self.host.notify(C.NOTIFY_MESSAGE, from_jid, msg, widget=self, profile=self.profile)
 
     def addUser(self, nick):
         occupant = super(Chat, self).addUser(nick)
--- a/frontends/src/primitivus/constants.py	Mon Jun 27 22:36:22 2016 +0200
+++ b/frontends/src/primitivus/constants.py	Mon Jun 27 22:37:51 2016 +0200
@@ -33,12 +33,15 @@
                ('default_focus', 'default,bold', 'default'),
                ('cl_notifs', 'default,underline', 'yellow'),
                ('cl_notifs_focus', 'default,bold,underline', 'yellow'),
+               ('cl_mention', 'dark red', 'default'),
+               ('cl_mention_focus', 'dark red,bold', 'default'),
                # Messages
                ('date', 'light gray', 'default'),
                ('my_nick', 'dark red,bold', 'default'),
                ('other_nick', 'dark cyan,bold', 'default'),
                ('info_msg', 'yellow', 'default', 'bold'),
                ('msg_lang', 'dark cyan', 'default'),
+               ('msg_mention', 'dark red, bold', 'default'),
 
                ('menubar', 'light gray,bold', 'dark red'),
                ('menubar_focus', 'light gray,bold', 'dark green'),
--- a/frontends/src/primitivus/contact_list.py	Mon Jun 27 22:36:22 2016 +0200
+++ b/frontends/src/primitivus/contact_list.py	Mon Jun 27 22:37:51 2016 +0200
@@ -201,6 +201,8 @@
         if notifs:
             entity_attr = 'cl_notifs'
             header = u'({}) '.format(len(notifs))
+            if self.host.getNotifs(entity.bare, C.NOTIFY_MENTION, profile=self.profile):
+                header = ('cl_mention', header)
         else:
             header = u''
 
--- a/frontends/src/quick_frontend/quick_chat.py	Mon Jun 27 22:36:22 2016 +0200
+++ b/frontends/src/quick_frontend/quick_chat.py	Mon Jun 27 22:37:51 2016 +0200
@@ -57,6 +57,12 @@
         self.nick = self.getNick(from_jid)
         # own_mess is True if message was sent by profile's jid
         self.own_mess = (from_jid.resource == self.parent.nick) if self.parent.type == C.CHAT_GROUP else (from_jid.bare == self.host.profiles[profile].whoami.bare)
+        # is user mentioned here ?
+        if self.parent.type == C.CHAT_GROUP and not self.own_mess:
+            for m in msg.itervalues():
+                if self.parent.nick.lower() in m.lower():
+                    self._mention = True
+                    break
         self.widgets = set()  # widgets linked to this message
 
     @property
@@ -67,6 +73,13 @@
     def info_type(self):
         return self.extra.get('info_type')
 
+    @property
+    def mention(self):
+        try:
+            return self._mention
+        except AttributeError:
+            return False
+
     def getNick(self, entity):
         """Return nick of an entity when possible"""
         contact_list = self.host.contact_lists[self.profile]