Subscription_queue.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Table Definition for subscription_queue
  18. */
  19. defined('GNUSOCIAL') || die();
  20. class Subscription_queue extends Managed_DataObject
  21. {
  22. public $__table = 'subscription_queue'; // table name
  23. public $subscriber;
  24. public $subscribed;
  25. public $created;
  26. public static function schemaDef()
  27. {
  28. return array(
  29. 'description' => 'Holder for subscription requests awaiting moderation.',
  30. 'fields' => array(
  31. 'subscriber' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile making the request'),
  32. 'subscribed' => array('type' => 'int', 'not null' => true, 'description' => 'remote or local profile being subscribed to'),
  33. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  34. ),
  35. 'primary key' => array('subscriber', 'subscribed'),
  36. 'indexes' => array(
  37. 'subscription_queue_subscriber_created_idx' => array('subscriber', 'created'),
  38. 'subscription_queue_subscribed_created_idx' => array('subscribed', 'created'),
  39. ),
  40. 'foreign keys' => array(
  41. 'subscription_queue_subscriber_fkey' => array('profile', array('subscriber' => 'id')),
  42. 'subscription_queue_subscribed_fkey' => array('profile', array('subscribed' => 'id')),
  43. )
  44. );
  45. }
  46. public static function saveNew(Profile $subscriber, Profile $subscribed)
  47. {
  48. if (self::exists($subscriber, $subscribed)) {
  49. throw new AlreadyFulfilledException(_('This subscription request is already in progress.'));
  50. }
  51. $rq = new Subscription_queue();
  52. $rq->subscriber = $subscriber->id;
  53. $rq->subscribed = $subscribed->id;
  54. $rq->created = common_sql_now();
  55. $rq->insert();
  56. return $rq;
  57. }
  58. public static function exists(Profile $subscriber, Profile $other)
  59. {
  60. $sub = Subscription_queue::pkeyGet(array('subscriber' => $subscriber->getID(),
  61. 'subscribed' => $other->getID()));
  62. return ($sub instanceof Subscription_queue);
  63. }
  64. public static function getSubQueue(Profile $subscriber, Profile $other)
  65. {
  66. // This is essentially a pkeyGet but we have an object to return in NoResultException
  67. $sub = new Subscription_queue();
  68. $sub->subscriber = $subscriber->id;
  69. $sub->subscribed = $other->id;
  70. if (!$sub->find(true)) {
  71. throw new NoResultException($sub);
  72. }
  73. return $sub;
  74. }
  75. /**
  76. * Complete a pending subscription, as we've got approval of some sort.
  77. *
  78. * @return Subscription
  79. */
  80. public function complete()
  81. {
  82. $subscriber = Profile::getKV('id', $this->subscriber);
  83. $subscribed = Profile::getKV('id', $this->subscribed);
  84. try {
  85. $sub = Subscription::start($subscriber, $subscribed, Subscription::FORCE);
  86. $this->delete();
  87. } catch (AlreadyFulfilledException $e) {
  88. common_debug('Tried to start a subscription which already existed.');
  89. }
  90. return $sub;
  91. }
  92. /**
  93. * Cancel an outstanding subscription request to the other profile.
  94. */
  95. public function abort()
  96. {
  97. $subscriber = Profile::getKV('id', $this->subscriber);
  98. $subscribed = Profile::getKV('id', $this->subscribed);
  99. if (Event::handle('StartCancelSubscription', array($subscriber, $subscribed))) {
  100. $this->delete();
  101. Event::handle('EndCancelSubscription', array($subscriber, $subscribed));
  102. }
  103. }
  104. /**
  105. * Send notifications via email etc to group administrators about
  106. * this exciting new pending moderation queue item!
  107. */
  108. public function notify()
  109. {
  110. $other = Profile::getKV('id', $this->subscriber);
  111. $listenee = User::getKV('id', $this->subscribed);
  112. mail_subscribe_pending_notify_profile($listenee, $other);
  113. }
  114. }