]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/microappplugin.php
Let showNotice() go unimplemented
[quix0rs-gnu-social.git] / lib / microappplugin.php
index 8bc657f44a828b3de09651ec5cdd6f00d5af61b3..0fd6868f8c25242beb19c3033e0f67accbe547a2 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 2011, StatusNet, Inc.
  *
  * Superclass for microapp plugin
- * 
+ *
  * PHP version 5
  *
  * This program is free software: you can redistribute it and/or modify
@@ -39,8 +39,8 @@ if (!defined('STATUSNET')) {
  *
  * This class lets you define micro-applications with different kinds of activities.
  *
- * The applications work more-or-less like other 
- * 
+ * The applications work more-or-less like other
+ *
  * @category  Microapp
  * @package   StatusNet
  * @author    Evan Prodromou <evan@status.net>
@@ -48,7 +48,6 @@ if (!defined('STATUSNET')) {
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  * @link      http://status.net/
  */
-
 abstract class MicroAppPlugin extends Plugin
 {
     /**
@@ -138,17 +137,6 @@ abstract class MicroAppPlugin extends Plugin
      */
     abstract function activityObjectFromNotice($notice);
 
-    /**
-     * Custom HTML output for your special notice; called when a
-     * matching notice turns up in a NoticeListItem.
-     *
-     * All micro-app classes must override this method.
-     *
-     * @param Notice $notice
-     * @param HTMLOutputter $out
-     */
-    abstract function showNotice($notice, $out);
-
     /**
      * When building the primary notice form, we'll fetch also some
      * alternate forms for specialized types -- that's you!
@@ -208,15 +196,52 @@ abstract class MicroAppPlugin extends Plugin
                 in_array($activity->objects[0]->type, $types));
     }
 
+    /**
+     * Called when generating Atom XML ActivityStreams output from an
+     * ActivityObject belonging to this plugin. Gives the plugin
+     * a chance to add custom output.
+     *
+     * Note that you can only add output of additional XML elements,
+     * not change existing stuff here.
+     *
+     * If output is already handled by the base Activity classes,
+     * you can leave this base implementation as a no-op.
+     *
+     * @param ActivityObject $obj
+     * @param XMLOutputter $out to add elements at end of object
+     */
+    function activityObjectOutputAtom(ActivityObject $obj, XMLOutputter $out)
+    {
+        // default is a no-op
+    }
+
+    /**
+     * Called when generating JSON ActivityStreams output from an
+     * ActivityObject belonging to this plugin. Gives the plugin
+     * a chance to add custom output.
+     *
+     * Modify the array contents to your heart's content, and it'll
+     * all get serialized out as JSON.
+     *
+     * If output is already handled by the base Activity classes,
+     * you can leave this base implementation as a no-op.
+     *
+     * @param ActivityObject $obj
+     * @param array &$out JSON-targeted array which can be modified
+     */
+    public function activityObjectOutputJson(ActivityObject $obj, array &$out)
+    {
+        // default is a no-op
+    }
+
     /**
      * When a notice is deleted, delete the related objects
      * by calling the overridable $this->deleteRelated().
      *
      * @param Notice $notice Notice being deleted
-     * 
+     *
      * @return boolean hook value
      */
-
     function onNoticeDeleteRelated($notice)
     {
         if ($this->isMyNotice($notice)) {
@@ -232,30 +257,63 @@ abstract class MicroAppPlugin extends Plugin
      * @param NoticeListItem $nli The list item being shown.
      *
      * @return boolean hook value
+     *
+     * @fixme WARNING WARNING WARNING this closes a 'div' that is implicitly opened in BookmarkPlugin's showNotice implementation
      */
-
     function onStartShowNoticeItem($nli)
     {
         if (!$this->isMyNotice($nli->notice)) {
             return true;
         }
 
+        $adapter = $this->adaptNoticeListItem($nli);
+
+        if (!empty($adapter)) {
+            $adapter->show();
+        } else {
+            $this->oldShowNotice($nli);
+        }
+
+        return false;
+    }
+
+    /**
+     * Given a notice list item, returns an adapter specific
+     * to this plugin.
+     *
+     * @param NoticeListItem $nli item to adapt
+     *
+     * @return NoticeListItemAdapter adapter or null
+     */
+    function adaptNoticeListItem($nli)
+    {
+      return null;
+    }
+
+    function oldShowNotice($nli)
+    {
         $out = $nli->out;
         $notice = $nli->notice;
 
-        $this->showNotice($notice, $out);
+        try {
+            $this->showNotice($notice, $out);
+        } catch (Exception $e) {
+            common_log(LOG_ERR, $e->getMessage());
+            // try to fall back
+            $out->elementStart('div');
+            $nli->showAuthor();
+            $nli->showContent();
+        }
 
         $nli->showNoticeLink();
         $nli->showNoticeSource();
         $nli->showNoticeLocation();
         $nli->showContext();
         $nli->showRepeat();
-        
+
         $out->elementEnd('div');
-        
-        $nli->showNoticeOptions();
 
-        return false;
+        $nli->showNoticeOptions();
     }
 
     /**
@@ -266,7 +324,6 @@ abstract class MicroAppPlugin extends Plugin
      *
      * @return boolean hook value
      */
-     
     function onStartActivityObjectFromNotice($notice, &$object)
     {
         if ($this->isMyNotice($notice)) {
@@ -285,7 +342,6 @@ abstract class MicroAppPlugin extends Plugin
      *
      * @return boolean hook value
      */
-
     function onStartHandleFeedEntryWithProfile($activity, $oprofile)
     {
         if ($this->isMyActivity($activity)) {
@@ -293,7 +349,8 @@ abstract class MicroAppPlugin extends Plugin
             $actor = $oprofile->checkAuthorship($activity);
 
             if (empty($actor)) {
-                throw new ClientException(_('Can\'t get author for activity.'));
+                // TRANS: Client exception thrown when no author for an activity was found.
+                throw new ClientException(_('Cannot get author for activity.'));
             }
 
             $object = $activity->objects[0];
@@ -302,8 +359,9 @@ abstract class MicroAppPlugin extends Plugin
                              'url' => $object->link,
                              'is_local' => Notice::REMOTE_OMB,
                              'source' => 'ostatus');
-            
-            $this->saveNoticeFromActivity($activity, $actor);
+
+            // $actor is an ostatus_profile
+            $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options);
 
             return false;
         }
@@ -323,31 +381,32 @@ abstract class MicroAppPlugin extends Plugin
     function onStartHandleSalmonTarget($activity, $target)
     {
         if ($this->isMyActivity($activity)) {
-
             $this->log(LOG_INFO, "Checking {$activity->id} as a valid Salmon slap.");
 
             if ($target instanceof User_group) {
                 $uri = $target->getUri();
                 if (!in_array($uri, $activity->context->attention)) {
-                    throw new ClientException(_("Bookmark not posted ".
-                                                "to this group."));
+                    // @todo FIXME: please document (i18n).
+                    // TRANS: Client exception.
+                    throw new ClientException(_('Bookmark not posted to this group.'));
                 }
             } else if ($target instanceof User) {
                 $uri      = $target->uri;
                 $original = null;
                 if (!empty($activity->context->replyToID)) {
-                    $original = Notice::staticGet('uri', 
-                                                  $activity->context->replyToID); 
+                    $original = Notice::staticGet('uri',
+                                                  $activity->context->replyToID);
                 }
                 if (!in_array($uri, $activity->context->attention) &&
                     (empty($original) ||
                      $original->profile_id != $target->id)) {
-                    throw new ClientException(_("Object not posted ".
-                                                "to this user."));
+                    // @todo FIXME: Please document (i18n).
+                    // TRANS: Client exception.
+                    throw new ClientException(_('Object not posted to this user.'));
                 }
             } else {
-                throw new ServerException(_("Don't know how to handle ".
-                                            "this kind of target."));
+                // TRANS: Server exception thrown when a micro app plugin uses a target that cannot be handled.
+                throw new ServerException(_('Do not know how to handle this kind of target.'));
             }
 
             $actor = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
@@ -359,7 +418,8 @@ abstract class MicroAppPlugin extends Plugin
                              'is_local' => Notice::REMOTE_OMB,
                              'source' => 'ostatus');
 
-            $this->saveNoticeFromActivity($activity, $actor, $options);
+            // $actor is an ostatus_profile
+            $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options);
 
             return false;
         }
@@ -376,13 +436,13 @@ abstract class MicroAppPlugin extends Plugin
      *
      * @return boolean hook value
      */
-
     function onStartAtomPubNewActivity(&$activity, $user, &$notice)
     {
         if ($this->isMyActivity($activity)) {
 
             $options = array('source' => 'atompub');
 
+            // $user->getProfile() is a Profile
             $this->saveNoticeFromActivity($activity,
                                           $user->getProfile(),
                                           $options);
@@ -404,7 +464,6 @@ abstract class MicroAppPlugin extends Plugin
      *
      * @return boolean hook value
      */
-
     function onStartImportActivity($user, $author, $activity, $trusted, &$done)
     {
         if ($this->isMyActivity($activity)) {
@@ -415,6 +474,7 @@ abstract class MicroAppPlugin extends Plugin
                              'url' => $object->link,
                              'source' => 'restore');
 
+            // $user->getProfile() is a Profile
             $saved = $this->saveNoticeFromActivity($activity,
                                                    $user->getProfile(),
                                                    $options);
@@ -429,6 +489,46 @@ abstract class MicroAppPlugin extends Plugin
         return true;
     }
 
+    /**
+     * Event handler gives the plugin a chance to add custom
+     * Atom XML ActivityStreams output from a previously filled-out
+     * ActivityObject.
+     *
+     * The atomOutput method is called if it's one of
+     * our matching types.
+     *
+     * @param ActivityObject $obj
+     * @param XMLOutputter $out to add elements at end of object
+     * @return boolean hook return value
+     */
+    function onEndActivityObjectOutputAtom(ActivityObject $obj, XMLOutputter $out)
+    {
+        if (in_array($obj->type, $this->types())) {
+            $this->activityObjectOutputAtom($obj, $out);
+        }
+        return true;
+    }
+
+    /**
+     * Event handler gives the plugin a chance to add custom
+     * JSON ActivityStreams output from a previously filled-out
+     * ActivityObject.
+     *
+     * The activityObjectOutputJson method is called if it's one of
+     * our matching types.
+     *
+     * @param ActivityObject $obj
+     * @param array &$out JSON-targeted array which can be modified
+     * @return boolean hook return value
+     */
+    function onEndActivityObjectOutputJson(ActivityObject $obj, array &$out)
+    {
+        if (in_array($obj->type, $this->types())) {
+            $this->activityObjectOutputJson($obj, $out);
+        }
+        return true;
+    }
+
     function onStartShowEntryForms(&$tabs)
     {
         $tabs[$this->tag()] = $this->appTitle();
@@ -437,8 +537,6 @@ abstract class MicroAppPlugin extends Plugin
 
     function onStartMakeEntryForm($tag, $out, &$form)
     {
-        $this->log(LOG_INFO, "onStartMakeEntryForm() called for tag '$tag'");
-
         if ($tag == $this->tag()) {
             $form = $this->entryForm($out);
             return false;
@@ -446,4 +544,9 @@ abstract class MicroAppPlugin extends Plugin
 
         return true;
     }
+
+    function showNotice($notice, $out)
+    {
+        throw new ServerException("You must implement either adaptNoticeListItem() or showNotice()");
+    }
 }