3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Superclass for microapp plugin
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Superclass for microapp plugins
40 * This class lets you define micro-applications with different kinds of activities.
42 * The applications work more-or-less like other
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/
52 abstract class MicroAppPlugin extends Plugin
55 * Returns a localized string which represents this micro-app,
56 * to be shown to users selecting what type of post to make.
57 * This is paired with the key string in $this->tag().
59 * All micro-app classes must override this method.
63 abstract function appTitle();
66 * Returns a key string which represents this micro-app in HTML
67 * ids etc, as when offering selection of what type of post to make.
68 * This is paired with the user-visible localizable $this->appTitle().
70 * All micro-app classes must override this method.
72 abstract function tag();
75 * Return a list of ActivityStreams object type URIs
76 * which this micro-app handles. Default implementations
77 * of the base class will use this list to check if a
78 * given ActivityStreams object belongs to us, via
79 * $this->isMyNotice() or $this->isMyActivity.
81 * All micro-app classes must override this method.
83 * @fixme can we confirm that these types are the same
84 * for Atom and JSON streams? Any limitations or issues?
86 * @return array of strings
88 abstract function types();
91 * Given a parsed ActivityStreams activity, your plugin
92 * gets to figure out how to actually save it into a notice
93 * and any additional data structures you require.
95 * This will handle things received via AtomPub, OStatus
96 * (PuSH and Salmon transports), or ActivityStreams-based
97 * backup/restore of account data.
99 * You should be able to accept as input the output from your
100 * $this->activityObjectFromNotice(). Where applicable, try to
101 * use existing ActivityStreams structures and object types,
102 * and be liberal in accepting input from what might be other
105 * All micro-app classes must override this method.
107 * @fixme are there any standard options?
109 * @param Activity $activity
110 * @param Profile $actor
111 * @param array $options=array()
113 * @return Notice the resulting notice
115 abstract function saveNoticeFromActivity($activity, $actor, $options=array());
118 * Given an existing Notice object, your plugin gets to
119 * figure out how to arrange it into an ActivityStreams
122 * This will be how your specialized notice gets output in
123 * Atom feeds and JSON-based ActivityStreams output, including
124 * account backup/restore and OStatus (PuSH and Salmon transports).
126 * You should be able to round-trip data from this format back
127 * through $this->saveNoticeFromActivity(). Where applicable, try
128 * to use existing ActivityStreams structures and object types,
129 * and consider interop with other compatible apps.
131 * All micro-app classes must override this method.
133 * @fixme this outputs an ActivityObject, not an Activity. Any compat issues?
135 * @param Notice $notice
137 * @return ActivityObject
139 abstract function activityObjectFromNotice($notice);
142 * Custom HTML output for your special notice; called when a
143 * matching notice turns up in a NoticeListItem.
145 * All micro-app classes must override this method.
147 * @param Notice $notice
148 * @param HTMLOutputter $out
150 abstract function showNotice($notice, $out);
153 * When building the primary notice form, we'll fetch also some
154 * alternate forms for specialized types -- that's you!
156 * Return a custom Widget or Form object for the given output
157 * object, and it'll be included in the HTML output. Beware that
158 * your form may be initially hidden.
160 * All micro-app classes must override this method.
162 * @param HTMLOutputter $out
165 abstract function entryForm($out);
168 * When a notice is deleted, you'll be called here for a chance
169 * to clean up any related resources.
171 * All micro-app classes must override this method.
173 * @param Notice $notice
175 abstract function deleteRelated($notice);
178 * Check if a given notice object should be handled by this micro-app
181 * The default implementation checks against the activity type list
182 * returned by $this->types(). You can override this method to expand
185 * @param Notice $notice
188 function isMyNotice($notice) {
189 $types = $this->types();
190 return in_array($notice->object_type, $types);
194 * Check if a given ActivityStreams activity should be handled by this
197 * The default implementation checks against the activity type list
198 * returned by $this->types(), and requires that exactly one matching
199 * object be present. You can override this method to expand
200 * your checks or to compare the activity's verb, etc.
202 * @param Activity $activity
205 function isMyActivity($activity) {
206 $types = $this->types();
207 return (count($activity->objects) == 1 &&
208 in_array($activity->objects[0]->type, $types));
212 * When a notice is deleted, delete the related objects
213 * by calling the overridable $this->deleteRelated().
215 * @param Notice $notice Notice being deleted
217 * @return boolean hook value
220 function onNoticeDeleteRelated($notice)
222 if ($this->isMyNotice($notice)) {
223 $this->deleteRelated($notice);
230 * Output the HTML for this kind of object in a list
232 * @param NoticeListItem $nli The list item being shown.
234 * @return boolean hook value
237 function onStartShowNoticeItem($nli)
239 if (!$this->isMyNotice($nli->notice)) {
244 $notice = $nli->notice;
246 $this->showNotice($notice, $out);
248 $nli->showNoticeLink();
249 $nli->showNoticeSource();
250 $nli->showNoticeLocation();
254 $out->elementEnd('div');
256 $nli->showNoticeOptions();
262 * Render a notice as one of our objects
264 * @param Notice $notice Notice to render
265 * @param ActivityObject &$object Empty object to fill
267 * @return boolean hook value
270 function onStartActivityObjectFromNotice($notice, &$object)
272 if ($this->isMyNotice($notice)) {
273 $object = $this->activityObjectFromNotice($notice);
281 * Handle a posted object from PuSH
283 * @param Activity $activity activity to handle
284 * @param Ostatus_profile $oprofile Profile for the feed
286 * @return boolean hook value
289 function onStartHandleFeedEntryWithProfile($activity, $oprofile)
291 if ($this->isMyActivity($activity)) {
293 $actor = $oprofile->checkAuthorship($activity);
296 throw new ClientException(_('Can\'t get author for activity.'));
299 $object = $activity->objects[0];
301 $options = array('uri' => $object->id,
302 'url' => $object->link,
303 'is_local' => Notice::REMOTE_OMB,
304 'source' => 'ostatus');
306 $this->saveNoticeFromActivity($activity, $actor);
315 * Handle a posted object from Salmon
317 * @param Activity $activity activity to handle
318 * @param mixed $target user or group targeted
320 * @return boolean hook value
323 function onStartHandleSalmonTarget($activity, $target)
325 if ($this->isMyActivity($activity)) {
327 $this->log(LOG_INFO, "Checking {$activity->id} as a valid Salmon slap.");
329 if ($target instanceof User_group) {
330 $uri = $target->getUri();
331 if (!in_array($uri, $activity->context->attention)) {
332 throw new ClientException(_("Bookmark not posted ".
335 } else if ($target instanceof User) {
338 if (!empty($activity->context->replyToID)) {
339 $original = Notice::staticGet('uri',
340 $activity->context->replyToID);
342 if (!in_array($uri, $activity->context->attention) &&
344 $original->profile_id != $target->id)) {
345 throw new ClientException(_("Object not posted ".
349 throw new ServerException(_("Don't know how to handle ".
350 "this kind of target."));
353 $actor = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
355 $object = $activity->objects[0];
357 $options = array('uri' => $object->id,
358 'url' => $object->link,
359 'is_local' => Notice::REMOTE_OMB,
360 'source' => 'ostatus');
362 $this->saveNoticeFromActivity($activity, $actor, $options);
371 * Handle object posted via AtomPub
373 * @param Activity &$activity Activity that was posted
374 * @param User $user User that posted it
375 * @param Notice &$notice Resulting notice
377 * @return boolean hook value
380 function onStartAtomPubNewActivity(&$activity, $user, &$notice)
382 if ($this->isMyActivity($activity)) {
384 $options = array('source' => 'atompub');
386 $this->saveNoticeFromActivity($activity,
397 * Handle object imported from a backup file
399 * @param User $user User to import for
400 * @param ActivityObject $author Original author per import file
401 * @param Activity $activity Activity to import
402 * @param boolean $trusted Is this a trusted user?
403 * @param boolean &$done Is this done (success or unrecoverable error)
405 * @return boolean hook value
408 function onStartImportActivity($user, $author, $activity, $trusted, &$done)
410 if ($this->isMyActivity($activity)) {
412 $obj = $activity->objects[0];
414 $options = array('uri' => $object->id,
415 'url' => $object->link,
416 'source' => 'restore');
418 $saved = $this->saveNoticeFromActivity($activity,
422 if (!empty($saved)) {
432 function onStartShowEntryForms(&$tabs)
434 $tabs[$this->tag()] = $this->appTitle();
438 function onStartMakeEntryForm($tag, $out, &$form)
440 $this->log(LOG_INFO, "onStartMakeEntryForm() called for tag '$tag'");
442 if ($tag == $this->tag()) {
443 $form = $this->entryForm($out);