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/
48 class Group_message extends Memcached_DataObject
50 public $__table = 'group_message'; // table name
51 public $id; // char(36) primary_key not_null
52 public $uri; // varchar(255)
53 public $from_profile; // int
54 public $to_group; // int
61 * Get an instance by key
63 * This is a utility method to get a single instance with a given key value.
65 * @param string $k Key to use to lookup (usually 'user_id' for this class)
66 * @param mixed $v Value to lookup
68 * @return Group_message object found, or null for no hits
71 function staticGet($k, $v=null)
73 return Memcached_DataObject::staticGet('Group_message', $k, $v);
77 * return table definition for DB_DataObject
79 * DB_DataObject needs to know something about the table to manipulate
80 * instances. This method provides all the DB_DataObject needs to know.
82 * @return array array of column definitions
86 return array('id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
87 'uri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
88 'from_profile' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
89 'to_group' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
90 'content' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
91 'rendered' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
92 'url' => DB_DATAOBJECT_STR,
93 'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
97 * return key definitions for DB_DataObject
99 * DB_DataObject needs to know about keys that the table has, since it
100 * won't appear in StatusNet's own keys list. In most cases, this will
101 * simply reference your keyTypes() function.
103 * @return array list of key field names
107 return array_keys($this->keyTypes());
111 * return key definitions for Memcached_DataObject
113 * @return array associative array of key definitions, field name to type:
114 * 'K' for primary key: for compound keys, add an entry for each component;
115 * 'U' for unique keys: compound keys are not well supported here.
119 return array('id' => 'K', 'uri' => 'U');
122 static function send($user, $group, $text)
124 if (!$user->hasRight(Right::NEWMESSAGE)) {
125 // XXX: maybe break this out into a separate right
126 throw new Exception(sprintf(_('User %s not allowed to send private messages.'),
130 Group_privacy_settings::ensurePost($user, $group);
132 $text = $user->shortenLinks($text);
134 // We use the same limits as for 'regular' private messages.
136 if (Message::contentTooLong($text)) {
137 throw new Exception(sprintf(_m('That\'s too long. Maximum message size is %d character.',
138 'That\'s too long. Maximum message size is %d characters.',
139 Message::maxContent()),
140 Message::maxContent()));
143 // Valid! Let's do this thing!
145 $gm = new Group_message();
147 $gm->id = UUID::gen();
148 $gm->uri = common_local_url('showgroupmessage', array('id' => $gm->id));
149 $gm->from_profile = $user->id;
150 $gm->to_group = $group->id;
151 $gm->content = $text; // XXX: is this cool?!
152 $gm->rendered = common_render_text($text);
154 $gm->created = common_sql_now();
156 // This throws a conniption if there's a problem
165 function distribute()
167 $group = User_group::staticGet('id', $this->to_group);
169 $member = $group->getMembers();
171 while ($member->fetch()) {
172 Group_message_profile::send($this, $member);
178 $group = User_group::staticGet('id', $this->to_group);
180 throw new ServerException(_('No group for group message'));
187 $sender = Profile::staticGet('id', $this->from_profile);
188 if (empty($sender)) {
189 throw new ServerException(_('No sender for group message'));
194 static function forGroup($group, $offset, $limit)
197 $gm = new Group_message();
199 $gm->to_group = $group->id;
200 $gm->orderBy('created DESC');
201 $gm->limit($offset, $limit);