Conversation.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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
  36. public $uri; // varchar(255) unique_key
  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' => 'int', 'not null' => true, 'description' => 'should be set from root notice id (since 2014-03-01 commit)'),
  44. 'uri' => array('type' => 'varchar', 'not null'=>true, 'length' => 255, '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. /**
  55. * Factory method for creating a new conversation.
  56. *
  57. * Use this for locally initiated conversations. Remote notices should
  58. * preferrably supply their own conversation URIs in the OStatus feed.
  59. *
  60. * @return Conversation the new conversation DO
  61. */
  62. static function create(Notice $notice, $uri=null)
  63. {
  64. if (empty($notice->id)) {
  65. throw new ServerException(_('Tried to create conversation for not yet inserted notice'));
  66. }
  67. $conv = new Conversation();
  68. $conv->created = common_sql_now();
  69. $conv->id = $notice->id;
  70. $conv->uri = $uri ?: sprintf('%s%s=%d:%s=%s:%s=%x',
  71. TagURI::mint(),
  72. 'noticeId', $notice->id,
  73. 'objectType', 'thread',
  74. 'crc32', crc32($notice->content));
  75. $result = $conv->insert();
  76. if ($result === false) {
  77. common_log_db_error($conv, 'INSERT', __FILE__);
  78. throw new ServerException(_('Failed to create conversation for notice'));
  79. }
  80. return $conv;
  81. }
  82. static function noticeCount($id)
  83. {
  84. $keypart = sprintf('conversation:notice_count:%d', $id);
  85. $cnt = self::cacheGet($keypart);
  86. if ($cnt !== false) {
  87. return $cnt;
  88. }
  89. $notice = new Notice();
  90. $notice->conversation = $id;
  91. $notice->whereAddIn('verb', array(ActivityVerb::POST, ActivityUtils::resolveUri(ActivityVerb::POST, true)), $notice->columnType('verb'));
  92. $cnt = $notice->count();
  93. self::cacheSet($keypart, $cnt);
  94. return $cnt;
  95. }
  96. static public function getUrlFromNotice(Notice $notice, $anchor=true)
  97. {
  98. $conv = self::getKV('id', $notice->conversation);
  99. return $conv->getUrl($anchor ? $notice->id : null);
  100. }
  101. public function getUri()
  102. {
  103. return $this->uri;
  104. }
  105. public function getUrl($noticeId=null)
  106. {
  107. // FIXME: the URL router should take notice-id as an argument...
  108. return common_local_url('conversation', array('id' => $this->id)) .
  109. ($noticeId===null ? '' : "#notice-{$noticeId}");
  110. }
  111. // FIXME: ...will 500 ever be too low? Taken from ConversationAction::MAX_NOTICES
  112. public function getNotices($offset=0, $limit=500, Profile $scoped=null)
  113. {
  114. if ($scoped === null) {
  115. $scoped = Profile::current();
  116. }
  117. $stream = new ConversationNoticeStream($this->id, $scoped);
  118. $notices = $stream->getNotices($offset, $limit);
  119. return $notices;
  120. }
  121. }