]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/activityhandlerplugin.php
[MEDIA] ImageFile now extends MediaFile and validates images more aggressively.
[quix0rs-gnu-social.git] / lib / activityhandlerplugin.php
index d972d175520cbf82cd73f70cf6d31799af618626..0a63bed13bb80a22913630cc0d2056dfc8bf13bf 100644 (file)
@@ -31,6 +31,15 @@ if (!defined('GNUSOCIAL')) { exit(1); }
  */
 abstract class ActivityHandlerPlugin extends Plugin
 {
+    /** 
+     * Returns a key string which represents this activity in HTML classes,
+     * ids etc, as when offering selection of what type of post to make. 
+     * In MicroAppPlugin, this is paired with the user-visible localizable appTitle(). 
+     *
+     * @return string (compatible with HTML classes)
+     */ 
+    abstract function tag();
+
     /**
      * Return a list of ActivityStreams object type IRIs
      * which this micro-app handles. Default implementations
@@ -40,8 +49,6 @@ abstract class ActivityHandlerPlugin extends Plugin
      *
      * An empty list means any type is ok. (Favorite verb etc.)
      *
-     * All micro-app classes must override this method.
-     *
      * @return array of strings
      */
     abstract function types();
@@ -57,7 +64,7 @@ abstract class ActivityHandlerPlugin extends Plugin
      *
      * @return array of strings
      */
-    function verbs() {
+    public function verbs() {
         return array(ActivityVerb::POST);
     }
 
@@ -97,10 +104,11 @@ abstract class ActivityHandlerPlugin extends Plugin
 
     function isMyVerb($verb) {
         $verb = $verb ?: ActivityVerb::POST;    // post is the default verb
-        return ActivityUtils::compareTypes($verb, $this->verbs());
+        return ActivityUtils::compareVerbs($verb, $this->verbs());
     }
 
     function isMyType($type) {
+        // Third argument to compareTypes is true, to allow for notices with empty object_type for example (verb-only)
         return count($this->types())===0 || ActivityUtils::compareTypes($type, $this->types());
     }
 
@@ -139,7 +147,7 @@ abstract class ActivityHandlerPlugin extends Plugin
     *
     * This will handle just about all events where an activity
     * object gets saved, whether it is via AtomPub, OStatus
-    * (PuSH and Salmon transports), or ActivityStreams-based
+    * (WebSub and Salmon transports), or ActivityStreams-based
     * backup/restore of account data.
     *
     * You should be able to accept as input the output from an
@@ -153,12 +161,13 @@ abstract class ActivityHandlerPlugin extends Plugin
     * @fixme are there any standard options?
     *
     * @param Activity $activity
-    * @param Profile $actor
+    * @param Notice   $stored       The notice in our database for this certain object
     * @param array $options=array()
     *
-    * @return Notice the resulting notice
+    * @return object    If the verb handling plugin creates an object, it can be returned here (otherwise true)
+    * @throws exception On any error.
     */
-    public function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
+    protected function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
     {
         throw new ServerException('This function should be abstract when all plugins have migrated to saveObjectFromActivity');
     }
@@ -167,18 +176,13 @@ abstract class ActivityHandlerPlugin extends Plugin
      * This usually gets called from Notice::saveActivity after a Notice object has been created,
      * so it contains a proper id and a uri for the object to be saved.
      */
-    public function onStoreActivityObject(Activity $act, Notice $stored, array $options=array(), &$object) {
+    public function onStoreActivityObject(Activity $act, Notice $stored, array $options, &$object) {
         // $this->oldSaveNew is there during a migration period of plugins, to start using
         // Notice::saveActivity instead of Notice::saveNew
         if (!$this->isMyActivity($act) || isset($this->oldSaveNew)) {
             return true;
         }
         $object = $this->saveObjectFromActivity($act, $stored, $options);
-        try {
-            $act->context->attention = array_merge($act->context->attention, $object->getAttentionArray());
-        } catch (Exception $e) {
-            common_debug('WARNING: Could not get attention list from object '.get_class($object).'!');
-        }
         return false;
     }
 
@@ -189,7 +193,7 @@ abstract class ActivityHandlerPlugin extends Plugin
      *
      * This will be how your specialized notice gets output in
      * Atom feeds and JSON-based ActivityStreams output, including
-     * account backup/restore and OStatus (PuSH and Salmon transports).
+     * account backup/restore and OStatus (WebSub and Salmon transports).
      *
      * You should be able to round-trip data from this format back
      * through $this->saveNoticeFromActivity(). Where applicable, try
@@ -216,6 +220,14 @@ abstract class ActivityHandlerPlugin extends Plugin
      */
     abstract function deleteRelated(Notice $notice);
 
+    protected function notifyMentioned(Notice $stored, array &$mentioned_ids)
+    {
+        // pass through silently by default
+
+        // If we want to stop any other plugin from notifying based on this activity, return false instead.
+        return true;
+    }
+
     /**
      * Called when generating Atom XML ActivityStreams output from an
      * ActivityObject belonging to this plugin. Gives the plugin
@@ -262,13 +274,35 @@ abstract class ActivityHandlerPlugin extends Plugin
      *
      * @return boolean hook value
      */
-    function onNoticeDeleteRelated(Notice $notice)
+    public function onNoticeDeleteRelated(Notice $notice)
     {
-        if (!$this->isMyNotice($notice)) {
+        if ($this->isMyNotice($notice)) {
+            try {
+                $this->deleteRelated($notice);
+            } catch (NoProfileException $e) {
+                // we failed because of database lookup failure, Notice has no recognized profile as creator
+                // so we skip this. If we want to remove missing notices we should do a SQL constraints check
+                // in the affected plugin.
+            } catch (AlreadyFulfilledException $e) {
+                // Nothing to see here, it's obviously already gone...
+            }
+        }
+
+        // Always continue this event in our activity handling plugins.
+        return true;
+    }
+
+    /**
+     * @param Notice $stored            The notice being distributed
+     * @param array  &$mentioned_ids    List of profiles (from $stored->getReplies())
+     */
+    public function onStartNotifyMentioned(Notice $stored, array &$mentioned_ids)
+    {
+        if (!$this->isMyNotice($stored)) {
             return true;
         }
 
-        $this->deleteRelated($notice);
+        return $this->notifyMentioned($stored, $mentioned_ids);
     }
 
     /**
@@ -285,16 +319,12 @@ abstract class ActivityHandlerPlugin extends Plugin
             return true;
         }
 
-        try {
-            $object = $this->activityObjectFromNotice($notice);
-        } catch (NoResultException $e) {
-            $object = null; // because getKV returns null on failure
-        }
+        $object = $this->activityObjectFromNotice($notice);
         return false;
     }
 
     /**
-     * Handle a posted object from PuSH
+     * Handle a posted object from WebSub
      *
      * @param Activity        $activity activity to handle
      * @param Profile         $actor Profile for the feed
@@ -314,10 +344,15 @@ abstract class ActivityHandlerPlugin extends Plugin
 
         $options = array('uri' => $object->id,
                          'url' => $object->link,
+                         'self' => $object->selfLink,
                          'is_local' => Notice::REMOTE,
                          'source' => 'ostatus');
 
-        $notice = $this->saveNoticeFromActivity($activity, $profile, $options);
+        if (!isset($this->oldSaveNew)) {
+            $notice = Notice::saveActivity($activity, $profile, $options);
+        } else {
+            $notice = $this->saveNoticeFromActivity($activity, $profile, $options);
+        }
 
         return false;
     }
@@ -336,25 +371,35 @@ abstract class ActivityHandlerPlugin extends Plugin
         if (!$this->isMyActivity($activity)) {
             return true;
         }
+        if (!isset($this->oldSaveNew)) {
+            // Handle saveActivity in OStatus class for incoming salmon, remove this event
+            // handler when all plugins have gotten rid of "oldSaveNew".
+            return true;
+        }
 
-        $this->log(LOG_INFO, "Checking {$activity->id} as a valid Salmon slap.");
+        $this->log(LOG_INFO, get_called_class()." checking {$activity->id} as a valid Salmon slap.");
 
-        if ($target instanceof User_group) {
+        if ($target instanceof User_group || $target->isGroup()) {
             $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;
+        } elseif ($target instanceof Profile && $target->isLocal()) {
             $original = null;
+            // FIXME: Shouldn't favorites show up with a 'target' activityobject?
+            if (!ActivityUtils::compareVerbs($activity->verb, array(ActivityVerb::POST)) && isset($activity->objects[0])) {
+                // If this is not a post, it's a verb targeted at something (such as a Favorite attached to a note)
+                if (!empty($activity->objects[0]->id)) {
+                    $activity->context->replyToID = $activity->objects[0]->id;
+                }
+            }
             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)) {
+            if ((!$original instanceof Notice || $original->profile_id != $target->id)
+                    && !array_key_exists($target->getUri(), $activity->context->attention)) {
                 // @todo FIXME: Please document (i18n).
                 // TRANS: Client exception when ...
                 throw new ClientException(_('Object not posted to this user.'));
@@ -364,17 +409,23 @@ abstract class ActivityHandlerPlugin extends Plugin
             throw new ServerException(_('Do not know how to handle this kind of target.'));
         }
 
-        $actor = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
+        $oactor = Ostatus_profile::ensureActivityObjectProfile($activity->actor);
+        $actor = $oactor->localProfile();
 
-        $object = $activity->objects[0];
+        // FIXME: will this work in all cases? I made it work for Favorite...
+        if (ActivityUtils::compareVerbs($activity->verb, array(ActivityVerb::POST))) {
+            $object = $activity->objects[0];
+        } else {
+            $object = $activity;
+        }
 
         $options = array('uri' => $object->id,
                          'url' => $object->link,
+                         'self' => $object->selfLink,
                          'is_local' => Notice::REMOTE,
                          'source' => 'ostatus');
 
-        // $actor is an ostatus_profile
-        $this->saveNoticeFromActivity($activity, $actor->localProfile(), $options);
+        $notice = $this->saveNoticeFromActivity($activity, $actor, $options);
 
         return false;
     }
@@ -382,13 +433,13 @@ abstract class ActivityHandlerPlugin extends Plugin
     /**
      * Handle object posted via AtomPub
      *
-     * @param Activity &$activity Activity that was posted
-     * @param User     $user      User that posted it
+     * @param Activity  $activity Activity that was posted
+     * @param Profile   $scoped   Profile of user posting
      * @param Notice   &$notice   Resulting notice
      *
      * @return boolean hook value
      */
-    function onStartAtomPubNewActivity(Activity &$activity, $user, &$notice)
+    public function onStartAtomPubNewActivity(Activity $activity, Profile $scoped, Notice &$notice=null)
     {
         if (!$this->isMyActivity($activity)) {
             return true;
@@ -396,10 +447,7 @@ abstract class ActivityHandlerPlugin extends Plugin
 
         $options = array('source' => 'atompub');
 
-        // $user->getProfile() is a Profile
-        $notice = $this->saveNoticeFromActivity($activity,
-                                                $user->getProfile(),
-                                                $options);
+        $notice = $this->saveNoticeFromActivity($activity, $scoped, $options);
 
         return false;
     }
@@ -425,6 +473,7 @@ abstract class ActivityHandlerPlugin extends Plugin
 
         $options = array('uri' => $object->id,
                          'url' => $object->link,
+                         'self' => $object->selfLink,
                          'source' => 'restore');
 
         // $user->getProfile() is a Profile
@@ -478,4 +527,114 @@ abstract class ActivityHandlerPlugin extends Plugin
         }
         return true;
     }
+
+    public function onStartOpenNoticeListItemElement(NoticeListItem $nli)
+    {   
+        if (!$this->isMyNotice($nli->notice)) {
+            return true;
+        }
+
+        $this->openNoticeListItemElement($nli);
+
+        Event::handle('EndOpenNoticeListItemElement', array($nli));
+        return false;
+    }
+
+    public function onStartCloseNoticeListItemElement(NoticeListItem $nli)
+    {   
+        if (!$this->isMyNotice($nli->notice)) {
+            return true;
+        }
+
+        $this->closeNoticeListItemElement($nli);
+
+        Event::handle('EndCloseNoticeListItemElement', array($nli));
+        return false;
+    }
+
+    protected function openNoticeListItemElement(NoticeListItem $nli)
+    {
+        $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
+        $class = 'h-entry notice ' . $this->tag();
+        if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
+            $class .= ' limited-scope';
+        }
+        try {
+            $class .= ' notice-source-'.common_to_alphanumeric($nli->notice->source);
+        } catch (Exception $e) {
+            // either source or what we filtered out was a zero-length string
+        }
+        $nli->out->elementStart('li', array('class' => $class,
+                                            'id' => 'notice-' . $id));
+    }
+
+    protected function closeNoticeListItemElement(NoticeListItem $nli)
+    {
+        $nli->out->elementEnd('li');
+    }
+
+
+    // FIXME: This is overriden in MicroAppPlugin but shouldn't have to be
+    public function onStartShowNoticeItem(NoticeListItem $nli)
+    {   
+        if (!$this->isMyNotice($nli->notice)) {
+            return true;
+        }
+
+        try {
+            $this->showNoticeListItem($nli);
+        } catch (Exception $e) {
+            common_log(LOG_ERR, 'Error showing notice '.$nli->getNotice()->getID().': ' . $e->getMessage());
+            $nli->out->element('p', 'error', sprintf(_('Error showing notice: %s'), $e->getMessage()));
+        }
+
+        Event::handle('EndShowNoticeItem', array($nli));
+        return false;
+    }
+
+    protected function showNoticeListItem(NoticeListItem $nli)
+    {
+        $nli->showNoticeHeaders();
+        $nli->showContent();
+        $nli->showNoticeFooter();
+    }
+
+    public function onStartShowNoticeItemNotice(NoticeListItem $nli)
+    {
+        if (!$this->isMyNotice($nli->notice)) {
+            return true;
+        }
+
+        $this->showNoticeItemNotice($nli);
+
+        Event::handle('EndShowNoticeItemNotice', array($nli));
+        return false;
+    }
+
+    protected function showNoticeItemNotice(NoticeListItem $nli)
+    {
+        $nli->showNoticeTitle();
+        $nli->showAuthor();
+        $nli->showAddressees();
+        $nli->showContent();
+    }
+
+    public function onStartShowNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
+    {
+        if (!$this->isMyNotice($stored)) {
+            return true;
+        }
+
+        try {
+            $this->showNoticeContent($stored, $out, $scoped);
+        } catch (Exception $e) {
+            $out->element('div', 'error', $e->getMessage());
+        }
+        return false;
+    }
+
+    protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
+    {
+        $out->text($stored->getContent());
+    }
 }