dac617d95a
This means we can handle DeleteNoticeAsProfile in plugins, such as the ActivityModeration plugin.
127 lines
4.0 KiB
PHP
127 lines
4.0 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @package Activity
|
|
* @maintainer Mikael Nordfeldth <mmn@hethane.se>
|
|
*/
|
|
class ActivityModerationPlugin extends ActivityVerbHandlerPlugin
|
|
{
|
|
public function tag()
|
|
{
|
|
return 'actmod';
|
|
}
|
|
|
|
public function types()
|
|
{
|
|
return array();
|
|
}
|
|
|
|
public function verbs()
|
|
{
|
|
return array(ActivityVerb::DELETE);
|
|
}
|
|
|
|
public function onBeforePluginCheckSchema()
|
|
{
|
|
Deleted_notice::beforeSchemaUpdate();
|
|
return true;
|
|
}
|
|
|
|
public function onCheckSchema()
|
|
{
|
|
$schema = Schema::get();
|
|
$schema->ensureTable('deleted_notice', Deleted_notice::schemaDef());
|
|
return true;
|
|
}
|
|
|
|
protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
|
|
{
|
|
// FIXME: switch based on action type
|
|
return _m('TITLE', 'Notice moderation');
|
|
}
|
|
|
|
protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
|
|
{
|
|
// pass
|
|
}
|
|
|
|
protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
|
|
{
|
|
switch (true) {
|
|
case ActivityUtils::compareVerbs($verb, array(ActivityVerb::DELETE)):
|
|
// do whatever preparation is necessary to delete a verb
|
|
$target->delete();
|
|
break;
|
|
default:
|
|
throw new ServerException('ActivityVerb POST not handled by plugin that was supposed to do it.');
|
|
}
|
|
}
|
|
|
|
public function deleteRelated(Notice $notice)
|
|
{
|
|
// 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;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
/**
|
|
* This is run when a 'delete' verb activity comes in.
|
|
*
|
|
* @return boolean hook flag
|
|
*/
|
|
protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
|
|
{
|
|
// Let's see if this has been deleted already.
|
|
$deleted = Deleted_notice::getKV('uri', $act->id);
|
|
if ($deleted instanceof Deleted_notice) {
|
|
return $deleted;
|
|
}
|
|
|
|
common_debug('DELETING notice: ' . $act->objects[0]->id);
|
|
$target = Notice::getByUri($act->objects[0]->id);
|
|
|
|
$deleted = new Deleted_notice();
|
|
|
|
$deleted->id = $target->getID();
|
|
$deleted->profile_id = $target->getProfile()->getID();
|
|
$deleted->uri = Deleted_notice::newUri($target->getProfile(), $target);
|
|
$deleted->act_uri = $target->getUri();
|
|
$deleted->act_created = $target->created;
|
|
$deleted->created = common_sql_now();
|
|
|
|
common_debug('DELETING notice, storing Deleted_notice entry');
|
|
$deleted->insert();
|
|
|
|
common_debug('DELETING notice, actually deleting now!');
|
|
$target->delete();
|
|
|
|
return $deleted;
|
|
}
|
|
|
|
public function activityObjectFromNotice(Notice $notice)
|
|
{
|
|
$object = Deleted_notice::fromStored($notice);
|
|
return $object->asActivityObject();
|
|
}
|
|
|
|
protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
|
|
{
|
|
if (!$scoped instanceof Profile || !($scoped->sameAs($target->getProfile()) || $scoped->hasRight(Right::DELETEOTHERSNOTICE))) {
|
|
throw new AuthorizationException(_('You are not allowed to delete other user\'s notices'));
|
|
}
|
|
return DeletenoticeForm($action, array('notice'=>$target));
|
|
}
|
|
}
|