]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Subscription_queue.php
Don't store object type for verbs (as they don't have it)
[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     static function exists(Profile $subscriber, Profile $other)
51     {
52         $sub = Subscription_queue::pkeyGet(array('subscriber' => $subscriber->getID(),
53                                                  'subscribed' => $other->getID()));
54         return ($sub instanceof Subscription_queue);
55     }
56
57     static function getSubQueue(Profile $subscriber, Profile $other)
58     {
59         // This is essentially a pkeyGet but we have an object to return in NoResultException
60         $sub = new Subscription_queue();
61         $sub->subscriber = $subscriber->id;
62         $sub->subscribed = $other->id;
63         if (!$sub->find(true)) {
64             throw new NoResultException($sub);
65         }
66         return $sub;
67     }
68
69     /**
70      * Complete a pending subscription, as we've got approval of some sort.
71      *
72      * @return Subscription
73      */
74     public function complete()
75     {
76         $subscriber = Profile::getKV('id', $this->subscriber);
77         $subscribed = Profile::getKV('id', $this->subscribed);
78         try {
79             $sub = Subscription::start($subscriber, $subscribed, Subscription::FORCE);
80             $this->delete();
81         } catch (AlreadyFulfilledException $e) {
82             common_debug('Tried to start a subscription which already existed.');
83         }
84         return $sub;
85     }
86
87     /**
88      * Cancel an outstanding subscription request to the other profile.
89      */
90     public function abort()
91     {
92         $subscriber = Profile::getKV('id', $this->subscriber);
93         $subscribed = Profile::getKV('id', $this->subscribed);
94         if (Event::handle('StartCancelSubscription', array($subscriber, $subscribed))) {
95             $this->delete();
96             Event::handle('EndCancelSubscription', array($subscriber, $subscribed));
97         }
98     }
99
100     /**
101      * Send notifications via email etc to group administrators about
102      * this exciting new pending moderation queue item!
103      */
104     public function notify()
105     {
106         $other = Profile::getKV('id', $this->subscriber);
107         $listenee = User::getKV('id', $this->subscribed);
108         mail_subscribe_pending_notify_profile($listenee, $other);
109     }
110 }