Daniel Brötzmann pushed to branch newcontrol at gajim / gajim
Commits: d63e9b8f by wurstsalat at 2022-07-18T21:06:58+02:00 GroupChatNickCompletion: Process gc messages
- - - - -
5 changed files:
- gajim/gtk/chat_action_processor.py - gajim/gtk/controls/groupchat.py - gajim/gtk/groupchat_nick_completion.py - gajim/gtk/message_input.py - test/no_gui/test_nick_completion.py
Changes:
===================================== gajim/gtk/chat_action_processor.py ===================================== @@ -326,7 +326,3 @@ def _move_selection(self, direction: Direction) -> None: def _item_has_focus(item: Gtk.ModelButton) -> bool: flags = item.get_state_flags() return 'GTK_STATE_FLAG_FOCUSED' in str(flags) - - def process_message(self, contact_name: str, highlight: bool) -> None: - if isinstance(self._contact, GroupchatContact): - self._nick_completion.process_message(contact_name, highlight)
===================================== gajim/gtk/controls/groupchat.py ===================================== @@ -210,8 +210,7 @@ def on_approve() -> None: modal=False).show()
def _on_message_received(self, event: events.MessageReceived) -> None: - if not event.msgtxt: - return + pass
def _on_mam_message_received(self, event: events.MamMessageReceived @@ -246,10 +245,6 @@ def _on_gc_message_received(self, event: events.GcMessageReceived) -> None: message_id=event.properties.id, stanza_id=event.stanza_id, additional_data=event.additional_data) - event.needs_highlight = helpers.message_needs_highlight( - event.msgtxt, - self.contact.nickname, - self._client.get_own_jid().bare)
def add_message(self, text: str, @@ -268,13 +263,6 @@ def add_message(self, kind = 'incoming' # muc-specific chatstate
- if kind == 'incoming': - highlight = helpers.message_needs_highlight( - text, self.contact.nickname, self._client.get_own_jid().bare) - - # TODO - # self.msg_textview.process_message(contact, highlight) - BaseControl.add_message(self, text, kind,
===================================== gajim/gtk/groupchat_nick_completion.py ===================================== @@ -24,8 +24,12 @@ from gi.repository import Gdk
from gajim.common import app +from gajim.common import ged from gajim.common import types +from gajim.common.events import GcMessageReceived +from gajim.common.ged import EventHelper from gajim.common.helpers import jid_is_blocked +from gajim.common.helpers import message_needs_highlight
if TYPE_CHECKING: from .message_input import MessageInputTextView @@ -33,8 +37,10 @@ log = logging.getLogger('gajim.gui.groupchat_nick_completion')
-class GroupChatNickCompletion: +class GroupChatNickCompletion(EventHelper): def __init__(self) -> None: + EventHelper.__init__(self) + self._account: Optional[str] = None self._contact: Optional[types.GroupchatContactT] = None
@@ -45,6 +51,9 @@ def __init__(self) -> None:
self._nick_data: dict[str, tuple[list[str], list[str]]] = {}
+ self.register_event( + 'gc-message-received', ged.GUI1, self._on_gc_message_received) + def switch_contact(self, contact: types.GroupchatContactT) -> None: self._nick_hits.clear() self._last_key_tab = False @@ -85,23 +94,41 @@ def _on_user_nickname_changed(self, if contact == old_name: lst[idx] = new_name
- def process_message(self, contact_name: str, highlight: bool) -> None: - assert self._contact - if contact_name == self._contact.nickname: + def _on_gc_message_received(self, event: GcMessageReceived) -> None: + if event.properties.muc_nickname is None: + # Message from server return
- log.debug('Recorded a message from %s, highlight; %s', - contact_name, - highlight) + client = app.get_client(event.account) + gc_contact = client.get_module('Contacts').get_contact( + event.room_jid) + + participant_nick = event.properties.muc_nickname + if participant_nick == gc_contact.nickname: + return + + highlight = message_needs_highlight( + event.msgtxt, gc_contact.nickname, client.get_own_jid().bare) + self._process_message(participant_nick, highlight, event.room_jid) + + def _process_message(self, + participant_nick: str, + highlight: bool, + room_jid: str + ) -> None: + nick_data = self._nick_data.get(room_jid) + if nick_data is None: + return
+ sender_list, highlight_list = nick_data if highlight: try: - self._highlight_list.remove(contact_name) + highlight_list.remove(participant_nick) except ValueError: pass - if len(self._highlight_list) > 6: - self._highlight_list.pop(0) # remove older - self._highlight_list.append(contact_name) + if len(highlight_list) > 6: + highlight_list.pop(0) # remove older + highlight_list.append(participant_nick)
# TODO implement it in a more efficient way # Currently it's O(n*m + n*s), where n is the number of participants and @@ -113,10 +140,10 @@ def process_message(self, contact_name: str, highlight: bool) -> None: # for each suggestion (currently generating the suggestions is O(n)) # this would give the expected complexity of O(m + s * n log n) try: - self._sender_list.remove(contact_name) + sender_list.remove(participant_nick) except ValueError: pass - self._sender_list.append(contact_name) + sender_list.append(participant_nick)
def _generate_suggestions(self, nicks: list[str],
===================================== gajim/gtk/message_input.py ===================================== @@ -336,9 +336,6 @@ def undo(self, *args: Any) -> None: buf.set_text(self._undo_list.pop()) self.undo_pressed = True
- def process_message(self, contact_name: str, highlight: bool) -> None: - self._chat_action_processor.process_message(contact_name, highlight) - def _on_populate_popup(self, _textview: MessageInputTextView, menu: Gtk.Widget
===================================== test/no_gui/test_nick_completion.py ===================================== @@ -12,50 +12,35 @@ class Test(unittest.TestCase): def test_generate_suggestions(self): gen = GroupChatNickCompletion() contact = MagicMock() + contact.jid = 'test' gen.switch_contact(contact)
- l = ['aaaa', 'fooo', 'xxxxz', 'xaaaz'] - for n in l: - gen.process_message(n, False) - l2 = ['xxx'] + l - r = gen._generate_suggestions(nicks=l2, beginning='x') - self.assertEqual(r, ['xaaaz', 'xxxxz', 'xxx']) + list_1 = ['aaaa', 'fooo', 'xxxxz', 'xaaaz'] + for name in list_1: + gen._process_message(name, False, contact.jid) + list_2 = list_1 + ['xxx'] + r = gen._generate_suggestions(nicks=list_2, beginning='x') + self.assertEqual(r, ['xaaaz', 'xxx', 'xxxxz'])
r = gen._generate_suggestions( - nicks=l2, + nicks=list_2, beginning='m' ) self.assertEqual(r, [])
- for n in ['xaaaz', 'xxxxz']: - gen.process_message(n, True) + for name in ['xaaaz', 'xxxxz']: + gen._process_message(name, True, contact.jid)
r = gen._generate_suggestions( - nicks=l2, + nicks=list_2, beginning='x' ) - self.assertEqual(r, ['xxxxz', 'xaaaz', 'xxx']) + self.assertEqual(r, ['xaaaz', 'xxx', 'xxxxz']) r = gen._generate_suggestions( - nicks=l2, + nicks=list_2, beginning='' ) - self.assertEqual(r, ['xxxxz', 'xaaaz', 'aaaa', 'fooo', 'xxx']) - - l2[1] = 'bbbb' - - old_name = 'aaaa' - new_name = 'bbbb' - - for lst in (gen._highlight_list, gen._sender_list): - for idx, contact in enumerate(lst): - if contact == old_name: - lst[idx] = new_name - - r = gen._generate_suggestions( - nicks=l2, - beginning='' - ) - self.assertEqual(r, ['xxxxz', 'xaaaz', 'bbbb', 'fooo', 'xxx']) + self.assertEqual(r, ['aaaa', 'fooo', 'xaaaz', 'xxx', 'xxxxz'])
if __name__ == '__main__':
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/d63e9b8f0911ccd9fbc1f7aeee02f6ceb...