3 * GNU Social - a federating social network
4 * Copyright (C) 2014, Free Software Foundation, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('GNUSOCIAL')) { exit(1); }
23 * Superclass for plugins which add Activity types and such
27 * @author Mikael Nordfeldth <mmn@hethane.se>
28 * @copyright 2014 Free Software Foundation, Inc.
29 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
30 * @link http://gnu.io/social
32 abstract class ActivityHandlerPlugin extends Plugin
35 * Return a list of ActivityStreams object type IRIs
36 * which this micro-app handles. Default implementations
37 * of the base class will use this list to check if a
38 * given ActivityStreams object belongs to us, via
39 * $this->isMyNotice() or $this->isMyActivity.
41 * An empty list means any type is ok. (Favorite verb etc.)
43 * All micro-app classes must override this method.
45 * @return array of strings
47 abstract function types();
50 * Return a list of ActivityStreams verb IRIs which
51 * this micro-app handles. Default implementations
52 * of the base class will use this list to check if a
53 * given ActivityStreams verb belongs to us, via
54 * $this->isMyNotice() or $this->isMyActivity.
56 * All micro-app classes must override this method.
58 * @return array of strings
61 return array(ActivityVerb::POST);
65 * Check if a given ActivityStreams activity should be handled by this
68 * The default implementation checks against the activity type list
69 * returned by $this->types(), and requires that exactly one matching
70 * object be present. You can override this method to expand
71 * your checks or to compare the activity's verb, etc.
73 * @param Activity $activity
76 function isMyActivity(Activity $act) {
77 return (count($act->objects) == 1
78 && ($act->objects[0] instanceof ActivityObject)
79 && $this->isMyVerb($act->verb)
80 && $this->isMyType($act->objects[0]->type));
84 * Check if a given notice object should be handled by this micro-app
87 * The default implementation checks against the activity type list
88 * returned by $this->types(). You can override this method to expand
89 * your checks, but follow the execution chain to get it right.
91 * @param Notice $notice
94 function isMyNotice(Notice $notice) {
95 return $this->isMyVerb($notice->verb) && $this->isMyType($notice->object_type);
98 function isMyVerb($verb) {
99 $verb = $verb ?: ActivityVerb::POST; // post is the default verb
100 return ActivityUtils::compareTypes($verb, $this->verbs());
103 function isMyType($type) {
104 return count($this->types())===0 || ActivityUtils::compareTypes($type, $this->types());