]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/microappplugin.php
adminUpdates setting not available now
[quix0rs-gnu-social.git] / lib / microappplugin.php
index ab6d5651579c17faf7cd0dd758bf838bdbee8c28..ec67d9fe2e30c8dc17172c93886401b0428c43db 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,21 +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
-     *
-     * @fixme WARNING WARNING WARNING base plugin stuff below tries to close
-     * a div that this function opens in the BookmarkPlugin child class.
-     * This is probably wrong.
-     */
-    abstract function showNotice($notice, $out);
-
     /**
      * When building the primary notice form, we'll fetch also some
      * alternate forms for specialized types -- that's you!
@@ -178,6 +162,14 @@ abstract class MicroAppPlugin extends Plugin
      */
     abstract function deleteRelated($notice);
 
+    /**
+     *
+     */
+    public function newFormAction() {
+        // such as 'newbookmark' or 'newevent' route
+        return 'new'.$this->tag();
+    }
+
     /**
      * Check if a given notice object should be handled by this micro-app
      * plugin.
@@ -191,7 +183,7 @@ abstract class MicroAppPlugin extends Plugin
      */
     function isMyNotice($notice) {
         $types = $this->types();
-        return in_array($notice->object_type, $types);
+        return ($notice->verb == ActivityVerb::POST) && in_array($notice->object_type, $types);
     }
 
     /**
@@ -209,6 +201,8 @@ abstract class MicroAppPlugin extends Plugin
     function isMyActivity($activity) {
         $types = $this->types();
         return (count($activity->objects) == 1 &&
+                ($activity->objects[0] instanceof ActivityObject) &&
+                ($activity->verb == ActivityVerb::POST) &&
                 in_array($activity->objects[0]->type, $types));
     }
 
@@ -255,17 +249,16 @@ abstract class MicroAppPlugin extends Plugin
      * by calling the overridable $this->deleteRelated().
      *
      * @param Notice $notice Notice being deleted
-     * 
+     *
      * @return boolean hook value
      */
-
     function onNoticeDeleteRelated($notice)
     {
-        if ($this->isMyNotice($notice)) {
-            $this->deleteRelated($notice);
+        if (!$this->isMyNotice($notice)) {
+            return true;
         }
 
-        return true;
+        $this->deleteRelated($notice);
     }
 
     /**
@@ -277,29 +270,63 @@ abstract class MicroAppPlugin extends Plugin
      *
      * @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->showNotice();
+            $adapter->showNoticeAttachments();
+            $adapter->showNoticeInfo();
+            $adapter->showNoticeOptions();
+        } 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();
     }
 
     /**
@@ -310,15 +337,14 @@ abstract class MicroAppPlugin extends Plugin
      *
      * @return boolean hook value
      */
-     
     function onStartActivityObjectFromNotice($notice, &$object)
     {
-        if ($this->isMyNotice($notice)) {
-            $object = $this->activityObjectFromNotice($notice);
-            return false;
+        if (!$this->isMyNotice($notice)) {
+            return true;
         }
 
-        return true;
+        $object = $this->activityObjectFromNotice($notice);
+        return false;
     }
 
     /**
@@ -329,31 +355,30 @@ abstract class MicroAppPlugin extends Plugin
      *
      * @return boolean hook value
      */
-
-    function onStartHandleFeedEntryWithProfile($activity, $oprofile)
+    function onStartHandleFeedEntryWithProfile($activity, $oprofile, &$notice)
     {
-        if ($this->isMyActivity($activity)) {
+        if (!$this->isMyActivity($activity)) {
+            return true;
+        }
 
-            $actor = $oprofile->checkAuthorship($activity);
+        $actor = $oprofile->checkAuthorship($activity);
 
-            if (empty($actor)) {
-                throw new ClientException(_('Can\'t get author for activity.'));
-            }
+        if (!$actor instanceof Ostatus_profile) {
+            // TRANS: Client exception thrown when no author for an activity was found.
+            throw new ClientException(_('Cannot get author for activity.'));
+        }
 
-            $object = $activity->objects[0];
+        $object = $activity->objects[0];
 
-            $options = array('uri' => $object->id,
-                             'url' => $object->link,
-                             'is_local' => Notice::REMOTE_OMB,
-                             'source' => 'ostatus');
+        $options = array('uri' => $object->id,
+                         'url' => $object->link,
+                         'is_local' => Notice::REMOTE,
+                         'source' => 'ostatus');
 
-            // $actor is an ostatus_profile
-            $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options);
+        // $actor is an ostatus_profile
+        $notice = $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options);
 
-            return false;
-        }
-
-        return true;
+        return false;
     }
 
     /**
@@ -367,50 +392,50 @@ 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."));
-                }
-            } else if ($target instanceof User) {
-                $uri      = $target->uri;
-                $original = null;
-                if (!empty($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."));
-                }
-            } else {
-                throw new ServerException(_("Don't know how to handle ".
-                                            "this kind of target."));
-            }
+        if (!$this->isMyActivity($activity)) {
+            return true;
+        }
 
-            $actor = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
+        $this->log(LOG_INFO, "Checking {$activity->id} as a valid Salmon slap.");
 
-            $object = $activity->objects[0];
+        if ($target instanceof User_group) {
+            $uri = $target->getUri();
+            if (!array_key_exists($uri, $activity->context->attention)) {
+                // @todo FIXME: please document (i18n).
+                // TRANS: Client exception thrown when ...
+                throw new ClientException(_('Object not posted to this group.'));
+            }
+        } else if ($target instanceof User) {
+            $uri      = $target->uri;
+            $original = null;
+            if (!empty($activity->context->replyToID)) {
+                $original = Notice::getKV('uri', $activity->context->replyToID);
+            }
+            if (!array_key_exists($uri, $activity->context->attention) &&
+                (empty($original) ||
+                 $original->profile_id != $target->id)) {
+                // @todo FIXME: Please document (i18n).
+                // TRANS: Client exception when ...
+                throw new ClientException(_('Object not posted to this user.'));
+            }
+        } else {
+            // 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.'));
+        }
 
-            $options = array('uri' => $object->id,
-                             'url' => $object->link,
-                             'is_local' => Notice::REMOTE_OMB,
-                             'source' => 'ostatus');
+        $actor = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
 
-            // $actor is an ostatus_profile
-            $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options);
+        $object = $activity->objects[0];
 
-            return false;
-        }
+        $options = array('uri' => $object->id,
+                         'url' => $object->link,
+                         'is_local' => Notice::REMOTE,
+                         'source' => 'ostatus');
 
-        return true;
+        // $actor is an ostatus_profile
+        $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options);
+
+        return false;
     }
 
     /**
@@ -422,22 +447,20 @@ abstract class MicroAppPlugin extends Plugin
      *
      * @return boolean hook value
      */
