Attention.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. * Data class for Attentions
  18. *
  19. * @category Data
  20. * @package GNUsocial
  21. * @copyright 2014 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. */
  24. defined('GNUSOCIAL') || die();
  25. class Attention extends Managed_DataObject
  26. {
  27. public $__table = 'attention'; // table name
  28. public $notice_id; // int(4) primary_key not_null
  29. public $profile_id; // int(4) primary_key not_null
  30. public $reason; // varchar(191)
  31. public $created; // datetime()
  32. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  33. public static function schemaDef()
  34. {
  35. return array(
  36. 'description' => 'Notice attentions to profiles (that are not a mention and not result of a subscription)',
  37. 'fields' => array(
  38. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice_id to give attention'),
  39. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile_id for feed receiver'),
  40. 'reason' => array('type' => 'varchar', 'length' => 191, 'description' => 'Optional reason why this was brought to the attention of profile_id'),
  41. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  42. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  43. ),
  44. 'primary key' => array('notice_id', 'profile_id'),
  45. 'foreign keys' => array(
  46. 'attention_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  47. 'attention_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  48. ),
  49. 'indexes' => array(
  50. 'attention_profile_id_idx' => array('profile_id'),
  51. ),
  52. );
  53. }
  54. public static function saveNew(Notice $notice, Profile $target, $reason=null)
  55. {
  56. try {
  57. $att = Attention::getByKeys(['notice_id'=>$notice->getID(), 'profile_id'=>$target->getID()]);
  58. throw new AlreadyFulfilledException('Attention already exists with reason: '._ve($att->reason));
  59. } catch (NoResultException $e) {
  60. $att = new Attention();
  61. $att->notice_id = $notice->getID();
  62. $att->profile_id = $target->getID();
  63. $att->reason = $reason;
  64. $att->created = common_sql_now();
  65. $result = $att->insert();
  66. if ($result === false) {
  67. throw new Exception('Failed Attention::saveNew for notice id=='.$notice->getID().' target id=='.$target->getID().', reason=="'.$reason.'"');
  68. }
  69. }
  70. self::blow('attention:stream:%d', $target->getID());
  71. return $att;
  72. }
  73. }