MicroAppPlugin extends to intermediate ActivityHandlerPlugin

This commit is contained in:
Mikael Nordfeldth 2014-06-24 16:42:34 +02:00
parent db7cc7fa75
commit b864caa665
3 changed files with 118 additions and 53 deletions

View File

@ -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());
}
}

View 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;
}
}

View 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