diff --git a/lib/activityhandlerplugin.php b/lib/activityhandlerplugin.php new file mode 100644 index 0000000000..6767633d61 --- /dev/null +++ b/lib/activityhandlerplugin.php @@ -0,0 +1,106 @@ +. + */ + +if (!defined('GNUSOCIAL')) { exit(1); } + +/** + * Superclass for plugins which add Activity types and such + * + * @category Activity + * @package GNUsocial + * @author Mikael Nordfeldth + * @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()); + } +} diff --git a/lib/activityutils.php b/lib/activityutils.php index c2c239f1d3..8a7039d909 100644 --- a/lib/activityutils.php +++ b/lib/activityutils.php @@ -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; + } } diff --git a/lib/microappplugin.php b/lib/microappplugin.php index ec67d9fe2e..50d5ac5ced 100644 --- a/lib/microappplugin.php +++ b/lib/microappplugin.php @@ -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