-function subs_subscribe_to($user, $other) {
-
- if ($user->isSubscribed($other)) {
- return _('Already subscribed!.');
- }
-
- if (!$user->subscribeTo($other)) {
- return _('Could not subscribe.');
- return;
- }
-
- subs_notify($other, $user);
-
- if (common_config('memcached', 'enabled')) {
- $cache = new Memcache();
- if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
- $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
- }
- }
-
- if ($other->autosubscribe && !$other->isSubscribed($user)) {
- if (!$other->subscribeTo($user)) {
- return _('Could not subscribe other to you.');
- }
- if (common_config('memcached', 'enabled')) {
- $cache = new Memcache();
- if ($cache->connect(common_config('memcached', 'server'), common_config('memcached', 'port'))) {
- $cache->delete(common_cache_key('user:notices_with_friends:' . $other->id));
- }
- }
-
- subs_notify($user, $other);
- }
-
- return true;
+/* Subscribe user $user to other user $other.
+ * Note: $other must be a local user, not a remote profile.
+ * Because the other way is quite a bit more complicated.
+ */
+
+function subs_subscribe_to($user, $other)
+{
+ if (!$user->hasRight(Right::SUBSCRIBE)) {
+ return _('You have been banned from subscribing.');
+ }
+
+ if ($user->isSubscribed($other)) {
+ return _('Already subscribed!');
+ }
+
+ if ($other->hasBlocked($user)) {
+ return _('User has blocked you.');
+ }
+
+ try {
+ if (Event::handle('StartSubscribe', array($user, $other))) {
+
+ if (!$user->subscribeTo($other)) {
+ return _('Could not subscribe.');
+ return;
+ }
+
+ subs_notify($other, $user);
+
+ $cache = common_memcache();
+
+ if ($cache) {
+ $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
+ }
+
+ $profile = $user->getProfile();
+
+ $profile->blowSubscriptionsCount();
+ $other->blowSubscribersCount();
+
+ if ($other->autosubscribe && !$other->isSubscribed($user) && !$user->hasBlocked($other)) {
+ if (!$other->subscribeTo($user)) {
+ return _('Could not subscribe other to you.');
+ }
+ $cache = common_memcache();
+
+ if ($cache) {
+ $cache->delete(common_cache_key('user:notices_with_friends:' . $other->id));
+ }
+
+ subs_notify($user, $other);
+ }
+
+ Event::handle('EndSubscribe', array($user, $other));
+ }
+ } catch (Exception $e) {
+ return $e->getMessage();
+ }
+
+ return true;