]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Subscription_queue.php
Subscription::ensureStart skips AlreadyFulfilledException
[quix0rs-gnu-social.git] / classes / Subscription_queue.php
1 <?php
2
3 if (!defined('GNUSOCIAL')) { exit(1); }
4
5 /**
6  * Table Definition for subscription_queue
7  */
8
9 class Subscription_queue extends Managed_DataObject
10 {
11     public $__table = 'subscription_queue';       // table name
12     public $subscriber;
13     public $subscribed;
14     public $created;
15
16     public static function schemaDef()
17     {
18         return array(
19             'description' => 'Holder for subscription requests awaiting moderation.',
20             'fields' => array(
21                 'subscriber' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile making the request'),
22                 'subscribed' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile being subscribed to'),
23                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
24             ),
25             'primary key' => array('subscriber', 'subscribed'),
26             'indexes' => array(
27                 'subscription_queue_subscriber_created_idx' => array('subscriber', 'created'),
28                 'subscription_queue_subscribed_created_idx' => array('subscribed', 'created'),
29             ),
30             'foreign keys' => array(
31                 'subscription_queue_subscriber_fkey' => array('profile', array('subscriber' => 'id')),
32                 'subscription_queue_subscribed_fkey' => array('profile', array('subscribed' => 'id')),
33             )
34         );
35     }
36
37     public static function saveNew(Profile $subscriber, Profile $subscribed)
38     {
39         if (self::exists($subscriber, $subscribed)) {
40             throw new AlreadyFulfilledException(_('This subscription request is already in progress.'));
41         }
42         $rq = new Subscription_queue();
43         $rq->subscriber = $subscriber->id;
44         $rq->subscribed = $subscribed->id;
45         $rq->created = common_sql_now();
46         $rq->insert();
47         return $rq;
48     }
49
50     public function exists(Profile $subscriber, Profile $other)
51     {
52         $sub = Subscription_queue::pkeyGet(array('subscriber' => $subscriber->id,
53                                                  'subscribed' => $other->id));
54         return ($sub instanceof Subscription_queue);
55     }
56
57     /**
58      * Complete a pending subscription, as we've got approval of some sort.
59      *
60      * @return Subscription
61      */
62     public function complete()
63     {
64         $subscriber = Profile::getKV('id', $this->subscriber);
65         $subscribed = Profile::getKV('id', $this->subscribed);
66         try {
67             $sub = Subscription::start($subscriber, $subscribed, Subscription::FORCE);
68             $this->delete();
69         } catch (AlreadyFulfilledException $e) {
70             common_debug('Tried to start a subscription which already existed.');
71         }
72         return $sub;
73     }
74
75     /**
76      * Cancel an outstanding subscription request to the other profile.
77      */
78     public function abort()
79     {
80         $subscriber = Profile::getKV('id', $this->subscriber);
81         $subscribed = Profile::getKV('id', $this->subscribed);
82         if (Event::handle('StartCancelSubscription', array($subscriber, $subscribed))) {
83             $this->delete();
84             Event::handle('EndCancelSubscription', array($subscriber, $subscribed));
85         }
86     }
87
88     /**
89      * Send notifications via email etc to group administrators about
90      * this exciting new pending moderation queue item!
91      */
92     public function notify()
93     {
94         $other = Profile::getKV('id', $this->subscriber);
95         $listenee = User::getKV('id', $this->subscribed);
96         mail_subscribe_pending_notify_profile($listenee, $other);
97     }
98 }