Message.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * GNUsocial implementation of Direct Messages
  18. *
  19. * @package GNUsocial
  20. * @author Mikael Nordfeldth <mmn@hethane.se>
  21. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Table definition for message.
  28. *
  29. * Since the new updates this class only has the necessary
  30. * logic to upgrade te plugin.
  31. *
  32. * @category Plugin
  33. * @package GNUsocial
  34. * @author Mikael Nordfeldth <mmn@hethane.se>
  35. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  36. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  37. */
  38. class Message extends Managed_DataObject
  39. {
  40. ###START_AUTOCODE
  41. /* the code below is auto generated do not remove the above tag */
  42. public $__table = 'message'; // table name
  43. public $id; // int(4) primary_key not_null
  44. public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space
  45. public $from_profile; // int(4) not_null
  46. public $to_profile; // int(4) not_null
  47. public $content; // text()
  48. public $rendered; // text()
  49. public $url; // varchar(191) not 255 because utf8mb4 takes more space
  50. public $created; // datetime() not_null
  51. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  52. public $source; // varchar(32)
  53. /* the code above is auto generated do not remove the tag below */
  54. ###END_AUTOCODE
  55. public static function schemaDef()
  56. {
  57. return array(
  58. 'fields' => array(
  59. 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
  60. 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universally unique identifier'),
  61. 'from_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is from'),
  62. 'to_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is to'),
  63. 'content' => array('type' => 'text', 'description' => 'message content'),
  64. 'rendered' => array('type' => 'text', 'description' => 'HTML version of the content'),
  65. 'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL of any attachment (image, video, bookmark, whatever)'),
  66. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  67. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  68. 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'),
  69. ),
  70. 'primary key' => array('id'),
  71. 'unique keys' => array(
  72. 'message_uri_key' => array('uri'),
  73. ),
  74. 'foreign keys' => array(
  75. 'message_from_profile_fkey' => array('profile', array('from_profile' => 'id')),
  76. 'message_to_profile_fkey' => array('profile', array('to_profile' => 'id')),
  77. ),
  78. 'indexes' => array(
  79. // @fixme these are really terrible indexes, since you can only sort on one of them at a time.
  80. // looks like we really need a (to_profile, created) for inbox and a (from_profile, created) for outbox
  81. 'message_from_idx' => array('from_profile'),
  82. 'message_to_idx' => array('to_profile'),
  83. 'message_created_idx' => array('created'),
  84. ),
  85. );
  86. }
  87. function getFrom()
  88. {
  89. return Profile::getKV('id', $this->from_profile);
  90. }
  91. function getTo()
  92. {
  93. return Profile::getKV('id', $this->to_profile);
  94. }
  95. function getSource()
  96. {
  97. if (empty($this->source)) {
  98. return false;
  99. }
  100. $ns = new Notice_source();
  101. switch ($this->source) {
  102. case 'web':
  103. case 'xmpp':
  104. case 'mail':
  105. case 'omb':
  106. case 'system':
  107. case 'api':
  108. $ns->code = $this->source;
  109. break;
  110. default:
  111. $ns = Notice_source::getKV($this->source);
  112. if (!$ns instanceof Notice_source) {
  113. $ns = new Notice_source();
  114. $ns->code = $this->source;
  115. $app = Oauth_application::getKV('name', $this->source);
  116. if ($app) {
  117. $ns->name = $app->name;
  118. $ns->url = $app->source_url;
  119. }
  120. }
  121. break;
  122. }
  123. return $ns;
  124. }
  125. function asActivity()
  126. {
  127. $act = new Activity();
  128. if (Event::handle('StartMessageAsActivity', array($this, &$act))) {
  129. $act->verb = ActivityVerb::POST;
  130. $act->time = strtotime($this->created);
  131. $actor_profile = $this->getFrom();
  132. if (is_null($actor_profile)) {
  133. throw new Exception(sprintf("Sender profile not found: %d", $this->from_profile));
  134. }
  135. $act->actor = $actor_profile->asActivityObject();
  136. $act->context = new ActivityContext();
  137. $options = ['source' => $this->source,
  138. 'uri' => TagURI::mint(sprintf('activity:message:%d', $this->id)),
  139. 'url' => $this->uri,
  140. 'scope' => Notice::MESSAGE_SCOPE];
  141. $to_profile = $this->getTo();
  142. if (is_null($to_profile)) {
  143. throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile));
  144. }
  145. $act->context->attention[$to_profile->getUri()] = ActivityObject::PERSON;
  146. $act->objects[] = ActivityObject::fromMessage($this);
  147. $source = $this->getSource();
  148. if ($source instanceof Notice_source) {
  149. $act->generator = ActivityObject::fromNoticeSource($source);
  150. }
  151. Event::handle('EndMessageAsActivity', array($this, &$act));
  152. }
  153. return $act;
  154. }
  155. }