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
60 * return table definition for DB_DataObject
62 * DB_DataObject needs to know something about the table to manipulate
63 * instances. This method provides all the DB_DataObject needs to know.
65 * @return array array of column definitions
69 return array('id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
70 'uri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
71 'from_profile' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
72 'to_group' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
73 'content' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
74 'rendered' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
75 'url' => DB_DATAOBJECT_STR,
76 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
80 * return key definitions for DB_DataObject
82 * DB_DataObject needs to know about keys that the table has, since it
83 * won't appear in StatusNet's own keys list. In most cases, this will
84 * simply reference your keyTypes() function.
86 * @return array list of key field names
90 return array_keys($this->keyTypes());
94 * return key definitions for Memcached_DataObject
96 * @return array associative array of key definitions, field name to type:
97 * 'K' for primary key: for compound keys, add an entry for each component;
98 * 'U' for unique keys: compound keys are not well supported here.
102 return array('id' => 'K', 'uri' => 'U');
105 static function send($user, $group, $text)
107 if (!$user->hasRight(Right::NEWMESSAGE)) {
108 // XXX: maybe break this out into a separate right
109 // TRANS: Exception thrown when trying to send group private message without having the right to do that.
110 // TRANS: %s is a user nickname.
111 throw new Exception(sprintf(_m('User %s is not allowed to send private messages.'),
115 Group_privacy_settings::ensurePost($user, $group);
117 $text = $user->shortenLinks($text);
119 // We use the same limits as for 'regular' private messages.
121 if (Message::contentTooLong($text)) {
122 // TRANS: Exception thrown when trying to send group private message that is too long.
123 // TRANS: %d is the maximum meggage length.
124 throw new Exception(sprintf(_m('That\'s too long. Maximum message size is %d character.',
125 'That\'s too long. Maximum message size is %d characters.',
126 Message::maxContent()),
127 Message::maxContent()));
130 // Valid! Let's do this thing!
132 $gm = new Group_message();
134 $gm->id = UUID::gen();
135 $gm->uri = common_local_url('showgroupmessage', array('id' => $gm->id));
136 $gm->from_profile = $user->id;
137 $gm->to_group = $group->id;
138 $gm->content = $text; // XXX: is this cool?!
139 $gm->rendered = common_render_text($text);
141 $gm->created = common_sql_now();
143 // This throws a conniption if there's a problem
152 function distribute()
154 $group = User_group::staticGet('id', $this->to_group);
156 $member = $group->getMembers();
158 while ($member->fetch()) {
159 Group_message_profile::send($this, $member);
165 $group = User_group::staticGet('id', $this->to_group);
167 // TRANS: Exception thrown when trying to send group private message to a non-existing group.
168 throw new ServerException(_m('No group for group message.'));
175 $sender = Profile::staticGet('id', $this->from_profile);
176 if (empty($sender)) {
177 // TRANS: Exception thrown when trying to send group private message without having a sender.
178 throw new ServerException(_m('No sender for group message.'));
183 static function forGroup($group, $offset, $limit)
186 $gm = new Group_message();
188 $gm->to_group = $group->id;
189 $gm->orderBy('created DESC');
190 $gm->limit($offset, $limit);