comparison libervia/backend/plugins/plugin_misc_forums.py @ 4191:5d056d524298

core, doc, cli (forums): new `forums set` commands + doc: - document the fact that if an empty `uri` is used, the forum node is created automatically - new `forums/set` CLI commands and its documentation
author Goffi <goffi@goffi.org>
date Mon, 11 Dec 2023 18:10:27 +0100
parents 4b842c1fb686
children 1d24ff583794
comparison
equal deleted inserted replaced
4190:92551baea115 4191:5d056d524298
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from typing import Iterable
21 from libervia.backend.core.core_types import SatXMPPEntity
20 from libervia.backend.core.i18n import _ 22 from libervia.backend.core.i18n import _
21 from libervia.backend.core.constants import Const as C 23 from libervia.backend.core.constants import Const as C
22 from libervia.backend.core import exceptions 24 from libervia.backend.core import exceptions
23 from libervia.backend.core.log import getLogger 25 from libervia.backend.core.log import getLogger
24 from libervia.backend.tools.common import uri, data_format 26 from libervia.backend.tools.common import uri, data_format
78 host.bridge.add_method("forum_topic_create", ".plugin", 80 host.bridge.add_method("forum_topic_create", ".plugin",
79 in_sign='ssa{ss}s', out_sign='', 81 in_sign='ssa{ss}s', out_sign='',
80 method=self._create_topic, 82 method=self._create_topic,
81 async_=True) 83 async_=True)
82 84
83 @defer.inlineCallbacks 85 async def _create_forums(
84 def _create_forums(self, client, forums, service, node, forums_elt=None, names=None): 86 self,
87 client: SatXMPPEntity,
88 forums: list[dict],
89 service: jid.JID,
90 node: str,
91 forums_elt: domish.Element|None = None,
92 names: Iterable = None
93 ) -> domish.Element:
85 """Recursively create <forums> element(s) 94 """Recursively create <forums> element(s)
86 95
87 @param forums(list): forums which may have subforums 96 @param forums(list): forums which may have subforums
88 @param service(jid.JID): service where the new nodes will be created 97 @param service(jid.JID): service where the new nodes will be created
89 @param node(unicode): node of the forums 98 @param node(unicode): node of the forums
109 forum_elt = forums_elt.addElement('forum') 118 forum_elt = forums_elt.addElement('forum')
110 119
111 for key, value in forum.items(): 120 for key, value in forum.items():
112 if key == 'name' and key in names: 121 if key == 'name' and key in names:
113 raise exceptions.ConflictError(_("following forum name is not unique: {name}").format(name=key)) 122 raise exceptions.ConflictError(_("following forum name is not unique: {name}").format(name=key))
114 if key == 'uri' and not value.strip(): 123 if key == 'uri' and value is None or not value.strip():
115 log.info(_("creating missing forum node")) 124 log.info(_("creating missing forum node"))
116 forum_node = FORUM_TOPICS_NODE_TPL.format(node=node, uuid=shortuuid.uuid()) 125 forum_node = FORUM_TOPICS_NODE_TPL.format(node=node, uuid=shortuuid.uuid())
117 yield self._p.createNode(client, service, forum_node, self._node_options) 126 await self._p.createNode(client, service, forum_node, self._node_options)
118 value = uri.build_xmpp_uri('pubsub', 127 value = uri.build_xmpp_uri('pubsub',
119 path=service.full(), 128 path=service.full(),
120 node=forum_node) 129 node=forum_node)
121 if key in FORUM_ATTR: 130 if key in FORUM_ATTR:
122 forum_elt[key] = value.strip() 131 forum_elt[key] = value.strip()
123 elif key in FORUM_SUB_ELTS: 132 elif key in FORUM_SUB_ELTS:
124 forum_elt.addElement(key, content=value) 133 forum_elt.addElement(key, content=value)
125 elif key == 'sub-forums': 134 elif key == 'sub-forums':
135 assert isinstance(value, list)
126 sub_forums_elt = forum_elt.addElement('forums') 136 sub_forums_elt = forum_elt.addElement('forums')
127 yield self._create_forums(client, value, service, node, sub_forums_elt, names=names) 137 await self._create_forums(client, value, service, node, sub_forums_elt, names=names)
128 else: 138 else:
129 log.warning(_("Unknown forum attribute: {key}").format(key=key)) 139 log.warning(_("Unknown forum attribute: {key}").format(key=key))
130 if not forum_elt.getAttribute('title'): 140 if not forum_elt.getAttribute('title'):
131 name = forum_elt.getAttribute('name') 141 name = forum_elt.getAttribute('name')
132 if name: 142 if name:
133 forum_elt['title'] = name 143 forum_elt['title'] = name
134 else: 144 else:
135 raise ValueError(_("forum need a title or a name")) 145 raise ValueError(_("forum need a title or a name"))
136 if not forum_elt.getAttribute('uri') and not forum_elt.children: 146 if not forum_elt.getAttribute('uri') and not forum_elt.children:
137 raise ValueError(_("forum need uri or sub-forums")) 147 raise ValueError(_("forum need uri or sub-forums"))
138 defer.returnValue(forums_elt) 148 return forums_elt
139 149
140 def _parse_forums(self, parent_elt=None, forums=None): 150 def _parse_forums(self, parent_elt=None, forums=None):
141 """Recursivly parse a <forums> elements and return corresponding forums data 151 """Recursivly parse a <forums> elements and return corresponding forums data
142 152
143 @param item(domish.Element): item with <forums> element 153 @param item(domish.Element): item with <forums> element