| 
									
										
										
										
											2011-03-23 16:42:36 -07:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2014-05-05 23:57:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | if (!defined('GNUSOCIAL')) { exit(1); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 16:42:36 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Table Definition for subscription_queue | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Subscription_queue extends Managed_DataObject | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public $__table = 'subscription_queue';       // table name
 | 
					
						
							|  |  |  |     public $subscriber; | 
					
						
							|  |  |  |     public $subscribed; | 
					
						
							|  |  |  |     public $created; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function schemaDef() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return array( | 
					
						
							|  |  |  |             'description' => 'Holder for subscription requests awaiting moderation.', | 
					
						
							|  |  |  |             'fields' => array( | 
					
						
							|  |  |  |                 'subscriber' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile making the request'), | 
					
						
							|  |  |  |                 'subscribed' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile being subscribed to'), | 
					
						
							|  |  |  |                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'primary key' => array('subscriber', 'subscribed'), | 
					
						
							|  |  |  |             'indexes' => array( | 
					
						
							| 
									
										
										
										
											2011-03-28 16:12:51 -07:00
										 |  |  |                 'subscription_queue_subscriber_created_idx' => array('subscriber', 'created'), | 
					
						
							|  |  |  |                 'subscription_queue_subscribed_created_idx' => array('subscribed', 'created'), | 
					
						
							| 
									
										
										
										
											2011-03-23 16:42:36 -07:00
										 |  |  |             ), | 
					
						
							|  |  |  |             'foreign keys' => array( | 
					
						
							| 
									
										
										
										
											2011-03-28 16:12:51 -07:00
										 |  |  |                 'subscription_queue_subscriber_fkey' => array('profile', array('subscriber' => 'id')), | 
					
						
							|  |  |  |                 'subscription_queue_subscribed_fkey' => array('profile', array('subscribed' => 'id')), | 
					
						
							| 
									
										
										
										
											2011-03-23 16:42:36 -07:00
										 |  |  |             ) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-28 16:12:51 -07:00
										 |  |  |     public static function saveNew(Profile $subscriber, Profile $subscribed) | 
					
						
							| 
									
										
										
										
											2011-03-23 16:42:36 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-03-28 16:12:51 -07:00
										 |  |  |         $rq = new Subscription_queue(); | 
					
						
							| 
									
										
										
										
											2011-03-23 16:42:36 -07:00
										 |  |  |         $rq->subscriber = $subscriber->id; | 
					
						
							|  |  |  |         $rq->subscribed = $subscribed->id; | 
					
						
							|  |  |  |         $rq->created = common_sql_now(); | 
					
						
							|  |  |  |         $rq->insert(); | 
					
						
							|  |  |  |         return $rq; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-05 23:57:41 +02:00
										 |  |  |     public function exists(Profile $subscriber, Profile $other) | 
					
						
							| 
									
										
										
										
											2011-03-28 16:12:51 -07:00
										 |  |  |     { | 
					
						
							|  |  |  |         $sub = Subscription_queue::pkeyGet(array('subscriber' => $subscriber->id, | 
					
						
							|  |  |  |                                                  'subscribed' => $other->id)); | 
					
						
							| 
									
										
										
										
											2014-05-05 23:57:41 +02:00
										 |  |  |         return ($sub instanceof Subscription_queue); | 
					
						
							| 
									
										
										
										
											2011-03-28 16:12:51 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-28 15:13:59 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Complete a pending subscription, as we've got approval of some sort. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Subscription | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function complete() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  |         $subscriber = Profile::getKV('id', $this->subscriber); | 
					
						
							|  |  |  |         $subscribed = Profile::getKV('id', $this->subscribed); | 
					
						
							| 
									
										
										
										
											2014-05-05 23:57:41 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $sub = Subscription::start($subscriber, $subscribed, Subscription::FORCE); | 
					
						
							| 
									
										
										
										
											2011-03-28 15:13:59 -07:00
										 |  |  |             $this->delete(); | 
					
						
							| 
									
										
										
										
											2014-05-05 23:57:41 +02:00
										 |  |  |         } catch (AlreadyFulfilledException $e) { | 
					
						
							|  |  |  |             common_debug('Tried to start a subscription which already existed.'); | 
					
						
							| 
									
										
										
										
											2011-03-28 15:13:59 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |         return $sub; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Cancel an outstanding subscription request to the other profile. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2011-03-28 17:06:02 -07:00
										 |  |  |     public function abort() | 
					
						
							| 
									
										
										
										
											2011-03-28 15:13:59 -07:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  |         $subscriber = Profile::getKV('id', $this->subscriber); | 
					
						
							|  |  |  |         $subscribed = Profile::getKV('id', $this->subscribed); | 
					
						
							| 
									
										
										
										
											2011-03-28 15:13:59 -07:00
										 |  |  |         if (Event::handle('StartCancelSubscription', array($subscriber, $subscribed))) { | 
					
						
							|  |  |  |             $this->delete(); | 
					
						
							|  |  |  |             Event::handle('EndCancelSubscription', array($subscriber, $subscribed)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-23 16:42:36 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Send notifications via email etc to group administrators about | 
					
						
							|  |  |  |      * this exciting new pending moderation queue item! | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function notify() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  |         $other = Profile::getKV('id', $this->subscriber); | 
					
						
							|  |  |  |         $listenee = User::getKV('id', $this->subscribed); | 
					
						
							| 
									
										
										
										
											2011-03-28 17:15:48 -07:00
										 |  |  |         mail_subscribe_pending_notify_profile($listenee, $other); | 
					
						
							| 
									
										
										
										
											2011-03-23 16:42:36 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | } |