Mercurial > libervia-backend
diff src/plugins/plugin_xep_0277.py @ 2227:79d279d1ee88
plugin XEP-0277: comments node access model changes:
comments node access model is now copied from parent item by default. If whitelist access is used, parent item affiliations are copied too.
publish model is now open by default instead of subscribers
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 17 Apr 2017 20:33:32 +0200 |
parents | 6a2fa651d7fa |
children | 5e12fc5ae52a |
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0277.py Mon Apr 17 20:31:12 2017 +0200 +++ b/src/plugins/plugin_xep_0277.py Mon Apr 17 20:33:32 2017 +0200 @@ -466,16 +466,17 @@ return client.pubsub_service if client.pubsub_service is not None else parent_service @defer.inlineCallbacks - def _manageComments(self, access, mb_data, service, node, item_id, profile): + def _manageComments(self, client, mb_data, service, node, item_id, access=None): """Check comments keys in mb_data and create comments node if necessary if mb_data['comments'] exists, it is used (or mb_data['comments_service'] and/or mb_data['comments_node']), else it is generated (if allow_comments is True). - @param access(unicode): access model @param mb_data(dict): microblog mb_data @param service(jid.JID, None): PubSub service of the parent item @param node(unicode): node of the parent item - @param item_id(unicoe): id of the parent item + @param item_id(unicode): id of the parent item + @param access(unicode, None): access model + None to use same access model as parent item """ # FIXME: if 'comments' already exists in mb_data, it is not used to create the Node allow_comments = C.bool(mb_data.pop("allow_comments", "false")) @@ -485,14 +486,18 @@ del mb_data['comments'] return - client = self.host.getClient(profile) + if access is None: + # TODO: cache access models per service/node + parent_node_config = yield self._p.getConfiguration(client, service, node) + access = parent_node_config.get(self._p.OPT_ACCESS_MODEL, self._p.ACCESS_OPEN) options = {self._p.OPT_ACCESS_MODEL: access, self._p.OPT_PERSIST_ITEMS: 1, self._p.OPT_MAX_ITEMS: -1, self._p.OPT_DELIVER_PAYLOADS: 1, self._p.OPT_SEND_ITEM_SUBSCRIBE: 1, - self._p.OPT_PUBLISH_MODEL: "subscribers", # TODO: should be open if *both* node and item access_model are open (public node and item) + # FIXME: would it make sense to restrict publish model to subscribers? + self._p.OPT_PUBLISH_MODEL: self._p.ACCESS_OPEN, } # if other plugins need to change the options @@ -518,6 +523,11 @@ log.info(u"node {} already exists on service {}".format(comments_node, comments_service)) else: raise e + else: + if access == self._p.ACCESS_WHITELIST: + # for whitelist access we need to copy affiliations from parent item + parent_affiliations = yield self._p.getNodeAffiliations(client, service, node) + yield self._p.setNodeAffiliations(client, comments_service, comments_node, parent_affiliations) if comments_service is None: comments_service = client.jid.userhostJID() @@ -533,11 +543,11 @@ def _mbSend(self, service, node, data, profile_key): service = jid.JID(service) if service else None node = node if node else NS_MICROBLOG - profile = self.host.memory.getProfileName(profile_key) - return self.send(data, service, node, profile) + client = self.host.getClient(profile_key) + return self.send(client, data, service, node) @defer.inlineCallbacks - def send(self, data, service=None, node=NS_MICROBLOG, profile=None): + def send(self, client, data, service=None, node=NS_MICROBLOG): """Send XEP-0277's microblog data @param data(dict): microblog data (must include at least a "content" or a "title" key). @@ -546,21 +556,20 @@ None to publish on profile's PEP @param node(unicode, None): PubSub node to use (defaut to microblog NS) None is equivalend as using default value - @param profile: %(doc_profile)s """ # TODO: check that all data keys are used, this would avoid sending publicly a private message # by accident (e.g. if group pluging is not loaded, and "grou*" key are not used) - assert profile is not None if node is None: node = NS_MICROBLOG item_id = data.get('id') or unicode(uuid.uuid4()) + try: - yield self._manageComments(self._p.ACCESS_OPEN, data, service, node, item_id, profile) + yield self._manageComments(client, data, service, node, item_id, access=None) except error.StanzaError: log.warning(u"Can't create comments node for item {}".format(item_id)) - item = yield self.data2entry(data, item_id, profile) - ret = yield self._p.publish(service, node, [item], profile_key=profile) + item = yield self.data2entry(data, item_id, client.profile) + ret = yield self._p.publish(service, node, [item], profile_key=client.profile) defer.returnValue(ret) ## retract ##