diff src/plugins/plugin_misc_account.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 34dd9287dfe5
children abd3a75d629c
line wrap: on
line diff
--- a/src/plugins/plugin_misc_account.py	Tue Mar 25 17:26:31 2014 +0100
+++ b/src/plugins/plugin_misc_account.py	Thu Mar 27 02:24:20 2014 +0100
@@ -34,6 +34,7 @@
     "type": "MISC",
     "protocols": [],
     "dependencies": [],
+    "recommendations": ['GROUPBLOG'],
     "main": "MiscAccount",
     "handler": "no",
     "description": _(u"""SàT account creation""")
@@ -133,6 +134,13 @@
         self.__account_cb_id = host.registerCallback(self._accountDialogCb, with_data=True)
         self.__delete_account_id = host.registerCallback(self.__deleteAccountCb, with_data=True)
 
+        def deleteBlogCallback(posts, comments):
+            return lambda data, profile: self.__deleteBlogPostsCb(posts, comments, data, profile)
+
+        self.__delete_posts_comments_id = host.registerCallback(deleteBlogCallback(True, True), with_data=True)
+        self.__delete_posts_id = host.registerCallback(deleteBlogCallback(True, False), with_data=True)
+        self.__delete_comments_id = host.registerCallback(deleteBlogCallback(False, True), with_data=True)
+
     def getConfig(self, name):
         return self.host.memory.getConfig(CONFIG_SECTION, name) or default_conf[name]
 
@@ -239,6 +247,7 @@
         """
         form_ui = xml_tools.XMLUI("form", "tabs", title=D_("Manage your XMPP account"), submit_id=self.__account_cb_id)
         tab_container = form_ui.current_container
+
         tab_container.addTab("update", D_("Change your password"), container=xml_tools.PairsContainer)
         form_ui.addLabel(D_("Current password"))
         form_ui.addPassword("current_passwd", value="")
@@ -246,6 +255,16 @@
         form_ui.addPassword("new_passwd1", value="")
         form_ui.addLabel(D_("New password (again)"))
         form_ui.addPassword("new_passwd2", value="")
+
+        if 'GROUPBLOG' in self.host.plugins:
+            tab_container.addTab("delete_posts", D_("Delete your posts"), container=xml_tools.PairsContainer)
+            form_ui.addLabel(D_("Current password"))
+            form_ui.addPassword("delete_posts_passwd", value="")
+            form_ui.addLabel(D_("Delete all your posts and their comments"))
+            form_ui.addBool("delete_posts_checkbox", "false")
+            form_ui.addLabel(D_("Delete all your comments on other's posts"))
+            form_ui.addBool("delete_comments_checkbox", "false")
+
         tab_container.addTab("delete", D_("Delete your account"), container=xml_tools.PairsContainer)
         form_ui.addLabel(D_("Current password"))
         form_ui.addPassword("delete_passwd", value="")
@@ -261,7 +280,7 @@
         password = self.host.memory.getParamA("Password", "Connection", profile_key=profile)
 
         def error_ui():
-            error_ui = xml_tools.XMLUI("popup", title="Error")
+            error_ui = xml_tools.XMLUI("popup", title=D_("Error"))
             error_ui.addText(D_("Passwords don't match!"))
             return defer.succeed({'xmlui': error_ui.toXml()})
 
@@ -273,6 +292,18 @@
                 return self.__deleteAccount(profile)
             return error_ui()
 
+        # check for blog posts deletion
+        if 'GROUPBLOG' in self.host.plugins:
+            delete_posts_passwd = data[xml_tools.SAT_FORM_PREFIX + 'delete_posts_passwd']
+            delete_posts_checkbox = data[xml_tools.SAT_FORM_PREFIX + 'delete_posts_checkbox']
+            delete_comments_checkbox = data[xml_tools.SAT_FORM_PREFIX + 'delete_comments_checkbox']
+            posts = delete_posts_checkbox == 'true'
+            comments = delete_comments_checkbox == 'true'
+            if posts or comments:
+                if password == delete_posts_passwd:
+                    return self.__deleteBlogPosts(posts, comments, profile)
+                return error_ui()
+
         # check for password modification
         current_passwd = data[xml_tools.SAT_FORM_PREFIX + 'current_passwd']
         new_passwd1 = data[xml_tools.SAT_FORM_PREFIX + 'new_passwd1']
@@ -291,12 +322,12 @@
         """
         def passwordChanged(result):
             self.host.memory.setParam("Password", password, "Connection", profile_key=profile)
-            confirm_ui = xml_tools.XMLUI("popup", title="Confirmation")
+            confirm_ui = xml_tools.XMLUI("popup", title=D_("Confirmation"))
             confirm_ui.addText(D_("Your password has been changed."))
             return defer.succeed({'xmlui': confirm_ui.toXml()})
 
         def errback(failure):
-            error_ui = xml_tools.XMLUI("popup", title="Error")
+            error_ui = xml_tools.XMLUI("popup", title=D_("Error"))
             error_ui.addText(D_("Your password could not be changed: %s") % failure.getErrorMessage())
             return defer.succeed({'xmlui': error_ui.toXml()})
 
@@ -308,8 +339,10 @@
         """Ask for a confirmation before deleting the XMPP account and SàT profile
         @param profile
         """
