Mercurial > libervia-backend
diff src/plugins/plugin_misc_groupblog.py @ 938:fc7e0828b18e
plugin account, groupblog: user can erase all their microblogs at once
author | souliane <souliane@mailoo.org> |
---|---|
date | Thu, 27 Mar 2014 02:24:20 +0100 |
parents | 1a759096ccbd |
children | c6d8fc63b1db |
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py Tue Mar 25 17:26:31 2014 +0100 +++ b/src/plugins/plugin_misc_groupblog.py Thu Mar 27 02:24:20 2014 +0100 @@ -22,7 +22,7 @@ from logging import debug, info, warning, error from twisted.internet import defer from twisted.words.protocols.jabber import jid -from twisted.words.xish.domish import Element +from twisted.words.xish.domish import Element, generateElementsNamed from sat.core import exceptions from wokkel import disco, data_form, iwokkel from zope.interface import implements @@ -53,7 +53,7 @@ PLUGIN_INFO = { "name": "Group blogging throught collections", - "import_name": "groupblog", + "import_name": "GROUPBLOG", "type": "MISC", "protocols": [], "dependencies": ["XEP-0277"], @@ -415,8 +415,8 @@ def notify(d): # TODO: this works only on the same host, and notifications for item deletion should be # implemented according to http://xmpp.org/extensions/xep-0060.html#publisher-delete-success-notify - # instead. The notification mechanisms implemented in sat_pubsub and wokkel seem not compatible, - # see wokkel.pubsub.PubSubClient._onEvent_items and sat_pubsub.backend._doNotifyRetraction + # instead. The notification mechanism implemented in sat_pubsub and wokkel have apriori + # a problem with retrieving the subscriptions, or something else. service, node, item_id = pub_data publisher = self.host.getJidNStream(profile_key)[0] profile = self.host.memory.getProfileName(profile_key) @@ -572,7 +572,6 @@ d.addCallback(get_comments) return d - def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key=C.PROF_KEY_NONE): """Get the last published microblogs @param pub_jid_s: jid of the publisher @@ -665,7 +664,7 @@ """ def sendResult(result): - """send result of DeferredList (list of microblogs to the calling method""" + """send result of DeferredList (dict of jid => microblogs) to the calling method""" ret = {} @@ -693,7 +692,6 @@ mblogs = [] - for jid_ in jids: d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(jid_), max_items=max_items, profile_key=profile_key) @@ -773,6 +771,109 @@ return self._initialise(profile_key).addCallback(initialised) #TODO: we need to use the server corresponding the the host of the jid + def deleteAllGroupBlogsAndComments(self, profile_key=C.PROF_KEY_NONE): + """Delete absolutely all the microblog data that the user has posted""" + calls = [self.deleteAllGroupBlogs(profile_key), self.deleteAllGroupBlogsComments(profile_key)] + return defer.DeferredList(calls) + + def deleteAllGroupBlogs(self, profile_key=C.PROF_KEY_NONE): + """Delete all the main items and their comments that the user has posted + """ + def initialised(result): + profile, client = result + service = client.item_access_pubsub + jid_ = client.jid + + main_node = self.getNodeName(jid_) + d = self.host.plugins["XEP-0060"].deleteNode(service, main_node, profile_key=profile) + d.addCallback(lambda dummy: info(_("All microblog's main items from %s have been deleted!") % jid_.userhost())) + d.addErrback(lambda failure: error(_("Deletion of node %(node)s failed: %(message)s") % + {'node': main_node, 'message': failure.getErrorMessage()})) + return d + + return self._initialise(profile_key).addCallback(initialised) + + def deleteAllGroupBlogsComments(self, profile_key=C.PROF_KEY_NONE): + """Delete all the comments that the user posted on other's main items. + We avoid the conversions from item to microblog data as we only need + to retrieve some attributes, no need to convert text syntax... + """ + def initialised(result): + """Get all the main items from our contact list + @return: a DeferredList + """ + profile, client = result + service = client.item_access_pubsub + jids = [contact.jid.userhostJID() for contact in client.roster.getItems()] + blogs = [] + for jid_ in jids: + main_node = self.getNodeName(jid_) + d = self.host.plugins["XEP-0060"].getItems(service, main_node, profile_key=profile) + d.addCallback(getComments, client) + d.addErrback(lambda failure, main_node: error(_("Retrieval of items for node %(node)s failed: %(message)s") % + {'node': main_node, 'message': failure.getErrorMessage()}), main_node) + blogs.append(d) + + return defer.DeferredList(blogs) + + def getComments(items, client): + """Get all the comments for a list of items + @param items: a list of main items for one user + @param client: the client of the user + @return: a DeferredList + """ + comments = [] + for item in items: + try: + entry = generateElementsNamed(item.elements(), 'entry').next() + link = generateElementsNamed(entry.elements(), 'link').next() + except StopIteration: + continue + href = link.getAttribute('href') + service, node = self.host.plugins['XEP-0277'].parseCommentUrl(href) + d = self.host.plugins["XEP-0060"].getItems(service, node, profile_key=profile_key) + d.addCallback(lambda items, service, node: (service, node, items), service, node) + d.addErrback(lambda failure, node: error(_("Retrieval of comments for node %(node)s failed: %(message)s") % + {'node': node, 'message': failure.getErrorMessage()}), node) + comments.append(d) + dlist = defer.DeferredList(comments) + dlist.addCallback(deleteComments, client) + return dlist + + def deleteComments(result, client): + """Delete all the comments of the user that are found in result + @param result: a list of couple (success, value) with success a + boolean and value a tuple (service as JID, node_id, comment_items) + @param client: the client of the user + @return: a DeferredList with the deletions result + """ + user_jid_s = client.jid.userhost() + for (success, value) in result: + if not success: + continue + service, node_id, comment_items = value + item_ids = [] + for comment_item in comment_items: # for all the comments on one post + try: + entry = generateElementsNamed(comment_item.elements(), 'entry').next() + author = generateElementsNamed(entry.elements(), 'author').next() + name = generateElementsNamed(author.elements(), 'name').next() + except StopIteration: + continue + if name.children[0] == user_jid_s: + item_ids.append(comment_item.getAttribute('id')) + deletions = [] + if item_ids: # remove the comments of the user on the given post + d = self.host.plugins['XEP-0060'].retractItems(service, node_id, item_ids, profile_key=profile_key) + d.addCallback(lambda dummy, node_id: debug(_('Comments of user %(user)s in node %(node)s have been retracted') % + {'user': user_jid_s, 'node': node_id}), node_id) + d.addErrback(lambda failure, node_id: error(_("Retraction of comments from %(user)s in node %(node)s failed: %(message)s") % + {'user': user_jid_s, 'node': node_id, 'message': failure.getErrorMessage()}), node_id) + deletions.append(d) + return defer.DeferredList(deletions) + + return self._initialise(profile_key).addCallback(initialised) + class GroupBlog_handler(XMPPHandler): implements(iwokkel.IDisco)