Conversation.php 6.6 KB

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