]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Notice.php
32998836862043f1ca74a9602f478a9328440774
[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)
52     { return Memcached_DataObject::staticGet('Notice',$k,$v); }
53
54     /* the code above is auto generated do not remove the tag below */
55     ###END_AUTOCODE
56
57     function getProfile()
58     {
59         return Profile::staticGet('id', $this->profile_id);
60     }
61
62     function delete()
63     {
64         $this->blowCaches(true);
65         $this->blowFavesCache(true);
66         $this->blowSubsCache(true);
67
68         $this->query('BEGIN');
69         $related = array('Reply',
70                          'Fave',
71                          'Notice_tag',
72                          'Group_inbox',
73                          'Queue_item');
74         if (common_config('inboxes', 'enabled')) {
75             $related[] = 'Notice_inbox';
76         }
77         foreach ($related as $cls) {
78             $inst = new $cls();
79             $inst->notice_id = $this->id;
80             $inst->delete();
81         }
82         $result = parent::delete();
83         $this->query('COMMIT');
84     }
85
86     function saveTags()
87     {
88         /* extract all #hastags */
89         $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($this->content), $match);
90         if (!$count) {
91             return true;
92         }
93
94         /* Add them to the database */
95         foreach(array_unique($match[1]) as $hashtag) {
96             /* elide characters we don't want in the tag */
97             $hashtag = common_canonical_tag($hashtag);
98
99             $tag = DB_DataObject::factory('Notice_tag');
100             $tag->notice_id = $this->id;
101             $tag->tag = $hashtag;
102             $tag->created = $this->created;
103             $id = $tag->insert();
104             if (!$id) {
105                 $last_error = PEAR::getStaticProperty('DB_DataObject','lastError');
106                 common_log(LOG_ERR, 'DB error inserting hashtag: ' . $last_error->message);
107                 common_server_error(sprintf(_('DB error inserting hashtag: %s'), $last_error->message));
108                 return;
109             }
110         }
111         return true;
112     }
113
114     static function saveNew($profile_id, $content, $source=null, $is_local=1, $reply_to=null, $uri=null) {
115
116         $profile = Profile::staticGet($profile_id);
117
118         if (!$profile) {
119             common_log(LOG_ERR, 'Problem saving notice. Unknown user.');
120             return _('Problem saving notice. Unknown user.');
121         }
122
123         if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
124             common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
125             return _('Too many notices too fast; take a breather and post again in a few minutes.');
126         }
127
128         $banned = common_config('profile', 'banned');
129
130         if ( in_array($profile_id, $banned) || in_array($profile->nickname, $banned)) {
131             common_log(LOG_WARNING, "Attempted post from banned user: $profile->nickname (user id = $profile_id).");
132             return _('You are banned from posting notices on this site.');
133         }
134
135         $notice = new Notice();
136         $notice->profile_id = $profile_id;
137
138         $blacklist = common_config('public', 'blacklist');
139
140         # Blacklisted are non-false, but not 1, either
141
142         if ($blacklist && in_array($profile_id, $blacklist)) {
143             $notice->is_local = -1;
144         } else {
145             $notice->is_local = $is_local;
146         }
147
148                 $notice->query('BEGIN');
149
150         $notice->reply_to = $reply_to;
151         $notice->created = common_sql_now();
152         $notice->content = common_shorten_links($content);
153         $notice->rendered = common_render_content($notice->content, $notice);
154         $notice->source = $source;
155         $notice->uri = $uri;
156
157         $id = $notice->insert();
158
159         if (!$id) {
160             common_log_db_error($notice, 'INSERT', __FILE__);
161             return _('Problem saving notice.');
162         }
163
164         # Update the URI after the notice is in the database
165         if (!$uri) {
166             $orig = clone($notice);
167             $notice->uri = common_notice_uri($notice);
168
169             if (!$notice->update($orig)) {
170                 common_log_db_error($notice, 'UPDATE', __FILE__);
171                 return _('Problem saving notice.');
172             }
173         }
174
175         # XXX: do we need to change this for remote users?
176
177         $notice->saveReplies();
178         $notice->saveTags();
179         $notice->saveGroups();
180
181         $notice->addToInboxes();
182                 $notice->query('COMMIT');
183
184         # Clear the cache for subscribed users, so they'll update at next request
185         # XXX: someone clever could prepend instead of clearing the cache
186
187         if (common_config('memcached', 'enabled')) {
188             $notice->blowCaches();
189         }
190
191         return $notice;
192     }
193
194     static function checkEditThrottle($profile_id) {
195         $profile = Profile::staticGet($profile_id);
196         if (!$profile) {
197             return false;
198         }
199         # Get the Nth notice
200         $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
201         if ($notice && $notice->fetch()) {
202             # If the Nth notice was posted less than timespan seconds ago
203             if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
204                 # Then we throttle
205                 return false;
206             }
207         }
208         # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
209         return true;
210     }
211
212     function blowCaches($blowLast=false)
213     {
214         $this->blowSubsCache($blowLast);
215         $this->blowNoticeCache($blowLast);
216         $this->blowRepliesCache($blowLast);
217         $this->blowPublicCache($blowLast);
218         $this->blowTagCache($blowLast);
219         $this->blowGroupCache($blowLast);
220     }
221
222     function blowGroupCache($blowLast=false)
223     {
224         $cache = common_memcache();
225         if ($cache) {
226             $group_inbox = new Group_inbox();
227             $group_inbox->notice_id = $this->id;
228             if ($group_inbox->find()) {
229                 while ($group_inbox->fetch()) {
230                     $cache->delete(common_cache_key('group:notices:'.$group_inbox->group_id));
231                     if ($blowLast) {
232                         $cache->delete(common_cache_key('group:notices:'.$group_inbox->group_id.';last'));
233                     }
234                     $member = new Group_member();
235                     $member->group_id = $group_inbox->group_id;
236                     if ($member->find()) {
237                         while ($member->fetch()) {
238                             $cache->delete(common_cache_key('user:notices_with_friends:' . $member->profile_id));
239                             if ($blowLast) {
240                                 $cache->delete(common_cache_key('user:notices_with_friends:' . $member->profile_id . ';last'));
241                             }
242                         }
243                     }
244                 }
245             }
246             $group_inbox->free();
247             unset($group_inbox);
248         }
249     }
250
251     function blowTagCache($blowLast=false)
252     {
253         $cache = common_memcache();
254         if ($cache) {
255             $tag = new Notice_tag();
256             $tag->notice_id = $this->id;
257             if ($tag->find()) {
258                 while ($tag->fetch()) {
259                     $cache->delete(common_cache_key('notice_tag:notice_stream:' . $tag->tag));
260                     if ($blowLast) {
261                         $cache->delete(common_cache_key('notice_tag:notice_stream:' . $tag->tag . ';last'));
262                     }
263                 }
264             }
265             $tag->free();
266             unset($tag);
267         }
268     }
269
270     function blowSubsCache($blowLast=false)
271     {
272         $cache = common_memcache();
273         if ($cache) {
274             $user = new User();
275
276             $UT = common_config('db','type')=='pgsql'?'"user"':'user';
277             $user->query('SELECT id ' .
278
279                          "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
280                          'WHERE subscription.subscribed = ' . $this->profile_id);
281
282             while ($user->fetch()) {
283                 $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id));
284                 if ($blowLast) {
285                     $cache->delete(common_cache_key('user:notices_with_friends:' . $user->id . ';last'));
286                 }
287             }
288             $user->free();
289             unset($user);
290         }
291     }
292
293     function blowNoticeCache($blowLast=false)
294     {
295         if ($this->is_local) {
296             $cache = common_memcache();
297             if ($cache) {
298                 $cache->delete(common_cache_key('profile:notices:'.$this->profile_id));
299                 if ($blowLast) {
300                     $cache->delete(common_cache_key('profile:notices:'.$this->profile_id.';last'));
301                 }
302             }
303         }
304     }
305
306     function blowRepliesCache($blowLast=false)
307     {
308         $cache = common_memcache();
309         if ($cache) {
310             $reply = new Reply();
311             $reply->notice_id = $this->id;
312             if ($reply->find()) {
313                 while ($reply->fetch()) {
314                     $cache->delete(common_cache_key('user:replies:'.$reply->profile_id));
315                     if ($blowLast) {
316                         $cache->delete(common_cache_key('user:replies:'.$reply->profile_id.';last'));
317                     }
318                 }
319             }
320             $reply->free();
321             unset($reply);
322         }
323     }
324
325     function blowPublicCache($blowLast=false)
326     {
327         if ($this->is_local == 1) {
328             $cache = common_memcache();
329             if ($cache) {
330                 $cache->delete(common_cache_key('public'));
331                 if ($blowLast) {
332                     $cache->delete(common_cache_key('public').';last');
333                 }
334             }
335         }
336     }
337
338     function blowFavesCache($blowLast=false)
339     {
340         $cache = common_memcache();
341         if ($cache) {
342             $fave = new Fave();
343             $fave->notice_id = $this->id;
344             if ($fave->find()) {
345                 while ($fave->fetch()) {
346                     $cache->delete(common_cache_key('user:faves:'.$fave->user_id));
347                     if ($blowLast) {
348                         $cache->delete(common_cache_key('user:faves:'.$fave->user_id.';last'));
349                     }
350                 }
351             }
352             $fave->free();
353             unset($fave);
354         }
355     }
356
357     # XXX: too many args; we need to move to named params or even a separate
358     # class for notice streams
359
360     static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $before_id=0, $order=null, $since=null) {
361
362         if (common_config('memcached', 'enabled')) {
363
364             # Skip the cache if this is a since, since_id or before_id qry
365             if ($since_id > 0 || $before_id > 0 || $since) {
366                 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order, $since);
367             } else {
368                 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
369             }
370         }
371
372         return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order, $since);
373     }
374
375     static function getStreamDirect($qry, $offset, $limit, $since_id, $before_id, $order, $since) {
376
377         $needAnd = false;
378         $needWhere = true;
379
380         if (preg_match('/\bWHERE\b/i', $qry)) {
381             $needWhere = false;
382             $needAnd = true;
383         }
384
385         if ($since_id > 0) {
386
387             if ($needWhere) {
388                 $qry .= ' WHERE ';
389                 $needWhere = false;
390             } else {
391                 $qry .= ' AND ';
392             }
393
394             $qry .= ' notice.id > ' . $since_id;
395         }
396
397         if ($before_id > 0) {
398
399             if ($needWhere) {
400                 $qry .= ' WHERE ';
401                 $needWhere = false;
402             } else {
403                 $qry .= ' AND ';
404             }
405
406             $qry .= ' notice.id < ' . $before_id;
407         }
408
409         if ($since) {
410
411             if ($needWhere) {
412                 $qry .= ' WHERE ';
413                 $needWhere = false;
414             } else {
415                 $qry .= ' AND ';
416             }
417
418             $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
419         }
420
421         # Allow ORDER override
422
423         if ($order) {
424             $qry .= $order;
425         } else {
426             $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
427         }
428
429         if (common_config('db','type') == 'pgsql') {
430             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
431         } else {
432             $qry .= ' LIMIT ' . $offset . ', ' . $limit;
433         }
434
435         $notice = new Notice();
436
437         $notice->query($qry);
438
439         return $notice;
440     }
441
442     # XXX: this is pretty long and should probably be broken up into
443     # some helper functions
444
445     static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
446
447         # If outside our cache window, just go to the DB
448
449         if ($offset + $limit > NOTICE_CACHE_WINDOW) {
450             return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
451         }
452
453         # Get the cache; if we can't, just go to the DB
454
455         $cache = common_memcache();
456
457         if (!$cache) {
458             return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
459         }
460
461         # Get the notices out of the cache
462
463         $notices = $cache->get(common_cache_key($cachekey));
464
465         # On a cache hit, return a DB-object-like wrapper
466
467         if ($notices !== false) {
468             $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
469             return $wrapper;
470         }
471
472         # If the cache was invalidated because of new data being
473         # added, we can try and just get the new stuff. We keep an additional
474         # copy of the data at the key + ';last'
475
476         # No cache hit. Try to get the *last* cached version
477
478         $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
479
480         if ($last_notices) {
481
482             # Reverse-chron order, so last ID is last.
483
484             $last_id = $last_notices[0]->id;
485
486             # XXX: this assumes monotonically increasing IDs; a fair
487             # bet with our DB.
488
489             $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
490                                                   $last_id, null, $order, null);
491
492             if ($new_notice) {
493                 $new_notices = array();
494                 while ($new_notice->fetch()) {
495                     $new_notices[] = clone($new_notice);
496                 }
497                 $new_notice->free();
498                 $notices = array_slice(array_merge($new_notices, $last_notices),
499                                        0, NOTICE_CACHE_WINDOW);
500
501                 # Store the array in the cache for next time
502
503                 $result = $cache->set(common_cache_key($cachekey), $notices);
504                 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
505
506                 # return a wrapper of the array for use now
507
508                 return new ArrayWrapper(array_slice($notices, $offset, $limit));
509             }
510         }
511
512         # Otherwise, get the full cache window out of the DB
513
514         $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
515
516         # If there are no hits, just return the value
517
518         if (!$notice) {
519             return $notice;
520         }
521
522         # Pack results into an array
523
524         $notices = array();
525
526         while ($notice->fetch()) {
527             $notices[] = clone($notice);
528         }
529
530         $notice->free();
531
532         # Store the array in the cache for next time
533
534         $result = $cache->set(common_cache_key($cachekey), $notices);
535         $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
536
537         # return a wrapper of the array for use now
538
539         $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
540
541         return $wrapper;
542     }
543
544     function publicStream($offset=0, $limit=20, $since_id=0, $before_id=0, $since=null)
545     {
546
547         $parts = array();
548
549         $qry = 'SELECT * FROM notice ';
550
551         if (common_config('public', 'localonly')) {
552             $parts[] = 'is_local = 1';
553         } else {
554             # -1 == blacklisted
555             $parts[] = 'is_local != -1';
556         }
557
558         if ($parts) {
559             $qry .= ' WHERE ' . implode(' AND ', $parts);
560         }
561
562         return Notice::getStream($qry,
563                                  'public',
564                                  $offset, $limit, $since_id, $before_id, null, $since);
565     }
566
567     function addToInboxes()
568     {
569         $enabled = common_config('inboxes', 'enabled');
570
571         if ($enabled === true || $enabled === 'transitional') {
572             $inbox = new Notice_inbox();
573             $UT = common_config('db','type')=='pgsql'?'"user"':'user';
574             $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
575               "SELECT $UT.id, " . $this->id . ', "' . $this->created . '" ' .
576               "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
577               'WHERE subscription.subscribed = ' . $this->profile_id . ' ' .
578               'AND NOT EXISTS (SELECT user_id, notice_id ' .
579               'FROM notice_inbox ' .
580               "WHERE user_id = $UT.id " .
581               'AND notice_id = ' . $this->id . ' )';
582             if ($enabled === 'transitional') {
583                 $qry .= " AND $UT.inboxed = 1";
584             }
585             $inbox->query($qry);
586         }
587         return;
588     }
589
590     function saveGroups()
591     {
592         $enabled = common_config('inboxes', 'enabled');
593         if ($enabled !== true && $enabled !== 'transitional') {
594             return;
595         }
596
597         /* extract all !group */
598         $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
599                                 strtolower($this->content),
600                                 $match);
601         if (!$count) {
602             return true;
603         }
604
605         $profile = $this->getProfile();
606
607         /* Add them to the database */
608
609         foreach (array_unique($match[1]) as $nickname) {
610             /* XXX: remote groups. */
611             $group = User_group::staticGet('nickname', $nickname);
612
613             if (!$group) {
614                 continue;
615             }
616
617             if ($profile->isMember($group)) {
618
619                 $gi = new Group_inbox();
620
621                 $gi->group_id  = $group->id;
622                 $gi->notice_id = $this->id;
623                 $gi->created   = common_sql_now();
624
625                 $result = $gi->insert();
626
627                 if (!$result) {
628                     common_log_db_error($gi, 'INSERT', __FILE__);
629                 }
630
631                 // FIXME: do this in an offline daemon
632
633                 $inbox = new Notice_inbox();
634                 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
635                 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' .
636                   "SELECT $UT.id, " . $this->id . ', "' . $this->created . '", 2 ' .
637                   "FROM $UT JOIN group_member ON $UT.id = group_member.profile_id " .
638                   'WHERE group_member.group_id = ' . $group->id . ' ' .
639                   'AND NOT EXISTS (SELECT user_id, notice_id ' .
640                   'FROM notice_inbox ' .
641                   "WHERE user_id = $UT.id " .
642                   'AND notice_id = ' . $this->id . ' )';
643                 if ($enabled === 'transitional') {
644                     $qry .= " AND $UT.inboxed = 1";
645                 }
646                 $result = $inbox->query($qry);
647             }
648         }
649     }
650
651     function saveReplies()
652     {
653         // Alternative reply format
654         $tname = false;
655         if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
656             $tname = $match[1];
657         }
658         // extract all @messages
659         $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
660
661         $names = array();
662
663         if ($cnt || $tname) {
664             // XXX: is there another way to make an array copy?
665             $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
666         }
667
668         $sender = Profile::staticGet($this->profile_id);
669
670         $replied = array();
671
672         // store replied only for first @ (what user/notice what the reply directed,
673         // we assume first @ is it)
674
675         for ($i=0; $i<count($names); $i++) {
676             $nickname = $names[$i];
677             $recipient = common_relative_profile($sender, $nickname, $this->created);
678             if (!$recipient) {
679                 continue;
680             }
681             if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
682                 $reply_for = $recipient;
683                 $recipient_notice = $reply_for->getCurrentNotice();
684                 if ($recipient_notice) {
685                     $orig = clone($this);
686                     $this->reply_to = $recipient_notice->id;
687                     $this->update($orig);
688                 }
689             }
690             // Don't save replies from blocked profile to local user
691             $recipient_user = User::staticGet('id', $recipient->id);
692             if ($recipient_user && $recipient_user->hasBlocked($sender)) {
693                 continue;
694             }
695             $reply = new Reply();
696             $reply->notice_id = $this->id;
697             $reply->profile_id = $recipient->id;
698             $id = $reply->insert();
699             if (!$id) {
700                 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
701                 common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
702                 common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
703                 return;
704             } else {
705                 $replied[$recipient->id] = 1;
706             }
707         }
708
709         // Hash format replies, too
710         $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
711         if ($cnt) {
712             foreach ($match[1] as $tag) {
713                 $tagged = Profile_tag::getTagged($sender->id, $tag);
714                 foreach ($tagged as $t) {
715                     if (!$replied[$t->id]) {
716                         // Don't save replies from blocked profile to local user
717                         $t_user = User::staticGet('id', $t->id);
718                         if ($t_user && $t_user->hasBlocked($sender)) {
719                             continue;
720                         }
721                         $reply = new Reply();
722                         $reply->notice_id = $this->id;
723                         $reply->profile_id = $t->id;
724                         $id = $reply->insert();
725                         if (!$id) {
726                             common_log_db_error($reply, 'INSERT', __FILE__);
727                             return;
728                         }
729                     }
730                 }
731             }
732         }
733     }
734 }