Group_message.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * Data class for group direct messages
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2009, StatusNet, Inc.
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
  33. /**
  34. * Data class for group direct messages
  35. *
  36. * @category GroupPrivateMessage
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  40. * @link http://status.net/
  41. *
  42. * @see DB_DataObject
  43. */
  44. class Group_message extends Managed_DataObject
  45. {
  46. public $__table = 'group_message'; // table name
  47. public $id; // char(36) primary_key not_null
  48. public $uri; // varchar(255)
  49. public $from_profile; // int
  50. public $to_group; // int
  51. public $content;
  52. public $rendered;
  53. public $url;
  54. public $created; // datetime() not_null
  55. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  56. public static function schemaDef()
  57. {
  58. return array(
  59. 'fields' => array(
  60. 'id' => array('type' => 'char', 'not null' => true, 'length' => 36, 'description' => 'message uuid'),
  61. 'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'message uri'),
  62. 'url' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'representation url'),
  63. 'from_profile' => array('type' => 'int', 'not null' => true, 'description' => 'sending profile ID'),
  64. 'to_group' => array('type' => 'int', 'not null' => true, 'description' => 'receiving group ID'),
  65. 'content' => array('type' => 'text', 'not null' => true, 'description' => 'message content'),
  66. 'rendered' => array('type' => 'text', 'not null' => true, 'description' => 'rendered message'),
  67. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  68. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  69. ),
  70. 'primary key' => array('id'),
  71. 'unique keys' => array(
  72. 'group_message_uri_key' => array('uri'),
  73. ),
  74. 'foreign keys' => array(
  75. 'group_message_from_profile_fkey' => array('profile', array('from_profile' => 'id')),
  76. 'group_message_to_group_fkey' => array('user_group', array('to_group' => 'id')),
  77. ),
  78. 'indexes' => array(
  79. 'group_message_from_profile_idx' => array('from_profile'),
  80. 'group_message_to_group_idx' => array('to_group'),
  81. 'group_message_url_idx' => array('url'),
  82. ),
  83. );
  84. }
  85. static function send($user, $group, $text)
  86. {
  87. if (!$user->hasRight(Right::NEWMESSAGE)) {
  88. // XXX: maybe break this out into a separate right
  89. // TRANS: Exception thrown when trying to send group private message without having the right to do that.
  90. // TRANS: %s is a user nickname.
  91. throw new Exception(sprintf(_m('User %s is not allowed to send private messages.'),
  92. $user->nickname));
  93. }
  94. Group_privacy_settings::ensurePost($user, $group);
  95. $text = $user->shortenLinks($text);
  96. // We use the same limits as for 'regular' private messages.
  97. if (Message::contentTooLong($text)) {
  98. // TRANS: Exception thrown when trying to send group private message that is too long.
  99. // TRANS: %d is the maximum meggage length.
  100. throw new Exception(sprintf(_m('That\'s too long. Maximum message size is %d character.',
  101. 'That\'s too long. Maximum message size is %d characters.',
  102. Message::maxContent()),
  103. Message::maxContent()));
  104. }
  105. // Valid! Let's do this thing!
  106. $gm = new Group_message();
  107. $gm->id = UUID::gen();
  108. $gm->uri = common_local_url('showgroupmessage', array('id' => $gm->id));
  109. $gm->from_profile = $user->id;
  110. $gm->to_group = $group->id;
  111. $gm->content = $text; // XXX: is this cool?!
  112. $gm->rendered = common_render_text($text);
  113. $gm->url = $gm->uri;
  114. $gm->created = common_sql_now();
  115. // This throws a conniption if there's a problem
  116. $gm->insert();
  117. $gm->distribute();
  118. return $gm;
  119. }
  120. function distribute()
  121. {
  122. $group = User_group::getKV('id', $this->to_group);
  123. $member = $group->getMembers();
  124. while ($member->fetch()) {
  125. Group_message_profile::send($this, $member);
  126. }
  127. }
  128. function getGroup()
  129. {
  130. $group = User_group::getKV('id', $this->to_group);
  131. if (empty($group)) {
  132. // TRANS: Exception thrown when trying to send group private message to a non-existing group.
  133. throw new ServerException(_m('No group for group message.'));
  134. }
  135. return $group;
  136. }
  137. function getSender()
  138. {
  139. $sender = Profile::getKV('id', $this->from_profile);
  140. if (empty($sender)) {
  141. // TRANS: Exception thrown when trying to send group private message without having a sender.
  142. throw new ServerException(_m('No sender for group message.'));
  143. }
  144. return $sender;
  145. }
  146. static function forGroup($group, $offset, $limit)
  147. {
  148. // XXX: cache
  149. $gm = new Group_message();
  150. $gm->to_group = $group->id;
  151. $gm->orderBy('created DESC');
  152. $gm->limit($offset, $limit);
  153. $gm->find();
  154. return $gm;
  155. }
  156. }