3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Plugin to throttle subscriptions by a user
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Subscription throttle
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2010 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
47 class SubscriptionThrottlePlugin extends Plugin
49 public $subLimits = array(86400 => 100,
52 public $groupLimits = array(86400 => 50,
56 * Filter subscriptions to see if they're coming too fast.
58 * @param User $user The user subscribing
59 * @param User $other The user being subscribed to
61 * @return boolean hook value
63 function onStartSubscribe($user, $other)
65 foreach ($this->subLimits as $seconds => $limit) {
66 $sub = $this->_getNthSub($user, $limit);
69 $subtime = strtotime($sub->created);
71 if ($now - $subtime < $seconds) {
72 // TRANS: Exception thrown when subscribing too quickly.
73 throw new Exception(_m('Too many subscriptions. Take a break and try again later.'));
82 * Filter group joins to see if they're coming too fast.
84 * @param Group $group The group being joined
85 * @param User $user The user joining
87 * @return boolean hook value
89 function onStartJoinGroup($group, $user)
91 foreach ($this->groupLimits as $seconds => $limit) {
92 $mem = $this->_getNthMem($user, $limit);
95 $jointime = strtotime($mem->created);
97 if ($now - $jointime < $seconds) {
98 // TRANS: Exception thrown when joing groups too quickly.
99 throw new Exception(_m('Too many memberships. Take a break and try again later.'));
108 * Get the Nth most recent subscription for this user
110 * @param User $user The user to get subscriptions for
111 * @param integer $n How far to count back
113 * @return Subscription a subscription or null
115 private function _getNthSub($user, $n)
117 $sub = new Subscription();
119 $sub->subscriber = $user->id;
120 $sub->orderBy('created DESC');
121 $sub->limit($n - 1, 1);
123 if ($sub->find(true)) {
131 * Get the Nth most recent group membership for this user
133 * @param User $user The user to get memberships for
134 * @param integer $n How far to count back
136 * @return Group_member a membership or null
138 private function _getNthMem($user, $n)
140 $mem = new Group_member();
142 $mem->profile_id = $user->id;
143 $mem->orderBy('created DESC');
144 $mem->limit($n - 1, 1);
146 if ($mem->find(true)) {
154 * Return plugin version data for display
156 * @param array &$versions Array of version arrays
158 * @return boolean hook value
160 function onPluginVersion(&$versions)
162 $versions[] = array('name' => 'SubscriptionThrottle',
163 'version' => STATUSNET_VERSION,
164 'author' => 'Evan Prodromou',
165 'homepage' => 'http://status.net/wiki/Plugin:SubscriptionThrottle',
167 // TRANS: Plugin description.
168 _m('Configurable limits for subscriptions and group memberships.'));