3 * Data class for group direct messages
9 * @author Evan Prodromou <evan@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2009, StatusNet, Inc.
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.
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.
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/>.
30 if (!defined('STATUSNET')) {
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
37 * Data class for group direct messages
39 * @category GroupPrivateMessage
41 * @author Evan Prodromou <evan@status.net>
42 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43 * @link http://status.net/
47 class Group_message extends Managed_DataObject
49 public $__table = 'group_message'; // table name
50 public $id; // char(36) primary_key not_null
51 public $uri; // varchar(255)
52 public $from_profile; // int
53 public $to_group; // int
57 public $created; // datetime() not_null
58 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
60 public static function schemaDef()
64 'id' => array('type' => 'char', 'not null' => true, 'length' => 36, 'description' => 'message uuid'),
65 'uri' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'message uri'),
66 'url' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'representation url'),
67 'from_profile' => array('type' => 'int', 'not null' => true, 'description' => 'sending profile ID'),
68 'to_group' => array('type' => 'int', 'not null' => true, 'description' => 'receiving group ID'),
69 'content' => array('type' => 'text', 'not null' => true, 'description' => 'message content'),
70 'rendered' => array('type' => 'text', 'not null' => true, 'description' => 'rendered message'),
71 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
72 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
74 'primary key' => array('id'),
75 'unique keys' => array(
76 'group_message_uri_key' => array('uri'),
78 'foreign keys' => array(
79 'group_message_from_profile_fkey' => array('profile', array('from_profile' => 'id')),
80 'group_message_to_group_fkey' => array('user_group', array('to_group' => 'id')),
83 'group_message_from_profile_idx' => array('from_profile'),
84 'group_message_to_group_idx' => array('to_group'),
85 'group_message_url_idx' => array('url'),
90 static function send($user, $group, $text)
92 if (!$user->hasRight(Right::NEWMESSAGE)) {
93 // XXX: maybe break this out into a separate right
94 // TRANS: Exception thrown when trying to send group private message without having the right to do that.
95 // TRANS: %s is a user nickname.
96 throw new Exception(sprintf(_m('User %s is not allowed to send private messages.'),
100 Group_privacy_settings::ensurePost($user, $group);
102 $text = $user->shortenLinks($text);
104 // We use the same limits as for 'regular' private messages.
106 if (Message::contentTooLong($text)) {
107 // TRANS: Exception thrown when trying to send group private message that is too long.
108 // TRANS: %d is the maximum meggage length.
109 throw new Exception(sprintf(_m('That\'s too long. Maximum message size is %d character.',
110 'That\'s too long. Maximum message size is %d characters.',
111 Message::maxContent()),
112 Message::maxContent()));
115 // Valid! Let's do this thing!
117 $gm = new Group_message();
119 $gm->id = UUID::gen();
120 $gm->uri = common_local_url('showgroupmessage', array('id' => $gm->id));
121 $gm->from_profile = $user->id;
122 $gm->to_group = $group->id;
123 $gm->content = $text; // XXX: is this cool?!
124 $gm->rendered = common_render_text($text);
126 $gm->created = common_sql_now();
128 // This throws a conniption if there's a problem
137 function distribute()
139 $group = User_group::getKV('id', $this->to_group);
141 $member = $group->getMembers();
143 while ($member->fetch()) {
144 Group_message_profile::send($this, $member);
150 $group = User_group::getKV('id', $this->to_group);
152 // TRANS: Exception thrown when trying to send group private message to a non-existing group.
153 throw new ServerException(_m('No group for group message.'));
160 $sender = Profile::getKV('id', $this->from_profile);
161 if (empty($sender)) {
162 // TRANS: Exception thrown when trying to send group private message without having a sender.
163 throw new ServerException(_m('No sender for group message.'));
168 static function forGroup($group, $offset, $limit)
171 $gm = new Group_message();
173 $gm->to_group = $group->id;
174 $gm->orderBy('created DESC');
175 $gm->limit($offset, $limit);