Message.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. if (!defined('GNUSOCIAL')) { exit(1); }
  3. /**
  4. * Table Definition for message
  5. */
  6. class Message extends Managed_DataObject
  7. {
  8. ###START_AUTOCODE
  9. /* the code below is auto generated do not remove the above tag */
  10. public $__table = 'message'; // table name
  11. public $id; // int(4) primary_key not_null
  12. public $uri; // varchar(191) unique_key not 255 because utf8mb4 takes more space
  13. public $from_profile; // int(4) not_null
  14. public $to_profile; // int(4) not_null
  15. public $content; // text()
  16. public $rendered; // text()
  17. public $url; // varchar(191) not 255 because utf8mb4 takes more space
  18. public $created; // datetime() not_null
  19. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  20. public $source; // varchar(32)
  21. /* the code above is auto generated do not remove the tag below */
  22. ###END_AUTOCODE
  23. public static function schemaDef()
  24. {
  25. return array(
  26. 'fields' => array(
  27. 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
  28. 'uri' => array('type' => 'varchar', 'length' => 191, 'description' => 'universally unique identifier'),
  29. 'from_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is from'),
  30. 'to_profile' => array('type' => 'int', 'not null' => true, 'description' => 'who the message is to'),
  31. 'content' => array('type' => 'text', 'description' => 'message content'),
  32. 'rendered' => array('type' => 'text', 'description' => 'HTML version of the content'),
  33. 'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL of any attachment (image, video, bookmark, whatever)'),
  34. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  35. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  36. 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'),
  37. ),
  38. 'primary key' => array('id'),
  39. 'unique keys' => array(
  40. 'message_uri_key' => array('uri'),
  41. ),
  42. 'foreign keys' => array(
  43. 'message_from_profile_fkey' => array('profile', array('from_profile' => 'id')),
  44. 'message_to_profile_fkey' => array('profile', array('to_profile' => 'id')),
  45. ),
  46. 'indexes' => array(
  47. // @fixme these are really terrible indexes, since you can only sort on one of them at a time.
  48. // looks like we really need a (to_profile, created) for inbox and a (from_profile, created) for outbox
  49. 'message_from_idx' => array('from_profile'),
  50. 'message_to_idx' => array('to_profile'),
  51. 'message_created_idx' => array('created'),
  52. ),
  53. );
  54. }
  55. function getFrom()
  56. {
  57. return Profile::getKV('id', $this->from_profile);
  58. }
  59. function getTo()
  60. {
  61. return Profile::getKV('id', $this->to_profile);
  62. }
  63. static function saveNew($from, $to, $content, $source) {
  64. $sender = Profile::getKV('id', $from);
  65. if (!$sender->hasRight(Right::NEWMESSAGE)) {
  66. // TRANS: Client exception thrown when a user tries to send a direct message while being banned from sending them.
  67. throw new ClientException(_('You are banned from sending direct messages.'));
  68. }
  69. $user = User::getKV('id', $sender->id);
  70. $msg = new Message();
  71. $msg->from_profile = $from;
  72. $msg->to_profile = $to;
  73. if ($user) {
  74. // Use the sender's URL shortening options.
  75. $msg->content = $user->shortenLinks($content);
  76. } else {
  77. $msg->content = common_shorten_links($content);
  78. }
  79. $msg->rendered = common_render_text($msg->content);
  80. $msg->created = common_sql_now();
  81. $msg->source = $source;
  82. $result = $msg->insert();
  83. if (!$result) {
  84. common_log_db_error($msg, 'INSERT', __FILE__);
  85. // TRANS: Message given when a message could not be stored on the server.
  86. throw new ServerException(_('Could not insert message.'));
  87. }
  88. $orig = clone($msg);
  89. $msg->uri = common_local_url('showmessage', array('message' => $msg->id));
  90. $result = $msg->update($orig);
  91. if (!$result) {
  92. common_log_db_error($msg, 'UPDATE', __FILE__);
  93. // TRANS: Message given when a message could not be updated on the server.
  94. throw new ServerException(_('Could not update message with new URI.'));
  95. }
  96. return $msg;
  97. }
  98. static function maxContent()
  99. {
  100. $desclimit = common_config('message', 'contentlimit');
  101. // null => use global limit (distinct from 0!)
  102. if (is_null($desclimit)) {
  103. $desclimit = common_config('site', 'textlimit');
  104. }
  105. return $desclimit;
  106. }
  107. static function contentTooLong($content)
  108. {
  109. $contentlimit = self::maxContent();
  110. return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
  111. }
  112. function notify()
  113. {
  114. $from = User::getKV('id', $this->from_profile);
  115. $to = User::getKV('id', $this->to_profile);
  116. mail_notify_message($this, $from, $to);
  117. }
  118. function getSource()
  119. {
  120. if (empty($this->source)) {
  121. return false;
  122. }
  123. $ns = new Notice_source();
  124. switch ($this->source) {
  125. case 'web':
  126. case 'xmpp':
  127. case 'mail':
  128. case 'omb':
  129. case 'system':
  130. case 'api':
  131. $ns->code = $this->source;
  132. break;
  133. default:
  134. $ns = Notice_source::getKV($this->source);
  135. if (!$ns instanceof Notice_source) {
  136. $ns = new Notice_source();
  137. $ns->code = $this->source;
  138. $app = Oauth_application::getKV('name', $this->source);
  139. if ($app) {
  140. $ns->name = $app->name;
  141. $ns->url = $app->source_url;
  142. }
  143. }
  144. break;
  145. }
  146. return $ns;
  147. }
  148. function asActivity()
  149. {
  150. $act = new Activity();
  151. if (Event::handle('StartMessageAsActivity', array($this, &$act))) {
  152. $act->id = TagURI::mint(sprintf('activity:message:%d', $this->id));
  153. $act->time = strtotime($this->created);
  154. $act->link = $this->url;
  155. $profile = Profile::getKV('id', $this->from_profile);
  156. if (empty($profile)) {
  157. throw new Exception(sprintf("Sender profile not found: %d", $this->from_profile));
  158. }
  159. $act->actor = $profile->asActivityObject();
  160. $act->actor->extra[] = $profile->profileInfo();
  161. $act->verb = ActivityVerb::POST;
  162. $act->objects[] = ActivityObject::fromMessage($this);
  163. $ctx = new ActivityContext();
  164. $rprofile = Profile::getKV('id', $this->to_profile);
  165. if (empty($rprofile)) {
  166. throw new Exception(sprintf("Receiver profile not found: %d", $this->to_profile));
  167. }
  168. $ctx->attention[$rprofile->getUri()] = ActivityObject::PERSON;
  169. $act->context = $ctx;
  170. $source = $this->getSource();
  171. if ($source instanceof Notice_source) {
  172. $act->generator = ActivityObject::fromNoticeSource($source);
  173. }
  174. Event::handle('EndMessageAsActivity', array($this, &$act));
  175. }
  176. return $act;
  177. }
  178. }