Daniel Brötzmann pushed to branch newcontrol at gajim / gajim
Commits:
-
d63e9b8f
by wurstsalat at 2022-07-18T21:06:58+02:00
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:
| ... | ... | @@ -326,7 +326,3 @@ def _move_selection(self, direction: Direction) -> None: |
| 326 | 326 | def _item_has_focus(item: Gtk.ModelButton) -> bool:
|
| 327 | 327 | flags = item.get_state_flags()
|
| 328 | 328 | return 'GTK_STATE_FLAG_FOCUSED' in str(flags) |
| 329 | - |
|
| 330 | - def process_message(self, contact_name: str, highlight: bool) -> None:
|
|
| 331 | - if isinstance(self._contact, GroupchatContact):
|
|
| 332 | - self._nick_completion.process_message(contact_name, highlight) |
| ... | ... | @@ -210,8 +210,7 @@ def on_approve() -> None: |
| 210 | 210 | modal=False).show()
|
| 211 | 211 | |
| 212 | 212 | def _on_message_received(self, event: events.MessageReceived) -> None:
|
| 213 | - if not event.msgtxt:
|
|
| 214 | - return
|
|
| 213 | + pass
|
|
| 215 | 214 | |
| 216 | 215 | def _on_mam_message_received(self,
|
| 217 | 216 | event: events.MamMessageReceived
|
| ... | ... | @@ -246,10 +245,6 @@ def _on_gc_message_received(self, event: events.GcMessageReceived) -> None: |
| 246 | 245 | message_id=event.properties.id,
|
| 247 | 246 | stanza_id=event.stanza_id,
|
| 248 | 247 | additional_data=event.additional_data)
|
| 249 | - event.needs_highlight = helpers.message_needs_highlight(
|
|
| 250 | - event.msgtxt,
|
|
| 251 | - self.contact.nickname,
|
|
| 252 | - self._client.get_own_jid().bare)
|
|
| 253 | 248 | |
| 254 | 249 | def add_message(self,
|
| 255 | 250 | text: str,
|
| ... | ... | @@ -268,13 +263,6 @@ def add_message(self, |
| 268 | 263 | kind = 'incoming'
|
| 269 | 264 | # muc-specific chatstate
|
| 270 | 265 | |
| 271 | - if kind == 'incoming':
|
|
| 272 | - highlight = helpers.message_needs_highlight(
|
|
| 273 | - text, self.contact.nickname, self._client.get_own_jid().bare)
|
|
| 274 | - |
|
| 275 | - # TODO
|
|
| 276 | - # self.msg_textview.process_message(contact, highlight)
|
|
| 277 | - |
|
| 278 | 266 | BaseControl.add_message(self,
|
| 279 | 267 | text,
|
| 280 | 268 | kind,
|
| ... | ... | @@ -24,8 +24,12 @@ |
| 24 | 24 | from gi.repository import Gdk
|
| 25 | 25 | |
| 26 | 26 | from gajim.common import app
|
| 27 | +from gajim.common import ged
|
|
| 27 | 28 | from gajim.common import types
|
| 29 | +from gajim.common.events import GcMessageReceived
|
|
| 30 | +from gajim.common.ged import EventHelper
|
|
| 28 | 31 | from gajim.common.helpers import jid_is_blocked
|
| 32 | +from gajim.common.helpers import message_needs_highlight
|
|
| 29 | 33 | |
| 30 | 34 | if TYPE_CHECKING:
|
| 31 | 35 | from .message_input import MessageInputTextView
|
| ... | ... | @@ -33,8 +37,10 @@ |
| 33 | 37 | log = logging.getLogger('gajim.gui.groupchat_nick_completion')
|
| 34 | 38 | |
| 35 | 39 | |
| 36 | -class GroupChatNickCompletion:
|
|
| 40 | +class GroupChatNickCompletion(EventHelper):
|
|
| 37 | 41 | def __init__(self) -> None:
|
| 42 | + EventHelper.__init__(self)
|
|
| 43 | + |
|
| 38 | 44 | self._account: Optional[str] = None
|
| 39 | 45 | self._contact: Optional[types.GroupchatContactT] = None
|
| 40 | 46 | |
| ... | ... | @@ -45,6 +51,9 @@ def __init__(self) -> None: |
| 45 | 51 | |
| 46 | 52 | self._nick_data: dict[str, tuple[list[str], list[str]]] = {}
|
| 47 | 53 | |
| 54 | + self.register_event(
|
|
| 55 | + 'gc-message-received', ged.GUI1, self._on_gc_message_received)
|
|
| 56 | + |
|
| 48 | 57 | def switch_contact(self, contact: types.GroupchatContactT) -> None:
|
| 49 | 58 | self._nick_hits.clear()
|
| 50 | 59 | self._last_key_tab = False
|
| ... | ... | @@ -85,23 +94,41 @@ def _on_user_nickname_changed(self, |
| 85 | 94 | if contact == old_name:
|
| 86 | 95 | lst[idx] = new_name
|
| 87 | 96 | |
| 88 | - def process_message(self, contact_name: str, highlight: bool) -> None:
|
|
| 89 | - assert self._contact
|
|
| 90 | - if contact_name == self._contact.nickname:
|
|
| 97 | + def _on_gc_message_received(self, event: GcMessageReceived) -> None:
|
|
| 98 | + if event.properties.muc_nickname is None:
|
|
| 99 | + # Message from server
|
|
| 91 | 100 | return
|
| 92 | 101 | |
| 93 | - log.debug('Recorded a message from %s, highlight; %s',
|
|
| 94 | - contact_name,
|
|
| 95 | - highlight)
|
|
| 102 | + client = app.get_client(event.account)
|
|
| 103 | + gc_contact = client.get_module('Contacts').get_contact(
|
|
| 104 | + event.room_jid)
|
|
| 105 | + |
|
| 106 | + participant_nick = event.properties.muc_nickname
|
|
| 107 | + if participant_nick == gc_contact.nickname:
|
|
| 108 | + return
|
|
| 109 | + |
|
| 110 | + highlight = message_needs_highlight(
|
|
| 111 | + event.msgtxt, gc_contact.nickname, client.get_own_jid().bare)
|
|
| 112 | + self._process_message(participant_nick, highlight, event.room_jid)
|
|
| 113 | + |
|
| 114 | + def _process_message(self,
|
|
| 115 | + participant_nick: str,
|
|
| 116 | + highlight: bool,
|
|
| 117 | + room_jid: str
|
|
| 118 | + ) -> None:
|
|
| 119 | + nick_data = self._nick_data.get(room_jid)
|
|
| 120 | + if nick_data is None:
|
|
| 121 | + return
|
|
| 96 | 122 | |
| 123 | + sender_list, highlight_list = nick_data
|
|
| 97 | 124 | if highlight:
|
| 98 | 125 | try:
|
| 99 | - self._highlight_list.remove(contact_name)
|
|
| 126 | + highlight_list.remove(participant_nick)
|
|
| 100 | 127 | except ValueError:
|
| 101 | 128 | pass
|
| 102 | - if len(self._highlight_list) > 6:
|
|
| 103 | - self._highlight_list.pop(0) # remove older
|
|
| 104 | - self._highlight_list.append(contact_name)
|
|
| 129 | + if len(highlight_list) > 6:
|
|
| 130 | + highlight_list.pop(0) # remove older
|
|
| 131 | + highlight_list.append(participant_nick)
|
|
| 105 | 132 | |
| 106 | 133 | # TODO implement it in a more efficient way
|
| 107 | 134 | # 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: |
| 113 | 140 | # for each suggestion (currently generating the suggestions is O(n))
|
| 114 | 141 | # this would give the expected complexity of O(m + s * n log n)
|
| 115 | 142 | try:
|
| 116 | - self._sender_list.remove(contact_name)
|
|
| 143 | + sender_list.remove(participant_nick)
|
|
| 117 | 144 | except ValueError:
|
| 118 | 145 | pass
|
| 119 | - self._sender_list.append(contact_name)
|
|
| 146 | + sender_list.append(participant_nick)
|
|
| 120 | 147 | |
| 121 | 148 | def _generate_suggestions(self,
|
| 122 | 149 | nicks: list[str],
|
| ... | ... | @@ -336,9 +336,6 @@ def undo(self, *args: Any) -> None: |
| 336 | 336 | buf.set_text(self._undo_list.pop())
|
| 337 | 337 | self.undo_pressed = True
|
| 338 | 338 | |
| 339 | - def process_message(self, contact_name: str, highlight: bool) -> None:
|
|
| 340 | - self._chat_action_processor.process_message(contact_name, highlight)
|
|
| 341 | - |
|
| 342 | 339 | def _on_populate_popup(self,
|
| 343 | 340 | _textview: MessageInputTextView,
|
| 344 | 341 | menu: Gtk.Widget
|
| ... | ... | @@ -12,50 +12,35 @@ class Test(unittest.TestCase): |
| 12 | 12 | def test_generate_suggestions(self):
|
| 13 | 13 | gen = GroupChatNickCompletion()
|
| 14 | 14 | contact = MagicMock()
|
| 15 | + contact.jid = 'test'
|
|
| 15 | 16 | gen.switch_contact(contact)
|
| 16 | 17 | |
| 17 | - l = ['aaaa', 'fooo', 'xxxxz', 'xaaaz']
|
|
| 18 | - for n in l:
|
|
| 19 | - gen.process_message(n, False)
|
|
| 20 | - l2 = ['xxx'] + l
|
|
| 21 | - r = gen._generate_suggestions(nicks=l2, beginning='x')
|
|
| 22 | - self.assertEqual(r, ['xaaaz', 'xxxxz', 'xxx'])
|
|
| 18 | + list_1 = ['aaaa', 'fooo', 'xxxxz', 'xaaaz']
|
|
| 19 | + for name in list_1:
|
|
| 20 | + gen._process_message(name, False, contact.jid)
|
|
| 21 | + list_2 = list_1 + ['xxx']
|
|
| 22 | + r = gen._generate_suggestions(nicks=list_2, beginning='x')
|
|
| 23 | + self.assertEqual(r, ['xaaaz', 'xxx', 'xxxxz'])
|
|
| 23 | 24 | |
| 24 | 25 | r = gen._generate_suggestions(
|
| 25 | - nicks=l2,
|
|
| 26 | + nicks=list_2,
|
|
| 26 | 27 | beginning='m'
|
| 27 | 28 | )
|
| 28 | 29 | self.assertEqual(r, [])
|
| 29 | 30 | |
| 30 | - for n in ['xaaaz', 'xxxxz']:
|
|
| 31 | - gen.process_message(n, True)
|
|
| 31 | + for name in ['xaaaz', 'xxxxz']:
|
|
| 32 | + gen._process_message(name, True, contact.jid)
|
|
| 32 | 33 | |
| 33 | 34 | r = gen._generate_suggestions(
|
| 34 | - nicks=l2,
|
|
| 35 | + nicks=list_2,
|
|
| 35 | 36 | beginning='x'
|
| 36 | 37 | )
|
| 37 | - self.assertEqual(r, ['xxxxz', 'xaaaz', 'xxx'])
|
|
| 38 | + self.assertEqual(r, ['xaaaz', 'xxx', 'xxxxz'])
|
|
| 38 | 39 | r = gen._generate_suggestions(
|
| 39 | - nicks=l2,
|
|
| 40 | + nicks=list_2,
|
|
| 40 | 41 | beginning=''
|
| 41 | 42 | )
|
| 42 | - self.assertEqual(r, ['xxxxz', 'xaaaz', 'aaaa', 'fooo', 'xxx'])
|
|
| 43 | - |
|
| 44 | - l2[1] = 'bbbb'
|
|
| 45 | - |
|
| 46 | - old_name = 'aaaa'
|
|
| 47 | - new_name = 'bbbb'
|
|
| 48 | - |
|
| 49 | - for lst in (gen._highlight_list, gen._sender_list):
|
|
| 50 | - for idx, contact in enumerate(lst):
|
|
| 51 | - if contact == old_name:
|
|
| 52 | - lst[idx] = new_name
|
|
| 53 | - |
|
| 54 | - r = gen._generate_suggestions(
|
|
| 55 | - nicks=l2,
|
|
| 56 | - beginning=''
|
|
| 57 | - )
|
|
| 58 | - self.assertEqual(r, ['xxxxz', 'xaaaz', 'bbbb', 'fooo', 'xxx'])
|
|
| 43 | + self.assertEqual(r, ['aaaa', 'fooo', 'xaaaz', 'xxx', 'xxxxz'])
|
|
| 59 | 44 | |
| 60 | 45 | |
| 61 | 46 | if __name__ == '__main__':
|