]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ForceGroup/ForceGroupPlugin.php
[VersionBump] 1.19.0, fairly late
[quix0rs-gnu-social.git] / plugins / ForceGroup / ForceGroupPlugin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, 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 /**
21  * @package ForceGroupPlugin
22  * @maintainer Brion Vibber <brion@status.net>
23  */
24
25 if (!defined('STATUSNET')) { exit(1); }
26
27 class ForceGroupPlugin extends Plugin
28 {
29     const PLUGIN_VERSION = '2.0.0';
30
31     /**
32      * Members of these groups will have all their posts mirrored into
33      * the group even if they don't explicitly mention it.
34      *
35      * List by local nickname.
36      */
37     public $post = array();
38
39     /**
40      * New user registrations will automatically join these groups on
41      * registration. They're not prevented from leaving, however.
42      *
43      * List by local nickname.
44      */
45     public $join = array();
46
47     /**
48      * If poster is in one of the forced groups, make sure their notice
49      * gets saved into that group even if not explicitly mentioned.
50      *
51      * @param Notice $notice
52      * @return boolean event hook return
53      */
54     function onStartNoticeDistribute($notice)
55     {
56         $profile = $notice->getProfile();
57
58         $isRemote = !(User::getKV('id', $profile->id));
59         if ($isRemote) {
60             /*
61              * Notices from remote users on other sites
62              * will normally not end up here unless they're
63              * specifically directed here, e.g.: via explicit
64              * post to a remote (to them) group. But remote
65              * notices can also be `pulled in' as a result of
66              * local users subscribing to the remote user;
67              * from the remote user's perspective, this results
68              * in group-forcing appearing effectively random.
69              * So let's be consistent, and just never force
70              * incoming remote notices into a ForceGroup:
71              */
72             return true;
73         }
74
75         foreach ($this->post as $nickname) {
76             $group = User_group::getForNickname($nickname);
77             if ($group && $profile->isMember($group)) {
78                 $notice->addToGroupInbox($group);
79             }
80         }
81         return true;
82     }
83
84     public function onEndUserRegister(Profile $profile)
85     {
86         foreach ($this->join as $nickname) {
87             $group = User_group::getForNickname($nickname);
88             if ($group && !$profile->isMember($group)) {
89                 try {
90                     $profile->joinGroup($group);
91                 } catch (Exception $e) {
92                     // TRANS: Server exception.
93                     // TRANS: %1$s is a user nickname, %2$s is a group nickname.
94                     throw new ServerException(sprintf(_m('Could not join user %1$s to group %2$s.'),
95                                                $profile->nickname, $group->nickname));
96                 }
97             }
98         }
99     }
100
101     /**
102      * Provide plugin version information.
103      *
104      * This data is used when showing the version page.
105      *
106      * @param array &$versions array of version data arrays; see EVENTS.txt
107      *
108      * @return boolean hook value
109      */
110     function onPluginVersion(array &$versions)
111     {
112         $url = 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/ForceGroup';
113
114         $versions[] = array('name' => 'ForceGroup',
115             'version' => self::PLUGIN_VERSION,
116             'author' => 'Brion Vibber',
117             'homepage' => $url,
118             'rawdescription' =>
119             // TRANS: Plugin description.
120             _m('Allows forced group memberships and forces all notices to appear in groups that users were forced in.'));
121
122         return true;
123     }
124 }