Conversation.php 6.6 KB

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