Commit 1dcb1785 authored by Philipp Hörist's avatar Philipp Hörist

Blocking: Refactor module

parent 8be9e7f4
Pipeline #6568 passed with stages
in 3 minutes and 24 seconds
......@@ -15,14 +15,15 @@
# XEP-0191: Blocking Command
import nbxmpp
from nbxmpp.protocol import JID
from nbxmpp.namespaces import Namespace
from nbxmpp.structs import StanzaHandler
from nbxmpp.util import is_error_result
from nbxmpp.modules.util import raise_if_error
from gajim.common import app
from gajim.common.nec import NetworkEvent
from gajim.common.nec import NetworkIncomingEvent
from gajim.common.modules.base import BaseModule
from gajim.common.modules.util import as_task
class Blocking(BaseModule):
......@@ -31,7 +32,7 @@ class Blocking(BaseModule):
_nbxmpp_methods = [
'block',
'unblock',
'get_blocking_list',
'request_blocking_list',
]
def __init__(self, con):
......@@ -46,9 +47,6 @@ def __init__(self, con):
ns=Namespace.BLOCKING),
]
self._register_callback('get_blocking_list',
self._blocking_list_received)
self.supported = False
def pass_disco(self, info):
......@@ -63,59 +61,65 @@ def pass_disco(self, info):
self._log.info('Discovered blocking: %s', info.jid)
def _blocking_list_received(self, result):
if is_error_result(result):
self._log.info(result)
return
@as_task
def get_blocking_list(self):
_task = yield
self.blocked = result.blocking_list
app.nec.push_incoming_event(
BlockingEvent(None, conn=self._con, changed=self.blocked))
blocking_list = yield self._nbxmpp('Blocking').request_blocking_list()
raise_if_error(blocking_list)
self.blocked = blocking_list
app.nec.push_incoming_event(NetworkEvent('blocking',
conn=self._con,
changed=self.blocked))
yield blocking_list
@as_task
def update_blocking_list(self, block, unblock):
_task = yield
if block:
result = yield self.block(block)
raise_if_error(result)
def _blocking_push_received(self, _con, stanza, _properties):
reply = stanza.buildReply('result')
children = reply.getChildren()
for child in children:
reply.delChild(child)
self._nbxmpp().send(reply)
if unblock:
result = yield self.unblock(unblock)
raise_if_error(result)
yield True
def _blocking_push_received(self, _con, _stanza, properties):
if not properties.is_blocking:
return
changed_list = []
unblock = stanza.getTag('unblock', namespace=Namespace.BLOCKING)
if unblock is not None:
items = unblock.getTags('item')
if not items:
# Unblock all
changed_list = list(self.blocked)
self.blocked = []
for jid in self.blocked:
self._presence_probe(jid)
self._log.info('Unblock all Push')
raise nbxmpp.NodeProcessed
for item in items:
# Unblock some contacts
jid = item.getAttr('jid')
changed_list.append(jid)
if jid not in self.blocked:
continue
self.blocked.remove(jid)
if properties.blocking.unblock_all:
self.blocked = []
for jid in self.blocked:
self._presence_probe(jid)
self._log.info('Unblock Push: %s', jid)
block = stanza.getTag('block', namespace=Namespace.BLOCKING)
if block is not None:
for item in block.getTags('item'):
jid = item.getAttr('jid')
if jid in self.blocked:
continue
changed_list.append(jid)
self.blocked.append(jid)
self._set_contact_offline(jid)
self._log.info('Block Push: %s', jid)
app.nec.push_incoming_event(
BlockingEvent(None, conn=self._con, changed=changed_list))
self._log.info('Unblock all Push')
for jid in properties.blocking.unblock:
changed_list.append(jid)
if jid not in self.blocked:
continue
self.blocked.remove(jid)
self._presence_probe(jid)
self._log.info('Unblock Push: %s', jid)
for jid in properties.blocking.block:
if jid in self.blocked:
continue
changed_list.append(jid)
self.blocked.append(jid)
self._set_contact_offline(str(jid))
self._log.info('Block Push: %s', jid)
app.nec.push_incoming_event(NetworkEvent('blocking',
conn=self._con,
changed=changed_list))
raise nbxmpp.NodeProcessed
......@@ -124,16 +128,12 @@ def _set_contact_offline(self, jid: str) -> None:
for contact in contact_list:
contact.show = 'offline'
def _presence_probe(self, jid: str) -> None:
def _presence_probe(self, jid: JID) -> None:
self._log.info('Presence probe: %s', jid)
# Send a presence Probe to get the current Status
probe = nbxmpp.Presence(jid, 'probe', frm=self._con.get_own_jid())
self._nbxmpp().send(probe)
class BlockingEvent(NetworkIncomingEvent):
name = 'blocking'
def get_instance(*args, **kwargs):
return Blocking(*args, **kwargs), 'Blocking'
......@@ -14,7 +14,7 @@
import logging
from nbxmpp.util import is_error_result
from nbxmpp.errors import StanzaError
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GLib
......@@ -42,8 +42,6 @@ def __init__(self, account):
self.account = account
self._con = app.connections[account]
self._prev_blocked_jids = set()
self._await_results = 2
self._received_errors = False
self._ui = get_builder('blocking_list.ui')
self.add(self._ui.blocking_grid)
......@@ -57,15 +55,9 @@ def __init__(self, account):
self._activate_spinner()
self._con.get_module('Blocking').get_blocking_list(
self._con.get_module('Blocking').request_blocking_list(
callback=self._on_blocking_list_received)
def _reset_after_error(self):
self._received_errors = False
self._await_results = 2
self._disable_spinner()
self._set_grid_state(True)
def _show_error(self, error):
dialog = HigDialog(
self, Gtk.MessageType.INFO, Gtk.ButtonsType.OK,
......@@ -73,31 +65,32 @@ def _show_error(self, error):
GLib.markup_escape_text(error))
dialog.popup()
def _on_blocking_list_received(self, result):
is_error = is_error_result(result)
self._disable_spinner()
self._set_grid_state(not is_error)
def _on_blocking_list_received(self, task):
try:
blocking_list = task.finish()
except StanzaError as error:
self._set_grid_state(False)
self._show_error(to_user_string(error))
return
if is_error:
self._show_error(to_user_string(result))
self._prev_blocked_jids = set(blocking_list)
self._ui.blocking_store.clear()
for item in blocking_list:
self._ui.blocking_store.append((item,))
else:
self._prev_blocked_jids = set(result.blocking_list)
self._ui.blocking_store.clear()
for item in result.blocking_list:
self._ui.blocking_store.append((item,))
self._set_grid_state(True)
self._disable_spinner()
def _on_save_result(self, result):
self._await_results -= 1
if is_error_result(result) and not self._received_errors:
self._show_error(to_user_string(result))
self._received_errors = True
def _on_save_result(self, task):
try:
_successful = task.finish()
except StanzaError as error:
self._show_error(to_user_string(error))
self._disable_spinner()
self._set_grid_state(True)
return
if not self._await_results:
if self._received_errors:
self._reset_after_error()
else:
self.destroy()
self.destroy()
def _set_grid_state(self, state):
self._ui.blocking_grid.set_sensitive(state)
......@@ -124,22 +117,10 @@ def _on_save(self, _button):
blocked_jids.add(item[0].lower())
unblock_jids = self._prev_blocked_jids - blocked_jids
if unblock_jids:
self._con.get_module('Blocking').unblock(
unblock_jids, callback=self._on_save_result)
else:
self._await_results -= 1
block_jids = blocked_jids - self._prev_blocked_jids
if block_jids:
self._con.get_module('Blocking').block(
block_jids, callback=self._on_save_result)
else:
self._await_results -= 1
if not self._await_results:
# No changes
self.destroy()
self._con.get_module('Blocking').update_blocking_list(
block_jids, unblock_jids, callback=self._on_save_result)
def _activate_spinner(self):
self._spinner.show()
......
......@@ -2614,7 +2614,7 @@ def remove():
def _nec_blocking(self, obj):
for jid in obj.changed:
self.draw_contact(jid, obj.conn.name)
self.draw_contact(str(jid), obj.conn.name)
def on_block(self, widget, list_):
"""
......
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