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);
36 define('MAX_BOXCARS', 128);
38 class Notice extends Memcached_DataObject
41 /* the code below is auto generated do not remove the above tag */
43 public $__table = 'notice'; // table name
44 public $id; // int(4) primary_key not_null
45 public $profile_id; // int(4) not_null
46 public $uri; // varchar(255) unique_key
47 public $content; // varchar(140)
48 public $rendered; // text()
49 public $url; // varchar(255)
50 public $created; // datetime() not_null
51 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
52 public $reply_to; // int(4)
53 public $is_local; // tinyint(1)
54 public $source; // varchar(32)
55 public $conversation; // int(4)
58 function staticGet($k,$v=NULL) {
59 return Memcached_DataObject::staticGet('Notice',$k,$v);
62 /* the code above is auto generated do not remove the tag below */
69 return Profile::staticGet('id', $this->profile_id);
74 $this->blowCaches(true);
75 $this->blowFavesCache(true);
76 $this->blowSubsCache(true);
78 // For auditing purposes, save a record that the notice
81 $deleted = new Deleted_notice();
83 $deleted->id = $this->id;
84 $deleted->profile_id = $this->profile_id;
85 $deleted->uri = $this->uri;
86 $deleted->created = $this->created;
87 $deleted->deleted = common_sql_now();
89 $this->query('BEGIN');
93 //Null any notices that are replies to this notice
94 $this->query(sprintf("UPDATE notice set reply_to = null WHERE reply_to = %d", $this->id));
95 $related = array('Reply',
100 if (common_config('inboxes', 'enabled')) {
101 $related[] = 'Notice_inbox';
103 foreach ($related as $cls) {
105 $inst->notice_id = $this->id;
108 $result = parent::delete();
109 $this->query('COMMIT');
114 /* extract all #hastags */
115 $count = preg_match_all('/(?:^|\s)#([\pL\pN_\-\.]{1,64})/', strtolower($this->content), $match);
120 //turn each into their canonical tag
121 //this is needed to remove dupes before saving e.g. #hash.tag = #hashtag
123 for($i=0; $i<count($match[1]); $i++) {
124 $hashtags[] = common_canonical_tag($match[1][$i]);
128 /* Add them to the database */
129 foreach(array_unique($hashtags) as $hashtag) {
130 /* elide characters we don't want in the tag */
131 $this->saveTag($hashtag);
136 function saveTag($hashtag)
138 $tag = new Notice_tag();
139 $tag->notice_id = $this->id;
140 $tag->tag = $hashtag;
141 $tag->created = $this->created;
142 $id = $tag->insert();
145 throw new ServerException(sprintf(_('DB error inserting hashtag: %s'),
146 $last_error->message));
151 static function saveNew($profile_id, $content, $source=null,
152 $is_local=1, $reply_to=null, $uri=null, $created=null) {
154 $profile = Profile::staticGet($profile_id);
156 $final = common_shorten_links($content);
158 if (mb_strlen($final) > 140) {
159 common_log(LOG_INFO, 'Rejecting notice that is too long.');
160 return _('Problem saving notice. Too long.');
164 common_log(LOG_ERR, 'Problem saving notice. Unknown user.');
165 return _('Problem saving notice. Unknown user.');
168 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
169 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
170 return _('Too many notices too fast; take a breather and post again in a few minutes.');
173 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
174 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
175 return _('Too many duplicate messages too quickly; take a breather and post again in a few minutes.');
178 $banned = common_config('profile', 'banned');
180 if ( in_array($profile_id, $banned) || in_array($profile->nickname, $banned)) {
181 common_log(LOG_WARNING, "Attempted post from banned user: $profile->nickname (user id = $profile_id).");
182 return _('You are banned from posting notices on this site.');
185 $notice = new Notice();
186 $notice->profile_id = $profile_id;
188 $blacklist = common_config('public', 'blacklist');
189 $autosource = common_config('public', 'autosource');
191 # Blacklisted are non-false, but not 1, either
193 if (($blacklist && in_array($profile_id, $blacklist)) ||
194 ($source && $autosource && in_array($source, $autosource))) {
195 $notice->is_local = -1;
197 $notice->is_local = $is_local;
200 $notice->query('BEGIN');
202 $notice->reply_to = $reply_to;
203 if (!empty($created)) {
204 $notice->created = $created;
206 $notice->created = common_sql_now();
208 $notice->content = $final;
209 $notice->rendered = common_render_content($final, $notice);
210 $notice->source = $source;
213 if (!empty($reply_to)) {
214 $reply_notice = Notice::staticGet('id', $reply_to);
215 if (!empty($reply_notice)) {
216 $notice->reply_to = $reply_to;
217 $notice->conversation = $reply_notice->conversation;
221 if (Event::handle('StartNoticeSave', array(&$notice))) {
223 $id = $notice->insert();
226 common_log_db_error($notice, 'INSERT', __FILE__);
227 return _('Problem saving notice.');
230 # Update the URI after the notice is in the database
232 $orig = clone($notice);
233 $notice->uri = common_notice_uri($notice);
235 if (!$notice->update($orig)) {
236 common_log_db_error($notice, 'UPDATE', __FILE__);
237 return _('Problem saving notice.');
241 # XXX: do we need to change this for remote users?
243 $notice->saveReplies();
246 $notice->addToInboxes();
249 $orig2 = clone($notice);
250 $notice->rendered = common_render_content($final, $notice);
251 if (!$notice->update($orig2)) {
252 common_log_db_error($notice, 'UPDATE', __FILE__);
253 return _('Problem saving notice.');
256 $notice->query('COMMIT');
258 Event::handle('EndNoticeSave', array($notice));
261 # Clear the cache for subscribed users, so they'll update at next request
262 # XXX: someone clever could prepend instead of clearing the cache
264 $notice->blowCaches();
269 /** save all urls in the notice to the db
271 * follow redirects and save all available file information
272 * (mimetype, date, size, oembed, etc.)
276 function saveUrls() {
277 common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
280 function saveUrl($data) {
281 list($url, $notice_id) = $data;
282 File::processNew($url, $notice_id);
285 static function checkDupes($profile_id, $content) {
286 $profile = Profile::staticGet($profile_id);
290 $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
293 while ($notice->fetch()) {
294 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
296 } else if ($notice->content == $content) {
301 # If we get here, oldest item in cache window is not
302 # old enough for dupe limit; do direct check against DB
303 $notice = new Notice();
304 $notice->profile_id = $profile_id;
305 $notice->content = $content;
306 if (common_config('db','type') == 'pgsql')
307 $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
309 $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
311 $cnt = $notice->count();
315 static function checkEditThrottle($profile_id) {
316 $profile = Profile::staticGet($profile_id);
321 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
322 if ($notice && $notice->fetch()) {
323 # If the Nth notice was posted less than timespan seconds ago
324 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
329 # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
333 function getUploadedAttachment() {
335 $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"';
336 $post->query($query);
338 if (empty($post->up) || empty($post->i)) {
341 $ret = array($post->up, $post->i);
347 function hasAttachments() {
349 $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);
350 $post->query($query);
352 $n_attachments = intval($post->n_attachments);
354 return $n_attachments;
357 function attachments() {
360 $f2p = new File_to_post;
361 $f2p->post_id = $this->id;
363 while ($f2p->fetch()) {
364 $f = File::staticGet($f2p->file_id);
371 function blowCaches($blowLast=false)
373 $this->blowSubsCache($blowLast);
374 $this->blowNoticeCache($blowLast);
375 $this->blowRepliesCache($blowLast);
376 $this->blowPublicCache($blowLast);
377 $this->blowTagCache($blowLast);
378 $this->blowGroupCache($blowLast);
379 $this->blowConversationCache($blowLast);
380 $profile = Profile::staticGet($this->profile_id);
381 $profile->blowNoticeCount();
384 function blowConversationCache($blowLast=false)
386 $cache = common_memcache();
388 $ck = common_cache_key('notice:conversation_ids:'.$this->conversation);
391 $cache->delete($ck.';last');
396 function blowGroupCache($blowLast=false)
398 $cache = common_memcache();
400 $group_inbox = new Group_inbox();
401 $group_inbox->notice_id = $this->id;
402 if ($group_inbox->find()) {
403 while ($group_inbox->fetch()) {
404 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id));
406 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id.';last'));
408 $member = new Group_member();
409 $member->group_id = $group_inbox->group_id;
410 if ($member->find()) {
411 while ($member->fetch()) {
412 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id));
414 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id . ';last'));
420 $group_inbox->free();
425 function blowTagCache($blowLast=false)
427 $cache = common_memcache();
429 $tag = new Notice_tag();
430 $tag->notice_id = $this->id;
432 while ($tag->fetch()) {
433 $tag->blowCache($blowLast);
434 $ck = 'profile:notice_ids_tagged:' . $this->profile_id . ':' . $tag->tag;
438 $cache->delete($ck . ';last');
447 function blowSubsCache($blowLast=false)
449 $cache = common_memcache();
453 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
454 $user->query('SELECT id ' .
456 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
457 'WHERE subscription.subscribed = ' . $this->profile_id);
459 while ($user->fetch()) {
460 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id));
461 $cache->delete(common_cache_key('notice_inbox:by_user_own:'.$user->id));
463 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id.';last'));
464 $cache->delete(common_cache_key('notice_inbox:by_user_own:'.$user->id.';last'));
472 function blowNoticeCache($blowLast=false)
474 if ($this->is_local) {
475 $cache = common_memcache();
476 if (!empty($cache)) {
477 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id));
479 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id.';last'));
485 function blowRepliesCache($blowLast=false)
487 $cache = common_memcache();
489 $reply = new Reply();
490 $reply->notice_id = $this->id;
491 if ($reply->find()) {
492 while ($reply->fetch()) {
493 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id));
495 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id.';last'));
504 function blowPublicCache($blowLast=false)
506 if ($this->is_local == 1) {
507 $cache = common_memcache();
509 $cache->delete(common_cache_key('public'));
511 $cache->delete(common_cache_key('public').';last');
517 function blowFavesCache($blowLast=false)
519 $cache = common_memcache();
522 $fave->notice_id = $this->id;
524 while ($fave->fetch()) {
525 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id));
526 $cache->delete(common_cache_key('fave:by_user_own:'.$fave->user_id));
528 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id.';last'));
529 $cache->delete(common_cache_key('fave:by_user_own:'.$fave->user_id.';last'));
538 # XXX: too many args; we need to move to named params or even a separate
539 # class for notice streams
541 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $order=null, $since=null) {
543 if (common_config('memcached', 'enabled')) {
545 # Skip the cache if this is a since, since_id or max_id qry
546 if ($since_id > 0 || $max_id > 0 || $since) {
547 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
549 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
553 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
556 static function getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since) {
561 if (preg_match('/\bWHERE\b/i', $qry)) {
575 $qry .= ' notice.id > ' . $since_id;
587 $qry .= ' notice.id <= ' . $max_id;
599 $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
602 # Allow ORDER override
607 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
610 if (common_config('db','type') == 'pgsql') {
611 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
613 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
616 $notice = new Notice();
618 $notice->query($qry);
623 # XXX: this is pretty long and should probably be broken up into
624 # some helper functions
626 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
628 # If outside our cache window, just go to the DB
630 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
631 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
634 # Get the cache; if we can't, just go to the DB
636 $cache = common_memcache();
639 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
642 # Get the notices out of the cache
644 $notices = $cache->get(common_cache_key($cachekey));
646 # On a cache hit, return a DB-object-like wrapper
648 if ($notices !== false) {
649 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
653 # If the cache was invalidated because of new data being
654 # added, we can try and just get the new stuff. We keep an additional
655 # copy of the data at the key + ';last'
657 # No cache hit. Try to get the *last* cached version
659 $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
663 # Reverse-chron order, so last ID is last.
665 $last_id = $last_notices[0]->id;
667 # XXX: this assumes monotonically increasing IDs; a fair
670 $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
671 $last_id, null, $order, null);
674 $new_notices = array();
675 while ($new_notice->fetch()) {
676 $new_notices[] = clone($new_notice);
679 $notices = array_slice(array_merge($new_notices, $last_notices),
680 0, NOTICE_CACHE_WINDOW);
682 # Store the array in the cache for next time
684 $result = $cache->set(common_cache_key($cachekey), $notices);
685 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
687 # return a wrapper of the array for use now
689 return new ArrayWrapper(array_slice($notices, $offset, $limit));
693 # Otherwise, get the full cache window out of the DB
695 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
697 # If there are no hits, just return the value
703 # Pack results into an array
707 while ($notice->fetch()) {
708 $notices[] = clone($notice);
713 # Store the array in the cache for next time
715 $result = $cache->set(common_cache_key($cachekey), $notices);
716 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
718 # return a wrapper of the array for use now
720 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
725 function getStreamByIds($ids)
727 $cache = common_memcache();
729 if (!empty($cache)) {
731 foreach ($ids as $id) {
732 $n = Notice::staticGet('id', $id);
737 return new ArrayWrapper($notices);
739 $notice = new Notice();
740 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
741 $notice->orderBy('id DESC');
748 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
750 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
753 $offset, $limit, $since_id, $max_id, $since);
755 return Notice::getStreamByIds($ids);
758 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
760 $notice = new Notice();
762 $notice->selectAdd(); // clears it
763 $notice->selectAdd('id');
765 $notice->orderBy('id DESC');
767 if (!is_null($offset)) {
768 $notice->limit($offset, $limit);
771 if (common_config('public', 'localonly')) {
772 $notice->whereAdd('is_local = 1');
775 $notice->whereAdd('is_local != -1');
778 if ($since_id != 0) {
779 $notice->whereAdd('id > ' . $since_id);
783 $notice->whereAdd('id <= ' . $max_id);
786 if (!is_null($since)) {
787 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
792 if ($notice->find()) {
793 while ($notice->fetch()) {
794 $ids[] = $notice->id;
804 function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
806 $ids = Notice::stream(array('Notice', '_conversationStreamDirect'),
808 'notice:conversation_ids:'.$id,
809 $offset, $limit, $since_id, $max_id, $since);
811 return Notice::getStreamByIds($ids);
814 function _conversationStreamDirect($id, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
816 $notice = new Notice();
818 $notice->selectAdd(); // clears it
819 $notice->selectAdd('id');
821 $notice->conversation = $id;
823 $notice->orderBy('id DESC');
825 if (!is_null($offset)) {
826 $notice->limit($offset, $limit);
829 if ($since_id != 0) {
830 $notice->whereAdd('id > ' . $since_id);
834 $notice->whereAdd('id <= ' . $max_id);
837 if (!is_null($since)) {
838 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
843 if ($notice->find()) {
844 while ($notice->fetch()) {
845 $ids[] = $notice->id;
855 function addToInboxes()
857 $enabled = common_config('inboxes', 'enabled');
859 if ($enabled === true || $enabled === 'transitional') {
861 // XXX: loads constants
863 $inbox = new Notice_inbox();
865 $users = $this->getSubscribedUsers();
867 // FIXME: kind of ignoring 'transitional'...
868 // we'll probably stop supporting inboxless mode
873 foreach ($users as $id) {
874 $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
877 $groups = $this->saveGroups();
879 foreach ($groups as $group) {
880 $users = $group->getUserMembers();
881 foreach ($users as $id) {
882 if (!array_key_exists($id, $ni)) {
883 $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
890 $qryhdr = 'INSERT INTO notice_inbox (user_id, notice_id, source, created) VALUES ';
893 foreach ($ni as $id => $source) {
897 $qry .= '('.$id.', '.$this->id.', '.$source.", '".$this->created. "') ";
899 if (rand() % NOTICE_INBOX_SOFT_LIMIT == 0) {
900 Notice_inbox::gc($id);
902 if ($cnt >= MAX_BOXCARS) {
903 $inbox = new Notice_inbox();
911 $inbox = new Notice_inbox();
919 function getSubscribedUsers()
923 if(common_config('db','quote_identifiers'))
924 $user_table = '"user"';
925 else $user_table = 'user';
929 'FROM '. $user_table .' JOIN subscription '.
930 'ON '. $user_table .'.id = subscription.subscriber ' .
931 'WHERE subscription.subscribed = %d ';
933 $user->query(sprintf($qry, $this->profile_id));
937 while ($user->fetch()) {
946 function saveGroups()
950 $enabled = common_config('inboxes', 'enabled');
951 if ($enabled !== true && $enabled !== 'transitional') {
955 /* extract all !group */
956 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
957 strtolower($this->content),
963 $profile = $this->getProfile();
965 /* Add them to the database */
967 foreach (array_unique($match[1]) as $nickname) {
968 /* XXX: remote groups. */
969 $group = User_group::getForNickname($nickname);
975 // we automatically add a tag for every group name, too
977 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
978 'notice_id' => $this->id));
981 $this->saveTag($nickname);
984 if ($profile->isMember($group)) {
986 $result = $this->addToGroupInbox($group);
989 common_log_db_error($gi, 'INSERT', __FILE__);
992 $groups[] = clone($group);
999 function addToGroupInbox($group)
1001 $gi = Group_inbox::pkeyGet(array('group_id' => $group->id,
1002 'notice_id' => $this->id));
1006 $gi = new Group_inbox();
1008 $gi->group_id = $group->id;
1009 $gi->notice_id = $this->id;
1010 $gi->created = $this->created;
1012 return $gi->insert();
1018 function saveReplies()
1020 // Alternative reply format
1022 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
1025 // extract all @messages
1026 $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
1030 if ($cnt || $tname) {
1031 // XXX: is there another way to make an array copy?
1032 $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
1035 $sender = Profile::staticGet($this->profile_id);
1039 // store replied only for first @ (what user/notice what the reply directed,
1040 // we assume first @ is it)
1042 for ($i=0; $i<count($names); $i++) {
1043 $nickname = $names[$i];
1044 $recipient = common_relative_profile($sender, $nickname, $this->created);
1048 if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
1049 $reply_for = $recipient;
1050 $recipient_notice = $reply_for->getCurrentNotice();
1051 if ($recipient_notice) {
1052 $orig = clone($this);
1053 $this->reply_to = $recipient_notice->id;
1054 $this->conversation = $recipient_notice->conversation;
1055 $this->update($orig);
1058 // Don't save replies from blocked profile to local user
1059 $recipient_user = User::staticGet('id', $recipient->id);
1060 if ($recipient_user && $recipient_user->hasBlocked($sender)) {
1063 $reply = new Reply();
1064 $reply->notice_id = $this->id;
1065 $reply->profile_id = $recipient->id;
1066 $id = $reply->insert();
1068 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
1069 common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
1070 common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
1073 $replied[$recipient->id] = 1;
1077 // Hash format replies, too
1078 $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
1080 foreach ($match[1] as $tag) {
1081 $tagged = Profile_tag::getTagged($sender->id, $tag);
1082 foreach ($tagged as $t) {
1083 if (!$replied[$t->id]) {
1084 // Don't save replies from blocked profile to local user
1085 $t_user = User::staticGet('id', $t->id);
1086 if ($t_user && $t_user->hasBlocked($sender)) {
1089 $reply = new Reply();
1090 $reply->notice_id = $this->id;
1091 $reply->profile_id = $t->id;
1092 $id = $reply->insert();
1094 common_log_db_error($reply, 'INSERT', __FILE__);
1097 $replied[$recipient->id] = 1;
1104 // If it's not a reply, make it the root of a new conversation
1106 if (empty($this->conversation)) {
1107 $orig = clone($this);
1108 $this->conversation = $this->id;
1109 $this->update($orig);
1112 foreach (array_keys($replied) as $recipient) {
1113 $user = User::staticGet('id', $recipient);
1115 mail_notify_attn($user, $this);
1120 function asAtomEntry($namespace=false, $source=false)
1122 $profile = $this->getProfile();
1124 $xs = new XMLStringer(true);
1127 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
1128 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
1133 $xs->elementStart('entry', $attrs);
1136 $xs->elementStart('source');
1137 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
1138 $xs->element('link', array('href' => $profile->profileurl));
1139 $user = User::staticGet('id', $profile->id);
1140 if (!empty($user)) {
1141 $atom_feed = common_local_url('api',
1142 array('apiaction' => 'statuses',
1143 'method' => 'user_timeline',
1144 'argument' => $profile->nickname.'.atom'));
1145 $xs->element('link', array('rel' => 'self',
1146 'type' => 'application/atom+xml',
1147 'href' => $profile->profileurl));
1148 $xs->element('link', array('rel' => 'license',
1149 'href' => common_config('license', 'url')));
1152 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
1155 $xs->elementStart('author');
1156 $xs->element('name', null, $profile->nickname);
1157 $xs->element('uri', null, $profile->profileurl);
1158 $xs->elementEnd('author');
1161 $xs->elementEnd('source');
1164 $xs->element('title', null, $this->content);
1165 $xs->element('summary', null, $this->content);
1167 $xs->element('link', array('rel' => 'alternate',
1168 'href' => $this->bestUrl()));
1170 $xs->element('id', null, $this->uri);
1172 $xs->element('published', null, common_date_w3dtf($this->created));
1173 $xs->element('updated', null, common_date_w3dtf($this->modified));
1175 if ($this->reply_to) {
1176 $reply_notice = Notice::staticGet('id', $this->reply_to);
1177 if (!empty($reply_notice)) {
1178 $xs->element('link', array('rel' => 'related',
1179 'href' => $reply_notice->bestUrl()));
1180 $xs->element('thr:in-reply-to',
1181 array('ref' => $reply_notice->uri,
1182 'href' => $reply_notice->bestUrl()));
1186 $xs->element('content', array('type' => 'html'), $this->rendered);
1188 $tag = new Notice_tag();
1189 $tag->notice_id = $this->id;
1191 while ($tag->fetch()) {
1192 $xs->element('category', array('term' => $tag->tag));
1198 $attachments = $this->attachments();
1200 foreach($attachments as $attachment){
1201 if ($attachment->isEnclosure()) {
1202 $attributes = array('rel'=>'enclosure','href'=>$attachment->url,'type'=>$attachment->mimetype,'length'=>$attachment->size);
1203 if($attachment->title){
1204 $attributes['title']=$attachment->title;
1206 $xs->element('link', $attributes, null);
1211 $xs->elementEnd('entry');
1213 return $xs->getString();
1218 if (!empty($this->url)) {
1220 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
1223 return common_local_url('shownotice',
1224 array('notice' => $this->id));
1228 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
1230 $cache = common_memcache();
1232 if (empty($cache) ||
1233 $since_id != 0 || $max_id != 0 || (!is_null($since) && $since > 0) ||
1235 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1236 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1240 $idkey = common_cache_key($cachekey);
1242 $idstr = $cache->get($idkey);
1244 if (!empty($idstr)) {
1245 // Cache hit! Woohoo!
1246 $window = explode(',', $idstr);
1247 $ids = array_slice($window, $offset, $limit);
1251 $laststr = $cache->get($idkey.';last');
1253 if (!empty($laststr)) {
1254 $window = explode(',', $laststr);
1255 $last_id = $window[0];
1256 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1257 $last_id, 0, null)));
1259 $new_window = array_merge($new_ids, $window);
1261 $new_windowstr = implode(',', $new_window);
1263 $result = $cache->set($idkey, $new_windowstr);
1264 $result = $cache->set($idkey . ';last', $new_windowstr);
1266 $ids = array_slice($new_window, $offset, $limit);
1271 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1274 $windowstr = implode(',', $window);
1276 $result = $cache->set($idkey, $windowstr);
1277 $result = $cache->set($idkey . ';last', $windowstr);
1279 $ids = array_slice($window, $offset, $limit);