5 * @maintainer Mikael Nordfeldth <mmn@hethane.se>
7 class ActivityModerationPlugin extends ActivityVerbHandlerPlugin
14 public function types()
19 public function verbs()
21 return array(ActivityVerb::DELETE);
24 public function onBeforePluginCheckSchema()
26 Deleted_notice::beforeSchemaUpdate();
30 public function onCheckSchema()
32 $schema = Schema::get();
33 $schema->ensureTable('deleted_notice', Deleted_notice::schemaDef());
37 public function onGetNoticeSqlTimestamp($id, &$timestamp)
40 $deleted = Deleted_notice::getByID($id);
41 $timestamp = $deleted->act_created;
42 } catch (NoResultException $e) {
45 // we're done for the event, so return false to stop it
49 public function onIsNoticeDeleted($id, &$deleted)
52 $found = Deleted_notice::getByID($id);
53 $deleted = ($found instanceof Deleted_notice);
54 } catch (NoResultException $e) {
57 // return true (continue event) if $deleted is false, return false (stop event) if deleted notice was found
61 protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
63 // FIXME: switch based on action type
64 return _m('TITLE', 'Notice moderation');
67 protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
72 protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
75 case ActivityUtils::compareVerbs($verb, array(ActivityVerb::DELETE)):
76 // do whatever preparation is necessary to delete a verb
80 throw new ServerException('ActivityVerb POST not handled by plugin that was supposed to do it.');
84 public function deleteRelated(Notice $notice)
89 public function onDeleteNoticeAsProfile(Notice $stored, Profile $actor, &$result) {
90 // By adding a new 'delete' verb we will eventually trigger $this->saveObjectFromActivity
91 if (false === Deleted_notice::addNew($stored, $actor)) {
92 // false is returned if we did not have an error, but did not create the object
93 // (i.e. the author is currently being deleted)
97 // We return false (to stop the event) if the deleted_notice entry was
98 // added, which means we have run $this->saveObjectFromActivity which
99 // in turn has called the delete function of the notice.
104 * This is run when a 'delete' verb activity comes in.
106 * @return boolean hook flag
108 protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
110 // Everything is done in the StartNoticeSave event
114 // FIXME: Put this in lib/activityhandlerplugin.php when we're ready
115 // with the other microapps/activityhandlers as well.
116 // Also it should be StartNoticeAsActivity (with a prepped Activity, including ->context etc.)
117 public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
119 if (!$this->isMyNotice($stored)) {
123 common_debug('Extending activity '.$stored->id.' with '.get_called_class());
124 $this->extendActivity($stored, $act, $scoped);
129 * This is run before ->insert, so our task in this function is just to
130 * delete if it is the delete verb.
132 public function onStartNoticeSave(Notice $stored)
134 // DELETE is a bit special, we have to remove the existing entry and then
135 // add a new one with the same URI in order to trigger the distribution.
136 // (that's why we don't use $this->isMyNotice(...))
137 if (!ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::DELETE))) {
142 $target = Notice::getByUri($stored->uri);
143 } catch (NoResultException $e) {
144 throw new AlreadyFulfilledException('Notice URI not found, so we have nothing to delete.');
147 $actor = $stored->getProfile();
148 $owner = $target->getProfile();
150 if ($owner->hasRole(Profile_role::DELETED)) {
151 // Don't bother with replacing notices if its author is being deleted.
152 // The later "StoreActivityObject" will pick this up and execute
153 // the deletion then.
154 // (the "delete verb notice" is too new to ever pass through Notice::saveNew
155 // which otherwise wouldn't execute the StoreActivityObject event)
159 // Since the user deleting may not be the same as the notice's owner,
160 // double-check this and also set the "re-stored" notice profile_id.
161 if (!$actor->sameAs($owner) && !$actor->hasRight(Right::DELETEOTHERSNOTICE)) {
162 throw new AuthorizationException(_('You are not allowed to delete another user\'s notice.'));
165 // We copy the identifying fields and replace the sensitive ones.
166 //$stored->id = $target->id; // We can't copy this since DB_DataObject won't inject it anyway
167 $props = array('uri', 'profile_id', 'conversation', 'reply_to', 'created', 'repeat_of', 'object_type', 'is_local', 'scope');
168 foreach($props as $prop) {
169 $stored->$prop = $target->$prop;
172 // Let's see if this has been deleted already.
174 $deleted = Deleted_notice::getByKeys( ['uri' => $stored->getUri()] );
176 } catch (NoResultException $e) {
177 $deleted = new Deleted_notice();
179 $deleted->id = $target->getID();
180 $deleted->profile_id = $actor->getID();
181 $deleted->uri = $stored->getUri();
182 $deleted->act_created = $stored->created;
183 $deleted->created = common_sql_now();
185 // throws exception on error
186 $result = $deleted->insert();
189 // Now we delete the original notice, leaving the id and uri free.
195 public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
197 Deleted_notice::extendActivity($stored, $act, $scoped);
200 public function activityObjectFromNotice(Notice $notice)
202 $object = Deleted_notice::fromStored($notice);
203 return $object->asActivityObject();
206 protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped)
208 if (!$scoped instanceof Profile || !($scoped->sameAs($target->getProfile()) || $scoped->hasRight(Right::DELETEOTHERSNOTICE))) {
209 throw new AuthorizationException(_('You are not allowed to delete other user\'s notices'));
211 return DeletenoticeForm($action, array('notice'=>$target));