Commit 961f6c17 authored by Maxime Buquet's avatar Maxime Buquet Committed by mathieui

Fix highlights by introducing a delayed property on ui.types.Message

Signed-off-by: Maxime Buquet's avatarMaxime “pep” Buquet <pep@bouah.net>
parent cf026cf1
...@@ -766,6 +766,7 @@ class HandlerCore: ...@@ -766,6 +766,7 @@ class HandlerCore:
replaced_id, replaced_id,
message['id'], message['id'],
time=delayed_date, time=delayed_date,
delayed=delayed,
nickname=nick_from, nickname=nick_from,
user=user): user=user):
self.core.events.trigger('highlight', message, tab) self.core.events.trigger('highlight', message, tab)
...@@ -777,7 +778,7 @@ class HandlerCore: ...@@ -777,7 +778,7 @@ class HandlerCore:
txt=body, txt=body,
time=date, time=date,
nickname=nick_from, nickname=nick_from,
history=delayed, delayed=delayed,
identifier=message['id'], identifier=message['id'],
jid=message['from'], jid=message['from'],
user=user, user=user,
......
...@@ -1092,7 +1092,7 @@ class MucTab(ChatTab): ...@@ -1092,7 +1092,7 @@ class MucTab(ChatTab):
msg.user.set_last_talked(msg.time) msg.user.set_last_talked(msg.time)
if config.get_by_tabname('notify_messages', self.jid.bare) and self.state != 'current': if config.get_by_tabname('notify_messages', self.jid.bare) and self.state != 'current':
self.state = 'message' self.state = 'message'
msg.highlight = self.do_highlight(msg.txt, msg.time, msg.nickname) msg.highlight = self.do_highlight(msg.txt, msg.nickname, msg.delayed)
return msg.highlight return msg.highlight
def modify_message(self, def modify_message(self,
...@@ -1100,10 +1100,11 @@ class MucTab(ChatTab): ...@@ -1100,10 +1100,11 @@ class MucTab(ChatTab):
old_id, old_id,
new_id, new_id,
time=None, time=None,
delayed: bool = False,
nickname=None, nickname=None,
user=None, user=None,
jid=None): jid=None):
highlight = self.do_highlight(txt, time, nickname, corrected=True) highlight = self.do_highlight(txt, nickname, delayed, corrected=True)
message = self._text_buffer.modify_message( message = self._text_buffer.modify_message(
txt, txt,
old_id, old_id,
...@@ -1310,28 +1311,38 @@ class MucTab(ChatTab): ...@@ -1310,28 +1311,38 @@ class MucTab(ChatTab):
def build_highlight_regex(self, nickname): def build_highlight_regex(self, nickname):
return re.compile(r"(^|\W)" + re.escape(nickname) + r"(\W|$)", re.I) return re.compile(r"(^|\W)" + re.escape(nickname) + r"(\W|$)", re.I)
def is_highlight(self, txt, time, nickname, own_nick, highlight_on, def is_highlight(self, txt: str, nick: str, highlight_on: List[str],
corrected=False): delayed, corrected: bool = False):
"""
Highlight algorithm for MUC tabs
"""
highlighted = False highlighted = False
if (not time or corrected) and nickname and nickname != own_nick: if not delayed and not corrected:
if self.build_highlight_regex(own_nick).search(txt): if self.build_highlight_regex(nick).search(txt):
highlighted = True highlighted = True
else: else:
highlight_words = highlight_on.split(':') for word in highlight_on:
for word in highlight_words:
if word and word.lower() in txt.lower(): if word and word.lower() in txt.lower():
highlighted = True highlighted = True
break break
return highlighted return highlighted
def do_highlight(self, txt, time, nickname, corrected=False): def do_highlight(self, txt, nickname, delayed, corrected=False):
""" """
Set the tab color and returns the nick color Set the tab color and returns the nick color
""" """
own_nick = self.own_nick own_nick = self.own_nick
highlight_on = config.get_by_tabname('highlight_on', self.general_jid) highlight_on = config.get_by_tabname(
highlighted = self.is_highlight(txt, time, nickname, own_nick, 'highlight_on',
highlight_on, corrected) self.general_jid,
).split(':')
# Don't highlight on info message or our own messages
if not nickname or nickname == own_nick:
return False
highlighted = self.is_highlight(txt, own_nick, highlight_on, delayed, corrected)
if highlighted and self.joined: if highlighted and self.joined:
if self.state != 'current': if self.state != 'current':
self.state = 'highlight' self.state = 'highlight'
......
...@@ -165,7 +165,7 @@ class TextBuffer: ...@@ -165,7 +165,7 @@ class TextBuffer:
raise CorrectionError('Wrong message type') raise CorrectionError('Wrong message type')
if msg.user and msg.user is not user: if msg.user and msg.user is not user:
raise CorrectionError("Different users") raise CorrectionError("Different users")
elif msg.history: elif msg.delayed:
raise CorrectionError("Delayed message") raise CorrectionError("Delayed message")
elif not msg.user and (msg.jid is None or jid is None): elif not msg.user and (msg.jid is None or jid is None):
raise CorrectionError('Could not check the ' raise CorrectionError('Could not check the '
...@@ -184,7 +184,6 @@ class TextBuffer: ...@@ -184,7 +184,6 @@ class TextBuffer:
time=time, time=time,
nickname=msg.nickname, nickname=msg.nickname,
nick_color=msg.nick_color, nick_color=msg.nick_color,
history=False,
user=msg.user, user=msg.user,
identifier=orig_id, identifier=orig_id,
highlight=highlight, highlight=highlight,
......
...@@ -85,7 +85,7 @@ class StatusMessage(BaseMessage): ...@@ -85,7 +85,7 @@ class StatusMessage(BaseMessage):
class Message(BaseMessage): class Message(BaseMessage):
__slots__ = ('txt', 'nick_color', 'time', 'nickname', 'user', 'history', __slots__ = ('txt', 'nick_color', 'time', 'nickname', 'user', 'delayed', 'history',
'identifier', 'top', 'highlight', 'me', 'old_message', 'revisions', 'identifier', 'top', 'highlight', 'me', 'old_message', 'revisions',
'jid', 'ack') 'jid', 'ack')
...@@ -94,6 +94,7 @@ class Message(BaseMessage): ...@@ -94,6 +94,7 @@ class Message(BaseMessage):
nickname: Optional[str], nickname: Optional[str],
time: Optional[datetime] = None, time: Optional[datetime] = None,
nick_color: Optional[Tuple] = None, nick_color: Optional[Tuple] = None,
delayed: bool = False,
history: bool = False, history: bool = False,
user: Optional[User] = None, user: Optional[User] = None,
identifier: Optional[str] = '', identifier: Optional[str] = '',
...@@ -120,6 +121,7 @@ class Message(BaseMessage): ...@@ -120,6 +121,7 @@ class Message(BaseMessage):
else: else:
me = False me = False
self.txt = txt self.txt = txt
self.delayed = delayed or history
self.history = history self.history = history
self.nickname = nickname self.nickname = nickname
self.nick_color = nick_color self.nick_color = nick_color
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment