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 class Notice extends Memcached_DataObject
40 /* the code below is auto generated do not remove the above tag */
42 public $__table = 'notice'; // table name
43 public $id; // int(4) primary_key not_null
44 public $profile_id; // int(4) not_null
45 public $uri; // varchar(255) unique_key
46 public $content; // varchar(140)
47 public $rendered; // text()
48 public $url; // varchar(255)
49 public $created; // datetime() not_null
50 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
51 public $reply_to; // int(4)
52 public $is_local; // tinyint(1)
53 public $source; // varchar(32)
54 public $conversation; // int(4)
57 function staticGet($k,$v=NULL) {
58 return Memcached_DataObject::staticGet('Notice',$k,$v);
61 /* the code above is auto generated do not remove the tag below */
66 return Profile::staticGet('id', $this->profile_id);
71 $this->blowCaches(true);
72 $this->blowFavesCache(true);
73 $this->blowSubsCache(true);
75 $this->query('BEGIN');
76 //Null any notices that are replies to this notice
77 $this->query(sprintf("UPDATE notice set reply_to = null WHERE reply_to = %d", $this->id));
78 $related = array('Reply',
83 if (common_config('inboxes', 'enabled')) {
84 $related[] = 'Notice_inbox';
86 foreach ($related as $cls) {
88 $inst->notice_id = $this->id;
91 $result = parent::delete();
92 $this->query('COMMIT');
97 /* extract all #hastags */
98 $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($this->content), $match);
103 /* Add them to the database */
104 foreach(array_unique($match[1]) as $hashtag) {
105 /* elide characters we don't want in the tag */
106 $this->saveTag($hashtag);
111 function saveTag($hashtag)
113 $hashtag = common_canonical_tag($hashtag);
115 $tag = new Notice_tag();
116 $tag->notice_id = $this->id;
117 $tag->tag = $hashtag;
118 $tag->created = $this->created;
119 $id = $tag->insert();
122 throw new ServerException(sprintf(_('DB error inserting hashtag: %s'),
123 $last_error->message));
128 static function saveNew($profile_id, $content, $source=null,
129 $is_local=1, $reply_to=null, $uri=null, $created=null) {
131 $profile = Profile::staticGet($profile_id);
133 $final = common_shorten_links($content);
135 if (mb_strlen($final) > 140) {
136 common_log(LOG_INFO, 'Rejecting notice that is too long.');
137 return _('Problem saving notice. Too long.');
141 common_log(LOG_ERR, 'Problem saving notice. Unknown user.');
142 return _('Problem saving notice. Unknown user.');
145 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
146 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
147 return _('Too many notices too fast; take a breather and post again in a few minutes.');
150 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
151 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
152 return _('Too many duplicate messages too quickly; take a breather and post again in a few minutes.');
155 $banned = common_config('profile', 'banned');
157 if ( in_array($profile_id, $banned) || in_array($profile->nickname, $banned)) {
158 common_log(LOG_WARNING, "Attempted post from banned user: $profile->nickname (user id = $profile_id).");
159 return _('You are banned from posting notices on this site.');
162 $notice = new Notice();
163 $notice->profile_id = $profile_id;
165 $blacklist = common_config('public', 'blacklist');
166 $autosource = common_config('public', 'autosource');
168 # Blacklisted are non-false, but not 1, either
170 if (($blacklist && in_array($profile_id, $blacklist)) ||
171 ($source && $autosource && in_array($source, $autosource))) {
172 $notice->is_local = -1;
174 $notice->is_local = $is_local;
177 $notice->query('BEGIN');
179 $notice->reply_to = $reply_to;
180 if (!empty($created)) {
181 $notice->created = $created;
183 $notice->created = common_sql_now();
185 $notice->content = $final;
186 $notice->rendered = common_render_content($final, $notice);
187 $notice->source = $source;
190 if (!empty($reply_to)) {
191 $reply_notice = Notice::staticGet('id', $reply_to);
192 if (!empty($reply_notice)) {
193 $notice->reply_to = $reply_to;
194 $notice->conversation = $reply_notice->conversation;
198 if (Event::handle('StartNoticeSave', array(&$notice))) {
200 $id = $notice->insert();
203 common_log_db_error($notice, 'INSERT', __FILE__);
204 return _('Problem saving notice.');
207 # Update the URI after the notice is in the database
209 $orig = clone($notice);
210 $notice->uri = common_notice_uri($notice);
212 if (!$notice->update($orig)) {
213 common_log_db_error($notice, 'UPDATE', __FILE__);
214 return _('Problem saving notice.');
218 # XXX: do we need to change this for remote users?
220 $notice->saveReplies();
223 $notice->addToInboxes();
224 $notice->saveGroups();
226 $orig2 = clone($notice);
227 $notice->rendered = common_render_content($final, $notice);
228 if (!$notice->update($orig2)) {
229 common_log_db_error($notice, 'UPDATE', __FILE__);
230 return _('Problem saving notice.');
233 $notice->query('COMMIT');
235 Event::handle('EndNoticeSave', array($notice));
238 # Clear the cache for subscribed users, so they'll update at next request
239 # XXX: someone clever could prepend instead of clearing the cache
241 $notice->blowCaches();
246 /** save all urls in the notice to the db
248 * follow redirects and save all available file information
249 * (mimetype, date, size, oembed, etc.)
253 function saveUrls() {
254 common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
257 function saveUrl($data) {
258 list($url, $notice_id) = $data;
259 File::processNew($url, $notice_id);
262 static function checkDupes($profile_id, $content) {
263 $profile = Profile::staticGet($profile_id);
267 $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
270 while ($notice->fetch()) {
271 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
273 } else if ($notice->content == $content) {
278 # If we get here, oldest item in cache window is not
279 # old enough for dupe limit; do direct check against DB
280 $notice = new Notice();
281 $notice->profile_id = $profile_id;
282 $notice->content = $content;
283 if (common_config('db','type') == 'pgsql')
284 $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
286 $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
288 $cnt = $notice->count();
292 static function checkEditThrottle($profile_id) {
293 $profile = Profile::staticGet($profile_id);
298 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
299 if ($notice && $notice->fetch()) {
300 # If the Nth notice was posted less than timespan seconds ago
301 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
306 # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
310 function getUploadedAttachment() {
312 $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"';
313 $post->query($query);
315 if (empty($post->up) || empty($post->i)) {
318 $ret = array($post->up, $post->i);
324 function hasAttachments() {
326 $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);
327 $post->query($query);
329 $n_attachments = intval($post->n_attachments);
331 return $n_attachments;
334 function blowCaches($blowLast=false)
336 $this->blowSubsCache($blowLast);
337 $this->blowNoticeCache($blowLast);
338 $this->blowRepliesCache($blowLast);
339 $this->blowPublicCache($blowLast);
340 $this->blowTagCache($blowLast);
341 $this->blowGroupCache($blowLast);
342 $this->blowConversationCache($blowLast);
345 function blowConversationCache($blowLast=false)
347 $cache = common_memcache();
349 $ck = 'notice:conversation_ids:'.$this->conversation;
352 $cache->delete($ck.';last');
357 function blowGroupCache($blowLast=false)
359 $cache = common_memcache();
361 $group_inbox = new Group_inbox();
362 $group_inbox->notice_id = $this->id;
363 if ($group_inbox->find()) {
364 while ($group_inbox->fetch()) {
365 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id));
367 $cache->delete(common_cache_key('user_group:notice_ids:' . $group_inbox->group_id.';last'));
369 $member = new Group_member();
370 $member->group_id = $group_inbox->group_id;
371 if ($member->find()) {
372 while ($member->fetch()) {
373 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id));
375 $cache->delete(common_cache_key('notice_inbox:by_user:' . $member->profile_id . ';last'));
381 $group_inbox->free();
386 function blowTagCache($blowLast=false)
388 $cache = common_memcache();
390 $tag = new Notice_tag();
391 $tag->notice_id = $this->id;
393 while ($tag->fetch()) {
394 $tag->blowCache($blowLast);
395 $ck = 'profile:notice_ids_tagged:' . $this->profile_id . ':' . $tag->tag;
399 $cache->delete($ck . ';last');
408 function blowSubsCache($blowLast=false)
410 $cache = common_memcache();
414 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
415 $user->query('SELECT id ' .
417 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
418 'WHERE subscription.subscribed = ' . $this->profile_id);
420 while ($user->fetch()) {
421 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id));
422 $cache->delete(common_cache_key('notice_inbox:by_user_own:'.$user->id));
424 $cache->delete(common_cache_key('notice_inbox:by_user:'.$user->id.';last'));
425 $cache->delete(common_cache_key('notice_inbox:by_user_own:'.$user->id.';last'));
433 function blowNoticeCache($blowLast=false)
435 if ($this->is_local) {
436 $cache = common_memcache();
437 if (!empty($cache)) {
438 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id));
440 $cache->delete(common_cache_key('profile:notice_ids:'.$this->profile_id.';last'));
446 function blowRepliesCache($blowLast=false)
448 $cache = common_memcache();
450 $reply = new Reply();
451 $reply->notice_id = $this->id;
452 if ($reply->find()) {
453 while ($reply->fetch()) {
454 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id));
456 $cache->delete(common_cache_key('reply:stream:'.$reply->profile_id.';last'));
465 function blowPublicCache($blowLast=false)
467 if ($this->is_local == 1) {
468 $cache = common_memcache();
470 $cache->delete(common_cache_key('public'));
472 $cache->delete(common_cache_key('public').';last');
478 function blowFavesCache($blowLast=false)
480 $cache = common_memcache();
483 $fave->notice_id = $this->id;
485 while ($fave->fetch()) {
486 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id));
487 $cache->delete(common_cache_key('fave:by_user_own:'.$fave->user_id));
489 $cache->delete(common_cache_key('fave:ids_by_user:'.$fave->user_id.';last'));
490 $cache->delete(common_cache_key('fave:by_user_own:'.$fave->user_id.';last'));
499 # XXX: too many args; we need to move to named params or even a separate
500 # class for notice streams
502 static function getStream($qry, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $order=null, $since=null) {
504 if (common_config('memcached', 'enabled')) {
506 # Skip the cache if this is a since, since_id or max_id qry
507 if ($since_id > 0 || $max_id > 0 || $since) {
508 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
510 return Notice::getCachedStream($qry, $cachekey, $offset, $limit, $order);
514 return Notice::getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since);
517 static function getStreamDirect($qry, $offset, $limit, $since_id, $max_id, $order, $since) {
522 if (preg_match('/\bWHERE\b/i', $qry)) {
536 $qry .= ' notice.id > ' . $since_id;
548 $qry .= ' notice.id <= ' . $max_id;
560 $qry .= ' notice.created > \'' . date('Y-m-d H:i:s', $since) . '\'';
563 # Allow ORDER override
568 $qry .= ' ORDER BY notice.created DESC, notice.id DESC ';
571 if (common_config('db','type') == 'pgsql') {
572 $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
574 $qry .= ' LIMIT ' . $offset . ', ' . $limit;
577 $notice = new Notice();
579 $notice->query($qry);
584 # XXX: this is pretty long and should probably be broken up into
585 # some helper functions
587 static function getCachedStream($qry, $cachekey, $offset, $limit, $order) {
589 # If outside our cache window, just go to the DB
591 if ($offset + $limit > NOTICE_CACHE_WINDOW) {
592 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
595 # Get the cache; if we can't, just go to the DB
597 $cache = common_memcache();
600 return Notice::getStreamDirect($qry, $offset, $limit, null, null, $order, null);
603 # Get the notices out of the cache
605 $notices = $cache->get(common_cache_key($cachekey));
607 # On a cache hit, return a DB-object-like wrapper
609 if ($notices !== false) {
610 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
614 # If the cache was invalidated because of new data being
615 # added, we can try and just get the new stuff. We keep an additional
616 # copy of the data at the key + ';last'
618 # No cache hit. Try to get the *last* cached version
620 $last_notices = $cache->get(common_cache_key($cachekey) . ';last');
624 # Reverse-chron order, so last ID is last.
626 $last_id = $last_notices[0]->id;
628 # XXX: this assumes monotonically increasing IDs; a fair
631 $new_notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW,
632 $last_id, null, $order, null);
635 $new_notices = array();
636 while ($new_notice->fetch()) {
637 $new_notices[] = clone($new_notice);
640 $notices = array_slice(array_merge($new_notices, $last_notices),
641 0, NOTICE_CACHE_WINDOW);
643 # Store the array in the cache for next time
645 $result = $cache->set(common_cache_key($cachekey), $notices);
646 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
648 # return a wrapper of the array for use now
650 return new ArrayWrapper(array_slice($notices, $offset, $limit));
654 # Otherwise, get the full cache window out of the DB
656 $notice = Notice::getStreamDirect($qry, 0, NOTICE_CACHE_WINDOW, null, null, $order, null);
658 # If there are no hits, just return the value
664 # Pack results into an array
668 while ($notice->fetch()) {
669 $notices[] = clone($notice);
674 # Store the array in the cache for next time
676 $result = $cache->set(common_cache_key($cachekey), $notices);
677 $result = $cache->set(common_cache_key($cachekey) . ';last', $notices);
679 # return a wrapper of the array for use now
681 $wrapper = new ArrayWrapper(array_slice($notices, $offset, $limit));
686 function getStreamByIds($ids)
688 $cache = common_memcache();
690 if (!empty($cache)) {
692 foreach ($ids as $id) {
693 $n = Notice::staticGet('id', $id);
698 return new ArrayWrapper($notices);
700 $notice = new Notice();
701 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
702 $notice->orderBy('id DESC');
709 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
711 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
714 $offset, $limit, $since_id, $max_id, $since);
716 return Notice::getStreamByIds($ids);
719 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
721 $notice = new Notice();
723 $notice->selectAdd(); // clears it
724 $notice->selectAdd('id');
726 $notice->orderBy('id DESC');
728 if (!is_null($offset)) {
729 $notice->limit($offset, $limit);
732 if (common_config('public', 'localonly')) {
733 $notice->whereAdd('is_local = 1');
736 $notice->whereAdd('is_local != -1');
739 if ($since_id != 0) {
740 $notice->whereAdd('id > ' . $since_id);
744 $notice->whereAdd('id <= ' . $max_id);
747 if (!is_null($since)) {
748 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
753 if ($notice->find()) {
754 while ($notice->fetch()) {
755 $ids[] = $notice->id;
765 function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
767 $ids = Notice::stream(array('Notice', '_conversationStreamDirect'),
769 'notice:conversation_ids:'.$id,
770 $offset, $limit, $since_id, $max_id, $since);
772 return Notice::getStreamByIds($ids);
775 function _conversationStreamDirect($id, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
777 $notice = new Notice();
779 $notice->selectAdd(); // clears it
780 $notice->selectAdd('id');
782 $notice->whereAdd('conversation = '.$id);
784 $notice->orderBy('id DESC');
786 if (!is_null($offset)) {
787 $notice->limit($offset, $limit);
790 if ($since_id != 0) {
791 $notice->whereAdd('id > ' . $since_id);
795 $notice->whereAdd('id <= ' . $max_id);
798 if (!is_null($since)) {
799 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
804 if ($notice->find()) {
805 while ($notice->fetch()) {
806 $ids[] = $notice->id;
816 function addToInboxes()
818 $enabled = common_config('inboxes', 'enabled');
820 if ($enabled === true || $enabled === 'transitional') {
821 $inbox = new Notice_inbox();
822 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
823 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created) ' .
824 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "' " .
825 "FROM $UT JOIN subscription ON $UT.id = subscription.subscriber " .
826 'WHERE subscription.subscribed = ' . $this->profile_id . ' ' .
827 'AND NOT EXISTS (SELECT user_id, notice_id ' .
828 'FROM notice_inbox ' .
829 "WHERE user_id = $UT.id " .
830 'AND notice_id = ' . $this->id . ' )';
831 if ($enabled === 'transitional') {
832 $qry .= " AND $UT.inboxed = 1";
839 function saveGroups()
841 $enabled = common_config('inboxes', 'enabled');
842 if ($enabled !== true && $enabled !== 'transitional') {
846 /* extract all !group */
847 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
848 strtolower($this->content),
854 $profile = $this->getProfile();
856 /* Add them to the database */
858 foreach (array_unique($match[1]) as $nickname) {
859 /* XXX: remote groups. */
860 $group = User_group::getForNickname($nickname);
866 // we automatically add a tag for every group name, too
868 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
869 'notice_id' => $this->id));
872 $this->saveTag($nickname);
875 if ($profile->isMember($group)) {
877 $gi = new Group_inbox();
879 $gi->group_id = $group->id;
880 $gi->notice_id = $this->id;
881 $gi->created = common_sql_now();
883 $result = $gi->insert();
886 common_log_db_error($gi, 'INSERT', __FILE__);
889 // FIXME: do this in an offline daemon
891 $this->addToGroupInboxes($group);
896 function addToGroupInboxes($group)
898 $inbox = new Notice_inbox();
899 $UT = common_config('db','type')=='pgsql'?'"user"':'user';
900 $qry = 'INSERT INTO notice_inbox (user_id, notice_id, created, source) ' .
901 "SELECT $UT.id, " . $this->id . ", '" . $this->created . "', " . NOTICE_INBOX_SOURCE_GROUP . " " .
902 "FROM $UT JOIN group_member ON $UT.id = group_member.profile_id " .
903 'WHERE group_member.group_id = ' . $group->id . ' ' .
904 'AND NOT EXISTS (SELECT user_id, notice_id ' .
905 'FROM notice_inbox ' .
906 "WHERE user_id = $UT.id " .
907 'AND notice_id = ' . $this->id . ' )';
908 if ($enabled === 'transitional') {
909 $qry .= " AND $UT.inboxed = 1";
911 $result = $inbox->query($qry);
914 function saveReplies()
916 // Alternative reply format
918 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $this->content, $match)) {
921 // extract all @messages
922 $cnt = preg_match_all('/(?:^|\s)@([a-z0-9]{1,64})/', $this->content, $match);
926 if ($cnt || $tname) {
927 // XXX: is there another way to make an array copy?
928 $names = ($tname) ? array_unique(array_merge(array(strtolower($tname)), $match[1])) : array_unique($match[1]);
931 $sender = Profile::staticGet($this->profile_id);
935 // store replied only for first @ (what user/notice what the reply directed,
936 // we assume first @ is it)
938 for ($i=0; $i<count($names); $i++) {
939 $nickname = $names[$i];
940 $recipient = common_relative_profile($sender, $nickname, $this->created);
944 if ($i == 0 && ($recipient->id != $sender->id) && !$this->reply_to) { // Don't save reply to self
945 $reply_for = $recipient;
946 $recipient_notice = $reply_for->getCurrentNotice();
947 if ($recipient_notice) {
948 $orig = clone($this);
949 $this->reply_to = $recipient_notice->id;
950 $this->conversation = $recipient_notice->conversation;
951 $this->update($orig);
954 // Don't save replies from blocked profile to local user
955 $recipient_user = User::staticGet('id', $recipient->id);
956 if ($recipient_user && $recipient_user->hasBlocked($sender)) {
959 $reply = new Reply();
960 $reply->notice_id = $this->id;
961 $reply->profile_id = $recipient->id;
962 $id = $reply->insert();
964 $last_error = &PEAR::getStaticProperty('DB_DataObject','lastError');
965 common_log(LOG_ERR, 'DB error inserting reply: ' . $last_error->message);
966 common_server_error(sprintf(_('DB error inserting reply: %s'), $last_error->message));
969 $replied[$recipient->id] = 1;
973 // Hash format replies, too
974 $cnt = preg_match_all('/(?:^|\s)@#([a-z0-9]{1,64})/', $this->content, $match);
976 foreach ($match[1] as $tag) {
977 $tagged = Profile_tag::getTagged($sender->id, $tag);
978 foreach ($tagged as $t) {
979 if (!$replied[$t->id]) {
980 // Don't save replies from blocked profile to local user
981 $t_user = User::staticGet('id', $t->id);
982 if ($t_user && $t_user->hasBlocked($sender)) {
985 $reply = new Reply();
986 $reply->notice_id = $this->id;
987 $reply->profile_id = $t->id;
988 $id = $reply->insert();
990 common_log_db_error($reply, 'INSERT', __FILE__);
993 $replied[$recipient->id] = 1;
1000 // If it's not a reply, make it the root of a new conversation
1002 if (empty($this->conversation)) {
1003 $orig = clone($this);
1004 $this->conversation = $this->id;
1005 $this->update($orig);
1008 foreach (array_keys($replied) as $recipient) {
1009 $user = User::staticGet('id', $recipient);
1011 mail_notify_attn($user, $this);
1016 function asAtomEntry($namespace=false, $source=false)
1018 $profile = $this->getProfile();
1020 $xs = new XMLStringer(true);
1023 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
1024 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0');
1029 $xs->elementStart('entry', $attrs);
1032 $xs->elementStart('source');
1033 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
1034 $xs->element('link', array('href' => $profile->profileurl));
1035 $user = User::staticGet('id', $profile->id);
1036 if (!empty($user)) {
1037 $atom_feed = common_local_url('api',
1038 array('apiaction' => 'statuses',
1039 'method' => 'user_timeline',
1040 'argument' => $profile->nickname.'.atom'));
1041 $xs->element('link', array('rel' => 'self',
1042 'type' => 'application/atom+xml',
1043 'href' => $profile->profileurl));
1044 $xs->element('link', array('rel' => 'license',
1045 'href' => common_config('license', 'url')));
1048 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
1051 $xs->elementStart('author');
1052 $xs->element('name', null, $profile->nickname);
1053 $xs->element('uri', null, $profile->profileurl);
1054 $xs->elementEnd('author');
1057 $xs->elementEnd('source');
1060 $xs->element('title', null, $this->content);
1061 $xs->element('summary', null, $this->content);
1063 $xs->element('link', array('rel' => 'alternate',
1064 'href' => $this->bestUrl()));
1066 $xs->element('id', null, $this->uri);
1068 $xs->element('published', null, common_date_w3dtf($this->created));
1069 $xs->element('updated', null, common_date_w3dtf($this->modified));
1071 if ($this->reply_to) {
1072 $reply_notice = Notice::staticGet('id', $this->reply_to);
1073 if (!empty($reply_notice)) {
1074 $xs->element('link', array('rel' => 'related',
1075 'href' => $reply_notice->bestUrl()));
1076 $xs->element('thr:in-reply-to',
1077 array('ref' => $reply_notice->uri,
1078 'href' => $reply_notice->bestUrl()));
1082 $xs->element('content', array('type' => 'html'), $this->rendered);
1084 $tag = new Notice_tag();
1085 $tag->notice_id = $this->id;
1087 while ($tag->fetch()) {
1088 $xs->element('category', array('term' => $tag->tag));
1093 $xs->elementEnd('entry');
1095 return $xs->getString();
1100 if (!empty($this->url)) {
1102 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
1105 return common_local_url('shownotice',
1106 array('notice' => $this->id));
1110 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
1112 $cache = common_memcache();
1114 if (empty($cache) ||
1115 $since_id != 0 || $max_id != 0 || (!is_null($since) && $since > 0) ||
1116 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1117 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1121 $idkey = common_cache_key($cachekey);
1123 $idstr = $cache->get($idkey);
1125 if (!empty($idstr)) {
1126 // Cache hit! Woohoo!
1127 $window = explode(',', $idstr);
1128 $ids = array_slice($window, $offset, $limit);
1132 $laststr = $cache->get($idkey.';last');
1134 if (!empty($laststr)) {
1135 $window = explode(',', $laststr);
1136 $last_id = $window[0];
1137 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1138 $last_id, 0, null, $tag)));
1140 $new_window = array_merge($new_ids, $window);
1142 $new_windowstr = implode(',', $new_window);
1144 $result = $cache->set($idkey, $new_windowstr);
1145 $result = $cache->set($idkey . ';last', $new_windowstr);
1147 $ids = array_slice($new_window, $offset, $limit);
1152 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1153 0, 0, null, $tag)));
1155 $windowstr = implode(',', $window);
1157 $result = $cache->set($idkey, $windowstr);
1158 $result = $cache->set($idkey . ';last', $windowstr);
1160 $ids = array_slice($window, $offset, $limit);