3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
23 * Table Definition for notice
25 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
27 /* We keep the first three 20-notice pages, plus one for pagination check,
28 * in the memcached cache. */
30 define('NOTICE_CACHE_WINDOW', 61);
32 define('NOTICE_LOCAL_PUBLIC', 1);
33 define('NOTICE_REMOTE_OMB', 0);
34 define('NOTICE_LOCAL_NONPUBLIC', -1);
35 define('NOTICE_GATEWAY', -2);
37 define('MAX_BOXCARS', 128);
39 class Notice extends Memcached_DataObject
42 /* the code below is auto generated do not remove the above tag */
44 public $__table = 'notice'; // table name
45 public $id; // int(4) primary_key not_null
46 public $profile_id; // int(4) not_null
47 public $uri; // varchar(255) unique_key
48 public $content; // varchar(140)
49 public $rendered; // text()
50 public $url; // varchar(255)
51 public $created; // datetime() not_null
52 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
53 public $reply_to; // int(4)
54 public $is_local; // tinyint(1)
55 public $source; // varchar(32)
56 public $conversation; // int(4)
59 function staticGet($k,$v=NULL) {
60 return Memcached_DataObject::staticGet('Notice',$k,$v);
63 /* the code above is auto generated do not remove the tag below */
68 return Profile::staticGet('id', $this->profile_id);
73 $this->blowCaches(true);
74 $this->blowFavesCache(true);
75 $this->blowSubsCache(true);
77 $this->query('BEGIN');
78 //Null any notices that are replies to this notice
79 $this->query(sprintf("UPDATE notice set reply_to = null WHERE reply_to = %d", $this->id));
80 $related = array('Reply',
85 if (common_config('inboxes', 'enabled')) {
86 $related[] = 'Notice_inbox';
88 foreach ($related as $cls) {
90 $inst->notice_id = $this->id;
93 $result = parent::delete();
94 $this->query('COMMIT');
99 /* extract all #hastags */
100 $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($this->content), $match);
105 /* Add them to the database */
106 foreach(array_unique($match[1]) as $hashtag) {
107 /* elide characters we don't want in the tag */
108 $this->saveTag($hashtag);
113 function saveTag($hashtag)
115 $hashtag = common_canonical_tag($hashtag);
117 $tag = new Notice_tag();
118 $tag->notice_id = $this->id;
119 $tag->tag = $hashtag;
120 $tag->created = $this->created;
121 $id = $tag->insert();
124 throw new ServerException(sprintf(_('DB error inserting hashtag: %s'),
125 $last_error->message));
130 static function saveNew($profile_id, $content, $source=null,
131 $is_local=1, $reply_to=null, $uri=null, $created=null) {
133 $profile = Profile::staticGet($profile_id);
135 $final = common_shorten_links($content);
137 if (mb_strlen($final) > 140) {
138 common_log(LOG_INFO, 'Rejecting notice that is too long.');
139 return _('Problem saving notice. Too long.');
143 common_log(LOG_ERR, 'Problem saving notice. Unknown user.');
144 return _('Problem saving notice. Unknown user.');
147 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
148 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
149 return _('Too many notices too fast; take a breather and post again in a few minutes.');
152 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
153 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
154 return _('Too many duplicate messages too quickly; take a breather and post again in a few minutes.');
157 $banned = common_config('profile', 'banned');
159 if ( in_array($profile_id, $banned) || in_array($profile->nickname, $banned)) {
160 common_log(LOG_WARNING, "Attempted post from banned user: $profile->nickname (user id = $profile_id).");
161 return _('You are banned from posting notices on this site.');
164 $notice = new Notice();
165 $notice->profile_id = $profile_id;
167 $blacklist = common_config('public', 'blacklist');
168 $autosource = common_config('public', 'autosource');
170 # Blacklisted are non-false, but not 1, either
172 if (($blacklist && in_array($profile_id, $blacklist)) ||
173 ($source && $autosource && in_array($source, $autosource))) {
174 $notice->is_local = -1;
176 $notice->is_local = $is_local;
179 $notice->query('BEGIN');
181 $notice->reply_to = $reply_to;
182 if (!empty($created)) {
183 $notice->created = $created;
185 $notice->created = common_sql_now();
187 $notice->content = $final;
188 $notice->rendered = common_render_content($final, $notice);
189 $notice->source = $source;
192 if (!empty($reply_to)) {
193 $reply_notice = Notice::staticGet('id', $reply_to);
194 if (!empty($reply_notice)) {
195 $notice->reply_to = $reply_to;
196 $notice->conversation = $reply_notice->conversation;
200 if (Event::handle('StartNoticeSave', array(&$notice))) {
202 $id = $notice->insert();
205 common_log_db_error($notice, 'INSERT', __FILE__);
206 return _('Problem saving notice.');
209 # Update the URI after the notice is in the database
211 $orig = clone($notice);
212 $notice->uri = common_notice_uri($notice);
214 if (!$notice->update($orig)) {
215 common_log_db_error($notice, 'UPDATE', __FILE__);
216 return _('Problem saving notice.');
220 # XXX: do we need to change this for remote users?
222 $notice->saveReplies();
225 $notice->addToInboxes();
228 $orig2 = clone($notice);
229 $notice->rendered = common_render_content($final, $notice);
230 if (!$notice->update($orig2)) {
231 common_log_db_error($notice, 'UPDATE', __FILE__);
232 return _('Problem saving notice.');
235 $notice->query('COMMIT');
237 Event::handle('EndNoticeSave', array($notice));
240 # Clear the cache for subscribed users, so they'll update at next request
241 # XXX: someone clever could prepend instead of clearing the cache
243 $notice->blowCaches();
248 /** save all urls in the notice to the db
250 * follow redirects and save all available file information
251 * (mimetype, date, size, oembed, etc.)
255 function saveUrls() {
256 common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
259 function saveUrl($data) {
260 list($url, $notice_id) = $data;
261 File::processNew($url, $notice_id);
264 static function checkDupes($profile_id, $content) {
265 $profile = Profile::staticGet($profile_id);
269 $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
272 while ($notice->fetch()) {
273 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
275 } else if ($notice->content == $content) {
280 # If we get here, oldest item in cache window is not
281 # old enough for dupe limit; do direct check against DB
282 $notice = new Notice();
283 $notice->profile_id = $profile_id;
284 $notice->content = $content;
285 if (common_config('db','type') == 'pgsql')
286 $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
288 $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
290 $cnt = $notice->count();
294 static function checkEditThrottle($profile_id) {
295 $profile = Profile::staticGet($profile_id);
300 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
301 if ($notice && $notice->fetch()) {
302 # If the Nth notice was posted less than timespan seconds ago
303 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
308 # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
312 function getUploadedAttachment() {
314 $query = 'select file.url as up, file.id as i from file join file_to_post on file.id = file_id where post_id=' . $post->escape($post->id) . ' and url like "%/notice/%/file"';
315 $post->query($query);
317 if (empty($post->up) || empty($post->i)) {
320 $ret = array($post->up, $post->i);
326 function hasAttachments() {
328 $query = "select count(file_id) as n_attachments from file join file_to_post on (file_id = file.id) join notice on (post_id = notice.id) where post_id = " . $post->escape($post->id);
329 $post->query($query);
331 $n_attachments = intval($post->n_attachments);
333 return $n_attachments;
336 function attachments() {
339 $f2p = new File_to_post;
340 $f2p->post_id = $this->id;
342 while ($f2p->fetch()) {
343 $f = File::staticGet($f2p->file_id);
350 function blowCaches($blowLast=false)
352 $this->blowSubsCache($blowLast);
353 $this->blowNoticeCache($blowLast);
354 $this->blowRepliesCache($blowLast);
355 $this->blowPublicCache($blowLast);
356 $this->blowTagCache($blowLast);
357 $this->blowGroupCache($blowLast);
358 $this->blowConversationCache($blowLast);
361 function blowConversationCache($blowLast=false)
363 $cache = common_memcache();
365 $ck = common_cache_key('notice:conversation_ids:'.$this->conversation);
368 $cache->delete($ck.';last');
373 function blowGroupCache($blowLast=false)
375 $cache = common_memcache();
377 $group_inbox = new Group_inbox();
378 $group_inbox->notice_id = $this->id;
379 if ($group_inbox->find()) {
380 while ($group_inbox->fetch()) {
381 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id));
383 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id.';last'));
385 $member = new Group_member();
386 $member->group_id = $group_inbox->group_id;
387 if ($member->find()) {
388 while ($member->fetch()) {
389 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id));
391 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id . ';last'));
397 $group_inbox->free();
402 function blowTagCache($blowLast=false)
404 $cache = common_memcache();
406 $tag = new Notice_tag();
407 $tag->notice_id = $this->id;
409 while ($tag->fetch()) {
410 $tag->blowCache($blowLast);
411 $ck = 'profile:notice_ids_tagged:' . $this->profile_id . ':' . $tag->tag;
415 $cache->delete($ck . ';last');
424 function blowSubsCache($blowLast=false)
426 $cache = common_memcache();
430 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
431 $user->query('SELECT id ' .
433 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
434 'WHERE subscription.subscribed = ' . $this->profile_id);
436 while ($user->fetch()) {
437 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id));
438 $cache->delete(common_cache_key('notice_inbox:by_user_own:'.$user->id));
440 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id.';last'));
441 $cache->delete(common_cache_key('notice_inbox:by_user_own:'.$user->id.';last'));
449 function blowNoticeCache($blowLast=false)
451 if ($this->is_local) {
452 $cache = common_memcache();
453 if (!empty($cache)) {
454 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id));
456 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id.';last'));
462 function blowRepliesCache($blowLast=false)
464 $cache = common_memcache();
466 $reply = new Reply();
467 $reply->notice_id = $this->id;
468 if ($reply->find()) {
469 while ($reply->fetch()) {
470 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id));
472 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id.';last'));
481 function blowPublicCache($blowLast=false)
483 if ($this->is_local == 1) {
484 $cache = common_memcache();
486 $cache->delete(common_cache_key('public'));
488 $cache->delete(common_cache_key('public').';last');
494 function blowFavesCache($blowLast=false)
496 $cache = common_memcache();
499 $fave->notice_id = $this->id;
501 while ($fave->fetch()) {
502 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id));
503 $cache->delete(common_cache_key('fave:by_user_own:'.$fave->user_id));
505 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id.';last'));
506 $cache->delete(common_cache_key('fave:by_user_own:'.$fave->user_id.';last'));
515 # XXX: too many args; we need to move to named params or even a separate
516 # class for notice streams
518 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $order=null, $since=null) {
520 if (common_config('memcached', 'enabled')) {
522 # Skip the cache if this is a since, since_id or max_id qry
523 if ($since_id > 0 || $max_id > 0 || $since) {
524 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
526 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
530 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
533 static function getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since) {
538 if (preg_match('/\bWHERE\b/i', $qry)) {
552 $qry .= ' notice.id > ' . $since_id;
564 $qry .= ' notice.id <= ' . $max_id;
576 $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
579 # Allow ORDER override
584 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
587 if (common_config('db','type') == 'pgsql') {
588 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
590 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
593 $notice = new Notice();
595 $notice->query($qry);
600 # XXX: this is pretty long and should probably be broken up into
601 # some helper functions
603 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
605 # If outside our cache window, just go to the DB
607 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
608 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
611 # Get the cache; if we can't, just go to the DB
613 $cache = common_memcache();
616 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
619 # Get the notices out of the cache
621 $notices = $cache->get(common_cache_key($cachekey));
623 # On a cache hit, return a DB-object-like wrapper
625 if ($notices !== false) {
626 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
630 # If the cache was invalidated because of new data being
631 # added, we can try and just get the new stuff. We keep an additional
632 # copy of the data at the key + ';last'
634 # No cache hit. Try to get the *last* cached version
636 $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
640 # Reverse-chron order, so last ID is last.
642 $last_id = $last_notices[0]->id;
644 # XXX: this assumes monotonically increasing IDs; a fair
647 $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
648 $last_id, null, $order, null);
651 $new_notices = array();
652 while ($new_notice->fetch()) {
653 $new_notices[] = clone($new_notice);
656 $notices = array_slice(array_merge($new_notices, $last_notices),
657 0, NOTICE_CACHE_WINDOW);
659 # Store the array in the cache for next time
661 $result = $cache->set(common_cache_key($cachekey), $notices);
662 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
664 # return a wrapper of the array for use now
666 return new ArrayWrapper(array_slice($notices, $offset, $limit));
670 # Otherwise, get the full cache window out of the DB
672 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
674 # If there are no hits, just return the value
680 # Pack results into an array
684 while ($notice->fetch()) {
685 $notices[] = clone($notice);
690 # Store the array in the cache for next time
692 $result = $cache->set(common_cache_key($cachekey), $notices);
693 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
695 # return a wrapper of the array for use now
697 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
702 function getStreamByIds($ids)
704 $cache = common_memcache();
706 if (!empty($cache)) {
708 foreach ($ids as $id) {
709 $n = Notice::staticGet('id', $id);
714 return new ArrayWrapper($notices);
716 $notice = new Notice();
717 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
718 $notice->orderBy('id DESC');
725 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
727 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
730 $offset, $limit, $since_id, $max_id, $since);
732 return Notice::getStreamByIds($ids);
735 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
737 $notice = new Notice();
739 $notice->selectAdd(); // clears it
740 $notice->selectAdd('id');
742 $notice->orderBy('id DESC');
744 if (!is_null($offset)) {
745 $notice->limit($offset, $limit);
748 if (common_config('public', 'localonly')) {
749 $notice->whereAdd('is_local = 1');
752 $notice->whereAdd('is_local != -1');
755 if ($since_id != 0) {
756 $notice->whereAdd('id > ' . $since_id);
760 $notice->whereAdd('id <= ' . $max_id);
763 if (!is_null($since)) {
764 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
769 if ($notice->find()) {
770 while ($notice->fetch()) {
771 $ids[] = $notice->id;
781 function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
783 $ids = Notice::stream(array('Notice', '_conversationStreamDirect'),
785 'notice:conversation_ids:'.$id,
786 $offset, $limit, $since_id, $max_id, $since);
788 return Notice::getStreamByIds($ids);
791 function _conversationStreamDirect($id, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
793 $notice = new Notice();
795 $notice->selectAdd(); // clears it
796 $notice->selectAdd('id');
798 $notice->conversation = $id;
800 $notice->orderBy('id DESC');
802 if (!is_null($offset)) {
803 $notice->limit($offset, $limit);
806 if ($since_id != 0) {
807 $notice->whereAdd('id > ' . $since_id);
811 $notice->whereAdd('id <= ' . $max_id);
814 if (!is_null($since)) {
815 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
820 if ($notice->find()) {
821 while ($notice->fetch()) {
822 $ids[] = $notice->id;
832 function addToInboxes()
834 $enabled = common_config('inboxes', 'enabled');
836 if ($enabled === true || $enabled === 'transitional') {
838 // XXX: loads constants
840 $inbox = new Notice_inbox();
842 $users = $this->getSubscribedUsers();
844 // FIXME: kind of ignoring 'transitional'...
845 // we'll probably stop supporting inboxless mode
850 foreach ($users as $id) {
851 $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
854 $groups = $this->saveGroups();
856 foreach ($groups as $group) {
857 $users = $group->getUserMembers();
858 foreach ($users as $id) {
859 if (!array_key_exists($id, $ni)) {
860 $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
867 $qryhdr = 'INSERT INTO notice_inbox (user_id, notice_id, source, created) VALUES ';
870 foreach ($ni as $id => $source) {
874 $qry .= '('.$id.', '.$this->id.', '.$source.', "'.$this->created.'") ';
876 if ($cnt >= MAX_BOXCARS) {
877 $inbox = new Notice_inbox();
885 $inbox = new Notice_inbox();
893 function getSubscribedUsers()
899 'FROM user JOIN subscription '.
900 'ON user.id = subscription.subscriber ' .
901 'WHERE subscription.subscribed = %d ';
903 $user->query(sprintf($qry, $this->profile_id));
907 while ($user->fetch()) {
916 function saveGroups()
920 $enabled = common_config('inboxes', 'enabled');
921 if ($enabled !== true && $enabled !== 'transitional') {
925 /* extract all !group */
926 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
927 strtolower($this->content),
933 $profile = $this->getProfile();
935 /* Add them to the database */
937 foreach (array_unique($match[1]) as $nickname) {
938 /* XXX: remote groups. */
939 $group = User_group::getForNickname($nickname);
945 // we automatically add a tag for every group name, too
947 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
948 'notice_id' => $this->id));
951 $this->saveTag($nickname);
954 if ($profile->isMember($group)) {
956 $result = $this->addToGroupInbox($group);
959 common_log_db_error($gi, 'INSERT', __FILE__);
962 $groups[] = clone($group);
969 function addToGroupInbox($group)
971 $gi = Group_inbox::pkeyGet(array('group_id' => $group->id,
972 'notice_id' => $this->id));
976 $gi = new Group_inbox();
978 $gi->group_id = $group->id;
979 $gi->notice_id = $this->id;
980 $gi->created = $this->created;
982 return $gi->insert();
988 function saveReplies()
990 // Alternative reply format
992 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
995 // extract all @messages
996 $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
1000 if ($cnt || $tname) {
1001 // XXX: is there another way to make an array copy?
1002 $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
1005 $sender = Profile::staticGet($this->profile_id);
1009 // store replied only for first @ (what user/notice what the reply directed,
1010 // we assume first @ is it)
1012 for ($i=0; $i<count($names); $i++) {
1013 $nickname = $names[$i];
1014 $recipient = common_relative_profile($sender, $nickname, $this->created);
1018 if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
1019 $reply_for = $recipient;
1020 $recipient_notice = $reply_for->getCurrentNotice();
1021 if ($recipient_notice) {
1022 $orig = clone($this);
1023 $this->reply_to = $recipient_notice->id;
1024 $this->conversation = $recipient_notice->conversation;
1025 $this->update($orig);
1028 // Don't save replies from blocked profile to local user
1029 $recipient_user = User::staticGet('id', $recipient->id);
1030 if ($recipient_user && $recipient_user->hasBlocked($sender)) {
1033 $reply = new Reply();
1034 $reply->notice_id = $this->id;
1035 $reply->profile_id = $recipient->id;
1036 $id = $reply->insert();
1038 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
1039 common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
1040 common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
1043 $replied[$recipient->id] = 1;
1047 // Hash format replies, too
1048 $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
1050 foreach ($match[1] as $tag) {
1051 $tagged = Profile_tag::getTagged($sender->id, $tag);
1052 foreach ($tagged as $t) {
1053 if (!$replied[$t->id]) {
1054 // Don't save replies from blocked profile to local user
1055 $t_user = User::staticGet('id', $t->id);
1056 if ($t_user && $t_user->hasBlocked($sender)) {
1059 $reply = new Reply();
1060 $reply->notice_id = $this->id;
1061 $reply->profile_id = $t->id;
1062 $id = $reply->insert();
1064 common_log_db_error($reply, 'INSERT', __FILE__);
1067 $replied[$recipient->id] = 1;
1074 // If it's not a reply, make it the root of a new conversation
1076 if (empty($this->conversation)) {
1077 $orig = clone($this);
1078 $this->conversation = $this->id;
1079 $this->update($orig);
1082 foreach (array_keys($replied) as $recipient) {
1083 $user = User::staticGet('id', $recipient);
1085 mail_notify_attn($user, $this);
1090 function asAtomEntry($namespace=false, $source=false)
1092 $profile = $this->getProfile();
1094 $xs = new XMLStringer(true);
1097 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
1098 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
1103 $xs->elementStart('entry', $attrs);
1106 $xs->elementStart('source');
1107 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
1108 $xs->element('link', array('href' => $profile->profileurl));
1109 $user = User::staticGet('id', $profile->id);
1110 if (!empty($user)) {
1111 $atom_feed = common_local_url('api',
1112 array('apiaction' => 'statuses',
1113 'method' => 'user_timeline',
1114 'argument' => $profile->nickname.'.atom'));
1115 $xs->element('link', array('rel' => 'self',
1116 'type' => 'application/atom+xml',
1117 'href' => $profile->profileurl));
1118 $xs->element('link', array('rel' => 'license',
1119 'href' => common_config('license', 'url')));
1122 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
1125 $xs->elementStart('author');
1126 $xs->element('name', null, $profile->nickname);
1127 $xs->element('uri', null, $profile->profileurl);
1128 $xs->elementEnd('author');
1131 $xs->elementEnd('source');
1134 $xs->element('title', null, $this->content);
1135 $xs->element('summary', null, $this->content);
1137 $xs->element('link', array('rel' => 'alternate',
1138 'href' => $this->bestUrl()));
1140 $xs->element('id', null, $this->uri);
1142 $xs->element('published', null, common_date_w3dtf($this->created));
1143 $xs->element('updated', null, common_date_w3dtf($this->modified));
1145 if ($this->reply_to) {
1146 $reply_notice = Notice::staticGet('id', $this->reply_to);
1147 if (!empty($reply_notice)) {
1148 $xs->element('link', array('rel' => 'related',
1149 'href' => $reply_notice->bestUrl()));
1150 $xs->element('thr:in-reply-to',
1151 array('ref' => $reply_notice->uri,
1152 'href' => $reply_notice->bestUrl()));
1156 $xs->element('content', array('type' => 'html'), $this->rendered);
1158 $tag = new Notice_tag();
1159 $tag->notice_id = $this->id;
1161 while ($tag->fetch()) {
1162 $xs->element('category', array('term' => $tag->tag));
1167 $xs->elementEnd('entry');
1169 return $xs->getString();
1174 if (!empty($this->url)) {
1176 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
1179 return common_local_url('shownotice',
1180 array('notice' => $this->id));
1184 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
1186 $cache = common_memcache();
1188 if (empty($cache) ||
1189 $since_id != 0 || $max_id != 0 || (!is_null($since) && $since > 0) ||
1191 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1192 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1196 $idkey = common_cache_key($cachekey);
1198 $idstr = $cache->get($idkey);
1200 if (!empty($idstr)) {
1201 // Cache hit! Woohoo!
1202 $window = explode(',', $idstr);
1203 $ids = array_slice($window, $offset, $limit);
1207 $laststr = $cache->get($idkey.';last');
1209 if (!empty($laststr)) {
1210 $window = explode(',', $laststr);
1211 $last_id = $window[0];
1212 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1213 $last_id, 0, null)));
1215 $new_window = array_merge($new_ids, $window);
1217 $new_windowstr = implode(',', $new_window);
1219 $result = $cache->set($idkey, $new_windowstr);
1220 $result = $cache->set($idkey . ';last', $new_windowstr);
1222 $ids = array_slice($new_window, $offset, $limit);
1227 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1230 $windowstr = implode(',', $window);
1232 $result = $cache->set($idkey, $windowstr);
1233 $result = $cache->set($idkey . ';last', $windowstr);
1235 $ids = array_slice($window, $offset, $limit);