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/
48 class SubscriptionThrottlePlugin extends Plugin
50 public $subLimits = array(86400 => 100,
53 public $groupLimits = array(86400 => 50,
57 * Filter subscriptions to see if they're coming too fast.
59 * @param User $user The user subscribing
60 * @param User $other The user being subscribed to
62 * @return boolean hook value
65 function onStartSubscribe($user, $other)
67 foreach ($this->subLimits as $seconds => $limit) {
68 $sub = $this->_getNthSub($user, $limit);
71 $subtime = strtotime($sub->created);
73 if ($now - $subtime < $seconds) {
74 throw new Exception(_("Too many subscriptions. Take a break and try again later."));
83 * Filter group joins to see if they're coming too fast.
85 * @param Group $group The group being joined
86 * @param User $user The user joining
88 * @return boolean hook value
91 function onStartJoinGroup($group, $user)
93 foreach ($this->groupLimits as $seconds => $limit) {
94 $mem = $this->_getNthMem($user, $limit);
97 $jointime = strtotime($mem->created);
99 if ($now - $jointime < $seconds) {
100 throw new Exception(_("Too many memberships. Take a break and try again later."));
109 * Get the Nth most recent subscription for this user
111 * @param User $user The user to get subscriptions for
112 * @param integer $n How far to count back
114 * @return Subscription a subscription or null
117 private function _getNthSub($user, $n)
119 $sub = new Subscription();
121 $sub->subscriber = $user->id;
122 $sub->orderBy('created DESC');
123 $sub->limit($n - 1, 1);
125 if ($sub->find(true)) {
133 * Get the Nth most recent group membership for this user
135 * @param User $user The user to get memberships for
136 * @param integer $n How far to count back
138 * @return Group_member a membership or null
141 private function _getNthMem($user, $n)
143 $mem = new Group_member();
145 $mem->profile_id = $user->id;
146 $mem->orderBy('created DESC');
147 $mem->limit($n - 1, 1);
149 if ($mem->find(true)) {
157 * Return plugin version data for display
159 * @param array &$versions Array of version arrays
161 * @return boolean hook value
164 function onPluginVersion(&$versions)
166 $versions[] = array('name' => 'SubscriptionThrottle',
167 'version' => STATUSNET_VERSION,
168 'author' => 'Evan Prodromou',
169 'homepage' => 'http://status.net/wiki/Plugin:SubscriptionThrottle',
171 _m('Configurable limits for subscriptions and group memberships.'));