]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Notice.php
save notice to inbox on saveNew
[quix0rs-gnu-social.git] / classes / Notice.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 /**
23  * Table Definition for notice
24  */
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
26
27 /* We keep the first three 20-notice pages, plus one for pagination check,
28  * in the memcached cache. */
29
30 define('NOTICE_CACHE_WINDOW', 61);
31
32 class Notice extends Memcached_DataObject 
33 {
34     ###START_AUTOCODE
35     /* the code below is auto generated do not remove the above tag */
36
37     public $__table = 'notice';                          // table name
38     public $id;                              // int(4)  primary_key not_null
39     public $profile_id;                      // int(4)   not_null
40     public $uri;                             // varchar(255)  unique_key
41     public $content;                         // varchar(140)  
42     public $rendered;                        // text()  
43     public $url;                             // varchar(255)  
44     public $created;                         // datetime()   not_null
45     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
46     public $reply_to;                        // int(4)  
47     public $is_local;                        // tinyint(1)  
48     public $source;                          // varchar(32)  
49
50     /* Static get */
51     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Notice',$k,$v); }
52
53     /* the code above is auto generated do not remove the tag below */
54     ###END_AUTOCODE
55
56         function getProfile() {
57                 return Profile::staticGet('id', $this->profile_id);
58         }
59
60         function delete() {
61                 $this->blowCaches();
62                 $this->blowFavesCache();
63                 parent::delete();
64         }
65         
66         function saveTags() {
67                 /* extract all #hastags */
68                 $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($this->content), $match);
69                 if (!$count) {
70                         return true;
71                 }
72                 
73                 /* elide characters we don't want in the tag */
74                 $match[1] = str_replace(array('-', '_', '.'), '', $match[1]);
75
76                 /* Add them to the database */
77                 foreach(array_unique($match[1]) as $hashtag) {
78                         $tag = DB_DataObject::factory('Notice_tag');
79                         $tag->notice_id = $this->id;
80                         $tag->tag = $hashtag;
81                         $tag->created = $this->created;
82                         $id = $tag->insert();
83                         if (!$id) {
84                                 $last_error = PEAR::getStaticProperty('DB_DataObject','lastError');
85                                 common_log(LOG_ERR, 'DB error inserting hashtag: ' . $last_error->message);
86                                 common_server_error(sprintf(_('DB error inserting hashtag: %s'), $last_error->message));
87                                 return;
88                         }
89                 }
90                 return true;
91         }
92
93         static function saveNew($profile_id, $content, $source=NULL, $is_local=1, $reply_to=NULL, $uri=NULL) {
94                 
95                 $notice = new Notice();
96                 $notice->profile_id = $profile_id;
97                 $notice->is_local = $is_local;
98                 $notice->reply_to = $reply_to;
99                 $notice->created = common_sql_now();
100                 $notice->content = $content;
101                 $notice->rendered = common_render_content($notice->content, $notice);
102                 $notice->source = $source;
103                 $notice->uri = $uri;
104                 
105                 $id = $notice->insert();
106
107                 if (!$id) {
108                         common_log_db_error($notice, 'INSERT', __FILE__);
109                         return _('Problem saving notice.');
110                 }
111
112                 # Update the URI after the notice is in the database
113                 if (!$uri) {
114                         $orig = clone($notice);
115                         $notice->uri = common_notice_uri($notice);
116
117                         if (!$notice->update($orig)) {
118                                 common_log_db_error($notice, 'UPDATE', __FILE__);
119                                 return _('Problem saving notice.');
120                         }
121                 }
122
123                 # XXX: do we need to change this for remote users?
124                 
125                 common_save_replies($notice);
126                 $notice->saveTags();
127
128                 # Clear the cache for subscribed users, so they'll update at next request
129                 # XXX: someone clever could prepend instead of clearing the cache
130                 
131                 if (common_config('memcached', 'enabled')) {
132                         $notice->blowCaches();
133                 }
134
135                 $notice->addToInboxes();
136                 return $notice;
137         }
138
139         function blowCaches() {
140                 $this->blowSubsCache();
141                 $this->blowNoticeCache();
142                 $this->blowRepliesCache();
143                 $this->blowPublicCache();
144                 $this->blowTagCache();
145         }
146
147         function blowTagCache() {
148                 $cache = common_memcache();
149                 if ($cache) {
150                         $tag = new Notice_tag();
151                         $tag->notice_id = $this->id;
152                         if ($tag->find()) {
153                                 while ($tag->fetch()) {
154                                         $cache->delete(common_cache_key('notice_tag:notice_stream:' . $tag->tag));
155                                 }
156                         }
157                         $tag->free();
158                         unset($tag);
159                 }
160         }
161         
162         function blowSubsCache() {
163                 $cache = common_memcache();
164                 if ($cache) {
165                         $user = new User();
166                         
167                         $user->query('SELECT id ' .
168                                                  'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
169                                                  'WHERE subscription.subscribed = ' . $this->profile_id);
170                         
171                         while ($user->fetch()) {
172                                 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
173                         }
174                         
175                         $user->free();
176                         unset($user);
177                 }
178         }
179
180         function blowNoticeCache() {
181                 if ($this->is_local) {
182                         $cache = common_memcache();
183                         if ($cache) {
184                                 $cache->delete(common_cache_key('user:notices:'.$this->profile_id));
185                         }
186                 }
187         }
188
189         function blowRepliesCache() {
190                 $cache = common_memcache();
191                 if ($cache) {
192                         $reply = new Reply();
193                         $reply->notice_id = $this->id;
194                         if ($reply->find()) {
195                                 while ($reply->fetch()) {
196                                         $cache->delete(common_cache_key('user:replies:'.$reply->profile_id));
197                                 }
198                         }
199                         $reply->free();
200                         unset($reply);
201                 }
202         }
203
204         function blowPublicCache() {
205                 if ($this->is_local) {
206                         $cache = common_memcache();
207                         if ($cache) {
208                                 $cache->delete(common_cache_key('public'));
209                         }
210                 }
211         }
212
213         function blowFavesCache() {
214                 $cache = common_memcache();
215                 if ($cache) {
216                         $fave = new Fave();
217                         $fave->notice_id = $this->id;
218                         if ($fave->find()) {
219                                 while ($fave->fetch()) {
220                                         $cache->delete(common_cache_key('user:faves:'.$fave->user_id));
221                                 }
222                         }
223                         $fave->free();
224                         unset($fave);
225                 }
226         }
227         
228         static function getStream($qry, $cachekey, $offset=0, $limit=20) {
229                 
230                 if (common_config('memcached', 'enabled')) {
231                         return Notice::getCachedStream($qry, $cachekey, $offset, $limit);
232                 } else {
233                         return Notice::getStreamDirect($qry, $offset, $limit);
234                 }
235         
236         }
237
238         static function getStreamDirect($qry, $offset, $limit) {
239                 
240                 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
241                 
242                 if(common_config('db','type')=='pgsql') {
243                         $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
244                 } else {
245                         $qry .= ' LIMIT ' . $offset . ', ' . $limit;
246                 }
247
248                 $notice = new Notice();
249
250                 $notice->query($qry);
251                 
252                 return $notice;
253         }
254         
255         static function getCachedStream($qry, $cachekey, $offset, $limit) {
256
257                 # If outside our cache window, just go to the DB
258                 
259                 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
260                         return Notice::getStreamDirect($qry, $offset, $limit);
261                 }
262
263                 # Get the cache; if we can't, just go to the DB
264                 
265                 $cache = common_memcache();
266
267                 
268                 if (!$cache) {
269                         return Notice::getStreamDirect($qry, $offset, $limit);
270                 }
271
272                 # Get the notices out of the cache
273                 
274                 $notices = $cache->get(common_cache_key($cachekey));
275                 
276                 # On a cache hit, return a DB-object-like wrapper
277                 
278                 if ($notices !== FALSE) {
279                         $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
280                         return $wrapper;
281                 }
282
283                 # Otherwise, get the full cache window out of the DB
284
285                 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW);
286                 
287                 # If there are no hits, just return the value
288                 
289                 if (!$notice) {
290                         return $notice;
291                 }
292
293                 # Pack results into an array
294                 
295                 $notices = array();
296
297                 while ($notice->fetch()) {
298                         $notices[] = clone($notice);
299                 }
300
301                 # Store the array in the cache for next time
302                 
303                 $result = $cache->set(common_cache_key($cachekey), $notices);
304
305                 # return a wrapper of the array for use now
306                 
307                 $wrapper = new NoticeWrapper(array_slice($notices, $offset, $limit));
308                 
309                 return $wrapper;
310         }
311
312         function publicStream($offset=0, $limit=20, $since_id=0, $before_id=0) {
313                 
314                 $needAnd = FALSE;
315         $needWhere = TRUE;
316
317                 $qry = 'SELECT * FROM notice ';
318
319                 if (common_config('public', 'localonly')) {
320                         $qry .= ' WHERE is_local = 1 ';
321                         $needWhere = FALSE;
322                         $needAnd = TRUE;
323                 }
324
325                 // NOTE: since_id and before_id are extensions to Twitter API
326         if ($since_id > 0) {
327             if ($needWhere)
328                 $qry .= ' WHERE ';
329             if ($needAnd)
330                                 $qry .= ' AND ';
331             $qry .= ' notice.id > ' . $since_id . ' ';
332                         $needAnd = FALSE;
333                         $needWhere = FALSE;
334         }
335
336                 if ($before_id > 0) {
337             if ($needWhere)
338                 $qry .= ' WHERE ';
339                         if ($needAnd)
340                                 $qry .= ' AND ';
341                         $qry .= ' notice.id < ' . $before_id . ' ';
342                         $needAnd = FALSE;
343                         $needWhere = FALSE;
344                 }
345
346                 return Notice::getStream($qry,
347                                                                  'public',
348                                                                  $offset, $limit);
349         }
350         
351         function addToInboxes() {
352
353                 $inbox = new Notice_inbox();
354                 
355                 $inbox->query('INSERT INTO notice_inbox (user_id, notice_id) ' .
356                                           'SELECT user.id, ' . $this->id . ' ' .
357                                           'FROM user JOIN subscription ON user.id = subscription.subscriber ' .
358                                           'WHERE subscription.subscribed = ' . $this->profile_id);
359                 
360                 return;
361         }
362 }
363