Subscription_queue.php 3.4 KB

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