]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Subscription_queue.php
If there's no Happening, we can't use the RSVP.
[quix0rs-gnu-social.git] / classes / Subscription_queue.php
index e7572ae7258654ad6681e448cf01ad31f38d559f..a60293e2158e9bf8b42ff6f01ff079b1a9056c1b 100644 (file)
@@ -1,30 +1,18 @@
 <?php
+
+if (!defined('GNUSOCIAL')) { exit(1); }
+
 /**
  * Table Definition for subscription_queue
  */
-require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
 
 class Subscription_queue extends Managed_DataObject
 {
-    ###START_AUTOCODE
-    /* the code below is auto generated do not remove the above tag */
-
     public $__table = 'subscription_queue';       // table name
     public $subscriber;
     public $subscribed;
     public $created;
 
-    /* Static get */
-    function staticGet($k,$v=null)
-    { return Memcached_DataObject::staticGet('Subscription_queue',$k,$v); }
-
-    /* Pkey get */
-    function pkeyGet($k)
-    { return Memcached_DataObject::pkeyGet('Subscription_queue',$k); }
-
-    /* the code above is auto generated do not remove the tag below */
-    ###END_AUTOCODE
-
     public static function schemaDef()
     {
         return array(
@@ -36,19 +24,22 @@ class Subscription_queue extends Managed_DataObject
             ),
             'primary key' => array('subscriber', 'subscribed'),
             'indexes' => array(
-                'group_join_queue_profile_id_created_idx' => array('subscriber', 'created'),
-                'group_join_queue_group_id_created_idx' => array('subscribed', 'created'),
+                'subscription_queue_subscriber_created_idx' => array('subscriber', 'created'),
+                'subscription_queue_subscribed_created_idx' => array('subscribed', 'created'),
             ),
             'foreign keys' => array(
-                'group_join_queue_subscriber_fkey' => array('profile', array('subscriber' => 'id')),
-                'group_join_queue_subscribed_fkey' => array('profile', array('subscribed' => 'id')),
+                'subscription_queue_subscriber_fkey' => array('profile', array('subscriber' => 'id')),
+                'subscription_queue_subscribed_fkey' => array('profile', array('subscribed' => 'id')),
             )
         );
     }
 
-    public static function saveNew(Profile $subscriber, Profile $other)
+    public static function saveNew(Profile $subscriber, Profile $subscribed)
     {
-        $rq = new Group_join_queue();
+        if (self::exists($subscriber, $subscribed)) {
+            throw new AlreadyFulfilledException(_('This subscription request is already in progress.'));
+        }
+        $rq = new Subscription_queue();
         $rq->subscriber = $subscriber->id;
         $rq->subscribed = $subscribed->id;
         $rq->created = common_sql_now();
@@ -56,6 +47,25 @@ class Subscription_queue extends Managed_DataObject
         return $rq;
     }
 
+    static function exists(Profile $subscriber, Profile $other)
+    {
+        $sub = Subscription_queue::pkeyGet(array('subscriber' => $subscriber->getID(),
+                                                 'subscribed' => $other->getID()));
+        return ($sub instanceof Subscription_queue);
+    }
+
+    static function getSubQueue(Profile $subscriber, Profile $other)
+    {
+        // This is essentially a pkeyGet but we have an object to return in NoResultException
+        $sub = new Subscription_queue();
+        $sub->subscriber = $subscriber->id;
+        $sub->subscribed = $other->id;
+        if (!$sub->find(true)) {
+            throw new NoResultException($sub);
+        }
+        return $sub;
+    }
+
     /**
      * Complete a pending subscription, as we've got approval of some sort.
      *
@@ -63,11 +73,13 @@ class Subscription_queue extends Managed_DataObject
      */
     public function complete()
     {
-        $subscriber = Profile::staticGet('id', $this->subscriber);
-        $subscribed = Profile::staticGet('id', $this->subscribed);
-        $sub = Subscription::start($subscriber, $other, Subscription::FORCE);
-        if ($sub) {
+        $subscriber = Profile::getKV('id', $this->subscriber);
+        $subscribed = Profile::getKV('id', $this->subscribed);
+        try {
+            $sub = Subscription::start($subscriber, $subscribed, Subscription::FORCE);
             $this->delete();
+        } catch (AlreadyFulfilledException $e) {
+            common_debug('Tried to start a subscription which already existed.');
         }
         return $sub;
     }
@@ -75,10 +87,10 @@ class Subscription_queue extends Managed_DataObject
     /**
      * Cancel an outstanding subscription request to the other profile.
      */
-    public function abort($profile)
+    public function abort()
     {
-        $subscriber = Profile::staticGet('id', $this->subscriber);
-        $subscribed = Profile::staticGet('id', $this->subscribed);
+        $subscriber = Profile::getKV('id', $this->subscriber);
+        $subscribed = Profile::getKV('id', $this->subscribed);
         if (Event::handle('StartCancelSubscription', array($subscriber, $subscribed))) {
             $this->delete();
             Event::handle('EndCancelSubscription', array($subscriber, $subscribed));
@@ -91,8 +103,8 @@ class Subscription_queue extends Managed_DataObject
      */
     public function notify()
     {
-        $subscriber = Profile::staticGet('id', $this->subscriber);
-        $subscribed = Profile::staticGet('id', $this->subscribed);
-        mail_notify_subscription_pending($subscribed, $subscriber);
+        $other = Profile::getKV('id', $this->subscriber);
+        $listenee = User::getKV('id', $this->subscribed);
+        mail_subscribe_pending_notify_profile($listenee, $other);
     }
 }