-
     function onStartAtomPubNewActivity(&$activity, $user, &$notice)
     {
-        if ($this->isMyActivity($activity)) {
+        if (!$this->isMyActivity($activity)) {
+            return true;
+        }
 
-            $options = array('source' => 'atompub');
+        $options = array('source' => 'atompub');
 
-            // $user->getProfile() is a Profile
-            $this->saveNoticeFromActivity($activity,
-                                          $user->getProfile(),
-                                          $options);
+        // $user->getProfile() is a Profile
+        $notice = $this->saveNoticeFromActivity($activity,
+                                                $user->getProfile(),
+                                                $options);
 
-            return false;
-        }
-
-        return true;
+        return false;
     }
 
     /**
@@ -451,30 +474,28 @@ abstract class MicroAppPlugin extends Plugin
      *
      * @return boolean hook value
      */
-
     function onStartImportActivity($user, $author, $activity, $trusted, &$done)
     {
-        if ($this->isMyActivity($activity)) {
+        if (!$this->isMyActivity($activity)) {
+            return true;
+        }
 
-            $obj = $activity->objects[0];
+        $obj = $activity->objects[0];
 
-            $options = array('uri' => $object->id,
-                             'url' => $object->link,
-                             'source' => 'restore');
+        $options = array('uri' => $object->id,
+                         'url' => $object->link,
+                         'source' => 'restore');
 
-            // $user->getProfile() is a Profile
-            $saved = $this->saveNoticeFromActivity($activity,
-                                                   $user->getProfile(),
-                                                   $options);
+        // $user->getProfile() is a Profile
+        $saved = $this->saveNoticeFromActivity($activity,
+                                               $user->getProfile(),
+                                               $options);
 
-            if (!empty($saved)) {
-                $done = true;
-            }
-
-            return false;
+        if (!empty($saved)) {
+            $done = true;
         }
 
-        return true;
+        return false;
     }
 
     /**
@@ -512,14 +533,16 @@ abstract class MicroAppPlugin extends Plugin
     function onEndActivityObjectOutputJson(ActivityObject $obj, array &$out)
     {
         if (in_array($obj->type, $this->types())) {
-            $this->activityObjectOutputJson($obj, &$out);
+            $this->activityObjectOutputJson($obj, $out);
         }
         return true;
     }
 
     function onStartShowEntryForms(&$tabs)
     {
-        $tabs[$this->tag()] = $this->appTitle();
+        $tabs[$this->tag()] = array('title' => $this->appTitle(),
+                                    'href'  => common_local_url($this->newFormAction()),
+                                   );
         return true;
     }
 
@@ -532,4 +555,10 @@ abstract class MicroAppPlugin extends Plugin
 
         return true;
     }
+
+    function showNotice($notice, $out)
+    {
+        // TRANS: Server exception thrown when a micro app plugin developer has not done his job too well.
+        throw new ServerException(_('You must implement either adaptNoticeListItem() or showNotice().'));
+    }
 }