]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
MicroAppPlugin extends to intermediate ActivityHandlerPlugin
authorMikael Nordfeldth <mmn@hethane.se>
Tue, 24 Jun 2014 14:42:34 +0000 (16:42 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Tue, 24 Jun 2014 14:46:11 +0000 (16:46 +0200)
lib/activityhandlerplugin.php [new file with mode: 0644]
lib/activityutils.php
lib/microappplugin.php

diff --git a/lib/activityhandlerplugin.php b/lib/activityhandlerplugin.php
new file mode 100644 (file)
index 0000000..6767633
--- /dev/null
@@ -0,0 +1,106 @@
+<?php
+/*  
+ * GNU Social - a federating social network  
+ * Copyright (C) 2014, Free Software Foundation, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify  
+ * it under the terms of the GNU Affero General Public License as published by  
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.  
+ *  
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of  
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
+ * GNU Affero General Public License for more details.
+ *  
+ * You should have received a copy of the GNU Affero General Public License  
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.  
+ */
+
+if (!defined('GNUSOCIAL')) { exit(1); }
+
+/**
+ * Superclass for plugins which add Activity types and such
+ *
+ * @category  Activity
+ * @package   GNUsocial
+ * @author    Mikael Nordfeldth <mmn@hethane.se>
+ * @copyright 2014 Free Software Foundation, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
+ * @link      http://gnu.io/social
+ */
+abstract class ActivityHandlerPlugin extends Plugin
+{
+    /**
+     * Return a list of ActivityStreams object type IRIs
+     * which this micro-app handles. Default implementations
+     * of the base class will use this list to check if a
+     * given ActivityStreams object belongs to us, via
+     * $this->isMyNotice() or $this->isMyActivity.
+     *
+     * 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();
+
+    /**
+     * Return a list of ActivityStreams verb IRIs which
+     * this micro-app handles. Default implementations
+     * of the base class will use this list to check if a
+     * given ActivityStreams verb belongs to us, via
+     * $this->isMyNotice() or $this->isMyActivity.
+     *
+     * All micro-app classes must override this method.
+     *
+     * @return array of strings
+     */
+    function verbs() {
+        return array(ActivityVerb::POST);
+    }
+
+    /**
+     * Check if a given ActivityStreams activity should be handled by this
+     * micro-app plugin.
+     *
+     * The default implementation checks against the activity type list
+     * returned by $this->types(), and requires that exactly one matching
+     * object be present. You can override this method to expand
+     * your checks or to compare the activity's verb, etc.
+     *
+     * @param Activity $activity
+     * @return boolean
+     */
+    function isMyActivity(Activity $act) {
+        return (count($act->objects) == 1
+            && ($act->objects[0] instanceof ActivityObject)
+            && $this->isMyVerb($act->verb)
+            && $this->isMyType($act->objects[0]->type));
+    }
+
+    /**
+     * Check if a given notice object should be handled by this micro-app
+     * plugin.
+     * 
+     * The default implementation checks against the activity type list 
+     * returned by $this->types(). You can override this method to expand 
+     * your checks, but follow the execution chain to get it right. 
+     * 
+     * @param Notice $notice 
+     * @return boolean 
+     */ 
+    function isMyNotice(Notice $notice) {
+        return $this->isMyVerb($notice->verb) && $this->isMyType($notice->object_type);
+    }
+
+    function isMyVerb($verb) {
+        $verb = $verb ?: ActivityVerb::POST;    // post is the default verb
+        return ActivityUtils::compareTypes($verb, $this->verbs());
+    }
+
+    function isMyType($type) {
+        return count($this->types())===0 || ActivityUtils::compareTypes($type, $this->types());
+    }
+}
index c2c239f1d3cc68ef32872da97835f7d8ec58710c..8a7039d9095e98495a56edfd9edd8428b94c04b3 100644 (file)
@@ -346,4 +346,15 @@ class ActivityUtils
 
         return null;
     }
+
+    static function compareTypes($type, $objects)    // this does verbs too!
+    {
+        $type = ActivityObject::canonicalType($type);
+        foreach ((array)$objects as $object) {
+            if ($type === ActivityObject::canonicalType($object)) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
index ec67d9fe2e30c8dc17172c93886401b0428c43db..50d5ac5cedf4bb485fc31868831c6cdf8ab9cb0c 100644 (file)
@@ -48,7 +48,7 @@ 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
+abstract class MicroAppPlugin extends ActivityHandlerPlugin
 {
     /**
      * Returns a localized string which represents this micro-app,
@@ -70,22 +70,6 @@ abstract class MicroAppPlugin extends Plugin
      */
     abstract function tag();
 
-    /**
-     * Return a list of ActivityStreams object type URIs
-     * which this micro-app handles. Default implementations
-     * of the base class will use this list to check if a
-     * given ActivityStreams object belongs to us, via
-     * $this->isMyNotice() or $this->isMyActivity.
-     *
-     * All micro-app classes must override this method.
-     *
-     * @fixme can we confirm that these types are the same
-     * for Atom and JSON streams? Any limitations or issues?
-     *
-     * @return array of strings
-     */
-    abstract function types();
-
     /**
      * Given a parsed ActivityStreams activity, your plugin
      * gets to figure out how to actually save it into a notice
@@ -170,42 +154,6 @@ abstract class MicroAppPlugin extends Plugin
         return 'new'.$this->tag();
     }
 
-    /**
-     * Check if a given notice object should be handled by this micro-app
-     * plugin.
-     *
-     * The default implementation checks against the activity type list
-     * returned by $this->types(). You can override this method to expand
-     * your checks.
-     *
-     * @param Notice $notice
-     * @return boolean
-     */
-    function isMyNotice($notice) {
-        $types = $this->types();
-        return ($notice->verb == ActivityVerb::POST) && in_array($notice->object_type, $types);
-    }
-
-    /**
-     * Check if a given ActivityStreams activity should be handled by this
-     * micro-app plugin.
-     *
-     * The default implementation checks against the activity type list
-     * returned by $this->types(), and requires that exactly one matching
-     * object be present. You can override this method to expand
-     * your checks or to compare the activity's verb, etc.
-     *
-     * @param Activity $activity
-     * @return boolean
-     */
-    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));
-    }
-
     /**
      * Called when generating Atom XML ActivityStreams output from an
      * ActivityObject belonging to this plugin. Gives the plugin