Notice_to_status.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 remembering notice-to-status mappings
  18. *
  19. * @category Data
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2010 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Data class for mapping notices to statuses
  28. *
  29. * Notices flow back and forth between Twitter and GNU social. We use this
  30. * table to remember which GNU social notice corresponds to which Twitter
  31. * status.
  32. *
  33. * Note that notice_id is unique only within a single database; if you
  34. * want to share this data for some reason, get the notice's URI and use
  35. * that instead, since it's universally unique.
  36. *
  37. * @category Action
  38. * @package GNUsocial
  39. * @author Evan Prodromou <evan@status.net>
  40. * @copyright 2010 StatusNet, Inc.
  41. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  42. *
  43. * @see DB_DataObject
  44. */
  45. class Notice_to_status extends Managed_DataObject
  46. {
  47. public $__table = 'notice_to_status'; // table name
  48. public $notice_id; // int(4) primary_key not_null
  49. public $status_id; // bigint not_null
  50. public $created; // datetime()
  51. public $modified; // timestamp() not_null
  52. public static function schemaDef()
  53. {
  54. return array(
  55. 'fields' => array(
  56. 'notice_id' => array('type' => 'int', 'not null' => true, 'description' => 'local notice id'),
  57. 'status_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'twitter status id'),
  58. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  59. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  60. ),
  61. 'primary key' => array('notice_id'),
  62. 'unique keys' => array(
  63. 'notice_to_status_status_id_key' => array('status_id'),
  64. ),
  65. 'foreign keys' => array(
  66. 'notice_to_status_notice_id_fkey' => array('notice', array('notice_id' => 'id')),
  67. ),
  68. );
  69. }
  70. /**
  71. * Save a mapping between a notice and a status
  72. * Warning: status_id values may not fit in 32-bit integers.
  73. *
  74. * @param integer $notice_id ID of the notice in StatusNet
  75. * @param integer $status_id ID of the status in Twitter
  76. *
  77. * @return Notice_to_status new object for this value
  78. */
  79. public static function saveNew($notice_id, $status_id)
  80. {
  81. if (empty($notice_id)) {
  82. throw new Exception("Invalid notice_id $notice_id");
  83. }
  84. $n2s = Notice_to_status::getKV('notice_id', $notice_id);
  85. if (!empty($n2s)) {
  86. return $n2s;
  87. }
  88. if (empty($status_id)) {
  89. throw new Exception("Invalid status_id $status_id");
  90. }
  91. $n2s = Notice_to_status::getKV('status_id', $status_id);
  92. if (!empty($n2s)) {
  93. return $n2s;
  94. }
  95. common_debug("Mapping notice {$notice_id} to Twitter status {$status_id}");
  96. $n2s = new Notice_to_status();
  97. $n2s->notice_id = $notice_id;
  98. $n2s->status_id = $status_id;
  99. $n2s->created = common_sql_now();
  100. $n2s->insert();
  101. return $n2s;
  102. }
  103. }