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