]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivityModeration/ActivityModerationPlugin.php
14948339928254903cbc9434ec1018a5b33c0a07
[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         if ($notice->getProfile()->hasRole(Profile_role::DELETED)) {
63             // Don't save a new Deleted_notice entry if the profile is being deleted
64             return true;
65         }
66
67         // For auditing purposes, save a record that the notice was deleted.
68         return Deleted_notice::addNew($notice);
69     }
70
71     /**
72      * This is run when a 'delete' verb activity comes in.
73      *
74      * @return boolean hook flag
75      */
76     protected function saveObjectFromActivity(Activity $act, Notice $stored, array $options=array())
77     {
78         // Let's see if this has been deleted already.
79         $deleted = Deleted_notice::getKV('uri', $act->id);
80         if ($deleted instanceof Deleted_notice) {
81             return $deleted;
82         }
83
84         common_debug('DELETING notice: ' . $act->objects[0]->id);
85         $target = Notice::getByUri($act->objects[0]->id);
86
87         $deleted = new Deleted_notice();
88
89         $deleted->id            = $target->getID();
90         $deleted->profile_id    = $target->getProfile()->getID();
91         $deleted->uri           = Deleted_notice::newUri($target->getProfile(), $target);
92         $deleted->act_uri       = $target->getUri();
93         $deleted->act_created   = $target->created;
94         $deleted->created       = common_sql_now();
95
96         common_debug('DELETING notice, storing Deleted_notice entry');
97         $deleted->insert();
98
99         common_debug('DELETING notice, actually deleting now!');
100         $target->delete();
101
102         return $deleted;
103     }
104
105     public function activityObjectFromNotice(Notice $notice)
106     {
107         $object = Deleted_notice::fromStored($notice);
108         return $object->asActivityObject();
109     }
110
111     protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped) 
112     { 
113         if (!$scoped instanceof Profile || !($scoped->sameAs($target->getProfile()) || $scoped->hasRight(Right::DELETEOTHERSNOTICE))) {
114             throw new AuthorizationException(_('You are not allowed to delete other user\'s notices'));
115         }
116         return DeletenoticeForm($action, array('notice'=>$target));
117     } 
118 }