-        form_ui = xml_tools.XMLUI("form", title=D_("Delete your account ?"), submit_id=self.__delete_account_id)
+        form_ui = xml_tools.XMLUI("form", title=D_("Delete your account?"), submit_id=self.__delete_account_id)
         form_ui.addText(D_("If you confirm this dialog, you will be disconnected and then your XMPP account AND your SàT profile will both be DELETED."))
+        target = D_('contact list, messages history, blog posts and comments' if 'GROUPBLOG' in self.host.plugins else D_('contact list and messages history'))
+        form_ui.addText(D_("All your data stored on %(server)s, including your %(target)s will be erased.") % {'server': self._getNewAccountDomain(), 'target': target})
         form_ui.addText(D_("There is no other confirmation dialog, this is the very last one! Are you sure?"))
         return {'xmlui': form_ui.toXml()}
 
@@ -327,15 +360,77 @@
             for jid_ in self.host.memory.getWaitingSub(profile):  # delete waiting subscriptions
                 self.host.memory.delWaitingSub(jid_)
 
-            self.host.disconnect(profile)
-            self.host.memory.asyncDeleteProfile(profile, force=True)
+            delete_profile = lambda: self.host.memory.asyncDeleteProfile(profile, force=True)
+            if 'GROUPBLOG' in self.host.plugins:
+                d = self.host.plugins['GROUPBLOG'].deleteAllGroupBlogsAndComments(profile_key=profile)
+                d.addCallback(lambda dummy: delete_profile())
+            else:
+                delete_profile()
+
             return defer.succeed({})
 
         def errback(failure):
-            error_ui = xml_tools.XMLUI("popup", title="Error")
+            error_ui = xml_tools.XMLUI("popup", title=D_("Error"))
             error_ui.addText(D_("Your XMPP account could not be deleted: %s") % failure.getErrorMessage())
             return defer.succeed({'xmlui': error_ui.toXml()})
 
         d = ProsodyRegisterProtocol.prosodyctl(self, 'deluser', profile=profile)
         d.addCallbacks(userDeleted, errback)
         return d
+
+    def __deleteBlogPosts(self, posts, comments, profile):
+        """Ask for a confirmation before deleting the blog posts
+        @param posts: delete all posts of the user (and their comments)
+        @param comments: delete all the comments of the user on other's posts
+        @param data
+        @param profile
+        """
+        if posts:
+            if comments:  # delete everything
+                form_ui = xml_tools.XMLUI("form", title=D_("Delete all your (micro-)blog posts and comments?"), submit_id=self.__delete_posts_comments_id)
+                form_ui.addText(D_("If you confirm this dialog, all the (micro-)blog data you submitted will be erased."))
+                form_ui.addText(D_("These are the public and private posts and comments you sent to any group."))
+                form_ui.addText(D_("There is no other confirmation dialog, this is the very last one! Are you sure?"))
+            else:  # delete only the posts
+                form_ui = xml_tools.XMLUI("form", title=D_("Delete all your (micro-)blog posts?"), submit_id=self.__delete_posts_id)
+                form_ui.addText(D_("If you confirm this dialog, all the public and private posts you sent to any group will be erased."))
+                form_ui.addText(D_("There is no other confirmation dialog, this is the very last one! Are you sure?"))
+        elif comments:  # delete only the comments
+            form_ui = xml_tools.XMLUI("form", title=D_("Delete all your (micro-)blog comments?"), submit_id=self.__delete_comments_id)
+            form_ui.addText(D_("If you confirm this dialog, all the public and private comments you made on other people's posts will be erased."))
+            form_ui.addText(D_("There is no other confirmation dialog, this is the very last one! Are you sure?"))
+
+        return {'xmlui': form_ui.toXml()}
+
+    def __deleteBlogPostsCb(self, posts, comments, data, profile):
+        """Actually delete the XMPP account and SàT profile
+        @param posts: delete all posts of the user (and their comments)
+        @param comments: delete all the comments of the user on other's posts
+        @param profile
+        """
+        if posts:
+            if comments:
+                target = D_('blog posts and comments')
+                d = self.host.plugins['GROUPBLOG'].deleteAllGroupBlogsAndComments(profile_key=profile)
+            else:
+                target = D_('blog posts')
+                d = self.host.plugins['GROUPBLOG'].deleteAllGroupBlogs(profile_key=profile)
+        elif comments:
+            target = D_('comments')
+            d = self.host.plugins['GROUPBLOG'].deleteAllGroupBlogsComments(profile_key=profile)
+
+        def deleted(result):
+            ui = xml_tools.XMLUI("popup", title=D_("Deletion confirmation"))
+            # TODO: change the message when delete/retract notifications are done with XEP-0060
+            ui.addText(D_("Your %(target)s have been deleted.") % {'target': target})
+            ui.addText(D_("Known issue of the demo version: you need to refresh the page to make the deleted posts actually disappear."))
+            return defer.succeed({'xmlui': ui.toXml()})
+
+        def errback(failure):
+            error_ui = xml_tools.XMLUI("popup", title=D_("Error"))
+            error_ui.addText(D_("Your %(target)s could not be deleted: %(message)s") % {'target': target, 'message': failure.getErrorMessage()})
+            return defer.succeed({'xmlui': error_ui.toXml()})
+
+        d.addCallbacks(deleted, errback)
+        return d
+