From fab745c6d677ac1d937eda9deb871e006557d884 Mon Sep 17 00:00:00 2001
From: Mikael Nordfeldth <mmn@hethane.se>
Date: Thu, 31 Dec 2015 12:41:30 +0100
Subject: [PATCH] Exception throwing and proper db retrieval

ActivityModeration plugin and its Deleted_notice class.
---
 .../ActivityModerationPlugin.php              | 24 +++++++++----------
 .../classes/Deleted_notice.php                | 12 +++++++++-
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/plugins/ActivityModeration/ActivityModerationPlugin.php b/plugins/ActivityModeration/ActivityModerationPlugin.php
index 8a4f694df0..22384ba06b 100644
--- a/plugins/ActivityModeration/ActivityModerationPlugin.php
+++ b/plugins/ActivityModeration/ActivityModerationPlugin.php
@@ -149,22 +149,20 @@ class ActivityModerationPlugin extends ActivityVerbHandlerPlugin
         common_debug('DELETENOTICE: Replacement notice has been prepared: '.var_export($stored, true));
 
         // Let's see if this has been deleted already.
-        $deleted = Deleted_notice::getKV('uri', $stored->getUri());
-        if ($deleted instanceof Deleted_notice) {
+        try {
+            $deleted = Deleted_notice::getByKeys( ['uri' => $stored->getUri()] );
             return $deleted;
-        }
-
-        $deleted = new Deleted_notice();
+        } catch (NoResultException $e) {
+            $deleted = new Deleted_notice();
 
-        $deleted->id            = $target->getID();
-        $deleted->profile_id    = $actor->getID();
-        $deleted->uri           = $stored->getUri();
-        $deleted->act_created   = $stored->created;
-        $deleted->created       = common_sql_now();
+            $deleted->id            = $target->getID();
+            $deleted->profile_id    = $actor->getID();
+            $deleted->uri           = $stored->getUri();
+            $deleted->act_created   = $stored->created;
+            $deleted->created       = common_sql_now();
 
-        $result = $deleted->insert();
-        if ($result === false) {
-            throw new ServerException('Could not insert Deleted_notice entry into database!');
+            // throws exception on error
+            $result = $deleted->insert();
         }
 
         // Now we delete the original notice, leaving the id and uri free.
diff --git a/plugins/ActivityModeration/classes/Deleted_notice.php b/plugins/ActivityModeration/classes/Deleted_notice.php
index f834f88bb7..76674d9bec 100644
--- a/plugins/ActivityModeration/classes/Deleted_notice.php
+++ b/plugins/ActivityModeration/classes/Deleted_notice.php
@@ -96,7 +96,7 @@ class Deleted_notice extends Managed_DataObject
     static public function fromStored(Notice $stored)
     {
         $class = get_called_class();
-        return self::getByPK(array('uri' => $stored->getUri()));
+        return self::getByKeys( ['uri' => $stored->getUri()] );
     }
 
     // The one who deleted the notice, not the notice's author
@@ -206,4 +206,14 @@ class Deleted_notice extends Managed_DataObject
         print "Resuming core schema upgrade...";
     }
 
+    function insert()
+    {
+        $result = parent::insert();
+
+        if ($result === false) {
+            common_log_db_error($this, 'INSERT', __FILE__);
+            // TRANS: Server exception thrown when a stored object entry cannot be saved.
+            throw new ServerException('Could not save Deleted_notice');
+        }
+    }
 }
-- 
2.39.5