Profile_tag_subscription.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 profile_tag_subscription
  18. */
  19. defined('GNUSOCIAL') || die();
  20. class Profile_tag_subscription extends Managed_DataObject
  21. {
  22. ###START_AUTOCODE
  23. /* the code below is auto generated do not remove the above tag */
  24. public $__table = 'profile_tag_subscription'; // table name
  25. public $profile_tag_id; // int(4) not_null
  26. public $profile_id; // int(4) not_null
  27. public $created; // datetime()
  28. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  29. /* the code above is auto generated do not remove the tag below */
  30. ###END_AUTOCODE
  31. public static function schemaDef()
  32. {
  33. return array(
  34. 'fields' => array(
  35. 'profile_tag_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile_tag'),
  36. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
  37. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  38. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  39. ),
  40. 'primary key' => array('profile_tag_id', 'profile_id'),
  41. 'foreign keys' => array(
  42. 'profile_tag_subscription_profile_list_id_fkey' => array('profile_list', array('profile_tag_id' => 'id')),
  43. 'profile_tag_subscription_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  44. ),
  45. 'indexes' => array(
  46. // @fixme probably we want a (profile_id, created) index here?
  47. 'profile_tag_subscription_profile_id_idx' => array('profile_id'),
  48. 'profile_tag_subscription_created_idx' => array('created'),
  49. ),
  50. );
  51. }
  52. public static function add($peopletag, $profile)
  53. {
  54. if ($peopletag->private) {
  55. return false;
  56. }
  57. if (Event::handle('StartSubscribePeopletag', array($peopletag, $profile))) {
  58. $args = array('profile_tag_id' => $peopletag->id,
  59. 'profile_id' => $profile->id);
  60. $existing = Profile_tag_subscription::pkeyGet($args);
  61. if (!empty($existing)) {
  62. return $existing;
  63. }
  64. $sub = new Profile_tag_subscription();
  65. $sub->profile_tag_id = $peopletag->id;
  66. $sub->profile_id = $profile->id;
  67. $sub->created = common_sql_now();
  68. $result = $sub->insert();
  69. if (!$result) {
  70. common_log_db_error($sub, 'INSERT', __FILE__);
  71. // TRANS: Exception thrown when inserting a list subscription in the database fails.
  72. throw new Exception(_('Adding list subscription failed.'));
  73. }
  74. $ptag = Profile_list::getKV('id', $peopletag->id);
  75. $ptag->subscriberCount(true);
  76. Event::handle('EndSubscribePeopletag', array($peopletag, $profile));
  77. return $ptag;
  78. }
  79. }
  80. public static function remove($peopletag, $profile)
  81. {
  82. $sub = Profile_tag_subscription::pkeyGet(array('profile_tag_id' => $peopletag->id,
  83. 'profile_id' => $profile->id));
  84. if (empty($sub)) {
  85. // silence is golden?
  86. return true;
  87. }
  88. if (Event::handle('StartUnsubscribePeopletag', array($peopletag, $profile))) {
  89. $result = $sub->delete();
  90. if (!$result) {
  91. common_log_db_error($sub, 'DELETE', __FILE__);
  92. // TRANS: Exception thrown when deleting a list subscription from the database fails.
  93. throw new Exception(_('Removing list subscription failed.'));
  94. }
  95. $peopletag->subscriberCount(true);
  96. Event::handle('EndUnsubscribePeopletag', array($peopletag, $profile));
  97. return true;
  98. }
  99. }
  100. // called if a tag gets deleted / made private
  101. public static function cleanup($profile_list)
  102. {
  103. $subs = new self();
  104. $subs->profile_tag_id = $profile_list->id;
  105. $subs->find();
  106. while ($subs->fetch()) {
  107. $profile = Profile::getKV('id', $subs->profile_id);
  108. Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
  109. // Delete anyway
  110. $subs->delete();
  111. Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
  112. }
  113. }
  114. public function insert()
  115. {
  116. $result = parent::insert();
  117. if ($result) {
  118. self::blow(
  119. 'profile_list:subscriber_count:%d',
  120. $this->profile_tag_id
  121. );
  122. }
  123. return $result;
  124. }
  125. public function delete($useWhere = false)
  126. {
  127. $result = parent::delete($useWhere);
  128. if ($result !== false) {
  129. self::blow(
  130. 'profile_list:subscriber_count:%d',
  131. $this->profile_tag_id
  132. );
  133. }
  134. return $result;
  135. }
  136. }