Conversation.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Data class for Conversations
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Data
  23. * @package StatusNet
  24. * @author Zach Copley <zach@status.net>
  25. * @author Mikael Nordfeldth <mmn@hethane.se>
  26. * @copyright 2010 StatusNet Inc.
  27. * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('GNUSOCIAL')) { exit(1); }
  32. class Conversation extends Managed_DataObject
  33. {
  34. public $__table = 'conversation'; // table name
  35. public $id; // int(4) primary_key not_null auto_increment
  36. public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space
  37. public $created; // datetime not_null
  38. public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
  39. public static function schemaDef()
  40. {
  41. return array(
  42. 'fields' => array(
  43. 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique identifier, (again) unrelated to notice id since 2016-01-06'),
  44. 'uri' => array('type' => 'varchar', 'not null'=>true, 'length' => 191, 'description' => 'URI of the conversation'),
  45. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  46. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  47. ),
  48. 'primary key' => array('id'),
  49. 'unique keys' => array(
  50. 'conversation_uri_key' => array('uri'),
  51. ),
  52. );
  53. }
  54. static public function beforeSchemaUpdate()
  55. {
  56. $table = strtolower(get_called_class());
  57. $schema = Schema::get();
  58. $schemadef = $schema->getTableDef($table);
  59. // 2016-01-06 We have to make sure there is no conversation with id==0 since it will screw up auto increment resequencing
  60. if ($schemadef['fields']['id']['auto_increment']) {
  61. // since we already have auto incrementing ('serial') we can continue
  62. return;
  63. }
  64. // The conversation will be recreated in upgrade.php, which will
  65. // generate a new URI, but that's collateral damage for you.
  66. $conv = new Conversation();
  67. $conv->id = 0;
  68. if ($conv->find()) {
  69. while ($conv->fetch()) {
  70. // Since we have filtered on 0 this only deletes such entries
  71. // which I have been afraid wouldn't work, but apparently does!
  72. // (I thought it would act as null or something and find _all_ conversation entries)
  73. $conv->delete();
  74. }
  75. }
  76. }
  77. /**
  78. * Factory method for creating a new conversation.
  79. *
  80. * Use this for locally initiated conversations. Remote notices should
  81. * preferrably supply their own conversation URIs in the OStatus feed.
  82. *
  83. * @return Conversation the new conversation DO
  84. */
  85. static function create($uri=null, $created=null)
  86. {
  87. // Be aware that the Notice does not have an id yet since it's not inserted!
  88. $conv = new Conversation();
  89. $conv->created = $created ?: common_sql_now();
  90. $conv->uri = $uri ?: sprintf('%s%s=%s:%s=%s',
  91. TagURI::mint(),
  92. 'objectType', 'thread',
  93. 'nonce', common_random_hexstr(8));
  94. // This insert throws exceptions on failure
  95. $conv->insert();
  96. return $conv;
  97. }
  98. static function noticeCount($id)
  99. {
  100. $keypart = sprintf('conversation:notice_count:%d', $id);
  101. $cnt = self::cacheGet($keypart);
  102. if ($cnt !== false) {
  103. return $cnt;
  104. }
  105. $notice = new Notice();
  106. $notice->conversation = $id;
  107. $notice->whereAddIn('verb', array(ActivityVerb::POST, ActivityUtils::resolveUri(ActivityVerb::POST, true)), $notice->columnType('verb'));
  108. $cnt = $notice->count();
  109. self::cacheSet($keypart, $cnt);
  110. return $cnt;
  111. }
  112. static public function getUrlFromNotice(Notice $notice, $anchor=true)
  113. {
  114. $conv = Conversation::getByID($notice->conversation);
  115. return $conv->getUrl($anchor ? $notice->getID() : null);
  116. }
  117. public function getUri()
  118. {
  119. return $this->uri;
  120. }
  121. public function getUrl($noticeId=null)
  122. {
  123. // FIXME: the URL router should take notice-id as an argument...
  124. return common_local_url('conversation', array('id' => $this->getID())) .
  125. ($noticeId===null ? '' : "#notice-{$noticeId}");
  126. }
  127. // FIXME: ...will 500 ever be too low? Taken from ConversationAction::MAX_NOTICES
  128. public function getNotices(Profile $scoped=null, $offset=0, $limit=500)
  129. {
  130. $stream = new ConversationNoticeStream($this->getID(), $scoped);
  131. $notices = $stream->getNotices($offset, $limit);
  132. return $notices;
  133. }
  134. public function insert()
  135. {
  136. $result = parent::insert();
  137. if ($result === false) {
  138. common_log_db_error($this, 'INSERT', __FILE__);
  139. throw new ServerException(_('Failed to insert Conversation into database'));
  140. }
  141. return $result;
  142. }
  143. }