]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/GroupPrivateMessage/Group_message.php
Merge branch '1.1.x'
[quix0rs-gnu-social.git] / plugins / GroupPrivateMessage / Group_message.php
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
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
35
36 /**
37  * Data class for group direct messages
38  *
39  * @category GroupPrivateMessage
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43  * @link     http://status.net/
44  *
45  * @see      DB_DataObject
46  */
47 class Group_message extends Memcached_DataObject
48 {
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
54     public $content;
55     public $rendered;
56     public $url;
57     public $created;
58
59     /**
60      * Get an instance by key
61      *
62      * This is a utility method to get a single instance with a given key value.
63      *
64      * @param string $k Key to use to lookup (usually 'user_id' for this class)
65      * @param mixed  $v Value to lookup
66      *
67      * @return Group_message object found, or null for no hits
68      */
69     function staticGet($k, $v=null)
70     {
71         return Memcached_DataObject::staticGet('Group_message', $k, $v);
72     }
73
74     /**
75      * return table definition for DB_DataObject
76      *
77      * DB_DataObject needs to know something about the table to manipulate
78      * instances. This method provides all the DB_DataObject needs to know.
79      *
80      * @return array array of column definitions
81      */
82     function table()
83     {
84         return array('id' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
85                      'uri' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
86                      'from_profile' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
87                      'to_group' => DB_DATAOBJECT_INT + DB_DATAOBJECT_NOTNULL,
88                      'content' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
89                      'rendered' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
90                      'url' => DB_DATAOBJECT_STR,
91                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
92     }
93
94     /**
95      * return key definitions for DB_DataObject
96      *
97      * DB_DataObject needs to know about keys that the table has, since it
98      * won't appear in StatusNet's own keys list. In most cases, this will
99      * simply reference your keyTypes() function.
100      *
101      * @return array list of key field names
102      */
103     function keys()
104     {
105         return array_keys($this->keyTypes());
106     }
107
108     /**
109      * return key definitions for Memcached_DataObject
110      *
111      * @return array associative array of key definitions, field name to type:
112      *         'K' for primary key: for compound keys, add an entry for each component;
113      *         'U' for unique keys: compound keys are not well supported here.
114      */
115     function keyTypes()
116     {
117         return array('id' => 'K', 'uri' => 'U');
118     }
119
120     static function send($user, $group, $text)
121     {
122         if (!$user->hasRight(Right::NEWMESSAGE)) {
123             // XXX: maybe break this out into a separate right
124             // TRANS: Exception thrown when trying to send group private message without having the right to do that.
125             // TRANS: %s is a user nickname.
126             throw new Exception(sprintf(_m('User %s is not allowed to send private messages.'),
127                                         $user->nickname));
128         }
129
130         Group_privacy_settings::ensurePost($user, $group);
131
132         $text = $user->shortenLinks($text);
133
134         // We use the same limits as for 'regular' private messages.
135
136         if (Message::contentTooLong($text)) {
137             // TRANS: Exception thrown when trying to send group private message that is too long.
138             // TRANS: %d is the maximum meggage length.
139             throw new Exception(sprintf(_m('That\'s too long. Maximum message size is %d character.',
140                                            'That\'s too long. Maximum message size is %d characters.',
141                                            Message::maxContent()),
142                                         Message::maxContent()));
143         }
144
145         // Valid! Let's do this thing!
146
147         $gm = new Group_message();
148
149         $gm->id           = UUID::gen();
150         $gm->uri          = common_local_url('showgroupmessage', array('id' => $gm->id));
151         $gm->from_profile = $user->id;
152         $gm->to_group     = $group->id;
153         $gm->content      = $text; // XXX: is this cool?!
154         $gm->rendered     = common_render_text($text);
155         $gm->url          = $gm->uri;
156         $gm->created      = common_sql_now();
157
158         // This throws a conniption if there's a problem
159
160         $gm->insert();
161
162         $gm->distribute();
163
164         return $gm;
165     }
166
167     function distribute()
168     {
169         $group = User_group::staticGet('id', $this->to_group);
170
171         $member = $group->getMembers();
172
173         while ($member->fetch()) {
174             Group_message_profile::send($this, $member);
175         }
176     }
177
178     function getGroup()
179     {
180         $group = User_group::staticGet('id', $this->to_group);
181         if (empty($group)) {
182             // TRANS: Exception thrown when trying to send group private message to a non-existing group.
183             throw new ServerException(_m('No group for group message.'));
184         }
185         return $group;
186     }
187
188     function getSender()
189     {
190         $sender = Profile::staticGet('id', $this->from_profile);
191         if (empty($sender)) {
192             // TRANS: Exception thrown when trying to send group private message without having a sender.
193             throw new ServerException(_m('No sender for group message.'));
194         }
195         return $sender;
196     }
197
198     static function forGroup($group, $offset, $limit)
199     {
200         // XXX: cache
201         $gm = new Group_message();
202
203         $gm->to_group = $group->id;
204         $gm->orderBy('created DESC');
205         $gm->limit($offset, $limit);
206
207         $gm->find();
208
209         return $gm;
210     }
211 }