]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ActivityModeration/ActivityModerationPlugin.php
dc21b115526e7556c08102409c38736e241ab317
[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         // Let's see if this has been deleted already.
87         $deleted = Deleted_notice::getKV('uri', $act->id);
88         if ($deleted instanceof Deleted_notice) {
89             return $deleted;
90         }
91
92         $target = Notice::getByUri($act->objects[0]->id);
93         common_debug('DELETING notice: ' . $act->objects[0]->id . ' on behalf of profile id==' . $target->getProfile()->getID());
94
95         $deleted = new Deleted_notice();
96
97         $deleted->id            = $target->getID();
98         $deleted->profile_id    = $target->getProfile()->getID();
99         $deleted->uri           = $act->id;
100         $deleted->act_uri       = $target->getUri();
101         $deleted->act_created   = $target->created;
102         $deleted->created       = common_sql_now();
103
104         $result = $deleted->insert();
105         if ($result === false) {
106             throw new ServerException('Could not insert Deleted_notice entry into database!');
107         }
108
109         common_debug('DELETING notice, actually deleting now!');
110         $target->delete();
111
112         return $deleted;
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     public function extendActivity(Notice $stored, Activity $act, Profile $scoped=null)
130     {
131         Deleted_notice::extendActivity($stored, $act, $scoped);
132     }
133
134     public function activityObjectFromNotice(Notice $notice)
135     {
136         $object = Deleted_notice::fromStored($notice);
137         return $object->asActivityObject();
138     }
139
140     protected function getActivityForm(ManagedAction $action, $verb, Notice $target, Profile $scoped) 
141     { 
142         if (!$scoped instanceof Profile || !($scoped->sameAs($target->getProfile()) || $scoped->hasRight(Right::DELETEOTHERSNOTICE))) {
143             throw new AuthorizationException(_('You are not allowed to delete other user\'s notices'));
144         }
145         return DeletenoticeForm($action, array('notice'=>$target));
146     } 
147 }