Subscription_queue.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if (self::exists($subscriber, $subscribed)) {
  35. throw new AlreadyFulfilledException(_('This subscription request is already in progress.'));
  36. }
  37. $rq = new Subscription_queue();
  38. $rq->subscriber = $subscriber->id;
  39. $rq->subscribed = $subscribed->id;
  40. $rq->created = common_sql_now();
  41. $rq->insert();
  42. return $rq;
  43. }
  44. static function exists(Profile $subscriber, Profile $other)
  45. {
  46. $sub = Subscription_queue::pkeyGet(array('subscriber' => $subscriber->getID(),
  47. 'subscribed' => $other->getID()));
  48. return ($sub instanceof Subscription_queue);
  49. }
  50. static function getSubQueue(Profile $subscriber, Profile $other)
  51. {
  52. // This is essentially a pkeyGet but we have an object to return in NoResultException
  53. $sub = new Subscription_queue();
  54. $sub->subscriber = $subscriber->id;
  55. $sub->subscribed = $other->id;
  56. if (!$sub->find(true)) {
  57. throw new NoResultException($sub);
  58. }
  59. return $sub;
  60. }
  61. /**
  62. * Complete a pending subscription, as we've got approval of some sort.
  63. *
  64. * @return Subscription
  65. */
  66. public function complete()
  67. {
  68. $subscriber = Profile::getKV('id', $this->subscriber);
  69. $subscribed = Profile::getKV('id', $this->subscribed);
  70. try {
  71. $sub = Subscription::start($subscriber, $subscribed, Subscription::FORCE);
  72. $this->delete();
  73. } catch (AlreadyFulfilledException $e) {
  74. common_debug('Tried to start a subscription which already existed.');
  75. }
  76. return $sub;
  77. }
  78. /**
  79. * Cancel an outstanding subscription request to the other profile.
  80. */
  81. public function abort()
  82. {
  83. $subscriber = Profile::getKV('id', $this->subscriber);
  84. $subscribed = Profile::getKV('id', $this->subscribed);
  85. if (Event::handle('StartCancelSubscription', array($subscriber, $subscribed))) {
  86. $this->delete();
  87. Event::handle('EndCancelSubscription', array($subscriber, $subscribed));
  88. }
  89. }
  90. /**
  91. * Send notifications via email etc to group administrators about
  92. * this exciting new pending moderation queue item!
  93. */
  94. public function notify()
  95. {
  96. $other = Profile::getKV('id', $this->subscriber);
  97. $listenee = User::getKV('id', $this->subscribed);
  98. mail_subscribe_pending_notify_profile($listenee, $other);
  99. }
  100. }