]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityhandlerplugin.php
More Favorite pluginification (favecount, cache, menus(favecount, cache, menus))
[quix0rs-gnu-social.git] / lib / activityhandlerplugin.php
1 <?php
2 /*  
3  * GNU Social - a federating social network  
4  * Copyright (C) 2014, Free Software Foundation, Inc.
5  *
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.  
10  *  
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.
15  *  
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/>.  
18  */
19
20 if (!defined('GNUSOCIAL')) { exit(1); }
21
22 /**
23  * Superclass for plugins which add Activity types and such
24  *
25  * @category  Activity
26  * @package   GNUsocial
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
31  */
32 abstract class ActivityHandlerPlugin extends Plugin
33 {
34     /**
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.
40      *
41      * An empty list means any type is ok. (Favorite verb etc.)
42      *
43      * All micro-app classes must override this method.
44      *
45      * @return array of strings
46      */
47     abstract function types();
48
49     /**
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.
55      *
56      * All micro-app classes must override this method.
57      *
58      * @return array of strings
59      */
60     function verbs() {
61         return array(ActivityVerb::POST);
62     }
63
64     /**
65      * Check if a given ActivityStreams activity should be handled by this
66      * micro-app plugin.
67      *
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.
72      *
73      * @param Activity $activity
74      * @return boolean
75      */
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));
81     }
82
83     /**
84      * Check if a given notice object should be handled by this micro-app
85      * plugin.
86      * 
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. 
90      * 
91      * @param Notice $notice 
92      * @return boolean 
93      */ 
94     function isMyNotice(Notice $notice) {
95         return $this->isMyVerb($notice->verb) && $this->isMyType($notice->object_type);
96     }
97
98     function isMyVerb($verb) {
99         $verb = $verb ?: ActivityVerb::POST;    // post is the default verb
100         return ActivityUtils::compareTypes($verb, $this->verbs());
101     }
102
103     function isMyType($type) {
104         return count($this->types())===0 || ActivityUtils::compareTypes($type, $this->types());
105     }
106 }