Attention.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * GNU social - a federating social network
  4. * Copyright (C) 2014, Free Software Foundation, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. class Attention extends Managed_DataObject
  20. {
  21. public $__table = 'attention'; // table name
  22. public $notice_id; // int(4) primary_key not_null
  23. public $profile_id; // int(4) primary_key not_null
  24. public $reason; // varchar(191) not 255 because utf8mb4 takes more space
  25. public $created; // datetime() not_null default_0000-00-00%2000%3A00%3A00
  26. public $modified; // datetime() not_null default_CURRENT_TIMESTAMP
  27. public static function schemaDef()
  28. {
  29. return array(
  30. 'description' => 'Notice attentions to profiles (that are not a mention and not result of a subscription)',
  31. 'fields' => array(
  32. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'notice_id to give attention'),
  33. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'profile_id for feed receiver'),
  34. 'reason' => array('type' => 'varchar', 'length' => 191, 'description' => 'Optional reason why this was brought to the attention of profile_id'),
  35. 'created' => array('type' => 'datetime', 'not null' => true, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
  36. 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'),
  37. ),
  38. 'primary key' => array('notice_id', 'profile_id'),
  39. 'foreign keys' => array(
  40. 'attention_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  41. 'attention_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  42. ),
  43. 'indexes' => array(
  44. 'attention_notice_id_idx' => array('notice_id'),
  45. 'attention_profile_id_idx' => array('profile_id'),
  46. ),
  47. );
  48. }
  49. public static function saveNew(Notice $notice, Profile $target, $reason=null)
  50. {
  51. try {
  52. $att = Attention::getByKeys(['notice_id'=>$notice->getID(), 'profile_id'=>$target->getID()]);
  53. throw new AlreadyFulfilledException('Attention already exists with reason: '._ve($att->reason));
  54. } catch (NoResultException $e) {
  55. $att = new Attention();
  56. $att->notice_id = $notice->getID();
  57. $att->profile_id = $target->getID();
  58. $att->reason = $reason;
  59. $att->created = common_sql_now();
  60. $result = $att->insert();
  61. if ($result === false) {
  62. throw new Exception('Failed Attention::saveNew for notice id=='.$notice->getID().' target id=='.$target->getID().', reason=="'.$reason.'"');
  63. }
  64. }
  65. self::blow('attention:stream:%d', $target->getID());
  66. return $att;
  67. }
  68. }