plugin forums: parsing fix + formatting:

- reformatted with black
- fix parsing of forums
- minor improvements
This commit is contained in:
Goffi 2023-12-12 12:17:15 +01:00
commit 258c99b3c5

View file

@ -1,8 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Libervia plugin for pubsub forums
# SAT plugin for pubsub forums # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
# Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by # it under the terms of the GNU Affero General Public License as published by
@ -29,10 +28,11 @@ from twisted.words.xish import domish
from twisted.internet import defer from twisted.internet import defer
import shortuuid import shortuuid
import json import json
log = getLogger(__name__) log = getLogger(__name__)
NS_FORUMS = 'org.salut-a-toi.forums:0' NS_FORUMS = "org.salut-a-toi.forums:0"
NS_FORUMS_TOPICS = NS_FORUMS + '#topics' NS_FORUMS_TOPICS = NS_FORUMS + "#topics"
PLUGIN_INFO = { PLUGIN_INFO = {
C.PI_NAME: _("forums management"), C.PI_NAME: _("forums management"),
@ -42,54 +42,69 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0277"], C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0277"],
C.PI_MAIN: "forums", C.PI_MAIN: "forums",
C.PI_HANDLER: "no", C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _("""forums management plugin""") C.PI_DESCRIPTION: _("""forums management plugin"""),
} }
FORUM_ATTR = {'title', 'name', 'main-language', 'uri'} FORUM_ATTR = {"title", "name", "main-language", "uri"}
FORUM_SUB_ELTS = ('short-desc', 'desc') FORUM_SUB_ELTS = ("short-desc", "desc")
FORUM_TOPICS_NODE_TPL = '{node}#topics_{uuid}' FORUM_TOPICS_NODE_TPL = "{node}#topics_{uuid}"
FORUM_TOPIC_NODE_TPL = '{node}_{uuid}' FORUM_TOPIC_NODE_TPL = "{node}_{uuid}"
class forums(object): class forums:
def __init__(self, host): def __init__(self, host):
log.info(_("forums plugin initialization")) log.info(_("forums plugin initialization"))
self.host = host self.host = host
self._m = self.host.plugins['XEP-0277'] self._m = self.host.plugins["XEP-0277"]
self._p = self.host.plugins['XEP-0060'] self._p = self.host.plugins["XEP-0060"]
self._node_options = { self._node_options = {
self._p.OPT_ACCESS_MODEL: self._p.ACCESS_OPEN, self._p.OPT_ACCESS_MODEL: self._p.ACCESS_OPEN,
self._p.OPT_PERSIST_ITEMS: 1, self._p.OPT_PERSIST_ITEMS: 1,
self._p.OPT_DELIVER_PAYLOADS: 1, self._p.OPT_DELIVER_PAYLOADS: 1,
self._p.OPT_SEND_ITEM_SUBSCRIBE: 1, self._p.OPT_SEND_ITEM_SUBSCRIBE: 1,
self._p.OPT_PUBLISH_MODEL: self._p.ACCESS_OPEN, self._p.OPT_PUBLISH_MODEL: self._p.ACCESS_OPEN,
} }
host.register_namespace('forums', NS_FORUMS) host.register_namespace("forums", NS_FORUMS)
host.bridge.add_method("forums_get", ".plugin", host.bridge.add_method(
in_sign='ssss', out_sign='s', "forums_get",
method=self._get, ".plugin",
async_=True) in_sign="ssss",
host.bridge.add_method("forums_set", ".plugin", out_sign="s",
in_sign='sssss', out_sign='', method=self._get,
method=self._set, async_=True,
async_=True) )
host.bridge.add_method("forum_topics_get", ".plugin", host.bridge.add_method(
in_sign='ssa{ss}s', out_sign='(aa{ss}s)', "forums_set",
method=self._get_topics, ".plugin",
async_=True) in_sign="sssss",
host.bridge.add_method("forum_topic_create", ".plugin", out_sign="",
in_sign='ssa{ss}s', out_sign='', method=self._set,
method=self._create_topic, async_=True,
async_=True) )
host.bridge.add_method(
"forum_topics_get",
".plugin",
in_sign="ssa{ss}s",
out_sign="(aa{ss}s)",
method=self._get_topics,
async_=True,
)
host.bridge.add_method(
"forum_topic_create",
".plugin",
in_sign="ssa{ss}s",
out_sign="",
method=self._create_topic,
async_=True,
)
async def _create_forums( async def _create_forums(
self, self,
client: SatXMPPEntity, client: SatXMPPEntity,
forums: list[dict], forums: list[dict],
service: jid.JID, service: jid.JID,
node: str, node: str,
forums_elt: domish.Element|None = None, forums_elt: domish.Element | None = None,
names: Iterable = None names: Iterable = None,
) -> domish.Element: ) -> domish.Element:
"""Recursively create <forums> element(s) """Recursively create <forums> element(s)
@ -104,46 +119,54 @@ class forums(object):
if not isinstance(forums, list): if not isinstance(forums, list):
raise ValueError(_("forums arguments must be a list of forums")) raise ValueError(_("forums arguments must be a list of forums"))
if forums_elt is None: if forums_elt is None:
forums_elt = domish.Element((NS_FORUMS, 'forums')) forums_elt = domish.Element((NS_FORUMS, "forums"))
assert names is None assert names is None
names = set() names = set()
else: else:
if names is None or forums_elt.name != 'forums': if names is None or forums_elt.name != "forums":
raise exceptions.InternalError('invalid forums or names') raise exceptions.InternalError("invalid forums or names")
assert names is not None assert names is not None
for forum in forums: for forum in forums:
if not isinstance(forum, dict): if not isinstance(forum, dict):
raise ValueError(_("A forum item must be a dictionary")) raise ValueError(_("A forum item must be a dictionary"))
forum_elt = forums_elt.addElement('forum') forum_elt = forums_elt.addElement("forum")
for key, value in forum.items(): for key, value in forum.items():
if key == 'name' and key in names: if key == "name" and key in names:
raise exceptions.ConflictError(_("following forum name is not unique: {name}").format(name=key)) raise exceptions.ConflictError(
if key == 'uri' and value is None or not value.strip(): _("following forum name is not unique: {name}").format(name=key)
)
if key == "uri" and (value is None or not value.strip()):
log.info(_("creating missing forum node")) log.info(_("creating missing forum node"))
forum_node = FORUM_TOPICS_NODE_TPL.format(node=node, uuid=shortuuid.uuid()) forum_node = FORUM_TOPICS_NODE_TPL.format(
await self._p.createNode(client, service, forum_node, self._node_options) node=node, uuid=shortuuid.uuid()
value = uri.build_xmpp_uri('pubsub', )
path=service.full(), await self._p.createNode(
node=forum_node) client, service, forum_node, self._node_options
)
value = uri.build_xmpp_uri(
"pubsub", path=service.full(), node=forum_node
)
if key in FORUM_ATTR: if key in FORUM_ATTR:
forum_elt[key] = value.strip() forum_elt[key] = value.strip()
elif key in FORUM_SUB_ELTS: elif key in FORUM_SUB_ELTS:
forum_elt.addElement(key, content=value) forum_elt.addElement(key, content=value)
elif key == 'sub-forums': elif key == "sub-forums":
assert isinstance(value, list) assert isinstance(value, list)
sub_forums_elt = forum_elt.addElement('forums') sub_forums_elt = forum_elt.addElement("forums")
await self._create_forums(client, value, service, node, sub_forums_elt, names=names) await self._create_forums(
client, value, service, node, sub_forums_elt, names=names
)
else: else:
log.warning(_("Unknown forum attribute: {key}").format(key=key)) log.warning(_("Unknown forum attribute: {key}").format(key=key))
if not forum_elt.getAttribute('title'): if not forum_elt.getAttribute("title"):
name = forum_elt.getAttribute('name') name = forum_elt.getAttribute("name")
if name: if name:
forum_elt['title'] = name forum_elt["title"] = name
else: else:
raise ValueError(_("forum need a title or a name")) raise ValueError(_("forum need a title or a name"))
if not forum_elt.getAttribute('uri') and not forum_elt.children: if not forum_elt.getAttribute("uri") and not forum_elt.children:
raise ValueError(_("forum need uri or sub-forums")) raise ValueError(_("forum need uri or sub-forums"))
return forums_elt return forums_elt
@ -155,34 +178,42 @@ class forums(object):
@return (list): parsed data @return (list): parsed data
@raise ValueError: item is invalid @raise ValueError: item is invalid
""" """
if parent_elt.name == 'item': if parent_elt.name == "item":
forums = [] forums = []
try: try:
forums_elt = next(parent_elt.elements(NS_FORUMS, 'forums')) forums_elt = next(parent_elt.elements(NS_FORUMS, "forums"))
except StopIteration: except StopIteration:
raise ValueError(_("missing <forums> element")) raise ValueError(_("missing <forums> element"))
else: else:
forums_elt = parent_elt forums_elt = parent_elt
if forums is None: if forums is None:
raise exceptions.InternalError('expected forums') raise exceptions.InternalError("expected forums")
if forums_elt.name != 'forums': if forums_elt.name != "forums":
raise ValueError(_('Unexpected element: {xml}').format(xml=forums_elt.toXml())) raise ValueError(
_("Unexpected element: {xml}").format(xml=forums_elt.toXml())
)
for forum_elt in forums_elt.elements(): for forum_elt in forums_elt.elements():
if forum_elt.name == 'forum': if forum_elt.name == "forum":
data = {} data = {}
for attrib in FORUM_ATTR.intersection(forum_elt.attributes): for attrib in FORUM_ATTR.intersection(forum_elt.attributes):
data[attrib] = forum_elt[attrib] data[attrib] = forum_elt[attrib]
unknown = set(forum_elt.attributes).difference(FORUM_ATTR) unknown = set(forum_elt.attributes).difference(FORUM_ATTR)
if unknown: if unknown:
log.warning(_("Following attributes are unknown: {unknown}").format(unknown=unknown)) log.warning(
_("Following attributes are unknown: {unknown}").format(
unknown=unknown
)
)
for elt in forum_elt.elements(): for elt in forum_elt.elements():
if elt.name in FORUM_SUB_ELTS: if elt.name in FORUM_SUB_ELTS:
data[elt.name] = str(elt) data[elt.name] = str(elt)
elif elt.name == 'forums': elif elt.name == "forums":
sub_forums = data['sub-forums'] = [] sub_forums = data["sub-forums"] = []
self._parse_forums(elt, sub_forums) self._parse_forums(elt, sub_forums)
if not 'title' in data or not {'uri', 'sub-forums'}.intersection(data): if not "title" in data or not {"uri", "sub-forums"}.intersection(data):
log.warning(_("invalid forum, ignoring: {xml}").format(xml=forum_elt.toXml())) log.warning(
_("invalid forum, ignoring: {xml}").format(xml=forum_elt.toXml())
)
else: else:
forums.append(data) forums.append(data)
else: else:
@ -208,22 +239,28 @@ class forums(object):
if node is None: if node is None:
node = NS_FORUMS node = NS_FORUMS
if forums_key is None: if forums_key is None:
forums_key = 'default' forums_key = "default"
items_data = await self._p.get_items(client, service, node, item_ids=[forums_key]) items_data = await self._p.get_items(client, service, node, item_ids=[forums_key])
item = items_data[0][0] item = items_data[0][0]
# we have the item and need to convert it to json # we have the item and need to convert it to json
forums = self._parse_forums(item) forums = self._parse_forums(item)
return forums return forums
def _set(self, forums, service=None, node=None, forums_key=None, profile_key=C.PROF_KEY_NONE): def _set(
self,
forums: str,
service_s: str = "",
node_s: str = "",
forums_key: str = "",
profile_key: str = C.PROF_KEY_NONE,
) -> defer.Deferred:
client = self.host.get_client(profile_key) client = self.host.get_client(profile_key)
forums = json.loads(forums) forums = json.loads(forums)
if service.strip(): if not service_s.strip():
service = jid.JID(service)
else:
service = None service = None
if not node.strip(): else:
node = None service = jid.JID(service_s)
node = None if not node_s.strip() else node_s
return defer.ensureDeferred( return defer.ensureDeferred(
self.set(client, forums, service, node, forums_key or None) self.set(client, forums, service, node, forums_key or None)
) )
@ -246,11 +283,11 @@ class forums(object):
None to use "default" None to use "default"
""" """
if service is None: if service is None:
service = client.pubsub_service service = client.pubsub_service
if node is None: if node is None:
node = NS_FORUMS node = NS_FORUMS
if forums_key is None: if forums_key is None:
forums_key = 'default' forums_key = "default"
forums_elt = await self._create_forums(client, forums, service, node) forums_elt = await self._create_forums(client, forums, service, node)
return await self._p.send_item( return await self._p.send_item(
client, service, node, forums_elt, item_id=forums_key client, service, node, forums_elt, item_id=forums_key
@ -261,8 +298,11 @@ class forums(object):
extra = self._p.parse_extra(extra) extra = self._p.parse_extra(extra)
d = defer.ensureDeferred( d = defer.ensureDeferred(
self.get_topics( self.get_topics(
client, jid.JID(service), node, rsm_request=extra.rsm_request, client,
extra=extra.extra jid.JID(service),
node,
rsm_request=extra.rsm_request,
extra=extra.extra,
) )
) )
d.addCallback( d.addCallback(
@ -281,11 +321,13 @@ class forums(object):
topics = [] topics = []
item_elts, metadata = topics_data item_elts, metadata = topics_data
for item_elt in item_elts: for item_elt in item_elts:
topic_elt = next(item_elt.elements(NS_FORUMS, 'topic')) topic_elt = next(item_elt.elements(NS_FORUMS, "topic"))
title_elt = next(topic_elt.elements(NS_FORUMS, 'title')) title_elt = next(topic_elt.elements(NS_FORUMS, "title"))
topic = {'uri': topic_elt['uri'], topic = {
'author': topic_elt['author'], "uri": topic_elt["uri"],
'title': str(title_elt)} "author": topic_elt["author"],
"title": str(title_elt),
}
topics.append(topic) topics.append(topic)
return (topics, metadata) return (topics, metadata)
@ -297,21 +339,22 @@ class forums(object):
async def create_topic(self, client, service, node, mb_data): async def create_topic(self, client, service, node, mb_data):
try: try:
title = mb_data['title'] title = mb_data["title"]
content = mb_data.pop('content') content = mb_data.pop("content")
except KeyError as e: except KeyError as e:
raise exceptions.DataError("missing mandatory data: {key}".format(key=e.args[0])) raise exceptions.DataError(
"missing mandatory data: {key}".format(key=e.args[0])
)
else: else:
mb_data["content_rich"] = content mb_data["content_rich"] = content
topic_node = FORUM_TOPIC_NODE_TPL.format(node=node, uuid=shortuuid.uuid()) topic_node = FORUM_TOPIC_NODE_TPL.format(node=node, uuid=shortuuid.uuid())
await self._p.createNode(client, service, topic_node, self._node_options) await self._p.createNode(client, service, topic_node, self._node_options)
await self._m.send(client, mb_data, service, topic_node) await self._m.send(client, mb_data, service, topic_node)
topic_uri = uri.build_xmpp_uri('pubsub', topic_uri = uri.build_xmpp_uri(
subtype='microblog', "pubsub", subtype="microblog", path=service.full(), node=topic_node
path=service.full(), )
node=topic_node) topic_elt = domish.Element((NS_FORUMS, "topic"))
topic_elt = domish.Element((NS_FORUMS, 'topic')) topic_elt["uri"] = topic_uri
topic_elt['uri'] = topic_uri topic_elt["author"] = client.jid.userhost()
topic_elt['author'] = client.jid.userhost() topic_elt.addElement("title", content=title)
topic_elt.addElement('title', content = title)
await self._p.send_item(client, service, node, topic_elt) await self._p.send_item(client, service, node, topic_elt)