Philipp Hörist pushed to branch mainwindow at gajim / gajim
Commits: 271169f2 by lovetox at 2021-03-21T22:48:07+01:00 Refactor Presence module
- - - - -
3 changed files:
- gajim/common/modules/presence.py - gajim/gtk/account_page.py - gajim/gtk/notification_manager.py
Changes:
===================================== gajim/common/modules/presence.py ===================================== @@ -33,6 +33,12 @@
class Presence(BaseModule): + + _nbxmpp_extends = 'BasePresence' + _nbxmpp_methods = [ + 'subscribed', + ] + def __init__(self, con): BaseModule.__init__(self, con)
@@ -140,101 +146,11 @@ def _presence_received(self, _con, stanza, properties): 'contact': None, }
- event_ = NetworkEvent('presence-received', **event_attrs) - - # self._update_contact(event_, properties) - - app.nec.push_incoming_event(event_) - - def _update_contact(self, event, properties): - # Note: A similar method also exists in connection_zeroconf - jid = properties.jid.bare - resource = properties.jid.resource - - status_strings = ['offline', 'error', 'online', 'chat', 'away', - 'xa', 'dnd'] + app.nec.push_incoming_event( + NetworkEvent('presence-received', **event_attrs))
- event.new_show = status_strings.index(event.show) - - # Update contact - contact_list = app.contacts.get_contacts(self._account, jid) - if not contact_list: - self._log.warning('No contact found') - return - - event.contact_list = contact_list - - contact = app.contacts.get_contact_strict(self._account, - properties.jid.bare, - properties.jid.resource) - if contact is None: - contact = app.contacts.get_first_contact_from_jid(self._account, - jid) - if contact is None: - self._log.warning('First contact not found') - return - - if (self._is_resource_known(contact_list) and - not app.jid_is_transport(jid)): - # Another resource of an existing contact connected - # Add new contact - event.old_show = 0 - contact = app.contacts.copy_contact(contact) - contact.resource = resource - app.contacts.add_contact(self._account, contact) - else: - # Convert the initial roster contact to a contact with resource - contact.resource = resource - event.old_show = 0 - if contact.show in status_strings: - event.old_show = status_strings.index(contact.show) - - event.need_add_in_roster = True - - elif contact.show in status_strings: - event.old_show = status_strings.index(contact.show) - - # Update contact with presence data - contact.show = event.show - contact.status = properties.status - contact.priority = properties.priority - contact.idle_time = properties.idle_timestamp - - event.contact = contact - - if not app.jid_is_transport(jid) and len(contact_list) == 1: - # It's not an agent - if event.old_show == 0 and event.new_show > 1: - if not jid in app.newly_added[self._account]: - app.newly_added[self._account].append(jid) - if jid in app.to_be_removed[self._account]: - app.to_be_removed[self._account].remove(jid) - elif event.old_show > 1 and event.new_show == 0 and \ - self._con.state.is_available: - if not jid in app.to_be_removed[self._account]: - app.to_be_removed[self._account].append(jid) - if jid in app.newly_added[self._account]: - app.newly_added[self._account].remove(jid) - - if app.jid_is_transport(jid): - return - - if properties.type.is_unavailable: - # TODO: This causes problems when another - # resource signs off! - self._con.get_module('Bytestream').stop_all_active_file_transfers( - contact) self._log_presence(properties)
- @staticmethod - def _is_resource_known(contact_list): - if len(contact_list) > 1: - return True - - if contact_list[0].resource == '': - return False - return contact_list[0].show not in ('not in roster', 'offline') - def _log_presence(self, properties): if not app.settings.get('log_contact_status_changes'): return @@ -265,9 +181,8 @@ def _subscribe_received(self, _con, _stanza, properties): auto_auth, properties.nickname)
if auto_auth or jid in self._jids_for_auto_auth: - self.send_presence(fjid, 'subscribed') - self._jids_for_auto_auth.discard(jid) self._log.info('Auto respond with subscribed: %s', jid) + self.subscribed(jid) return
status = (properties.status or @@ -276,6 +191,7 @@ def _subscribe_received(self, _con, _stanza, properties): app.nec.push_incoming_event(NetworkEvent( 'subscribe-presence-received', conn=self._con, + account=self._account, jid=jid, fjid=fjid, status=status, @@ -305,59 +221,31 @@ def _unsubscribed_received(self, _con, _stanza, properties): self._log.info('Received Unsubscribed: %s', properties.jid) app.nec.push_incoming_event(NetworkEvent( 'unsubscribed-presence-received', - conn=self._con, jid=properties.jid.bare)) + conn=self._con, + account=self._account, + jid=properties.jid.bare)) raise nbxmpp.NodeProcessed
- def subscribed(self, jid): - if not app.account_is_available(self._account): - return - self._log.info('Subscribed: %s', jid) - self.send_presence(jid, 'subscribed') - def unsubscribed(self, jid): - if not app.account_is_available(self._account): - return - self._log.info('Unsubscribed: %s', jid) self._jids_for_auto_auth.discard(jid) - self.send_presence(jid, 'unsubscribed') + self._nbxmpp('BasePresence').unsubscribed(jid)
- def unsubscribe(self, jid, remove_auth=True): - if not app.account_is_available(self._account): - return - if remove_auth: - self._con.get_module('Roster').del_item(jid) - else: - self._log.info('Unsubscribe from %s', jid) - self._jids_for_auto_auth.discard(jid) - self._con.get_module('Roster').unsubscribe(jid) - self._con.get_module('Roster').set_item(jid) - - def subscribe(self, jid, msg=None, name='', groups=None, auto_auth=False): - if not app.account_is_available(self._account): - return - if groups is None: - groups = [] + def unsubscribe(self, jid): + self._log.info('Unsubscribe from %s', jid) + self._jids_for_auto_auth.discard(jid) + self._nbxmpp('BasePresence').unsubscribe(jid)
+ def subscribe(self, jid, msg=None, name=None, groups=None, auto_auth=False): self._log.info('Request Subscription to %s', jid)
if auto_auth: self._jids_for_auto_auth.add(jid)
- infos = {'jid': jid} - if name: - infos['name'] = name - iq = nbxmpp.Iq('set', Namespace.ROSTER) - query = iq.setQuery() - item = query.addChild('item', attrs=infos) - for group in groups: - item.addChild('group').setData(group) - self._con.connection.send(iq) - - self.send_presence(jid, - 'subscribe', - status=msg, - nick=app.nicks[self._account]) + self._con.get_module('Roster').set_item(jid, name, groups=groups) + self._nbxmpp('BasePresence').subscribe(jid, + status=msg, + nick=app.nicks[self._account])
def get_presence(self, to=None, typ=None, priority=None, show=None, status=None, nick=None, caps=True,
===================================== gajim/gtk/account_page.py ===================================== @@ -111,15 +111,23 @@ def update(self): self._status_selector.update()
def _subscribe_received(self, event): + if event.account != self._account: + return self._notification_manager.add_subscription_request(event)
def _unsubscribed_received(self, event): + if event.account != self._account: + return self._notification_manager.add_unsubscribed(event)
def _muc_invitation_received(self, event): + if event.account != self._account: + return self._notification_manager.add_invitation_received(event)
def _muc_invitation_declined(self, event): + if event.account != self._account: + return self._notification_manager.add_invitation_declined(event)
def process_event(self, event):
===================================== gajim/gtk/notification_manager.py ===================================== @@ -99,7 +99,7 @@ def _on_subscription_accept(self, _action, param): self._client.get_module('Presence').subscribed(jid) contact = self._client.get_module('Contacts').get_contact(jid) if not contact.is_in_roster: - AddNewContactWindow(self._account, jid, row.user_nick) + AddNewContactWindow(self._account, jid, contact.name) self._listbox.remove(row)
def _on_subscription_block(self, _action, param):
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/271169f262ef4c255026ea0834a6d7902...