Attention.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 $is_mariadb;
  28. public $__table = 'attention'; // table name
  29. public $notice_id; // int(4) primary_key not_null
  30. public $profile_id; // int(4) primary_key not_null
  31. public $reason; // varchar(191)
  32. public $created; // datetime()
  33. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  34. public static function schemaDef()
  35. {
  36. return array(
  37. 'description' => 'Notice attentions to profiles (that are not a mention and not result of a subscription)',
  38. 'fields' => array(
  39. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice_id to give attention'),
  40. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile_id for feed receiver'),
  41. 'reason' => array('type' => 'varchar', 'length' => 191, 'description' => 'Optional reason why this was brought to the attention of profile_id'),
  42. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  43. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  44. ),
  45. 'primary key' => array('notice_id', 'profile_id'),
  46. 'foreign keys' => array(
  47. 'attention_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  48. 'attention_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  49. ),
  50. 'indexes' => array(
  51. 'attention_profile_id_idx' => array('profile_id'),
  52. ),
  53. );
  54. }
  55. public static function saveNew(Notice $notice, Profile $target, $reason=null)
  56. {
  57. try {
  58. $att = Attention::getByKeys(['notice_id'=>$notice->getID(), 'profile_id'=>$target->getID()]);
  59. throw new AlreadyFulfilledException('Attention already exists with reason: '._ve($att->reason));
  60. } catch (NoResultException $e) {
  61. $att = new Attention();
  62. $att->notice_id = $notice->getID();
  63. $att->profile_id = $target->getID();
  64. $att->reason = $reason;
  65. $att->created = common_sql_now();
  66. $result = $att->insert();
  67. if ($result === false) {
  68. throw new Exception('Failed Attention::saveNew for notice id=='.$notice->getID().' target id=='.$target->getID().', reason=="'.$reason.'"');
  69. }
  70. }
  71. self::blow('attention:stream:%d', $target->getID());
  72. return $att;
  73. }
  74. }