Twitter_synch_status.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * Store last-touched ID for various timelines
  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. require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
  27. /**
  28. * Store various timeline data
  29. *
  30. * We don't want to keep re-fetching the same statuses and direct messages from Twitter.
  31. * So, we store the last ID we see from a timeline, and store it. Next time
  32. * around, we use that ID in the since_id parameter.
  33. *
  34. * @category Action
  35. * @copyright 2010 StatusNet, Inc.
  36. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  37. *
  38. * @see DB_DataObject
  39. */
  40. class Twitter_synch_status extends Managed_DataObject
  41. {
  42. public $__table = 'twitter_synch_status'; // table name
  43. public $foreign_id; // bigint primary_key not_null
  44. public $timeline; // varchar(191) primary_key not_null not 255 because utf8mb4 takes more space
  45. public $last_id; // bigint not_null
  46. public $created; // datetime() not_null
  47. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  48. public static function schemaDef()
  49. {
  50. return array(
  51. 'fields' => array(
  52. 'foreign_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'Foreign message ID'),
  53. 'timeline' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'timeline name'),
  54. 'last_id' => array('type' => 'int', 'size' => 'big', 'not null' => true, 'description' => 'last id fetched'),
  55. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  56. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  57. ),
  58. 'primary key' => array('foreign_id', 'timeline'),
  59. );
  60. }
  61. public static function getLastId($foreign_id, $timeline)
  62. {
  63. $tss = self::pkeyGet(array('foreign_id' => $foreign_id,
  64. 'timeline' => $timeline));
  65. if (empty($tss)) {
  66. return null;
  67. } else {
  68. return $tss->last_id;
  69. }
  70. }
  71. public static function setLastId($foreign_id, $timeline, $last_id)
  72. {
  73. $tss = self::pkeyGet(array('foreign_id' => $foreign_id,
  74. 'timeline' => $timeline));
  75. if (empty($tss)) {
  76. $tss = new Twitter_synch_status();
  77. $tss->foreign_id = $foreign_id;
  78. $tss->timeline = $timeline;
  79. $tss->last_id = $last_id;
  80. $tss->created = common_sql_now();
  81. $tss->modified = $tss->created;
  82. $tss->insert();
  83. return true;
  84. } else {
  85. $orig = clone($tss);
  86. $tss->last_id = $last_id;
  87. $tss->modified = common_sql_now();
  88. $tss->update();
  89. return true;
  90. }
  91. }
  92. }