Message.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 'message_from_profile_created_id_idx' => array('from_profile', 'created', 'id'),
  80. 'message_to_profile_created_id_idx' => array('to_profile', 'created', 'id'),
  81. ),
  82. );
  83. }
  84. public function getFrom()
  85. {
  86. return Profile::getKV('id', $this->from_profile);
  87. }
  88. public function getTo()
  89. {
  90. return Profile::getKV('id', $this->to_profile);
  91. }
  92. public function getSource()
  93. {
  94. if (empty($this->source)) {
  95. return false;
  96. }
  97. $ns = new Notice_source();
  98. switch ($this->source) {
  99. case 'web':
  100. case 'xmpp':
  101. case 'mail':
  102. case 'omb':
  103. case 'system':
  104. case 'api':
  105. $ns->code = $this->source;
  106. break;
  107. default:
  108. $ns = Notice_source::getKV($this->source);
  109. if (!$ns instanceof Notice_source) {
  110. $ns = new Notice_source();
  111. $ns->code = $this->source;
  112. $app = Oauth_application::getKV('name', $this->source);
  113. if ($app) {
  114. $ns->name = $app->name;
  115. $ns->url = $app->source_url;
  116. }
  117. }
  118. break;
  119. }
  120. return $ns;
  121. }
  122. public function asActivity()
  123. {
  124. $act = new Activity();
  125. if (Event::handle('StartMessageAsActivity', array($this, &$act))) {
  126. $act->verb = ActivityVerb::POST;
  127. $act->time = strtotime($this->created);
  128. $actor_profile = $this->getFrom();
  129. if (is_null($actor_profile)) {
  130. throw new Exception(sprintf("Sender profile not found: %d", $this->from_profile));
  131. }
  132. $act->actor = $actor_profile->asActivityObject();
  133. $act->context = new ActivityContext();
  134. $options = ['source' => $this->source,
  135. 'uri' => TagURI::mint(sprintf('activity:message:%d', $this->id)),
  136. 'url' => $this->uri,
  137. 'scope' => Notice::MESSAGE_SCOPE];
  138. $to_profile = $this->getTo();
  139. if (is_null($to_profile)) {
  140. throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile));
  141. }
  142. $act->context->attention[$to_profile->getUri()] = ActivityObject::PERSON;
  143. $act->objects[] = ActivityObject::fromMessage($this);
  144. $source = $this->getSource();
  145. if ($source instanceof Notice_source) {
  146. $act->generator = ActivityObject::fromNoticeSource($source);
  147. }
  148. Event::handle('EndMessageAsActivity', array($this, &$act));
  149. }
  150. return $act;
  151. }
  152. }