]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Subscription_queue.php
utf8mb4 conversion on database with index adjusts
[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         $rq = new Subscription_queue();
40         $rq->subscriber = $subscriber->id;
41         $rq->subscribed = $subscribed->id;
42         $rq->created = common_sql_now();
43         $rq->insert();
44         return $rq;
45     }
46
47     public function exists(Profile $subscriber, Profile $other)
48     {
49         $sub = Subscription_queue::pkeyGet(array('subscriber' => $subscriber->id,
50                                                  'subscribed' => $other->id));
51         return ($sub instanceof Subscription_queue);
52     }
53
54     /**
55      * Complete a pending subscription, as we've got approval of some sort.
56      *
57      * @return Subscription
58      */
59     public function complete()
60     {
61         $subscriber = Profile::getKV('id', $this->subscriber);
62         $subscribed = Profile::getKV('id', $this->subscribed);
63         try {
64             $sub = Subscription::start($subscriber, $subscribed, Subscription::FORCE);
65             $this->delete();
66         } catch (AlreadyFulfilledException $e) {
67             common_debug('Tried to start a subscription which already existed.');
68         }
69         return $sub;
70     }
71
72     /**
73      * Cancel an outstanding subscription request to the other profile.
74      */
75     public function abort()
76     {
77         $subscriber = Profile::getKV('id', $this->subscriber);
78         $subscribed = Profile::getKV('id', $this->subscribed);
79         if (Event::handle('StartCancelSubscription', array($subscriber, $subscribed))) {
80             $this->delete();
81             Event::handle('EndCancelSubscription', array($subscriber, $subscribed));
82         }
83     }
84
85     /**
86      * Send notifications via email etc to group administrators about
87      * this exciting new pending moderation queue item!
88      */
89     public function notify()
90     {
91         $other = Profile::getKV('id', $this->subscriber);
92         $listenee = User::getKV('id', $this->subscribed);
93         mail_subscribe_pending_notify_profile($listenee, $other);
94     }
95 }