]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/microappplugin.php
made the input-form switcher work, kinda
[quix0rs-gnu-social.git] / lib / microappplugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Superclass for microapp plugin
7  * 
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Microapp
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Superclass for microapp plugins
39  *
40  * This class lets you define micro-applications with different kinds of activities.
41  *
42  * The applications work more-or-less like other 
43  * 
44  * @category  Microapp
45  * @package   StatusNet
46  * @author    Evan Prodromou <evan@status.net>
47  * @copyright 2011 StatusNet, Inc.
48  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
49  * @link      http://status.net/
50  */
51
52 abstract class MicroAppPlugin extends Plugin
53 {
54     abstract function appTitle();
55     abstract function tag();
56     abstract function types();
57     abstract function saveNoticeFromActivity($activity, $actor, $options);
58     abstract function activityObjectFromNotice($notice);
59     abstract function showNotice($notice, $out);
60     abstract function entryForm($out);
61     abstract function deleteRelated($notice);
62
63     function isMyNotice($notice) {
64         $types = $this->types();
65         return in_array($notice->object_type, $types);
66     }
67
68     function isMyActivity($activity) {
69         $types = $this->types();
70         return (count($activity->objects) == 1 &&
71                 in_array($activity->objects[0]->type, $types));
72     }
73
74     /**
75      * When a notice is deleted, delete the related objects
76      *
77      * @param Notice $notice Notice being deleted
78      * 
79      * @return boolean hook value
80      */
81
82     function onNoticeDeleteRelated($notice)
83     {
84         if ($this->isMyNotice($notice)) {
85             $this->deleteRelated($notice);
86         }
87
88         return true;
89     }
90
91     /**
92      * Output the HTML for this kind of object in a list
93      *
94      * @param NoticeListItem $nli The list item being shown.
95      *
96      * @return boolean hook value
97      */
98
99     function onStartShowNoticeItem($nli)
100     {
101         if (!$this->isMyNotice($nli->notice)) {
102             return true;
103         }
104
105         $out = $nli->out;
106
107         $this->showNotice($notice, $out);
108
109         $nli->showNoticeLink();
110         $nli->showNoticeSource();
111         $nli->showNoticeLocation();
112         $nli->showContext();
113         $nli->showRepeat();
114         
115         $out->elementEnd('div');
116         
117         $nli->showNoticeOptions();
118
119         return false;
120     }
121
122     /**
123      * Render a notice as one of our objects
124      *
125      * @param Notice         $notice  Notice to render
126      * @param ActivityObject &$object Empty object to fill
127      *
128      * @return boolean hook value
129      */
130      
131     function onStartActivityObjectFromNotice($notice, &$object)
132     {
133         if ($this->isMyNotice($notice)) {
134             $object = $this->activityObjectFromNotice($notice);
135             return false;
136         }
137
138         return true;
139     }
140
141     /**
142      * Handle a posted object from PuSH
143      *
144      * @param Activity        $activity activity to handle
145      * @param Ostatus_profile $oprofile Profile for the feed
146      *
147      * @return boolean hook value
148      */
149
150     function onStartHandleFeedEntryWithProfile($activity, $oprofile)
151     {
152         if ($this->isMyActivity($activity)) {
153
154             $actor = $oprofile->checkAuthorship($activity);
155
156             if (empty($actor)) {
157                 throw new ClientException(_('Can\'t get author for activity.'));
158             }
159
160             $object = $activity->objects[0];
161
162             $options = array('uri' => $object->id,
163                              'url' => $object->link,
164                              'is_local' => Notice::REMOTE_OMB,
165                              'source' => 'ostatus');
166             
167             $this->saveNoticeFromActivity($activity, $actor);
168
169             return false;
170         }
171
172         return true;
173     }
174
175     /**
176      * Handle a posted object from Salmon
177      *
178      * @param Activity $activity activity to handle
179      * @param mixed    $target   user or group targeted
180      *
181      * @return boolean hook value
182      */
183
184     function onStartHandleSalmonTarget($activity, $target)
185     {
186         if ($this->isMyActivity($activity)) {
187
188             $this->log(LOG_INFO, "Checking {$activity->id} as a valid Salmon slap.");
189
190             if ($target instanceof User_group) {
191                 $uri = $target->getUri();
192                 if (!in_array($uri, $activity->context->attention)) {
193                     throw new ClientException(_("Bookmark not posted ".
194                                                 "to this group."));
195                 }
196             } else if ($target instanceof User) {
197                 $uri      = $target->uri;
198                 $original = null;
199                 if (!empty($activity->context->replyToID)) {
200                     $original = Notice::staticGet('uri', 
201                                                   $activity->context->replyToID); 
202                 }
203                 if (!in_array($uri, $activity->context->attention) &&
204                     (empty($original) ||
205                      $original->profile_id != $target->id)) {
206                     throw new ClientException(_("Object not posted ".
207                                                 "to this user."));
208                 }
209             } else {
210                 throw new ServerException(_("Don't know how to handle ".
211                                             "this kind of target."));
212             }
213
214             $actor = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
215
216             $object = $activity->objects[0];
217
218             $options = array('uri' => $object->id,
219                              'url' => $object->link,
220                              'is_local' => Notice::REMOTE_OMB,
221                              'source' => 'ostatus');
222
223             $this->saveNoticeFromActivity($activity, $actor, $options);
224
225             return false;
226         }
227
228         return true;
229     }
230
231     /**
232      * Handle object posted via AtomPub
233      *
234      * @param Activity &$activity Activity that was posted
235      * @param User     $user      User that posted it
236      * @param Notice   &$notice   Resulting notice
237      *
238      * @return boolean hook value
239      */
240
241     function onStartAtomPubNewActivity(&$activity, $user, &$notice)
242     {
243         if ($this->isMyActivity($activity)) {
244
245             $options = array('source' => 'atompub');
246
247             $this->saveNoticeFromActivity($activity,
248                                           $user->getProfile(),
249                                           $options);
250
251             return false;
252         }
253
254         return true;
255     }
256
257     /**
258      * Handle object imported from a backup file
259      *
260      * @param User           $user     User to import for
261      * @param ActivityObject $author   Original author per import file
262      * @param Activity       $activity Activity to import
263      * @param boolean        $trusted  Is this a trusted user?
264      * @param boolean        &$done    Is this done (success or unrecoverable error)
265      *
266      * @return boolean hook value
267      */
268
269     function onStartImportActivity($user, $author, $activity, $trusted, &$done)
270     {
271         if ($this->isMyActivity($activity)) {
272
273             $obj = $activity->objects[0];
274
275             $options = array('uri' => $object->id,
276                              'url' => $object->link,
277                              'source' => 'restore');
278
279             $saved = $this->saveNoticeFromActivity($activity,
280                                                    $user->getProfile(),
281                                                    $options);
282
283             if (!empty($saved)) {
284                 $done = true;
285             }
286
287             return false;
288         }
289
290         return true;
291     }
292
293     function onStartShowEntryForms(&$tabs)
294     {
295         $tabs[$this->tag()] = $this->appTitle();
296         return true;
297     }
298
299     function onStartMakeEntryForm($tag, $out, &$form)
300     {
301         $this->log(LOG_INFO, "onStartMakeEntryForm() called for tag '$tag'");
302
303         if ($tag == $this->tag()) {
304             $form = $this->entryForm($out);
305             return false;
306         }
307
308         return true;
309     }
310 }