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
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);
63 function isMyNotice($notice) {
64 $types = $this->types();
65 return in_array($notice->object_type, $types);
68 function isMyActivity($activity) {
69 $types = $this->types();
70 return (count($activity->objects) == 1 &&
71 in_array($activity->objects[0]->type, $types));
75 * When a notice is deleted, delete the related objects
77 * @param Notice $notice Notice being deleted
79 * @return boolean hook value
82 function onNoticeDeleteRelated($notice)
84 if ($this->isMyNotice($notice)) {
85 $this->deleteRelated($notice);
92 * Output the HTML for this kind of object in a list
94 * @param NoticeListItem $nli The list item being shown.
96 * @return boolean hook value
99 function onStartShowNoticeItem($nli)
101 if (!$this->isMyNotice($nli->notice)) {
106 $notice = $nli->notice;
108 $this->showNotice($notice, $out);
110 $nli->showNoticeLink();
111 $nli->showNoticeSource();
112 $nli->showNoticeLocation();
116 $out->elementEnd('div');
118 $nli->showNoticeOptions();
124 * Render a notice as one of our objects
126 * @param Notice $notice Notice to render
127 * @param ActivityObject &$object Empty object to fill
129 * @return boolean hook value
132 function onStartActivityObjectFromNotice($notice, &$object)
134 if ($this->isMyNotice($notice)) {
135 $object = $this->activityObjectFromNotice($notice);
143 * Handle a posted object from PuSH
145 * @param Activity $activity activity to handle
146 * @param Ostatus_profile $oprofile Profile for the feed
148 * @return boolean hook value
151 function onStartHandleFeedEntryWithProfile($activity, $oprofile)
153 if ($this->isMyActivity($activity)) {
155 $actor = $oprofile->checkAuthorship($activity);
158 throw new ClientException(_('Can\'t get author for activity.'));
161 $object = $activity->objects[0];
163 $options = array('uri' => $object->id,
164 'url' => $object->link,
165 'is_local' => Notice::REMOTE_OMB,
166 'source' => 'ostatus');
168 $this->saveNoticeFromActivity($activity, $actor);
177 * Handle a posted object from Salmon
179 * @param Activity $activity activity to handle
180 * @param mixed $target user or group targeted
182 * @return boolean hook value
185 function onStartHandleSalmonTarget($activity, $target)
187 if ($this->isMyActivity($activity)) {
189 $this->log(LOG_INFO, "Checking {$activity->id} as a valid Salmon slap.");
191 if ($target instanceof User_group) {
192 $uri = $target->getUri();
193 if (!in_array($uri, $activity->context->attention)) {
194 throw new ClientException(_("Bookmark not posted ".
197 } else if ($target instanceof User) {
200 if (!empty($activity->context->replyToID)) {
201 $original = Notice::staticGet('uri',
202 $activity->context->replyToID);
204 if (!in_array($uri, $activity->context->attention) &&
206 $original->profile_id != $target->id)) {
207 throw new ClientException(_("Object not posted ".
211 throw new ServerException(_("Don't know how to handle ".
212 "this kind of target."));
215 $actor = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
217 $object = $activity->objects[0];
219 $options = array('uri' => $object->id,
220 'url' => $object->link,
221 'is_local' => Notice::REMOTE_OMB,
222 'source' => 'ostatus');
224 $this->saveNoticeFromActivity($activity, $actor, $options);
233 * Handle object posted via AtomPub
235 * @param Activity &$activity Activity that was posted
236 * @param User $user User that posted it
237 * @param Notice &$notice Resulting notice
239 * @return boolean hook value
242 function onStartAtomPubNewActivity(&$activity, $user, &$notice)
244 if ($this->isMyActivity($activity)) {
246 $options = array('source' => 'atompub');
248 $this->saveNoticeFromActivity($activity,
259 * Handle object imported from a backup file
261 * @param User $user User to import for
262 * @param ActivityObject $author Original author per import file
263 * @param Activity $activity Activity to import
264 * @param boolean $trusted Is this a trusted user?
265 * @param boolean &$done Is this done (success or unrecoverable error)
267 * @return boolean hook value
270 function onStartImportActivity($user, $author, $activity, $trusted, &$done)
272 if ($this->isMyActivity($activity)) {
274 $obj = $activity->objects[0];
276 $options = array('uri' => $object->id,
277 'url' => $object->link,
278 'source' => 'restore');
280 $saved = $this->saveNoticeFromActivity($activity,
284 if (!empty($saved)) {
294 function onStartShowEntryForms(&$tabs)
296 $tabs[$this->tag()] = $this->appTitle();
300 function onStartMakeEntryForm($tag, $out, &$form)
302 $this->log(LOG_INFO, "onStartMakeEntryForm() called for tag '$tag'");
304 if ($tag == $this->tag()) {
305 $form = $this->entryForm($out);