]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Subscription.php
Ticket #2811 use more consistent max limit for OAuth application registration descrip...
[quix0rs-gnu-social.git] / classes / Subscription.php
index d6fb3fcbdda82f17d70dcce6692868dfed6f1840..e9ad2a5a208e727471db1d59b38b708f334b9998 100644 (file)
@@ -62,39 +62,37 @@ class Subscription extends Memcached_DataObject
 
     static function start($subscriber, $other)
     {
+        // @fixme should we enforce this as profiles in callers instead?
+        if ($subscriber instanceof User) {
+            $subscriber = $subscriber->getProfile();
+        }
+        if ($other instanceof User) {
+            $other = $other->getProfile();
+        }
+
         if (!$subscriber->hasRight(Right::SUBSCRIBE)) {
+            // TRANS: Exception thrown when trying to subscribe while being banned from subscribing.
             throw new Exception(_('You have been banned from subscribing.'));
         }
 
         if (self::exists($subscriber, $other)) {
+            // TRANS: Exception thrown when trying to subscribe while already subscribed.
             throw new Exception(_('Already subscribed!'));
         }
 
         if ($other->hasBlocked($subscriber)) {
+            // TRANS: Exception thrown when trying to subscribe to a user who has blocked the subscribing user.
             throw new Exception(_('User has blocked you.'));
         }
 
         if (Event::handle('StartSubscribe', array($subscriber, $other))) {
-
-            $sub = new Subscription();
-
-            $sub->subscriber = $subscriber->id;
-            $sub->subscribed = $other->id;
-            $sub->created    = common_sql_now();
-
-            $result = $sub->insert();
-
-            if (!$result) {
-                common_log_db_error($sub, 'INSERT', __FILE__);
-                throw new Exception(_('Could not save subscription.'));
-            }
-
+            $sub = self::saveNew($subscriber->id, $other->id);
             $sub->notify();
 
             self::blow('user:notices_with_friends:%d', $subscriber->id);
 
-            $subscriber->blowSubscriptionsCount();
-            $other->blowSubscribersCount();
+            $subscriber->blowSubscriptionCount();
+            $other->blowSubscriberCount();
 
             $otherUser = User::staticGet('id', $other->id);
 
@@ -103,20 +101,11 @@ class Subscription extends Memcached_DataObject
                 !self::exists($other, $subscriber) &&
                 !$subscriber->hasBlocked($other)) {
 
-                $auto = new Subscription();
-
-                $auto->subscriber = $subscriber->id;
-                $auto->subscribed = $other->id;
-                $auto->created    = common_sql_now();
-
-                $result = $auto->insert();
-
-                if (!$result) {
-                    common_log_db_error($auto, 'INSERT', __FILE__);
-                    throw new Exception(_('Could not save subscription.'));
+                try {
+                    self::start($other, $subscriber);
+                } catch (Exception $e) {
+                    common_log(LOG_ERR, "Exception during autosubscribe of {$other->nickname} to profile {$subscriber->id}: {$e->getMessage()}");
                 }
-
-                $auto->notify();
             }
 
             Event::handle('EndSubscribe', array($subscriber, $other));
@@ -125,6 +114,31 @@ class Subscription extends Memcached_DataObject
         return true;
     }
 
+    /**
+     * Low-level subscription save.
+     * Outside callers should use Subscription::start()
+     */
+    protected function saveNew($subscriber_id, $other_id)
+    {
+        $sub = new Subscription();
+
+        $sub->subscriber = $subscriber_id;
+        $sub->subscribed = $other_id;
+        $sub->jabber     = 1;
+        $sub->sms        = 1;
+        $sub->created    = common_sql_now();
+
+        $result = $sub->insert();
+
+        if (!$result) {
+            common_log_db_error($sub, 'INSERT', __FILE__);
+            // TRANS: Exception thrown when a subscription could not be stored on the server.
+            throw new Exception(_('Could not save subscription.'));
+        }
+
+        return $sub;
+    }
+
     function notify()
     {
         # XXX: add other notifications (Jabber, SMS) here
@@ -150,17 +164,18 @@ class Subscription extends Memcached_DataObject
      * Cancel a subscription
      *
      */
-
     function cancel($subscriber, $other)
     {
         if (!self::exists($subscriber, $other)) {
+            // TRANS: Exception thrown when trying to unsibscribe without a subscription.
             throw new Exception(_('Not subscribed!'));
         }
 
         // Don't allow deleting self subs
 
         if ($subscriber->id == $other->id) {
-            throw new Exception(_('Couldn\'t delete self-subscription.'));
+            // TRANS: Exception thrown when trying to unsubscribe a user from themselves.
+            throw new Exception(_('Could not delete self-subscription.'));
         }
 
         if (Event::handle('StartUnsubscribe', array($subscriber, $other))) {
@@ -172,17 +187,41 @@ class Subscription extends Memcached_DataObject
 
             assert(!empty($sub));
 
+            // @todo: move this block to EndSubscribe handler for
+            // OMB plugin when it exists.
+
+            if (!empty($sub->token)) {
+
+                $token = new Token();
+
+                $token->tok    = $sub->token;
+
+                if ($token->find(true)) {
+
+                    $result = $token->delete();
+
+                    if (!$result) {
+                        common_log_db_error($token, 'DELETE', __FILE__);
+                        // TRANS: Exception thrown when the OMB token for a subscription could not deleted on the server.
+                        throw new Exception(_('Could not delete subscription OMB token.'));
+                    }
+                } else {
+                    common_log(LOG_ERR, "Couldn't find credentials with token {$token->tok}");
+                }
+            }
+
             $result = $sub->delete();
 
             if (!$result) {
                 common_log_db_error($sub, 'DELETE', __FILE__);
-                throw new Exception(_('Couldn\'t delete subscription.'));
+                // TRANS: Exception thrown when a subscription could not be deleted on the server.
+                throw new Exception(_('Could not delete subscription.'));
             }
 
             self::blow('user:notices_with_friends:%d', $subscriber->id);
 
-            $subscriber->blowSubscriptionsCount();
-            $other->blowSubscribersCount();
+            $subscriber->blowSubscriptionCount();
+            $other->blowSubscriberCount();
 
             Event::handle('EndUnsubscribe', array($subscriber, $other));
         }
@@ -196,4 +235,33 @@ class Subscription extends Memcached_DataObject
                                            'subscribed' => $other->id));
         return (empty($sub)) ? false : true;
     }
+
+    function asActivity()
+    {
+        $subscriber = Profile::staticGet('id', $this->subscriber);
+        $subscribed = Profile::staticGet('id', $this->subscribed);
+
+        $act = new Activity();
+
+        $act->verb = ActivityVerb::FOLLOW;
+
+        $act->id   = TagURI::mint('follow:%d:%d:%s',
+                                  $subscriber->id,
+                                  $subscribed->id,
+                                  common_date_iso8601($this->created));
+
+        $act->time    = strtotime($this->created);
+        // TRANS: Activity tile when subscribing to another person.
+        $act->title   = _("Follow");
+        // TRANS: Notification given when one person starts following another.
+        // TRANS: %1$s is the subscriber, %2$s is the subscribed.
+        $act->content = sprintf(_('%1$s is now following %2$s.'),
+                               $subscriber->getBestName(),
+                               $subscribed->getBestName());
+
+        $act->actor     = ActivityObject::fromProfile($subscriber);
+        $act->objects[] = ActivityObject::fromProfile($subscribed);
+
+        return $act;
+    }
 }