Group_message.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * Data class for group direct messages
  18. *
  19. * @category Data
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2009 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
  27. /**
  28. * Data class for group direct messages
  29. *
  30. * @category GroupPrivateMessage
  31. * @package GNUsocial
  32. * @author Evan Prodromou <evan@status.net>
  33. * @copyright 2009 StatusNet, Inc.
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class Group_message extends Managed_DataObject
  37. {
  38. public $__table = 'group_message'; // table name
  39. public $id; // char(36) primary_key not_null
  40. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  41. public $from_profile; // int
  42. public $to_group; // int
  43. public $content;
  44. public $rendered;
  45. public $url; // varchar(191) not 255 because utf8mb4 takes more space
  46. public $created; // datetime() not_null
  47. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  48. public static function schemaDef()
  49. {
  50. return array(
  51. 'fields' => array(
  52. 'id' => array('type' => 'char', 'not null' => true, 'length' => 36, 'description' => 'message uuid'),
  53. 'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'message uri'),
  54. 'url' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'representation url'),
  55. 'from_profile' => array('type' => 'int', 'not null' => true, 'description' => 'sending profile ID'),
  56. 'to_group' => array('type' => 'int', 'not null' => true, 'description' => 'receiving group ID'),
  57. 'content' => array('type' => 'text', 'not null' => true, 'description' => 'message content'),
  58. 'rendered' => array('type' => 'text', 'not null' => true, 'description' => 'rendered message'),
  59. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  60. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  61. ),
  62. 'primary key' => array('id'),
  63. 'unique keys' => array(
  64. 'group_message_uri_key' => array('uri'),
  65. ),
  66. 'foreign keys' => array(
  67. 'group_message_from_profile_fkey' => array('profile', array('from_profile' => 'id')),
  68. 'group_message_to_group_fkey' => array('user_group', array('to_group' => 'id')),
  69. ),
  70. 'indexes' => array(
  71. 'group_message_from_profile_idx' => array('from_profile'),
  72. 'group_message_to_group_created_idx' => array('to_group', 'created'),
  73. 'group_message_url_idx' => array('url'),
  74. ),
  75. );
  76. }
  77. public static function send($user, $group, $text)
  78. {
  79. if (!$user->hasRight(Right::NEWMESSAGE)) {
  80. // XXX: maybe break this out into a separate right
  81. // TRANS: Exception thrown when trying to send group private message without having the right to do that.
  82. // TRANS: %s is a user nickname.
  83. throw new Exception(sprintf(
  84. _m('User %s is not allowed to send private messages.'),
  85. $user->nickname
  86. ));
  87. }
  88. Group_privacy_settings::ensurePost($user, $group);
  89. $text = $user->shortenLinks($text);
  90. // We use the same limits as for 'regular' private messages.
  91. if (Message::contentTooLong($text)) {
  92. // TRANS: Exception thrown when trying to send group private message that is too long.
  93. // TRANS: %d is the maximum meggage length.
  94. throw new Exception(sprintf(
  95. _m(
  96. 'That\'s too long. Maximum message size is %d character.',
  97. 'That\'s too long. Maximum message size is %d characters.',
  98. Message::maxContent()
  99. ),
  100. Message::maxContent()
  101. ));
  102. }
  103. // Valid! Let's do this thing!
  104. $gm = new Group_message();
  105. $gm->id = UUID::gen();
  106. $gm->uri = common_local_url('showgroupmessage', array('id' => $gm->id));
  107. $gm->from_profile = $user->id;
  108. $gm->to_group = $group->id;
  109. $gm->content = $text; // XXX: is this cool?!
  110. $gm->rendered = common_render_text($text);
  111. $gm->url = $gm->uri;
  112. $gm->created = common_sql_now();
  113. // This throws a conniption if there's a problem
  114. $gm->insert();
  115. $gm->distribute();
  116. return $gm;
  117. }
  118. public function distribute()
  119. {
  120. $group = User_group::getKV('id', $this->to_group);
  121. $member = $group->getMembers();
  122. while ($member->fetch()) {
  123. Group_message_profile::send($this, $member);
  124. }
  125. }
  126. public function getGroup()
  127. {
  128. $group = User_group::getKV('id', $this->to_group);
  129. if (empty($group)) {
  130. // TRANS: Exception thrown when trying to send group private message to a non-existing group.
  131. throw new ServerException(_m('No group for group message.'));
  132. }
  133. return $group;
  134. }
  135. public function getSender()
  136. {
  137. $sender = Profile::getKV('id', $this->from_profile);
  138. if (empty($sender)) {
  139. // TRANS: Exception thrown when trying to send group private message without having a sender.
  140. throw new ServerException(_m('No sender for group message.'));
  141. }
  142. return $sender;
  143. }
  144. public static function forGroup($group, $offset, $limit)
  145. {
  146. // XXX: cache
  147. $gm = new Group_message();
  148. $gm->to_group = $group->id;
  149. $gm->orderBy('created DESC');
  150. $gm->limit($offset, $limit);
  151. $gm->find();
  152. return $gm;
  153. }
  154. }