3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, 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/>.
21 * @author Brenda Wallace <shiny@cpan.org>
22 * @author Christopher Vollick <psycotica0@gmail.com>
23 * @author CiaranG <ciaran@ciarang.com>
24 * @author Craig Andrews <candrews@integralblue.com>
25 * @author Evan Prodromou <evan@controlezvous.ca>
26 * @author Gina Haeussge <osd@foosel.net>
27 * @author Jeffery To <jeffery.to@gmail.com>
28 * @author Mike Cochrane <mikec@mikenz.geek.nz>
29 * @author Robin Millette <millette@controlyourself.ca>
30 * @author Sarven Capadisli <csarven@controlyourself.ca>
31 * @author Tom Adams <tom@holizz.com>
32 * @license GNU Affero General Public License http://www.gnu.org/licenses/
35 if (!defined('STATUSNET') && !defined('LACONICA')) {
40 * Table Definition for notice
42 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
44 /* We keep the first three 20-notice pages, plus one for pagination check,
45 * in the memcached cache. */
47 define('NOTICE_CACHE_WINDOW', 61);
49 define('MAX_BOXCARS', 128);
51 class Notice extends Memcached_DataObject
54 /* the code below is auto generated do not remove the above tag */
56 public $__table = 'notice'; // table name
57 public $id; // int(4) primary_key not_null
58 public $profile_id; // int(4) multiple_key not_null
59 public $uri; // varchar(255) unique_key
60 public $content; // text
61 public $rendered; // text
62 public $url; // varchar(255)
63 public $created; // datetime multiple_key not_null default_0000-00-00%2000%3A00%3A00
64 public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
65 public $reply_to; // int(4)
66 public $is_local; // int(4)
67 public $source; // varchar(32)
68 public $conversation; // int(4)
69 public $lat; // decimal(10,7)
70 public $lon; // decimal(10,7)
71 public $location_id; // int(4)
72 public $location_ns; // int(4)
73 public $repeat_of; // int(4)
76 function staticGet($k,$v=NULL)
78 return Memcached_DataObject::staticGet('Notice',$k,$v);
81 /* the code above is auto generated do not remove the tag below */
85 const LOCAL_PUBLIC = 1;
87 const LOCAL_NONPUBLIC = -1;
92 return Profile::staticGet('id', $this->profile_id);
97 // For auditing purposes, save a record that the notice
100 $deleted = new Deleted_notice();
102 $deleted->id = $this->id;
103 $deleted->profile_id = $this->profile_id;
104 $deleted->uri = $this->uri;
105 $deleted->created = $this->created;
106 $deleted->deleted = common_sql_now();
110 // Clear related records
112 $this->clearReplies();
113 $this->clearRepeats();
116 $this->clearGroupInboxes();
118 // NOTE: we don't clear inboxes
119 // NOTE: we don't clear queue items
121 $result = parent::delete();
125 * Extract #hashtags from this notice's content and save them to the database.
129 /* extract all #hastags */
130 $count = preg_match_all('/(?:^|\s)#([\pL\pN_\-\.]{1,64})/', strtolower($this->content), $match);
135 /* Add them to the database */
136 return $this->saveKnownTags($match[1]);
140 * Record the given set of hash tags in the db for this notice.
141 * Given tag strings will be normalized and checked for dupes.
143 function saveKnownTags($hashtags)
145 //turn each into their canonical tag
146 //this is needed to remove dupes before saving e.g. #hash.tag = #hashtag
147 for($i=0; $i<count($hashtags); $i++) {
148 $hashtags[$i] = common_canonical_tag($hashtags[$i]);
151 foreach(array_unique($hashtags) as $hashtag) {
152 /* elide characters we don't want in the tag */
153 $this->saveTag($hashtag);
154 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, $hashtag);
160 * Record a single hash tag as associated with this notice.
161 * Tag format and uniqueness must be validated by caller.
163 function saveTag($hashtag)
165 $tag = new Notice_tag();
166 $tag->notice_id = $this->id;
167 $tag->tag = $hashtag;
168 $tag->created = $this->created;
169 $id = $tag->insert();
172 throw new ServerException(sprintf(_('DB error inserting hashtag: %s'),
173 $last_error->message));
177 // if it's saved, blow its cache
178 $tag->blowCache(false);
182 * Save a new notice and push it out to subscribers' inboxes.
183 * Poster's permissions are checked before sending.
185 * @param int $profile_id Profile ID of the poster
186 * @param string $content source message text; links may be shortened
187 * per current user's preference
188 * @param string $source source key ('web', 'api', etc)
189 * @param array $options Associative array of optional properties:
190 * string 'created' timestamp of notice; defaults to now
191 * int 'is_local' source/gateway ID, one of:
192 * Notice::LOCAL_PUBLIC - Local, ok to appear in public timeline
193 * Notice::REMOTE_OMB - Sent from a remote OMB service;
194 * hide from public timeline but show in
195 * local "and friends" timelines
196 * Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
197 * Notice::GATEWAY - From another non-OMB service;
198 * will not appear in public views
199 * float 'lat' decimal latitude for geolocation
200 * float 'lon' decimal longitude for geolocation
201 * int 'location_id' geoname identifier
202 * int 'location_ns' geoname namespace to interpret location_id
203 * int 'reply_to'; notice ID this is a reply to
204 * int 'repeat_of'; notice ID this is a repeat of
205 * string 'uri' unique ID for notice; defaults to local notice URL
206 * string 'url' permalink to notice; defaults to local notice URL
207 * string 'rendered' rendered HTML version of content
208 * array 'replies' list of profile URIs for reply delivery in
209 * place of extracting @-replies from content.
210 * array 'groups' list of group IDs to deliver to, in place of
211 * extracting ! tags from content
212 * array 'tags' list of hashtag strings to save with the notice
213 * in place of extracting # tags from content
214 * @fixme tag override
217 * @throws ClientException
219 static function saveNew($profile_id, $content, $source, $options=null) {
220 $defaults = array('uri' => null,
223 'repeat_of' => null);
225 if (!empty($options)) {
226 $options = $options + $defaults;
230 if (!isset($is_local)) {
231 $is_local = Notice::LOCAL_PUBLIC;
234 $profile = Profile::staticGet($profile_id);
236 $final = common_shorten_links($content);
238 if (Notice::contentTooLong($final)) {
239 throw new ClientException(_('Problem saving notice. Too long.'));
242 if (empty($profile)) {
243 throw new ClientException(_('Problem saving notice. Unknown user.'));
246 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
247 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
248 throw new ClientException(_('Too many notices too fast; take a breather '.
249 'and post again in a few minutes.'));
252 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
253 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
254 throw new ClientException(_('Too many duplicate messages too quickly;'.
255 ' take a breather and post again in a few minutes.'));
258 if (!$profile->hasRight(Right::NEWNOTICE)) {
259 common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
260 throw new ClientException(_('You are banned from posting notices on this site.'));
263 $notice = new Notice();
264 $notice->profile_id = $profile_id;
266 $autosource = common_config('public', 'autosource');
268 # Sandboxed are non-false, but not 1, either
270 if (!$profile->hasRight(Right::PUBLICNOTICE) ||
271 ($source && $autosource && in_array($source, $autosource))) {
272 $notice->is_local = Notice::LOCAL_NONPUBLIC;
274 $notice->is_local = $is_local;
277 if (!empty($created)) {
278 $notice->created = $created;
280 $notice->created = common_sql_now();
283 $notice->content = $final;
285 $notice->source = $source;
289 // Handle repeat case
291 if (isset($repeat_of)) {
292 $notice->repeat_of = $repeat_of;
294 $notice->reply_to = self::getReplyTo($reply_to, $profile_id, $source, $final);
297 if (!empty($notice->reply_to)) {
298 $reply = Notice::staticGet('id', $notice->reply_to);
299 $notice->conversation = $reply->conversation;
302 if (!empty($lat) && !empty($lon)) {
307 if (!empty($location_ns) && !empty($location_id)) {
308 $notice->location_id = $location_id;
309 $notice->location_ns = $location_ns;
312 if (!empty($rendered)) {
313 $notice->rendered = $rendered;
315 $notice->rendered = common_render_content($final, $notice);
318 if (Event::handle('StartNoticeSave', array(&$notice))) {
320 // XXX: some of these functions write to the DB
322 $id = $notice->insert();
325 common_log_db_error($notice, 'INSERT', __FILE__);
326 throw new ServerException(_('Problem saving notice.'));
329 // Update ID-dependent columns: URI, conversation
331 $orig = clone($notice);
336 $notice->uri = common_notice_uri($notice);
340 // If it's not part of a conversation, it's
341 // the beginning of a new conversation.
343 if (empty($notice->conversation)) {
344 $conv = Conversation::create();
345 $notice->conversation = $conv->id;
350 if (!$notice->update($orig)) {
351 common_log_db_error($notice, 'UPDATE', __FILE__);
352 throw new ServerException(_('Problem saving notice.'));
358 # Clear the cache for subscribed users, so they'll update at next request
359 # XXX: someone clever could prepend instead of clearing the cache
361 $notice->blowOnInsert();
363 // Save per-notice metadata...
365 if (isset($replies)) {
366 $notice->saveKnownReplies($replies);
368 $notice->saveReplies();
371 if (isset($groups)) {
372 $notice->saveKnownGroups($groups);
374 $notice->saveGroups();
378 $notice->saveKnownTags($tags);
383 // @fixme pass in data for URLs too?
386 // Prepare inbox delivery, may be queued to background.
387 $notice->distribute();
392 function blowOnInsert($conversation = false)
394 self::blow('profile:notice_ids:%d', $this->profile_id);
395 self::blow('public');
397 // XXX: Before we were blowing the casche only if the notice id
398 // was not the root of the conversation. What to do now?
400 self::blow('notice:conversation_ids:%d', $this->conversation);
402 if (!empty($this->repeat_of)) {
403 self::blow('notice:repeats:%d', $this->repeat_of);
406 $original = Notice::staticGet('id', $this->repeat_of);
408 if (!empty($original)) {
409 $originalUser = User::staticGet('id', $original->profile_id);
410 if (!empty($originalUser)) {
411 self::blow('user:repeats_of_me:%d', $originalUser->id);
415 $profile = Profile::staticGet($this->profile_id);
416 $profile->blowNoticeCount();
419 /** save all urls in the notice to the db
421 * follow redirects and save all available file information
422 * (mimetype, date, size, oembed, etc.)
426 function saveUrls() {
427 common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
430 function saveUrl($data) {
431 list($url, $notice_id) = $data;
432 File::processNew($url, $notice_id);
435 static function checkDupes($profile_id, $content) {
436 $profile = Profile::staticGet($profile_id);
437 if (empty($profile)) {
440 $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
441 if (!empty($notice)) {
443 while ($notice->fetch()) {
444 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
446 } else if ($notice->content == $content) {
451 # If we get here, oldest item in cache window is not
452 # old enough for dupe limit; do direct check against DB
453 $notice = new Notice();
454 $notice->profile_id = $profile_id;
455 $notice->content = $content;
456 if (common_config('db','type') == 'pgsql')
457 $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
459 $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
461 $cnt = $notice->count();
465 static function checkEditThrottle($profile_id) {
466 $profile = Profile::staticGet($profile_id);
467 if (empty($profile)) {
471 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
472 if ($notice && $notice->fetch()) {
473 # If the Nth notice was posted less than timespan seconds ago
474 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
479 # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
483 function getUploadedAttachment() {
485 $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"';
486 $post->query($query);
488 if (empty($post->up) || empty($post->i)) {
491 $ret = array($post->up, $post->i);
497 function hasAttachments() {
499 $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);
500 $post->query($query);
502 $n_attachments = intval($post->n_attachments);
504 return $n_attachments;
507 function attachments() {
510 $f2p = new File_to_post;
511 $f2p->post_id = $this->id;
513 while ($f2p->fetch()) {
514 $f = File::staticGet($f2p->file_id);
521 function getStreamByIds($ids)
523 $cache = common_memcache();
525 if (!empty($cache)) {
527 foreach ($ids as $id) {
528 $n = Notice::staticGet('id', $id);
533 return new ArrayWrapper($notices);
535 $notice = new Notice();
537 //if no IDs requested, just return the notice object
540 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
546 while ($notice->fetch()) {
547 $temp[$notice->id] = clone($notice);
552 foreach ($ids as $id) {
553 if (array_key_exists($id, $temp)) {
554 $wrapped[] = $temp[$id];
558 return new ArrayWrapper($wrapped);
562 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
564 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
567 $offset, $limit, $since_id, $max_id, $since);
569 return Notice::getStreamByIds($ids);
572 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
574 $notice = new Notice();
576 $notice->selectAdd(); // clears it
577 $notice->selectAdd('id');
579 $notice->orderBy('id DESC');
581 if (!is_null($offset)) {
582 $notice->limit($offset, $limit);
585 if (common_config('public', 'localonly')) {
586 $notice->whereAdd('is_local = ' . Notice::LOCAL_PUBLIC);
588 # -1 == blacklisted, -2 == gateway (i.e. Twitter)
589 $notice->whereAdd('is_local !='. Notice::LOCAL_NONPUBLIC);
590 $notice->whereAdd('is_local !='. Notice::GATEWAY);
593 if ($since_id != 0) {
594 $notice->whereAdd('id > ' . $since_id);
598 $notice->whereAdd('id <= ' . $max_id);
601 if (!is_null($since)) {
602 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
607 if ($notice->find()) {
608 while ($notice->fetch()) {
609 $ids[] = $notice->id;
619 function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
621 $ids = Notice::stream(array('Notice', '_conversationStreamDirect'),
623 'notice:conversation_ids:'.$id,
624 $offset, $limit, $since_id, $max_id, $since);
626 return Notice::getStreamByIds($ids);
629 function _conversationStreamDirect($id, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
631 $notice = new Notice();
633 $notice->selectAdd(); // clears it
634 $notice->selectAdd('id');
636 $notice->conversation = $id;
638 $notice->orderBy('id DESC');
640 if (!is_null($offset)) {
641 $notice->limit($offset, $limit);
644 if ($since_id != 0) {
645 $notice->whereAdd('id > ' . $since_id);
649 $notice->whereAdd('id <= ' . $max_id);
652 if (!is_null($since)) {
653 $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
658 if ($notice->find()) {
659 while ($notice->fetch()) {
660 $ids[] = $notice->id;
671 * @param $groups array of Group *objects*
672 * @param $recipients array of profile *ids*
674 function whoGets($groups=null, $recipients=null)
676 $c = self::memcache();
679 $ni = $c->get(common_cache_key('notice:who_gets:'.$this->id));
685 if (is_null($groups)) {
686 $groups = $this->getGroups();
689 if (is_null($recipients)) {
690 $recipients = $this->getReplies();
693 $users = $this->getSubscribedUsers();
695 // FIXME: kind of ignoring 'transitional'...
696 // we'll probably stop supporting inboxless mode
701 foreach ($users as $id) {
702 $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
705 $profile = $this->getProfile();
707 foreach ($groups as $group) {
708 $users = $group->getUserMembers();
709 foreach ($users as $id) {
710 if (!array_key_exists($id, $ni)) {
711 $user = User::staticGet('id', $id);
712 if (!$user->hasBlocked($profile)) {
713 $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
719 foreach ($recipients as $recipient) {
721 if (!array_key_exists($recipient, $ni)) {
722 $recipientUser = User::staticGet('id', $recipient);
723 if (!empty($recipientUser)) {
724 $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
730 // XXX: pack this data better
731 $c->set(common_cache_key('notice:who_gets:'.$this->id), $ni);
738 * Adds this notice to the inboxes of each local user who should receive
739 * it, based on author subscriptions, group memberships, and @-replies.
741 * Warning: running a second time currently will make items appear
742 * multiple times in users' inboxes.
744 * @fixme make more robust against errors
745 * @fixme break up massive deliveries to smaller background tasks
747 * @param array $groups optional list of Group objects;
748 * if left empty, will be loaded from group_inbox records
749 * @param array $recipient optional list of reply profile ids
750 * if left empty, will be loaded from reply records
752 function addToInboxes($groups=null, $recipients=null)
754 $ni = $this->whoGets($groups, $recipients);
756 $ids = array_keys($ni);
758 // We remove the author (if they're a local user),
759 // since we'll have already done this in distribute()
761 $i = array_search($this->profile_id, $ids);
769 Inbox::bulkInsert($this->id, $ids);
774 function getSubscribedUsers()
778 if(common_config('db','quote_identifiers'))
779 $user_table = '"user"';
780 else $user_table = 'user';
784 'FROM '. $user_table .' JOIN subscription '.
785 'ON '. $user_table .'.id = subscription.subscriber ' .
786 'WHERE subscription.subscribed = %d ';
788 $user->query(sprintf($qry, $this->profile_id));
792 while ($user->fetch()) {
802 * Record this notice to the given group inboxes for delivery.
803 * Overrides the regular parsing of !group markup.
805 * @param string $group_ids
806 * @fixme might prefer URIs as identifiers, as for replies?
807 * best with generalizations on user_group to support
808 * remote groups better.
810 function saveKnownGroups($group_ids)
812 if (!is_array($group_ids)) {
813 throw new ServerException("Bad type provided to saveKnownGroups");
817 foreach ($group_ids as $id) {
818 $group = User_group::staticGet('id', $id);
820 common_log(LOG_ERR, "Local delivery to group id $id, $group->nickname");
821 $result = $this->addToGroupInbox($group);
823 common_log_db_error($gi, 'INSERT', __FILE__);
826 // @fixme should we save the tags here or not?
827 $groups[] = clone($group);
829 common_log(LOG_ERR, "Local delivery to group id $id skipped, doesn't exist");
837 * Parse !group delivery and record targets into group_inbox.
838 * @return array of Group objects
840 function saveGroups()
842 // Don't save groups for repeats
844 if (!empty($this->repeat_of)) {
850 /* extract all !group */
851 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
852 strtolower($this->content),
858 $profile = $this->getProfile();
860 /* Add them to the database */
862 foreach (array_unique($match[1]) as $nickname) {
863 /* XXX: remote groups. */
864 $group = User_group::getForNickname($nickname);
870 // we automatically add a tag for every group name, too
872 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
873 'notice_id' => $this->id));
876 $this->saveTag($nickname);
879 if ($profile->isMember($group)) {
881 $result = $this->addToGroupInbox($group);
884 common_log_db_error($gi, 'INSERT', __FILE__);
887 $groups[] = clone($group);
894 function addToGroupInbox($group)
896 $gi = Group_inbox::pkeyGet(array('group_id' => $group->id,
897 'notice_id' => $this->id));
901 $gi = new Group_inbox();
903 $gi->group_id = $group->id;
904 $gi->notice_id = $this->id;
905 $gi->created = $this->created;
907 $result = $gi->insert();
910 common_log_db_error($gi, 'INSERT', __FILE__);
911 throw new ServerException(_('Problem saving group inbox.'));
914 self::blow('user_group:notice_ids:%d', $gi->group_id);
921 * Save reply records indicating that this notice needs to be
922 * delivered to the local users with the given URIs.
924 * Since this is expected to be used when saving foreign-sourced
925 * messages, we won't deliver to any remote targets as that's the
926 * source service's responsibility.
928 * @fixme Unlike saveReplies() there's no mail notification here.
929 * Move that to distrib queue handler?
931 * @param array of unique identifier URIs for recipients
933 function saveKnownReplies($uris)
935 foreach ($uris as $uri) {
937 $user = User::staticGet('uri', $uri);
941 $reply = new Reply();
943 $reply->notice_id = $this->id;
944 $reply->profile_id = $user->id;
946 $id = $reply->insert();
948 self::blow('reply:stream:%d', $user->id);
956 * Pull @-replies from this message's content in StatusNet markup format
957 * and save reply records indicating that this message needs to be
958 * delivered to those users.
960 * Side effect: local recipients get e-mail notifications here.
961 * @fixme move mail notifications to distrib?
963 * @return array of integer profile IDs
966 function saveReplies()
968 // Don't save reply data for repeats
970 if (!empty($this->repeat_of)) {
974 $sender = Profile::staticGet($this->profile_id);
976 // @todo ideally this parser information would only
977 // be calculated once.
979 $mentions = common_find_mentions($this->content, $this);
983 // store replied only for first @ (what user/notice what the reply directed,
984 // we assume first @ is it)
986 foreach ($mentions as $mention) {
988 foreach ($mention['mentioned'] as $mentioned) {
990 // skip if they're already covered
992 if (!empty($replied[$mentioned->id])) {
996 // Don't save replies from blocked profile to local user
998 $mentioned_user = User::staticGet('id', $mentioned->id);
999 if (!empty($mentioned_user) && $mentioned_user->hasBlocked($sender)) {
1003 $reply = new Reply();
1005 $reply->notice_id = $this->id;
1006 $reply->profile_id = $mentioned->id;
1008 $id = $reply->insert();
1011 common_log_db_error($reply, 'INSERT', __FILE__);
1012 throw new ServerException("Couldn't save reply for {$this->id}, {$mentioned->id}");
1014 $replied[$mentioned->id] = 1;
1019 $recipientIds = array_keys($replied);
1021 foreach ($recipientIds as $recipientId) {
1022 $user = User::staticGet('id', $recipientId);
1023 if (!empty($user)) {
1024 self::blow('reply:stream:%d', $reply->profile_id);
1025 mail_notify_attn($user, $this);
1029 return $recipientIds;
1032 function getReplies()
1038 $reply = new Reply();
1039 $reply->selectAdd();
1040 $reply->selectAdd('profile_id');
1041 $reply->notice_id = $this->id;
1043 if ($reply->find()) {
1044 while($reply->fetch()) {
1045 $ids[] = $reply->profile_id;
1055 * Pull list of groups this notice needs to be delivered to,
1056 * as previously recorded by saveGroups() or saveKnownGroups().
1058 * @return array of Group objects
1060 function getGroups()
1062 // Don't save groups for repeats
1064 if (!empty($this->repeat_of)) {
1072 $gi = new Group_inbox();
1075 $gi->selectAdd('group_id');
1077 $gi->notice_id = $this->id;
1080 while ($gi->fetch()) {
1081 $group = User_group::staticGet('id', $gi->group_id);
1093 function asAtomEntry($namespace=false, $source=false)
1095 $profile = $this->getProfile();
1097 $xs = new XMLStringer(true);
1100 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
1101 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
1102 'xmlns:georss' => 'http://www.georss.org/georss',
1103 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
1104 'xmlns:media' => 'http://purl.org/syndication/atommedia',
1105 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
1106 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0');
1111 $xs->elementStart('entry', $attrs);
1114 $xs->elementStart('source');
1115 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
1116 $xs->element('link', array('href' => $profile->profileurl));
1117 $user = User::staticGet('id', $profile->id);
1118 if (!empty($user)) {
1119 $atom_feed = common_local_url('ApiTimelineUser',
1120 array('format' => 'atom',
1121 'id' => $profile->nickname));
1122 $xs->element('link', array('rel' => 'self',
1123 'type' => 'application/atom+xml',
1124 'href' => $profile->profileurl));
1125 $xs->element('link', array('rel' => 'license',
1126 'href' => common_config('license', 'url')));
1129 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
1133 $xs->elementEnd('source');
1136 $xs->element('title', null, $this->content);
1137 $xs->element('summary', null, $this->content);
1139 $xs->raw($profile->asAtomAuthor());
1140 $xs->raw($profile->asActivityActor());
1142 $xs->element('link', array('rel' => 'alternate',
1143 'type' => 'text/html',
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->created));
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 if (!empty($this->conversation)) {
1164 $conv = Conversation::staticGet('id', $this->conversation);
1166 if (!empty($conv)) {
1169 'rel' => 'ostatus:conversation',
1170 'href' => $conv->uri
1176 $reply_ids = $this->getReplies();
1178 foreach ($reply_ids as $id) {
1179 $profile = Profile::staticGet('id', $id);
1180 if (!empty($profile)) {
1183 'rel' => 'ostatus:attention',
1184 'href' => $profile->getUri()
1190 $groups = $this->getGroups();
1192 foreach ($groups as $group) {
1195 'rel' => 'ostatus:attention',
1196 'href' => $group->permalink()
1201 if (!empty($this->repeat_of)) {
1202 $repeat = Notice::staticGet('id', $this->repeat_of);
1203 if (!empty($repeat)) {
1206 array('ref' => $repeat->uri, 'href' => $repeat->bestUrl())
1211 $xs->element('content', array('type' => 'html'), $this->rendered);
1213 $tag = new Notice_tag();
1214 $tag->notice_id = $this->id;
1216 while ($tag->fetch()) {
1217 $xs->element('category', array('term' => $tag->tag));
1223 $attachments = $this->attachments();
1225 foreach($attachments as $attachment){
1226 $enclosure=$attachment->getEnclosure();
1228 $attributes = array('rel'=>'enclosure','href'=>$enclosure->url,'type'=>$enclosure->mimetype,'length'=>$enclosure->size);
1229 if($enclosure->title){
1230 $attributes['title']=$enclosure->title;
1232 $xs->element('link', $attributes, null);
1237 if (!empty($this->lat) && !empty($this->lon)) {
1238 $xs->element('georss:point', null, $this->lat . ' ' . $this->lon);
1241 $xs->elementEnd('entry');
1243 return $xs->getString();
1247 * Returns an XML string fragment with a reference to a notice as an
1248 * Activity Streams noun object with the given element type.
1250 * Assumes that 'activity' namespace has been previously defined.
1252 * @param string $element one of 'subject', 'object', 'target'
1255 function asActivityNoun($element)
1257 $noun = ActivityObject::fromNotice($this);
1258 return $noun->asString('activity:' . $element);
1263 if (!empty($this->url)) {
1265 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
1268 return common_local_url('shownotice',
1269 array('notice' => $this->id));
1273 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0, $since=null)
1275 $cache = common_memcache();
1277 if (empty($cache) ||
1278 $since_id != 0 || $max_id != 0 || (!is_null($since) && $since > 0) ||
1280 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1281 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1285 $idkey = common_cache_key($cachekey);
1287 $idstr = $cache->get($idkey);
1289 if ($idstr !== false) {
1290 // Cache hit! Woohoo!
1291 $window = explode(',', $idstr);
1292 $ids = array_slice($window, $offset, $limit);
1296 $laststr = $cache->get($idkey.';last');
1298 if ($laststr !== false) {
1299 $window = explode(',', $laststr);
1300 $last_id = $window[0];
1301 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1302 $last_id, 0, null)));
1304 $new_window = array_merge($new_ids, $window);
1306 $new_windowstr = implode(',', $new_window);
1308 $result = $cache->set($idkey, $new_windowstr);
1309 $result = $cache->set($idkey . ';last', $new_windowstr);
1311 $ids = array_slice($new_window, $offset, $limit);
1316 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1319 $windowstr = implode(',', $window);
1321 $result = $cache->set($idkey, $windowstr);
1322 $result = $cache->set($idkey . ';last', $windowstr);
1324 $ids = array_slice($window, $offset, $limit);
1330 * Determine which notice, if any, a new notice is in reply to.
1332 * For conversation tracking, we try to see where this notice fits
1333 * in the tree. Rough algorithm is:
1335 * if (reply_to is set and valid) {
1337 * } else if ((source not API or Web) and (content starts with "T NAME" or "@name ")) {
1338 * return ID of last notice by initial @name in content;
1341 * Note that all @nickname instances will still be used to save "reply" records,
1342 * so the notice shows up in the mentioned users' "replies" tab.
1344 * @param integer $reply_to ID passed in by Web or API
1345 * @param integer $profile_id ID of author
1346 * @param string $source Source tag, like 'web' or 'gwibber'
1347 * @param string $content Final notice content
1349 * @return integer ID of replied-to notice, or null for not a reply.
1352 static function getReplyTo($reply_to, $profile_id, $source, $content)
1354 static $lb = array('xmpp', 'mail', 'sms', 'omb');
1356 // If $reply_to is specified, we check that it exists, and then
1357 // return it if it does
1359 if (!empty($reply_to)) {
1360 $reply_notice = Notice::staticGet('id', $reply_to);
1361 if (!empty($reply_notice)) {
1366 // If it's not a "low bandwidth" source (one where you can't set
1367 // a reply_to argument), we return. This is mostly web and API
1370 if (!in_array($source, $lb)) {
1374 // Is there an initial @ or T?
1376 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $content, $match) ||
1377 preg_match('/^@([a-z0-9]{1,64})\s+/', $content, $match)) {
1378 $nickname = common_canonical_nickname($match[1]);
1383 // Figure out who that is.
1385 $sender = Profile::staticGet('id', $profile_id);
1386 if (empty($sender)) {
1390 $recipient = common_relative_profile($sender, $nickname, common_sql_now());
1392 if (empty($recipient)) {
1396 // Get their last notice
1398 $last = $recipient->getCurrentNotice();
1400 if (!empty($last)) {
1405 static function maxContent()
1407 $contentlimit = common_config('notice', 'contentlimit');
1408 // null => use global limit (distinct from 0!)
1409 if (is_null($contentlimit)) {
1410 $contentlimit = common_config('site', 'textlimit');
1412 return $contentlimit;
1415 static function contentTooLong($content)
1417 $contentlimit = self::maxContent();
1418 return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
1421 function getLocation()
1425 if (!empty($this->location_id) && !empty($this->location_ns)) {
1426 $location = Location::fromId($this->location_id, $this->location_ns);
1429 if (is_null($location)) { // no ID, or Location::fromId() failed
1430 if (!empty($this->lat) && !empty($this->lon)) {
1431 $location = Location::fromLatLon($this->lat, $this->lon);
1438 function repeat($repeater_id, $source)
1440 $author = Profile::staticGet('id', $this->profile_id);
1442 $content = sprintf(_('RT @%1$s %2$s'),
1446 $maxlen = common_config('site', 'textlimit');
1447 if ($maxlen > 0 && mb_strlen($content) > $maxlen) {
1448 // Web interface and current Twitter API clients will
1449 // pull the original notice's text, but some older
1450 // clients and RSS/Atom feeds will see this trimmed text.
1452 // Unfortunately this is likely to lose tags or URLs
1453 // at the end of long notices.
1454 $content = mb_substr($content, 0, $maxlen - 4) . ' ...';
1457 return self::saveNew($repeater_id, $content, $source,
1458 array('repeat_of' => $this->id));
1461 // These are supposed to be in chron order!
1463 function repeatStream($limit=100)
1465 $cache = common_memcache();
1467 if (empty($cache)) {
1468 $ids = $this->_repeatStreamDirect($limit);
1470 $idstr = $cache->get(common_cache_key('notice:repeats:'.$this->id));
1471 if ($idstr !== false) {
1472 $ids = explode(',', $idstr);
1474 $ids = $this->_repeatStreamDirect(100);
1475 $cache->set(common_cache_key('notice:repeats:'.$this->id), implode(',', $ids));
1478 // We do a max of 100, so slice down to limit
1479 $ids = array_slice($ids, 0, $limit);
1483 return Notice::getStreamByIds($ids);
1486 function _repeatStreamDirect($limit)
1488 $notice = new Notice();
1490 $notice->selectAdd(); // clears it
1491 $notice->selectAdd('id');
1493 $notice->repeat_of = $this->id;
1495 $notice->orderBy('created'); // NB: asc!
1497 if (!is_null($offset)) {
1498 $notice->limit($offset, $limit);
1503 if ($notice->find()) {
1504 while ($notice->fetch()) {
1505 $ids[] = $notice->id;
1515 function locationOptions($lat, $lon, $location_id, $location_ns, $profile = null)
1519 if (!empty($location_id) && !empty($location_ns)) {
1521 $options['location_id'] = $location_id;
1522 $options['location_ns'] = $location_ns;
1524 $location = Location::fromId($location_id, $location_ns);
1526 if (!empty($location)) {
1527 $options['lat'] = $location->lat;
1528 $options['lon'] = $location->lon;
1531 } else if (!empty($lat) && !empty($lon)) {
1533 $options['lat'] = $lat;
1534 $options['lon'] = $lon;
1536 $location = Location::fromLatLon($lat, $lon);
1538 if (!empty($location)) {
1539 $options['location_id'] = $location->location_id;
1540 $options['location_ns'] = $location->location_ns;
1542 } else if (!empty($profile)) {
1544 if (isset($profile->lat) && isset($profile->lon)) {
1545 $options['lat'] = $profile->lat;
1546 $options['lon'] = $profile->lon;
1549 if (isset($profile->location_id) && isset($profile->location_ns)) {
1550 $options['location_id'] = $profile->location_id;
1551 $options['location_ns'] = $profile->location_ns;
1558 function clearReplies()
1560 $replyNotice = new Notice();
1561 $replyNotice->reply_to = $this->id;
1563 //Null any notices that are replies to this notice
1565 if ($replyNotice->find()) {
1566 while ($replyNotice->fetch()) {
1567 $orig = clone($replyNotice);
1568 $replyNotice->reply_to = null;
1569 $replyNotice->update($orig);
1575 $reply = new Reply();
1576 $reply->notice_id = $this->id;
1578 if ($reply->find()) {
1579 while($reply->fetch()) {
1580 self::blow('reply:stream:%d', $reply->profile_id);
1588 function clearRepeats()
1590 $repeatNotice = new Notice();
1591 $repeatNotice->repeat_of = $this->id;
1593 //Null any notices that are repeats of this notice
1595 if ($repeatNotice->find()) {
1596 while ($repeatNotice->fetch()) {
1597 $orig = clone($repeatNotice);
1598 $repeatNotice->repeat_of = null;
1599 $repeatNotice->update($orig);
1604 function clearFaves()
1607 $fave->notice_id = $this->id;
1609 if ($fave->find()) {
1610 while ($fave->fetch()) {
1611 self::blow('fave:ids_by_user_own:%d', $fave->user_id);
1612 self::blow('fave:ids_by_user_own:%d;last', $fave->user_id);
1613 self::blow('fave:ids_by_user:%d', $fave->user_id);
1614 self::blow('fave:ids_by_user:%d;last', $fave->user_id);
1622 function clearTags()
1624 $tag = new Notice_tag();
1625 $tag->notice_id = $this->id;
1628 while ($tag->fetch()) {
1629 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, common_keyize($tag->tag));
1630 self::blow('profile:notice_ids_tagged:%d:%s;last', $this->profile_id, common_keyize($tag->tag));
1631 self::blow('notice_tag:notice_ids:%s', common_keyize($tag->tag));
1632 self::blow('notice_tag:notice_ids:%s;last', common_keyize($tag->tag));
1640 function clearGroupInboxes()
1642 $gi = new Group_inbox();
1644 $gi->notice_id = $this->id;
1647 while ($gi->fetch()) {
1648 self::blow('user_group:notice_ids:%d', $gi->group_id);
1656 function distribute()
1658 // We always insert for the author so they don't
1661 $user = User::staticGet('id', $this->profile_id);
1662 if (!empty($user)) {
1663 Inbox::insertNotice($user->id, $this->id);
1666 if (common_config('queue', 'inboxes')) {
1667 // If there's a failure, we want to _force_
1668 // distribution at this point.
1670 $qm = QueueManager::get();
1671 $qm->enqueue($this, 'distrib');
1672 } catch (Exception $e) {
1673 // If the exception isn't transient, this
1674 // may throw more exceptions as DQH does
1675 // its own enqueueing. So, we ignore them!
1677 $handler = new DistribQueueHandler();
1678 $handler->handle($this);
1679 } catch (Exception $e) {
1680 common_log(LOG_ERR, "emergency redistribution resulted in " . $e->getMessage());
1682 // Re-throw so somebody smarter can handle it.
1686 $handler = new DistribQueueHandler();
1687 $handler->handle($this);
1693 $result = parent::insert();
1696 // Profile::hasRepeated() abuses pkeyGet(), so we
1697 // have to clear manually
1698 if (!empty($this->repeat_of)) {
1699 $c = self::memcache();
1701 $ck = self::multicacheKey('Notice',
1702 array('profile_id' => $this->profile_id,
1703 'repeat_of' => $this->repeat_of));