]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
I think all the notice deletion calls are event-compatible now
authorMikael Nordfeldth <mmn@hethane.se>
Sat, 3 Oct 2015 10:26:09 +0000 (12:26 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Sat, 3 Oct 2015 10:26:09 +0000 (12:26 +0200)
This means we can handle DeleteNoticeAsProfile in plugins, such as
the ActivityModeration plugin.

actions/apistatusesdestroy.php
actions/apistatusesshow.php
actions/deletenotice.php
classes/Notice.php
lib/activitymover.php
plugins/ActivityModeration/ActivityModerationPlugin.php
plugins/ActivityModeration/classes/Deleted_notice.php
plugins/Event/actions/cancelrsvp.php
plugins/GNUsocialPhotos/actions/editphoto.php

index db41c87ad0f5cca2f443d4e247e53e5df1bf8959..2d32124c42d48bcd984f0423848f1b374cb05322 100644 (file)
@@ -124,7 +124,7 @@ class ApiStatusesDestroyAction extends ApiAuthAction
 
         if ($this->user->id == $this->notice->profile_id) {
             if (Event::handle('StartDeleteOwnNotice', array($this->user, $this->notice))) {
-                $this->notice->delete();
+                $this->notice->deleteAs($this->scoped);
                 Event::handle('EndDeleteOwnNotice', array($this->user, $this->notice));
             }
                $this->showNotice();
index 8b9cc347797bdfeb4b7bbff62def398e354edd2c..70b9a9c27a49427fc3ab6eed4ed5cea7bb92c9d5 100644 (file)
@@ -236,7 +236,7 @@ class ApiStatusesShowAction extends ApiPrivateAuthAction
         }
 
         if (Event::handle('StartDeleteOwnNotice', array($this->auth_user, $this->notice))) {
-            $this->notice->delete();
+            $this->notice->deleteAs($this->scoped);
             Event::handle('EndDeleteOwnNotice', array($this->auth_user, $this->notice));
         }
 
index 40b276a34895e664c2801fc4b741a876ae378360..f0aa767916f551caaf6e44cbe3104b012124ef03 100644 (file)
@@ -65,7 +65,7 @@ class DeletenoticeAction extends FormAction
     {
         if ($this->arg('yes')) {
             if (Event::handle('StartDeleteOwnNotice', array($this->scoped->getUser(), $this->notice))) {
-                $this->notice->delete();
+                $this->notice->deleteAs($this->scoped);
                 Event::handle('EndDeleteOwnNotice', array($this->scoped->getUser(), $this->notice));
             }
         } else {
index 737f186a107b6c7d0f63b18ca69acc4080747fdb..8f98abd306709a257ac8128e2ba340cb4dab2e48 100644 (file)
@@ -158,20 +158,14 @@ class Notice extends Managed_DataObject
         $this->_profile[$this->profile_id] = $profile;
     }
 
-    public function deleteAs(Profile $actor)
+    public function deleteAs(Profile $actor, $delete_event=true)
     {
-        if ($this->getProfile()->sameAs($actor) || $actor->hasRight(Right::DELETEOTHERSNOTICE)) {
-            return $this->delete();
+        if (!$this->getProfile()->sameAs($actor) && !$actor->hasRight(Right::DELETEOTHERSNOTICE)) {
+            throw new AuthorizationException(_('You are not allowed to delete another user\'s notice.'));
         }
-        throw new AuthorizationException(_('You are not allowed to delete other user\'s notices'));
-    }
 
-    public function delete($useWhere=false)
-    {
         if (Event::handle('NoticeDeleteRelated', array($this))) {
-
             // Clear related records
-
             $this->clearReplies();
             $this->clearLocation();
             $this->clearRepeats();
@@ -179,10 +173,21 @@ class Notice extends Managed_DataObject
             $this->clearGroupInboxes();
             $this->clearFiles();
             $this->clearAttentions();
-
             // NOTE: we don't clear queue items
         }
 
+        $result = null;
+        if (!$delete_event || Event::handle('DeleteNoticeAsProfile', array($this, $actor, &$result))) {
+            // If $delete_event is true, we run the event. If the Event then 
+            // returns false it is assumed everything was handled properly 
+            // and the notice was deleted.
+            $result = $this->delete();
+        }
+        return $result;
+    }
+
+    public function delete($useWhere=false)
+    {
         $result = parent::delete($useWhere);
 
         $this->blowOnDelete();
index ac828d94912f940a56fa1d9587a104d2bf02cf34..74c5c68ad688d8d824394f2d414e66ae2e0dc72c 100644 (file)
@@ -114,7 +114,7 @@ class ActivityMover extends QueueHandler
             $sink->postActivity($act);
             $notice = Notice::getKV('uri', $act->objects[0]->id);
             if (!empty($notice)) {
-                $notice->delete();
+                $notice->deleteAs($user->getProfile(), false);
             }
             break;
         case ActivityVerb::JOIN:
index 14948339928254903cbc9434ec1018a5b33c0a07..4422310e58af1abb680c3eccfd7f76664bea1003 100644 (file)
@@ -59,13 +59,21 @@ class ActivityModerationPlugin extends ActivityVerbHandlerPlugin
 
     public function deleteRelated(Notice $notice)
     {
-        if ($notice->getProfile()->hasRole(Profile_role::DELETED)) {
-            // Don't save a new Deleted_notice entry if the profile is being deleted
+        // pass
+    }
+
+    public function onDeleteNoticeAsProfile(Notice $stored, Profile $actor, &$result) {
+        // By adding a new 'delete' verb we will eventually trigger $this->saveObjectFromActivity
+        if (false === Deleted_notice::addNew($stored, $actor)) {
+            // false is returned if we did not have an error, but did not create the object
+            // (i.e. the author is currently being deleted)
             return true;
         }
 
-        // For auditing purposes, save a record that the notice was deleted.
-        return Deleted_notice::addNew($notice);
+        // We return false (to stop the event) if the deleted_notice entry was 
+        // added, which means we have run $this->saveObjectFromActivity which 
+        // in turn has called the delete function of the notice.
+        return false;
     }
 
     /**
index c450c0eb6389f8bf52b6fddb3bf422d085a680d8..790e2ab38d6b3562e80738b6dfcc16d0257e312d 100644 (file)
@@ -54,13 +54,15 @@ class Deleted_notice extends Managed_DataObject
         );
     }
 
-    public static function addNew(Notice $notice)
+    public static function addNew(Notice $notice, Profile $actor=null)
     {
-        $actor = $notice->getProfile();
+        if (is_null($actor)) {
+            $actor = $notice->getProfile();
+        }
 
-        if ($actor->hasRole(Profile_role::DELETED)) {
-            // Don't emit notices if the user is deleted
-            return true;
+        if ($notice->getProfile()->hasRole(Profile_role::DELETED)) {
+            // Don't emit notices if the notice author is (being) deleted
+            return false;
         }
 
         $act = new Activity();
@@ -80,7 +82,7 @@ class Deleted_notice extends Managed_DataObject
                            );
 
         $act->actor = $actor->asActivityObject();
-        $act->target = new ActivityObject();
+        $act->target = new ActivityObject();    // We don't save the notice object, as it's supposed to be removed!
         $act->target->id = $notice->getUri();
         $act->objects = array(clone($act->target));
 
index 1f8e6fde71a3fcbfb547a6b696606afa0f33d668..83f6a73ae07035f8cf6dd557ad555a0ac153dd4f 100644 (file)
@@ -138,7 +138,7 @@ class CancelrsvpAction extends Action
             // NB: this will delete the rsvp, too
             if (!empty($notice)) {
                 common_log(LOG_DEBUG, "Deleting notice...");
-                $notice->delete();
+                $notice->deleteAs($this->scoped);
             } else {
                 common_log(LOG_DEBUG, "Deleting RSVP alone...");
                 $this->rsvp->delete();
index 8f719919f970ffdf4255c603d368e740e7f257fa..35dac7bf3f6298d921d62f100ffdd8b7d6059e5b 100644 (file)
@@ -192,7 +192,7 @@ class EditphotoAction extends Action
         $this->photo->delete();
         
         if (Event::handle('StartDeleteOwnNotice', array($this->user, $notice))) {
-            $notice->delete();
+            $notice->deleteAs($this->scoped);
             Event::handle('EndDeleteOwnNotice', array($this->user, $notice));
         }
        $this->showForm(_('Success!'));