]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivityModeration/ActivityModerationPlugin.php
Added type-hint for adaptNoticeListItem() method
[quix0rs-gnu-social.git] / plugins / ActivityModeration / ActivityModerationPlugin.php
1 <?php
2
3 /**
4  * @package     Activity
5  * @maintainer  Mikael Nordfeldth <mmn@hethane.se>
6  */
7 class ActivityModerationPlugin extends ActivityVerbHandlerPlugin
8 {
9     public function tag()
10     {
11         return 'actmod';
12     }
13
14     public function types()
15     {
16         return array();
17     }
18
19     public function verbs()
20     {
21         return array(ActivityVerb::DELETE);
22     }
23
24     public function onBeforePluginCheckSchema()
25     {
26         Deleted_notice::beforeSchemaUpdate();
27         return true;
28     }
29
30     public function onCheckSchema()
31     {
32         $schema = Schema::get();
33         $schema->ensureTable('deleted_notice', Deleted_notice::schemaDef());
34         return true;
35     }
36
37     protected function getActionTitle(ManagedAction $action, $verb, Notice $target, Profile $scoped)
38     {
39         // FIXME: switch based on action type
40         return _m('TITLE', 'Notice moderation');
41     }
42
43     protected function doActionPreparation(ManagedAction $action, $verb, Notice $target, Profile $scoped)
44     {
45         // pass
46     }
47
48     protected function doActionPost(ManagedAction $action, $verb, Notice $target, Profile $scoped)
49     {
50         switch (true) {
51         case ActivityUtils::compareVerbs($verb, array(ActivityVerb::DELETE)):
52             // do whatever preparation is necessary to delete a verb
53             $target->delete();
54             break;
55         default:
56             throw new ServerException('ActivityVerb POST not handled by plugin that was supposed to do it.');
57         }
58     }
59
60     public function deleteRelated(Notice $notice)
61     {
62         // pass
63     }
64
65     public function onDeleteNoticeAsProfile(Notice $stored, Profile $actor, &$result) {
66         // By adding a new 'delete' verb we will eventually trigger $this->saveObjectFromActivity
67         if (false === Deleted_notice::addNew($stored, $actor)) {
68             // false is returned if we did not have an error, but did not create the object
69             // (i.e. the author is currently being deleted)
70             return true;
71         }
72
73         // We return false (to stop the event) if the deleted_notice entry was 
74         // added, which means we have run $this->saveObjectFromActivity which 
75         // in turn has called the delete function of the notice.
76         return false;
77     }
78
79     /**
80      * This is run when a 'delete' verb activity comes in.
81      *
82      * @return boolean hook flag
83      */
84     protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
85     {
86         // Everything is done in the StartNoticeSave event
87         return true;
88     }
89
90     // FIXME: Put this in lib/activityhandlerplugin.php when we're ready
91     //          with the other microapps/activityhandlers as well.
92     //          Also it should be StartNoticeAsActivity (with a prepped Activity, including ->context etc.)
93     public function onEndNoticeAsActivity(Notice $stored, Activity $act, Profile $scoped=null)
94     {
95         if (!$this->isMyNotice($stored)) {
96             return true;
97         }
98
99         common_debug('Extending activity '.$stored->id.' with '.get_called_class());
100         $this->extendActivity($stored, $act, $scoped);
101         return false;
102     }
103
104     /**
105      * This is run before ->insert, so our task in this function is just to
106      * delete if it is the delete verb.
107      */ 
108     public function onStartNoticeSave(Notice $stored)
109     {
110         // DELETE is a bit special, we have to remove the existing entry and then
111         // add a new one with the same URI in order to trigger the distribution.
112         // (that's why we don't use $this->isMyNotice(...))
113         if (!ActivityUtils::compareVerbs($stored->verb, array(ActivityVerb::DELETE))) {
114             return true;
115         }
116
117         try {
118             $target = Notice::getByUri($stored->uri);
119         } catch (NoResultException $e) {
120             throw new AlreadyFulfilledException('Notice URI not found, so we have nothing to delete.');
121         }
122
123         $actor = $stored->getProfile();
124         $owner = $target->getProfile();
125
126         if ($owner->hasRole(Profile_role::DELETED)) {
127             // Don't bother with replacing notices if its author is being deleted.
128             // The later "StoreActivityObject" will pick this up and execute
129             // the deletion then.
130             // (the "delete verb notice" is too new to ever pass through Notice::saveNew
131             // which otherwise wouldn't execute the StoreActivityObject event)
132             return true;
133         }
134
135         // Since the user deleting may not be the same as the notice's owner,
136         // double-check this and also set the "re-stored" notice profile_id.
137         if (!$actor->sameAs($owner) && !$actor->hasRight(Right::DELETEOTHERSNOTICE)) {
138             throw new AuthorizationException(_('You are not allowed to delete another user\'s notice.'));
139         }
140
141         // We copy the identifying fields and replace the sensitive ones.
142         //$stored->id = $target->id;    // We can't copy this since DB_DataObject won't inject it anyway
143         $props = array('uri', 'profile_id', 'conversation', 'reply_to', 'created', 'repeat_of', 'object_type', 'is_local', 'scope');
144         foreach($props as $prop) {
145             $stored->$prop = $target->$prop;
146         }
147
148         // Let's see if this has been deleted already.
149         try {
150             $deleted = Deleted_notice::getByKeys( ['uri' => $stored->getUri()] );
151             return $deleted;
152         } catch (NoResultException $e) {
153             $deleted = new Deleted_notice();
154
155             $deleted->id            = $target->getID();
156             $deleted->profile_id    = $actor->getID();
157             $deleted->uri           = $stored->getUri();
158             $deleted->act_created   = $stored->created;
159             $deleted->created       = common_sql_now();
160
161             // throws exception on error
162             $result = $deleted->insert();
163         }
164
165         // Now we delete the original notice, leaving the id and uri free.
166         $target->delete();
167
168         return true;
169     }
170
171     public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
172     {
173         Deleted_notice::extendActivity($stored, $act, $scoped);
174     }
175
176     public function activityObjectFromNotice(Notice $notice)
177     {
178         $object = Deleted_notice::fromStored($notice);
179         return $object->asActivityObject();
180     }
181
182     protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped) 
183     { 
184         if (!$scoped instanceof Profile || !($scoped->sameAs($target->getProfile()) || $scoped->hasRight(Right::DELETEOTHERSNOTICE))) {
185             throw new AuthorizationException(_('You are not allowed to delete other user\'s notices'));
186         }
187         return DeletenoticeForm($action, array('notice'=>$target));
188     } 
189 }