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);
359 $profile = Profile::staticGet($this->profile_id);
360 $profile->blowNoticeCount();
363 function blowConversationCache($blowLast=false)
365 $cache = common_memcache();
367 $ck = common_cache_key('notice:conversation_ids:'.$this->conversation);
370 $cache->delete($ck.';last');
375 function blowGroupCache($blowLast=false)
377 $cache = common_memcache();
379 $group_inbox = new Group_inbox();
380 $group_inbox->notice_id = $this->id;
381 if ($group_inbox->find()) {
382 while ($group_inbox->fetch()) {
383 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id));
385 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id.';last'));
387 $member = new Group_member();
388 $member->group_id = $group_inbox->group_id;
389 if ($member->find()) {
390 while ($member->fetch()) {
391 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id));
393 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id . ';last'));
399 $group_inbox->free();
404 function blowTagCache($blowLast=false)
406 $cache = common_memcache();
408 $tag = new Notice_tag();
409 $tag->notice_id = $this->id;
411 while ($tag->fetch()) {
412 $tag->blowCache($blowLast);
413 $ck = 'profile:notice_ids_tagged:' . $this->profile_id . ':' . $tag->tag;
417 $cache->delete($ck . ';last');
426 function blowSubsCache($blowLast=false)
428 $cache = common_memcache();
432 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
433 $user->query('SELECT id ' .
435 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
436 'WHERE subscription.subscribed = ' . $this->profile_id);
438 while ($user->fetch()) {
439 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id));
440 $cache->delete(common_cache_key('notice_inbox:by_user_own:'.$user->id));
442 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id.';last'));
443 $cache->delete(common_cache_key('notice_inbox:by_user_own:'.$user->id.';last'));
451 function blowNoticeCache($blowLast=false)
453 if ($this->is_local) {
454 $cache = common_memcache();
455 if (!empty($cache)) {
456 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id));
458 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id.';last'));
464 function blowRepliesCache($blowLast=false)
466 $cache = common_memcache();
468 $reply = new Reply();
469 $reply->notice_id = $this->id;
470 if ($reply->find()) {
471 while ($reply->fetch()) {
472 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id));
474 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id.';last'));
483 function blowPublicCache($blowLast=false)
485 if ($this->is_local == 1) {
486 $cache = common_memcache();
488 $cache->delete(common_cache_key('public'));
490 $cache->delete(common_cache_key('public').';last');
496 function blowFavesCache($blowLast=false)
498 $cache = common_memcache();
501 $fave->notice_id = $this->id;
503 while ($fave->fetch()) {
504 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id));
505 $cache->delete(common_cache_key('fave:by_user_own:'.$fave->user_id));
507 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id.';last'));
508 $cache->delete(common_cache_key('fave:by_user_own:'.$fave->user_id.';last'));
517 # XXX: too many args; we need to move to named params or even a separate
518 # class for notice streams
520 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $order=null, $since=null) {
522 if (common_config('memcached', 'enabled')) {
524 # Skip the cache if this is a since, since_id or max_id qry
525 if ($since_id > 0 || $max_id > 0 || $since) {
526 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
528 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
532 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
535 static function getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since) {
540 if (preg_match('/\bWHERE\b/i', $qry)) {
554 $qry .= ' notice.id > ' . $since_id;
566 $qry .= ' notice.id <= ' . $max_id;
578 $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
581 # Allow ORDER override
586 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
589 if (common_config('db','type') == 'pgsql') {
590 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
592 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
595 $notice = new Notice();
597 $notice->query($qry);
602 # XXX: this is pretty long and should probably be broken up into
603 # some helper functions
605 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
607 # If outside our cache window, just go to the DB
609 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
610 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
613 # Get the cache; if we can't, just go to the DB
615 $cache = common_memcache();
618 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
621 # Get the notices out of the cache
623 $notices = $cache->get(common_cache_key($cachekey));
625 # On a cache hit, return a DB-object-like wrapper
627 if ($notices !== false) {
628 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
632 # If the cache was invalidated because of new data being
633 # added, we can try and just get the new stuff. We keep an additional
634 # copy of the data at the key + ';last'
636 # No cache hit. Try to get the *last* cached version
638 $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
642 # Reverse-chron order, so last ID is last.
644 $last_id = $last_notices[0]->id;
646 # XXX: this assumes monotonically increasing IDs; a fair
649 $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
650 $last_id, null, $order, null);
653 $new_notices = array();
654 while ($new_notice->fetch()) {
655 $new_notices[] = clone($new_notice);
658 $notices = array_slice(array_merge($new_notices, $last_notices),
659 0, NOTICE_CACHE_WINDOW);
661 # Store the array in the cache for next time
663 $result = $cache->set(common_cache_key($cachekey), $notices);
664 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
666 # return a wrapper of the array for use now
668 return new ArrayWrapper(array_slice($notices, $offset, $limit));
672 # Otherwise, get the full cache window out of the DB
674 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
676 # If there are no hits, just return the value
682 # Pack results into an array
686 while ($notice->fetch()) {
687 $notices[] = clone($notice);
692 # Store the array in the cache for next time
694 $result = $cache->set(common_cache_key($cachekey), $notices);
695 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
697 # return a wrapper of the array for use now
699 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
704 function getStreamByIds($ids)
706 $cache = common_memcache();
708 if (!empty($cache)) {
710 foreach ($ids as $id) {
711 $n = Notice::staticGet('id', $id);
716 return new ArrayWrapper($notices);
718 $notice = new Notice();
719 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
720 $notice->orderBy('id DESC');
727 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
729 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
732 $offset, $limit, $since_id, $max_id, $since);
734 return Notice::getStreamByIds($ids);
737 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
739 $notice = new Notice();
741 $notice->selectAdd(); // clears it
742 $notice->selectAdd('id');
744 $notice->orderBy('id DESC');
746 if (!is_null($offset)) {
747 $notice->limit($offset, $limit);
750 if (common_config('public', 'localonly')) {
751 $notice->whereAdd('is_local = 1');
754 $notice->whereAdd('is_local != -1');
757 if ($since_id != 0) {
758 $notice->whereAdd('id > ' . $since_id);
762 $notice->whereAdd('id <= ' . $max_id);
765 if (!is_null($since)) {
766 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
771 if ($notice->find()) {
772 while ($notice->fetch()) {
773 $ids[] = $notice->id;
783 function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
785 $ids = Notice::stream(array('Notice', '_conversationStreamDirect'),
787 'notice:conversation_ids:'.$id,
788 $offset, $limit, $since_id, $max_id, $since);
790 return Notice::getStreamByIds($ids);
793 function _conversationStreamDirect($id, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
795 $notice = new Notice();
797 $notice->selectAdd(); // clears it
798 $notice->selectAdd('id');
800 $notice->conversation = $id;
802 $notice->orderBy('id DESC');
804 if (!is_null($offset)) {
805 $notice->limit($offset, $limit);
808 if ($since_id != 0) {
809 $notice->whereAdd('id > ' . $since_id);
813 $notice->whereAdd('id <= ' . $max_id);
816 if (!is_null($since)) {
817 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
822 if ($notice->find()) {
823 while ($notice->fetch()) {
824 $ids[] = $notice->id;
834 function addToInboxes()
836 $enabled = common_config('inboxes', 'enabled');
838 if ($enabled === true || $enabled === 'transitional') {
840 // XXX: loads constants
842 $inbox = new Notice_inbox();
844 $users = $this->getSubscribedUsers();
846 // FIXME: kind of ignoring 'transitional'...
847 // we'll probably stop supporting inboxless mode
852 foreach ($users as $id) {
853 $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
856 $groups = $this->saveGroups();
858 foreach ($groups as $group) {
859 $users = $group->getUserMembers();
860 foreach ($users as $id) {
861 if (!array_key_exists($id, $ni)) {
862 $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
869 $qryhdr = 'INSERT INTO notice_inbox (user_id, notice_id, source, created) VALUES ';
872 foreach ($ni as $id => $source) {
876 $qry .= '('.$id.', '.$this->id.', '.$source.", '".$this->created. "') ";
878 if ($cnt >= MAX_BOXCARS) {
879 $inbox = new Notice_inbox();
887 $inbox = new Notice_inbox();
895 function getSubscribedUsers()
899 if(common_config('db','quote_identifiers'))
900 $user_table = '"user"';
901 else $user_table = 'user';
905 'FROM '. $user_table .' JOIN subscription '.
906 'ON '. $user_table .'.id = subscription.subscriber ' .
907 'WHERE subscription.subscribed = %d ';
909 $user->query(sprintf($qry, $this->profile_id));
913 while ($user->fetch()) {
922 function saveGroups()
926 $enabled = common_config('inboxes', 'enabled');
927 if ($enabled !== true && $enabled !== 'transitional') {
931 /* extract all !group */
932 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
933 strtolower($this->content),
939 $profile = $this->getProfile();
941 /* Add them to the database */
943 foreach (array_unique($match[1]) as $nickname) {
944 /* XXX: remote groups. */
945 $group = User_group::getForNickname($nickname);
951 // we automatically add a tag for every group name, too
953 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
954 'notice_id' => $this->id));
957 $this->saveTag($nickname);
960 if ($profile->isMember($group)) {
962 $result = $this->addToGroupInbox($group);
965 common_log_db_error($gi, 'INSERT', __FILE__);
968 $groups[] = clone($group);
975 function addToGroupInbox($group)
977 $gi = Group_inbox::pkeyGet(array('group_id' => $group->id,
978 'notice_id' => $this->id));
982 $gi = new Group_inbox();
984 $gi->group_id = $group->id;
985 $gi->notice_id = $this->id;
986 $gi->created = $this->created;
988 return $gi->insert();
994 function saveReplies()
996 // Alternative reply format
998 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
1001 // extract all @messages
1002 $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
1006 if ($cnt || $tname) {
1007 // XXX: is there another way to make an array copy?
1008 $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
1011 $sender = Profile::staticGet($this->profile_id);
1015 // store replied only for first @ (what user/notice what the reply directed,
1016 // we assume first @ is it)
1018 for ($i=0; $i<count($names); $i++) {
1019 $nickname = $names[$i];
1020 $recipient = common_relative_profile($sender, $nickname, $this->created);
1024 if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
1025 $reply_for = $recipient;
1026 $recipient_notice = $reply_for->getCurrentNotice();
1027 if ($recipient_notice) {
1028 $orig = clone($this);
1029 $this->reply_to = $recipient_notice->id;
1030 $this->conversation = $recipient_notice->conversation;
1031 $this->update($orig);
1034 // Don't save replies from blocked profile to local user
1035 $recipient_user = User::staticGet('id', $recipient->id);
1036 if ($recipient_user && $recipient_user->hasBlocked($sender)) {
1039 $reply = new Reply();
1040 $reply->notice_id = $this->id;
1041 $reply->profile_id = $recipient->id;
1042 $id = $reply->insert();
1044 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
1045 common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
1046 common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
1049 $replied[$recipient->id] = 1;
1053 // Hash format replies, too
1054 $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
1056 foreach ($match[1] as $tag) {
1057 $tagged = Profile_tag::getTagged($sender->id, $tag);
1058 foreach ($tagged as $t) {
1059 if (!$replied[$t->id]) {
1060 // Don't save replies from blocked profile to local user
1061 $t_user = User::staticGet('id', $t->id);
1062 if ($t_user && $t_user->hasBlocked($sender)) {
1065 $reply = new Reply();
1066 $reply->notice_id = $this->id;
1067 $reply->profile_id = $t->id;
1068 $id = $reply->insert();
1070 common_log_db_error($reply, 'INSERT', __FILE__);
1073 $replied[$recipient->id] = 1;
1080 // If it's not a reply, make it the root of a new conversation
1082 if (empty($this->conversation)) {
1083 $orig = clone($this);
1084 $this->conversation = $this->id;
1085 $this->update($orig);
1088 foreach (array_keys($replied) as $recipient) {
1089 $user = User::staticGet('id', $recipient);
1091 mail_notify_attn($user, $this);
1096 function asAtomEntry($namespace=false, $source=false)
1098 $profile = $this->getProfile();
1100 $xs = new XMLStringer(true);
1103 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
1104 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
1109 $xs->elementStart('entry', $attrs);
1112 $xs->elementStart('source');
1113 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
1114 $xs->element('link', array('href' => $profile->profileurl));
1115 $user = User::staticGet('id', $profile->id);
1116 if (!empty($user)) {
1117 $atom_feed = common_local_url('api',
1118 array('apiaction' => 'statuses',
1119 'method' => 'user_timeline',
1120 'argument' => $profile->nickname.'.atom'));
1121 $xs->element('link', array('rel' => 'self',
1122 'type' => 'application/atom+xml',
1123 'href' => $profile->profileurl));
1124 $xs->element('link', array('rel' => 'license',
1125 'href' => common_config('license', 'url')));
1128 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
1131 $xs->elementStart('author');
1132 $xs->element('name', null, $profile->nickname);
1133 $xs->element('uri', null, $profile->profileurl);
1134 $xs->elementEnd('author');
1137 $xs->elementEnd('source');
1140 $xs->element('title', null, $this->content);
1141 $xs->element('summary', null, $this->content);
1143 $xs->element('link', array('rel' => 'alternate',
1144 'href' => $this->bestUrl()));
1146 $xs->element('id', null, $this->uri);
1148 $xs->element('published', null, common_date_w3dtf($this->created));
1149 $xs->element('updated', null, common_date_w3dtf($this->modified));
1151 if ($this->reply_to) {
1152 $reply_notice = Notice::staticGet('id', $this->reply_to);
1153 if (!empty($reply_notice)) {
1154 $xs->element('link', array('rel' => 'related',
1155 'href' => $reply_notice->bestUrl()));
1156 $xs->element('thr:in-reply-to',
1157 array('ref' => $reply_notice->uri,
1158 'href' => $reply_notice->bestUrl()));
1162 $xs->element('content', array('type' => 'html'), $this->rendered);
1164 $tag = new Notice_tag();
1165 $tag->notice_id = $this->id;
1167 while ($tag->fetch()) {
1168 $xs->element('category', array('term' => $tag->tag));
1174 $attachments = $this->attachments();
1176 foreach($attachments as $attachment){
1177 if ($attachment->isEnclosure()) {
1178 $attributes = array('rel'=>'enclosure','href'=>$attachment->url,'type'=>$attachment->mimetype,'length'=>$attachment->size);
1179 if($attachment->title){
1180 $attributes['title']=$attachment->title;
1182 $xs->element('link', $attributes, null);
1187 $xs->elementEnd('entry');
1189 return $xs->getString();
1194 if (!empty($this->url)) {
1196 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
1199 return common_local_url('shownotice',
1200 array('notice' => $this->id));
1204 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
1206 $cache = common_memcache();
1208 if (empty($cache) ||
1209 $since_id != 0 || $max_id != 0 || (!is_null($since) && $since > 0) ||
1211 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1212 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1216 $idkey = common_cache_key($cachekey);
1218 $idstr = $cache->get($idkey);
1220 if (!empty($idstr)) {
1221 // Cache hit! Woohoo!
1222 $window = explode(',', $idstr);
1223 $ids = array_slice($window, $offset, $limit);
1227 $laststr = $cache->get($idkey.';last');
1229 if (!empty($laststr)) {
1230 $window = explode(',', $laststr);
1231 $last_id = $window[0];
1232 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1233 $last_id, 0, null)));
1235 $new_window = array_merge($new_ids, $window);
1237 $new_windowstr = implode(',', $new_window);
1239 $result = $cache->set($idkey, $new_windowstr);
1240 $result = $cache->set($idkey . ';last', $new_windowstr);
1242 $ids = array_slice($new_window, $offset, $limit);
1247 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1250 $windowstr = implode(',', $window);
1252 $result = $cache->set($idkey, $windowstr);
1253 $result = $cache->set($idkey . ';last', $windowstr);
1255 $ids = array_slice($window, $offset, $limit);