Exception throwing and proper db retrieval

ActivityModeration plugin and its Deleted_notice class.
This commit is contained in:
Mikael Nordfeldth 2015-12-31 12:41:30 +01:00
parent 6606781916
commit fab745c6d6
2 changed files with 22 additions and 14 deletions

View File

@ -149,22 +149,20 @@ class ActivityModerationPlugin extends ActivityVerbHandlerPlugin
common_debug('DELETENOTICE: Replacement notice has been prepared: '.var_export($stored, true)); common_debug('DELETENOTICE: Replacement notice has been prepared: '.var_export($stored, true));
// Let's see if this has been deleted already. // Let's see if this has been deleted already.
$deleted = Deleted_notice::getKV('uri', $stored->getUri()); try {
if ($deleted instanceof Deleted_notice) { $deleted = Deleted_notice::getByKeys( ['uri' => $stored->getUri()] );
return $deleted; return $deleted;
} } catch (NoResultException $e) {
$deleted = new Deleted_notice();
$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(); // throws exception on error
$deleted->profile_id = $actor->getID(); $result = $deleted->insert();
$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!');
} }
// Now we delete the original notice, leaving the id and uri free. // Now we delete the original notice, leaving the id and uri free.

View File

@ -96,7 +96,7 @@ class Deleted_notice extends Managed_DataObject
static public function fromStored(Notice $stored) static public function fromStored(Notice $stored)
{ {
$class = get_called_class(); $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 // 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..."; 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');
}
}
} }