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