Philipp Hörist pushed to branch master at gajim / gajim
Commits:
-
1dcb1785
by lovetox at 2020-10-12T21:59:55+02:00
3 changed files:
Changes:
| ... | ... | @@ -15,14 +15,15 @@ |
| 15 | 15 |
# XEP-0191: Blocking Command
|
| 16 | 16 |
|
| 17 | 17 |
import nbxmpp
|
| 18 |
+from nbxmpp.protocol import JID
|
|
| 18 | 19 |
from nbxmpp.namespaces import Namespace
|
| 19 | 20 |
from nbxmpp.structs import StanzaHandler
|
| 20 |
-from nbxmpp.util import is_error_result
|
|
| 21 |
+from nbxmpp.modules.util import raise_if_error
|
|
| 21 | 22 |
|
| 22 | 23 |
from gajim.common import app
|
| 23 | 24 |
from gajim.common.nec import NetworkEvent
|
| 24 |
-from gajim.common.nec import NetworkIncomingEvent
|
|
| 25 | 25 |
from gajim.common.modules.base import BaseModule
|
| 26 |
+from gajim.common.modules.util import as_task
|
|
| 26 | 27 |
|
| 27 | 28 |
|
| 28 | 29 |
class Blocking(BaseModule):
|
| ... | ... | @@ -31,7 +32,7 @@ class Blocking(BaseModule): |
| 31 | 32 |
_nbxmpp_methods = [
|
| 32 | 33 |
'block',
|
| 33 | 34 |
'unblock',
|
| 34 |
- 'get_blocking_list',
|
|
| 35 |
+ 'request_blocking_list',
|
|
| 35 | 36 |
]
|
| 36 | 37 |
|
| 37 | 38 |
def __init__(self, con):
|
| ... | ... | @@ -46,9 +47,6 @@ class Blocking(BaseModule): |
| 46 | 47 |
ns=Namespace.BLOCKING),
|
| 47 | 48 |
]
|
| 48 | 49 |
|
| 49 |
- self._register_callback('get_blocking_list',
|
|
| 50 |
- self._blocking_list_received)
|
|
| 51 |
- |
|
| 52 | 50 |
self.supported = False
|
| 53 | 51 |
|
| 54 | 52 |
def pass_disco(self, info):
|
| ... | ... | @@ -63,59 +61,65 @@ class Blocking(BaseModule): |
| 63 | 61 |
|
| 64 | 62 |
self._log.info('Discovered blocking: %s', info.jid)
|
| 65 | 63 |
|
| 66 |
- def _blocking_list_received(self, result):
|
|
| 67 |
- if is_error_result(result):
|
|
| 68 |
- self._log.info(result)
|
|
| 69 |
- return
|
|
| 64 |
+ @as_task
|
|
| 65 |
+ def get_blocking_list(self):
|
|
| 66 |
+ _task = yield
|
|
| 70 | 67 |
|
| 71 |
- self.blocked = result.blocking_list
|
|
| 72 |
- app.nec.push_incoming_event(
|
|
| 73 |
- BlockingEvent(None, conn=self._con, changed=self.blocked))
|
|
| 68 |
+ blocking_list = yield self._nbxmpp('Blocking').request_blocking_list()
|
|
| 69 |
+ |
|
| 70 |
+ raise_if_error(blocking_list)
|
|
| 71 |
+ |
|
| 72 |
+ self.blocked = blocking_list
|
|
| 73 |
+ app.nec.push_incoming_event(NetworkEvent('blocking',
|
|
| 74 |
+ conn=self._con,
|
|
| 75 |
+ changed=self.blocked))
|
|
| 76 |
+ yield blocking_list
|
|
| 77 |
+ |
|
| 78 |
+ @as_task
|
|
| 79 |
+ def update_blocking_list(self, block, unblock):
|
|
| 80 |
+ _task = yield
|
|
| 81 |
+ |
|
| 82 |
+ if block:
|
|
| 83 |
+ result = yield self.block(block)
|
|
| 84 |
+ raise_if_error(result)
|
|
| 74 | 85 |
|
| 75 |
- def _blocking_push_received(self, _con, stanza, _properties):
|
|
| 76 |
- reply = stanza.buildReply('result')
|
|
| 77 |
- children = reply.getChildren()
|
|
| 78 |
- for child in children:
|
|
| 79 |
- reply.delChild(child)
|
|
| 80 |
- self._nbxmpp().send(reply)
|
|
| 86 |
+ if unblock:
|
|
| 87 |
+ result = yield self.unblock(unblock)
|
|
| 88 |
+ raise_if_error(result)
|
|
| 89 |
+ |
|
| 90 |
+ yield True
|
|
| 91 |
+ |
|
| 92 |
+ def _blocking_push_received(self, _con, _stanza, properties):
|
|
| 93 |
+ if not properties.is_blocking:
|
|
| 94 |
+ return
|
|
| 81 | 95 |
|
| 82 | 96 |
changed_list = []
|
| 83 | 97 |
|
| 84 |
- unblock = stanza.getTag('unblock', namespace=Namespace.BLOCKING)
|
|
| 85 |
- if unblock is not None:
|
|
| 86 |
- items = unblock.getTags('item')
|
|
| 87 |
- if not items:
|
|
| 88 |
- # Unblock all
|
|
| 89 |
- changed_list = list(self.blocked)
|
|
| 90 |
- self.blocked = []
|
|
| 91 |
- for jid in self.blocked:
|
|
| 92 |
- self._presence_probe(jid)
|
|
| 93 |
- self._log.info('Unblock all Push')
|
|
| 94 |
- raise nbxmpp.NodeProcessed
|
|
| 95 |
- |
|
| 96 |
- for item in items:
|
|
| 97 |
- # Unblock some contacts
|
|
| 98 |
- jid = item.getAttr('jid')
|
|
| 99 |
- changed_list.append(jid)
|
|
| 100 |
- if jid not in self.blocked:
|
|
| 101 |
- continue
|
|
| 102 |
- self.blocked.remove(jid)
|
|
| 98 |
+ if properties.blocking.unblock_all:
|
|
| 99 |
+ self.blocked = []
|
|
| 100 |
+ for jid in self.blocked:
|
|
| 103 | 101 |
self._presence_probe(jid)
|
| 104 |
- self._log.info('Unblock Push: %s', jid)
|
|
| 105 |
- |
|
| 106 |
- block = stanza.getTag('block', namespace=Namespace.BLOCKING)
|
|
| 107 |
- if block is not None:
|
|
| 108 |
- for item in block.getTags('item'):
|
|
| 109 |
- jid = item.getAttr('jid')
|
|
| 110 |
- if jid in self.blocked:
|
|
| 111 |
- continue
|
|
| 112 |
- changed_list.append(jid)
|
|
| 113 |
- self.blocked.append(jid)
|
|
| 114 |
- self._set_contact_offline(jid)
|
|
| 115 |
- self._log.info('Block Push: %s', jid)
|
|
| 116 |
- |
|
| 117 |
- app.nec.push_incoming_event(
|
|
| 118 |
- BlockingEvent(None, conn=self._con, changed=changed_list))
|
|
| 102 |
+ self._log.info('Unblock all Push')
|
|
| 103 |
+ |
|
| 104 |
+ for jid in properties.blocking.unblock:
|
|
| 105 |
+ changed_list.append(jid)
|
|
| 106 |
+ if jid not in self.blocked:
|
|
| 107 |
+ continue
|
|
| 108 |
+ self.blocked.remove(jid)
|
|
| 109 |
+ self._presence_probe(jid)
|
|
| 110 |
+ self._log.info('Unblock Push: %s', jid)
|
|
| 111 |
+ |
|
| 112 |
+ for jid in properties.blocking.block:
|
|
| 113 |
+ if jid in self.blocked:
|
|
| 114 |
+ continue
|
|
| 115 |
+ changed_list.append(jid)
|
|
| 116 |
+ self.blocked.append(jid)
|
|
| 117 |
+ self._set_contact_offline(str(jid))
|
|
| 118 |
+ self._log.info('Block Push: %s', jid)
|
|
| 119 |
+ |
|
| 120 |
+ app.nec.push_incoming_event(NetworkEvent('blocking',
|
|
| 121 |
+ conn=self._con,
|
|
| 122 |
+ changed=changed_list))
|
|
| 119 | 123 |
|
| 120 | 124 |
raise nbxmpp.NodeProcessed
|
| 121 | 125 |
|
| ... | ... | @@ -124,16 +128,12 @@ class Blocking(BaseModule): |
| 124 | 128 |
for contact in contact_list:
|
| 125 | 129 |
contact.show = 'offline'
|
| 126 | 130 |
|
| 127 |
- def _presence_probe(self, jid: str) -> None:
|
|
| 131 |
+ def _presence_probe(self, jid: JID) -> None:
|
|
| 128 | 132 |
self._log.info('Presence probe: %s', jid)
|
| 129 | 133 |
# Send a presence Probe to get the current Status
|
| 130 | 134 |
probe = nbxmpp.Presence(jid, 'probe', frm=self._con.get_own_jid())
|
| 131 | 135 |
self._nbxmpp().send(probe)
|
| 132 | 136 |
|
| 133 | 137 |
|
| 134 |
-class BlockingEvent(NetworkIncomingEvent):
|
|
| 135 |
- name = 'blocking'
|
|
| 136 |
- |
|
| 137 |
- |
|
| 138 | 138 |
def get_instance(*args, **kwargs):
|
| 139 | 139 |
return Blocking(*args, **kwargs), 'Blocking'
|
| ... | ... | @@ -14,7 +14,7 @@ |
| 14 | 14 |
|
| 15 | 15 |
import logging
|
| 16 | 16 |
|
| 17 |
-from nbxmpp.util import is_error_result
|
|
| 17 |
+from nbxmpp.errors import StanzaError
|
|
| 18 | 18 |
from gi.repository import Gtk
|
| 19 | 19 |
from gi.repository import Gdk
|
| 20 | 20 |
from gi.repository import GLib
|
| ... | ... | @@ -42,8 +42,6 @@ class BlockingList(Gtk.ApplicationWindow): |
| 42 | 42 |
self.account = account
|
| 43 | 43 |
self._con = app.connections[account]
|
| 44 | 44 |
self._prev_blocked_jids = set()
|
| 45 |
- self._await_results = 2
|
|
| 46 |
- self._received_errors = False
|
|
| 47 | 45 |
|
| 48 | 46 |
self._ui = get_builder('blocking_list.ui')
|
| 49 | 47 |
self.add(self._ui.blocking_grid)
|
| ... | ... | @@ -57,15 +55,9 @@ class BlockingList(Gtk.ApplicationWindow): |
| 57 | 55 |
|
| 58 | 56 |
self._activate_spinner()
|
| 59 | 57 |
|
| 60 |
- self._con.get_module('Blocking').get_blocking_list(
|
|
| 58 |
+ self._con.get_module('Blocking').request_blocking_list(
|
|
| 61 | 59 |
callback=self._on_blocking_list_received)
|
| 62 | 60 |
|
| 63 |
- def _reset_after_error(self):
|
|
| 64 |
- self._received_errors = False
|
|
| 65 |
- self._await_results = 2
|
|
| 66 |
- self._disable_spinner()
|
|
| 67 |
- self._set_grid_state(True)
|
|
| 68 |
- |
|
| 69 | 61 |
def _show_error(self, error):
|
| 70 | 62 |
dialog = HigDialog(
|
| 71 | 63 |
self, Gtk.MessageType.INFO, Gtk.ButtonsType.OK,
|
| ... | ... | @@ -73,31 +65,32 @@ class BlockingList(Gtk.ApplicationWindow): |
| 73 | 65 |
GLib.markup_escape_text(error))
|
| 74 | 66 |
dialog.popup()
|
| 75 | 67 |
|
| 76 |
- def _on_blocking_list_received(self, result):
|
|
| 77 |
- is_error = is_error_result(result)
|
|
| 78 |
- self._disable_spinner()
|
|
| 79 |
- self._set_grid_state(not is_error)
|
|
| 68 |
+ def _on_blocking_list_received(self, task):
|
|
| 69 |
+ try:
|
|
| 70 |
+ blocking_list = task.finish()
|
|
| 71 |
+ except StanzaError as error:
|
|
| 72 |
+ self._set_grid_state(False)
|
|
| 73 |
+ self._show_error(to_user_string(error))
|
|
| 74 |
+ return
|
|
| 80 | 75 |
|
| 81 |
- if is_error:
|
|
| 82 |
- self._show_error(to_user_string(result))
|
|
| 76 |
+ self._prev_blocked_jids = set(blocking_list)
|
|
| 77 |
+ self._ui.blocking_store.clear()
|
|
| 78 |
+ for item in blocking_list:
|
|
| 79 |
+ self._ui.blocking_store.append((item,))
|
|
| 83 | 80 |
|
| 84 |
- else:
|
|
| 85 |
- self._prev_blocked_jids = set(result.blocking_list)
|
|
| 86 |
- self._ui.blocking_store.clear()
|
|
| 87 |
- for item in result.blocking_list:
|
|
| 88 |
- self._ui.blocking_store.append((item,))
|
|
| 81 |
+ self._set_grid_state(True)
|
|
| 82 |
+ self._disable_spinner()
|
|
| 89 | 83 |
|
| 90 |
- def _on_save_result(self, result):
|
|
| 91 |
- self._await_results -= 1
|
|
| 92 |
- if is_error_result(result) and not self._received_errors:
|
|
| 93 |
- self._show_error(to_user_string(result))
|
|
| 94 |
- self._received_errors = True
|
|
| 84 |
+ def _on_save_result(self, task):
|
|
| 85 |
+ try:
|
|
| 86 |
+ _successful = task.finish()
|
|
| 87 |
+ except StanzaError as error:
|
|
| 88 |
+ self._show_error(to_user_string(error))
|
|
| 89 |
+ self._disable_spinner()
|
|
| 90 |
+ self._set_grid_state(True)
|
|
| 91 |
+ return
|
|
| 95 | 92 |
|
| 96 |
- if not self._await_results:
|
|
| 97 |
- if self._received_errors:
|
|
| 98 |
- self._reset_after_error()
|
|
| 99 |
- else:
|
|
| 100 |
- self.destroy()
|
|
| 93 |
+ self.destroy()
|
|
| 101 | 94 |
|
| 102 | 95 |
def _set_grid_state(self, state):
|
| 103 | 96 |
self._ui.blocking_grid.set_sensitive(state)
|
| ... | ... | @@ -124,22 +117,10 @@ class BlockingList(Gtk.ApplicationWindow): |
| 124 | 117 |
blocked_jids.add(item[0].lower())
|
| 125 | 118 |
|
| 126 | 119 |
unblock_jids = self._prev_blocked_jids - blocked_jids
|
| 127 |
- if unblock_jids:
|
|
| 128 |
- self._con.get_module('Blocking').unblock(
|
|
| 129 |
- unblock_jids, callback=self._on_save_result)
|
|
| 130 |
- else:
|
|
| 131 |
- self._await_results -= 1
|
|
| 132 |
- |
|
| 133 | 120 |
block_jids = blocked_jids - self._prev_blocked_jids
|
| 134 |
- if block_jids:
|
|
| 135 |
- self._con.get_module('Blocking').block(
|
|
| 136 |
- block_jids, callback=self._on_save_result)
|
|
| 137 |
- else:
|
|
| 138 |
- self._await_results -= 1
|
|
| 139 |
- |
|
| 140 |
- if not self._await_results:
|
|
| 141 |
- # No changes
|
|
| 142 |
- self.destroy()
|
|
| 121 |
+ |
|
| 122 |
+ self._con.get_module('Blocking').update_blocking_list(
|
|
| 123 |
+ block_jids, unblock_jids, callback=self._on_save_result)
|
|
| 143 | 124 |
|
| 144 | 125 |
def _activate_spinner(self):
|
| 145 | 126 |
self._spinner.show()
|
| ... | ... | @@ -2614,7 +2614,7 @@ class RosterWindow: |
| 2614 | 2614 |
|
| 2615 | 2615 |
def _nec_blocking(self, obj):
|
| 2616 | 2616 |
for jid in obj.changed:
|
| 2617 |
- self.draw_contact(jid, obj.conn.name)
|
|
| 2617 |
+ self.draw_contact(str(jid), obj.conn.name)
|
|
| 2618 | 2618 |
|
| 2619 | 2619 |
def on_block(self, widget, list_):
|
| 2620 | 2620 |
"""
|