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