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 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
33 * @license GNU Affero General Public License http://www.gnu.org/licenses/
36 if (!defined('STATUSNET') && !defined('LACONICA')) {
41 * Table Definition for notice
43 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
45 /* We keep 200 notices, the max number of notices available per API request,
46 * in the memcached cache. */
48 define('NOTICE_CACHE_WINDOW', 200);
50 define('MAX_BOXCARS', 128);
52 class Notice extends Memcached_DataObject
55 /* the code below is auto generated do not remove the above tag */
57 public $__table = 'notice'; // table name
58 public $id; // int(4) primary_key not_null
59 public $profile_id; // int(4) multiple_key not_null
60 public $uri; // varchar(255) unique_key
61 public $content; // text
62 public $rendered; // text
63 public $url; // varchar(255)
64 public $created; // datetime multiple_key not_null default_0000-00-00%2000%3A00%3A00
65 public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
66 public $reply_to; // int(4)
67 public $is_local; // int(4)
68 public $source; // varchar(32)
69 public $conversation; // int(4)
70 public $lat; // decimal(10,7)
71 public $lon; // decimal(10,7)
72 public $location_id; // int(4)
73 public $location_ns; // int(4)
74 public $repeat_of; // int(4)
77 function staticGet($k,$v=NULL)
79 return Memcached_DataObject::staticGet('Notice',$k,$v);
82 /* the code above is auto generated do not remove the tag below */
86 const LOCAL_PUBLIC = 1;
88 const LOCAL_NONPUBLIC = -1;
93 $profile = Profile::staticGet('id', $this->profile_id);
95 if (empty($profile)) {
96 throw new ServerException(sprintf(_('No such profile (%d) for notice (%d)'), $this->profile_id, $this->id));
104 // For auditing purposes, save a record that the notice
107 // @fixme we have some cases where things get re-run and so the
109 $deleted = Deleted_notice::staticGet('id', $this->id);
111 $deleted = new Deleted_notice();
113 $deleted->id = $this->id;
114 $deleted->profile_id = $this->profile_id;
115 $deleted->uri = $this->uri;
116 $deleted->created = $this->created;
117 $deleted->deleted = common_sql_now();
122 // Clear related records
124 $this->clearReplies();
125 $this->clearRepeats();
128 $this->clearGroupInboxes();
130 // NOTE: we don't clear inboxes
131 // NOTE: we don't clear queue items
133 $result = parent::delete();
135 $this->blowOnDelete();
140 * Extract #hashtags from this notice's content and save them to the database.
144 /* extract all #hastags */
145 $count = preg_match_all('/(?:^|\s)#([\pL\pN_\-\.]{1,64})/', strtolower($this->content), $match);
150 /* Add them to the database */
151 return $this->saveKnownTags($match[1]);
155 * Record the given set of hash tags in the db for this notice.
156 * Given tag strings will be normalized and checked for dupes.
158 function saveKnownTags($hashtags)
160 //turn each into their canonical tag
161 //this is needed to remove dupes before saving e.g. #hash.tag = #hashtag
162 for($i=0; $i<count($hashtags); $i++) {
163 /* elide characters we don't want in the tag */
164 $hashtags[$i] = common_canonical_tag($hashtags[$i]);
167 foreach(array_unique($hashtags) as $hashtag) {
168 $this->saveTag($hashtag);
169 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, $hashtag);
175 * Record a single hash tag as associated with this notice.
176 * Tag format and uniqueness must be validated by caller.
178 function saveTag($hashtag)
180 $tag = new Notice_tag();
181 $tag->notice_id = $this->id;
182 $tag->tag = $hashtag;
183 $tag->created = $this->created;
184 $id = $tag->insert();
187 // TRANS: Server exception. %s are the error details.
188 throw new ServerException(sprintf(_('Database error inserting hashtag: %s'),
189 $last_error->message));
193 // if it's saved, blow its cache
194 $tag->blowCache(false);
198 * Save a new notice and push it out to subscribers' inboxes.
199 * Poster's permissions are checked before sending.
201 * @param int $profile_id Profile ID of the poster
202 * @param string $content source message text; links may be shortened
203 * per current user's preference
204 * @param string $source source key ('web', 'api', etc)
205 * @param array $options Associative array of optional properties:
206 * string 'created' timestamp of notice; defaults to now
207 * int 'is_local' source/gateway ID, one of:
208 * Notice::LOCAL_PUBLIC - Local, ok to appear in public timeline
209 * Notice::REMOTE_OMB - Sent from a remote OMB service;
210 * hide from public timeline but show in
211 * local "and friends" timelines
212 * Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
213 * Notice::GATEWAY - From another non-OMB service;
214 * will not appear in public views
215 * float 'lat' decimal latitude for geolocation
216 * float 'lon' decimal longitude for geolocation
217 * int 'location_id' geoname identifier
218 * int 'location_ns' geoname namespace to interpret location_id
219 * int 'reply_to'; notice ID this is a reply to
220 * int 'repeat_of'; notice ID this is a repeat of
221 * string 'uri' unique ID for notice; defaults to local notice URL
222 * string 'url' permalink to notice; defaults to local notice URL
223 * string 'rendered' rendered HTML version of content
224 * array 'replies' list of profile URIs for reply delivery in
225 * place of extracting @-replies from content.
226 * array 'groups' list of group IDs to deliver to, in place of
227 * extracting ! tags from content
228 * array 'tags' list of hashtag strings to save with the notice
229 * in place of extracting # tags from content
230 * array 'urls' list of attached/referred URLs to save with the
231 * notice in place of extracting links from content
232 * @fixme tag override
235 * @throws ClientException
237 static function saveNew($profile_id, $content, $source, $options=null) {
238 $defaults = array('uri' => null,
241 'repeat_of' => null);
243 if (!empty($options)) {
244 $options = $options + $defaults;
248 if (!isset($is_local)) {
249 $is_local = Notice::LOCAL_PUBLIC;
252 $profile = Profile::staticGet($profile_id);
254 $final = common_shorten_links($content);
256 if (Notice::contentTooLong($final)) {
257 throw new ClientException(_('Problem saving notice. Too long.'));
260 if (empty($profile)) {
261 throw new ClientException(_('Problem saving notice. Unknown user.'));
264 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
265 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
266 throw new ClientException(_('Too many notices too fast; take a breather '.
267 'and post again in a few minutes.'));
270 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
271 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
272 throw new ClientException(_('Too many duplicate messages too quickly;'.
273 ' take a breather and post again in a few minutes.'));
276 if (!$profile->hasRight(Right::NEWNOTICE)) {
277 common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
278 throw new ClientException(_('You are banned from posting notices on this site.'));
281 $notice = new Notice();
282 $notice->profile_id = $profile_id;
284 $autosource = common_config('public', 'autosource');
286 # Sandboxed are non-false, but not 1, either
288 if (!$profile->hasRight(Right::PUBLICNOTICE) ||
289 ($source && $autosource && in_array($source, $autosource))) {
290 $notice->is_local = Notice::LOCAL_NONPUBLIC;
292 $notice->is_local = $is_local;
295 if (!empty($created)) {
296 $notice->created = $created;
298 $notice->created = common_sql_now();
301 $notice->content = $final;
303 $notice->source = $source;
307 // Handle repeat case
309 if (isset($repeat_of)) {
310 $notice->repeat_of = $repeat_of;
312 $notice->reply_to = self::getReplyTo($reply_to, $profile_id, $source, $final);
315 if (!empty($notice->reply_to)) {
316 $reply = Notice::staticGet('id', $notice->reply_to);
317 $notice->conversation = $reply->conversation;
320 if (!empty($lat) && !empty($lon)) {
325 if (!empty($location_ns) && !empty($location_id)) {
326 $notice->location_id = $location_id;
327 $notice->location_ns = $location_ns;
330 if (!empty($rendered)) {
331 $notice->rendered = $rendered;
333 $notice->rendered = common_render_content($final, $notice);
336 if (Event::handle('StartNoticeSave', array(&$notice))) {
338 // XXX: some of these functions write to the DB
340 $id = $notice->insert();
343 common_log_db_error($notice, 'INSERT', __FILE__);
344 throw new ServerException(_('Problem saving notice.'));
347 // Update ID-dependent columns: URI, conversation
349 $orig = clone($notice);
354 $notice->uri = common_notice_uri($notice);
358 // If it's not part of a conversation, it's
359 // the beginning of a new conversation.
361 if (empty($notice->conversation)) {
362 $conv = Conversation::create();
363 $notice->conversation = $conv->id;
368 if (!$notice->update($orig)) {
369 common_log_db_error($notice, 'UPDATE', __FILE__);
370 throw new ServerException(_('Problem saving notice.'));
376 # Clear the cache for subscribed users, so they'll update at next request
377 # XXX: someone clever could prepend instead of clearing the cache
379 $notice->blowOnInsert();
381 // Save per-notice metadata...
383 if (isset($replies)) {
384 $notice->saveKnownReplies($replies);
386 $notice->saveReplies();
390 $notice->saveKnownTags($tags);
395 // Note: groups may save tags, so must be run after tags are saved
396 // to avoid errors on duplicates.
397 if (isset($groups)) {
398 $notice->saveKnownGroups($groups);
400 $notice->saveGroups();
404 $notice->saveKnownUrls($urls);
409 // Prepare inbox delivery, may be queued to background.
410 $notice->distribute();
415 function blowOnInsert($conversation = false)
417 self::blow('profile:notice_ids:%d', $this->profile_id);
418 self::blow('public');
420 // XXX: Before we were blowing the casche only if the notice id
421 // was not the root of the conversation. What to do now?
423 self::blow('notice:conversation_ids:%d', $this->conversation);
425 if (!empty($this->repeat_of)) {
426 self::blow('notice:repeats:%d', $this->repeat_of);
429 $original = Notice::staticGet('id', $this->repeat_of);
431 if (!empty($original)) {
432 $originalUser = User::staticGet('id', $original->profile_id);
433 if (!empty($originalUser)) {
434 self::blow('user:repeats_of_me:%d', $originalUser->id);
438 $profile = Profile::staticGet($this->profile_id);
439 if (!empty($profile)) {
440 $profile->blowNoticeCount();
445 * Clear cache entries related to this notice at delete time.
446 * Necessary to avoid breaking paging on public, profile timelines.
448 function blowOnDelete()
450 $this->blowOnInsert();
452 self::blow('profile:notice_ids:%d;last', $this->profile_id);
453 self::blow('public;last');
456 /** save all urls in the notice to the db
458 * follow redirects and save all available file information
459 * (mimetype, date, size, oembed, etc.)
463 function saveUrls() {
464 common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
468 * Save the given URLs as related links/attachments to the db
470 * follow redirects and save all available file information
471 * (mimetype, date, size, oembed, etc.)
475 function saveKnownUrls($urls)
477 // @fixme validation?
478 foreach ($urls as $url) {
479 File::processNew($url, $this->id);
486 function saveUrl($data) {
487 list($url, $notice_id) = $data;
488 File::processNew($url, $notice_id);
491 static function checkDupes($profile_id, $content) {
492 $profile = Profile::staticGet($profile_id);
493 if (empty($profile)) {
496 $notice = $profile->getNotices(0, NOTICE_CACHE_WINDOW);
497 if (!empty($notice)) {
499 while ($notice->fetch()) {
500 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
502 } else if ($notice->content == $content) {
507 # If we get here, oldest item in cache window is not
508 # old enough for dupe limit; do direct check against DB
509 $notice = new Notice();
510 $notice->profile_id = $profile_id;
511 $notice->content = $content;
512 if (common_config('db','type') == 'pgsql')
513 $notice->whereAdd('extract(epoch from now() - created) < ' . common_config('site', 'dupelimit'));
515 $notice->whereAdd('now() - created < ' . common_config('site', 'dupelimit'));
517 $cnt = $notice->count();
521 static function checkEditThrottle($profile_id) {
522 $profile = Profile::staticGet($profile_id);
523 if (empty($profile)) {
527 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
528 if ($notice && $notice->fetch()) {
529 # If the Nth notice was posted less than timespan seconds ago
530 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
535 # Either not N notices in the stream, OR the Nth was not posted within timespan seconds
539 function getUploadedAttachment() {
541 $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"';
542 $post->query($query);
544 if (empty($post->up) || empty($post->i)) {
547 $ret = array($post->up, $post->i);
553 function hasAttachments() {
555 $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);
556 $post->query($query);
558 $n_attachments = intval($post->n_attachments);
560 return $n_attachments;
563 function attachments() {
566 $f2p = new File_to_post;
567 $f2p->post_id = $this->id;
569 while ($f2p->fetch()) {
570 $f = File::staticGet($f2p->file_id);
577 function getStreamByIds($ids)
579 $cache = common_memcache();
581 if (!empty($cache)) {
583 foreach ($ids as $id) {
584 $n = Notice::staticGet('id', $id);
589 return new ArrayWrapper($notices);
591 $notice = new Notice();
593 //if no IDs requested, just return the notice object
596 $notice->whereAdd('id in (' . implode(', ', $ids) . ')');
602 while ($notice->fetch()) {
603 $temp[$notice->id] = clone($notice);
608 foreach ($ids as $id) {
609 if (array_key_exists($id, $temp)) {
610 $wrapped[] = $temp[$id];
614 return new ArrayWrapper($wrapped);
618 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0)
620 $ids = Notice::stream(array('Notice', '_publicStreamDirect'),
623 $offset, $limit, $since_id, $max_id);
624 return Notice::getStreamByIds($ids);
627 function _publicStreamDirect($offset=0, $limit=20, $since_id=0, $max_id=0)
629 $notice = new Notice();
631 $notice->selectAdd(); // clears it
632 $notice->selectAdd('id');
634 $notice->orderBy('id DESC');
636 if (!is_null($offset)) {
637 $notice->limit($offset, $limit);
640 if (common_config('public', 'localonly')) {
641 $notice->whereAdd('is_local = ' . Notice::LOCAL_PUBLIC);
643 # -1 == blacklisted, -2 == gateway (i.e. Twitter)
644 $notice->whereAdd('is_local !='. Notice::LOCAL_NONPUBLIC);
645 $notice->whereAdd('is_local !='. Notice::GATEWAY);
648 if ($since_id != 0) {
649 $notice->whereAdd('id > ' . $since_id);
653 $notice->whereAdd('id <= ' . $max_id);
658 if ($notice->find()) {
659 while ($notice->fetch()) {
660 $ids[] = $notice->id;
670 function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0)
672 $ids = Notice::stream(array('Notice', '_conversationStreamDirect'),
674 'notice:conversation_ids:'.$id,
675 $offset, $limit, $since_id, $max_id);
677 return Notice::getStreamByIds($ids);
680 function _conversationStreamDirect($id, $offset=0, $limit=20, $since_id=0, $max_id=0)
682 $notice = new Notice();
684 $notice->selectAdd(); // clears it
685 $notice->selectAdd('id');
687 $notice->conversation = $id;
689 $notice->orderBy('id DESC');
691 if (!is_null($offset)) {
692 $notice->limit($offset, $limit);
695 if ($since_id != 0) {
696 $notice->whereAdd('id > ' . $since_id);
700 $notice->whereAdd('id <= ' . $max_id);
705 if ($notice->find()) {
706 while ($notice->fetch()) {
707 $ids[] = $notice->id;
718 * Is this notice part of an active conversation?
720 * @return boolean true if other messages exist in the same
721 * conversation, false if this is the only one
723 function hasConversation()
725 if (!empty($this->conversation)) {
726 $conversation = Notice::conversationStream(
731 if ($conversation->N > 0) {
739 * @param $groups array of Group *objects*
740 * @param $recipients array of profile *ids*
742 function whoGets($groups=null, $recipients=null)
744 $c = self::memcache();
747 $ni = $c->get(common_cache_key('notice:who_gets:'.$this->id));
753 if (is_null($groups)) {
754 $groups = $this->getGroups();
757 if (is_null($recipients)) {
758 $recipients = $this->getReplies();
761 $users = $this->getSubscribedUsers();
763 // FIXME: kind of ignoring 'transitional'...
764 // we'll probably stop supporting inboxless mode
769 foreach ($users as $id) {
770 $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
773 $profile = $this->getProfile();
775 foreach ($groups as $group) {
776 $users = $group->getUserMembers();
777 foreach ($users as $id) {
778 if (!array_key_exists($id, $ni)) {
779 $user = User::staticGet('id', $id);
780 if (!$user->hasBlocked($profile)) {
781 $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
787 foreach ($recipients as $recipient) {
789 if (!array_key_exists($recipient, $ni)) {
790 $recipientUser = User::staticGet('id', $recipient);
791 if (!empty($recipientUser)) {
792 $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
798 // XXX: pack this data better
799 $c->set(common_cache_key('notice:who_gets:'.$this->id), $ni);
806 * Adds this notice to the inboxes of each local user who should receive
807 * it, based on author subscriptions, group memberships, and @-replies.
809 * Warning: running a second time currently will make items appear
810 * multiple times in users' inboxes.
812 * @fixme make more robust against errors
813 * @fixme break up massive deliveries to smaller background tasks
815 * @param array $groups optional list of Group objects;
816 * if left empty, will be loaded from group_inbox records
817 * @param array $recipient optional list of reply profile ids
818 * if left empty, will be loaded from reply records
820 function addToInboxes($groups=null, $recipients=null)
822 $ni = $this->whoGets($groups, $recipients);
824 $ids = array_keys($ni);
826 // We remove the author (if they're a local user),
827 // since we'll have already done this in distribute()
829 $i = array_search($this->profile_id, $ids);
837 Inbox::bulkInsert($this->id, $ids);
842 function getSubscribedUsers()
846 if(common_config('db','quote_identifiers'))
847 $user_table = '"user"';
848 else $user_table = 'user';
852 'FROM '. $user_table .' JOIN subscription '.
853 'ON '. $user_table .'.id = subscription.subscriber ' .
854 'WHERE subscription.subscribed = %d ';
856 $user->query(sprintf($qry, $this->profile_id));
860 while ($user->fetch()) {
870 * Record this notice to the given group inboxes for delivery.
871 * Overrides the regular parsing of !group markup.
873 * @param string $group_ids
874 * @fixme might prefer URIs as identifiers, as for replies?
875 * best with generalizations on user_group to support
876 * remote groups better.
878 function saveKnownGroups($group_ids)
880 if (!is_array($group_ids)) {
881 throw new ServerException("Bad type provided to saveKnownGroups");
885 foreach ($group_ids as $id) {
886 $group = User_group::staticGet('id', $id);
888 common_log(LOG_ERR, "Local delivery to group id $id, $group->nickname");
889 $result = $this->addToGroupInbox($group);
891 common_log_db_error($gi, 'INSERT', __FILE__);
894 // @fixme should we save the tags here or not?
895 $groups[] = clone($group);
897 common_log(LOG_ERR, "Local delivery to group id $id skipped, doesn't exist");
905 * Parse !group delivery and record targets into group_inbox.
906 * @return array of Group objects
908 function saveGroups()
910 // Don't save groups for repeats
912 if (!empty($this->repeat_of)) {
918 /* extract all !group */
919 $count = preg_match_all('/(?:^|\s)!([A-Za-z0-9]{1,64})/',
920 strtolower($this->content),
926 $profile = $this->getProfile();
928 /* Add them to the database */
930 foreach (array_unique($match[1]) as $nickname) {
931 /* XXX: remote groups. */
932 $group = User_group::getForNickname($nickname, $profile);
938 // we automatically add a tag for every group name, too
940 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($nickname),
941 'notice_id' => $this->id));
944 $this->saveTag($nickname);
947 if ($profile->isMember($group)) {
949 $result = $this->addToGroupInbox($group);
952 common_log_db_error($gi, 'INSERT', __FILE__);
955 $groups[] = clone($group);
962 function addToGroupInbox($group)
964 $gi = Group_inbox::pkeyGet(array('group_id' => $group->id,
965 'notice_id' => $this->id));
969 $gi = new Group_inbox();
971 $gi->group_id = $group->id;
972 $gi->notice_id = $this->id;
973 $gi->created = $this->created;
975 $result = $gi->insert();
978 common_log_db_error($gi, 'INSERT', __FILE__);
979 throw new ServerException(_('Problem saving group inbox.'));
982 self::blow('user_group:notice_ids:%d', $gi->group_id);
989 * Save reply records indicating that this notice needs to be
990 * delivered to the local users with the given URIs.
992 * Since this is expected to be used when saving foreign-sourced
993 * messages, we won't deliver to any remote targets as that's the
994 * source service's responsibility.
996 * Mail notifications etc will be handled later.
998 * @param array of unique identifier URIs for recipients
1000 function saveKnownReplies($uris)
1005 $sender = Profile::staticGet($this->profile_id);
1007 foreach ($uris as $uri) {
1009 $user = User::staticGet('uri', $uri);
1011 if (!empty($user)) {
1012 if ($user->hasBlocked($sender)) {
1016 $reply = new Reply();
1018 $reply->notice_id = $this->id;
1019 $reply->profile_id = $user->id;
1021 $id = $reply->insert();
1029 * Pull @-replies from this message's content in StatusNet markup format
1030 * and save reply records indicating that this message needs to be
1031 * delivered to those users.
1033 * Mail notifications to local profiles will be sent later.
1035 * @return array of integer profile IDs
1038 function saveReplies()
1040 // Don't save reply data for repeats
1042 if (!empty($this->repeat_of)) {
1046 $sender = Profile::staticGet($this->profile_id);
1048 // @todo ideally this parser information would only
1049 // be calculated once.
1051 $mentions = common_find_mentions($this->content, $this);
1055 // store replied only for first @ (what user/notice what the reply directed,
1056 // we assume first @ is it)
1058 foreach ($mentions as $mention) {
1060 foreach ($mention['mentioned'] as $mentioned) {
1062 // skip if they're already covered
1064 if (!empty($replied[$mentioned->id])) {
1068 // Don't save replies from blocked profile to local user
1070 $mentioned_user = User::staticGet('id', $mentioned->id);
1071 if (!empty($mentioned_user) && $mentioned_user->hasBlocked($sender)) {
1075 $reply = new Reply();
1077 $reply->notice_id = $this->id;
1078 $reply->profile_id = $mentioned->id;
1080 $id = $reply->insert();
1083 common_log_db_error($reply, 'INSERT', __FILE__);
1084 throw new ServerException("Couldn't save reply for {$this->id}, {$mentioned->id}");
1086 $replied[$mentioned->id] = 1;
1087 self::blow('reply:stream:%d', $mentioned->id);
1092 $recipientIds = array_keys($replied);
1094 return $recipientIds;
1098 * Pull the complete list of @-reply targets for this notice.
1100 * @return array of integer profile ids
1102 function getReplies()
1108 $reply = new Reply();
1109 $reply->selectAdd();
1110 $reply->selectAdd('profile_id');
1111 $reply->notice_id = $this->id;
1113 if ($reply->find()) {
1114 while($reply->fetch()) {
1115 $ids[] = $reply->profile_id;
1125 * Send e-mail notifications to local @-reply targets.
1127 * Replies must already have been saved; this is expected to be run
1128 * from the distrib queue handler.
1130 function sendReplyNotifications()
1132 // Don't send reply notifications for repeats
1134 if (!empty($this->repeat_of)) {
1138 $recipientIds = $this->getReplies();
1140 foreach ($recipientIds as $recipientId) {
1141 $user = User::staticGet('id', $recipientId);
1142 if (!empty($user)) {
1143 mail_notify_attn($user, $this);
1149 * Pull list of groups this notice needs to be delivered to,
1150 * as previously recorded by saveGroups() or saveKnownGroups().
1152 * @return array of Group objects
1154 function getGroups()
1156 // Don't save groups for repeats
1158 if (!empty($this->repeat_of)) {
1166 $gi = new Group_inbox();
1169 $gi->selectAdd('group_id');
1171 $gi->notice_id = $this->id;
1174 while ($gi->fetch()) {
1175 $group = User_group::staticGet('id', $gi->group_id);
1187 function asAtomEntry($namespace=false, $source=false, $author=true, $cur=null)
1189 $profile = $this->getProfile();
1191 $xs = new XMLStringer(true);
1194 $attrs = array('xmlns' => 'http://www.w3.org/2005/Atom',
1195 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0',
1196 'xmlns:georss' => 'http://www.georss.org/georss',
1197 'xmlns:activity' => 'http://activitystrea.ms/spec/1.0/',
1198 'xmlns:media' => 'http://purl.org/syndication/atommedia',
1199 'xmlns:poco' => 'http://portablecontacts.net/spec/1.0',
1200 'xmlns:ostatus' => 'http://ostatus.org/schema/1.0',
1201 'xmlns:statusnet' => 'http://status.net/schema/api/1/');
1206 $xs->elementStart('entry', $attrs);
1209 $xs->elementStart('source');
1210 $xs->element('id', null, $profile->profileurl);
1211 $xs->element('title', null, $profile->nickname . " - " . common_config('site', 'name'));
1212 $xs->element('link', array('href' => $profile->profileurl));
1213 $user = User::staticGet('id', $profile->id);
1214 if (!empty($user)) {
1215 $atom_feed = common_local_url('ApiTimelineUser',
1216 array('format' => 'atom',
1217 'id' => $profile->nickname));
1218 $xs->element('link', array('rel' => 'self',
1219 'type' => 'application/atom+xml',
1220 'href' => $profile->profileurl));
1221 $xs->element('link', array('rel' => 'license',
1222 'href' => common_config('license', 'url')));
1225 $xs->element('icon', null, $profile->avatarUrl(AVATAR_PROFILE_SIZE));
1226 $xs->element('updated', null, common_date_w3dtf($this->created));
1230 $xs->elementEnd('source');
1233 $xs->element('title', null, common_xml_safe_str($this->content));
1236 $xs->raw($profile->asAtomAuthor($cur));
1237 $xs->raw($profile->asActivityActor());
1240 $xs->element('link', array('rel' => 'alternate',
1241 'type' => 'text/html',
1242 'href' => $this->bestUrl()));
1244 $xs->element('id', null, $this->uri);
1246 $xs->element('published', null, common_date_w3dtf($this->created));
1247 $xs->element('updated', null, common_date_w3dtf($this->created));
1251 $ns = $this->getSource();
1254 if (!empty($ns->name) && !empty($ns->url)) {
1255 $source = '<a href="'
1256 . htmlspecialchars($ns->url)
1257 . '" rel="nofollow">'
1258 . htmlspecialchars($ns->name)
1261 $source = $ns->code;
1265 $noticeInfoAttr = array(
1266 'local_id' => $this->id, // local notice ID (useful to clients for ordering)
1267 'source' => $source, // the client name (source attribution)
1270 $ns = $this->getSource();
1272 if (!empty($ns->url)) {
1273 $noticeInfoAttr['source_link'] = $ns->url;
1278 $noticeInfoAttr['favorite'] = ($cur->hasFave($this)) ? "true" : "false";
1279 $profile = $cur->getProfile();
1280 $noticeInfoAttr['repeated'] = ($profile->hasRepeated($this->id)) ? "true" : "false";
1283 if (!empty($this->repeat_of)) {
1284 $noticeInfoAttr['repeat_of'] = $this->repeat_of;
1287 $xs->element('statusnet:notice_info', $noticeInfoAttr, null);
1289 if ($this->reply_to) {
1290 $reply_notice = Notice::staticGet('id', $this->reply_to);
1291 if (!empty($reply_notice)) {
1292 $xs->element('link', array('rel' => 'related',
1293 'href' => $reply_notice->bestUrl()));
1294 $xs->element('thr:in-reply-to',
1295 array('ref' => $reply_notice->uri,
1296 'href' => $reply_notice->bestUrl()));
1300 if (!empty($this->conversation)) {
1302 $conv = Conversation::staticGet('id', $this->conversation);
1304 if (!empty($conv)) {
1307 'rel' => 'ostatus:conversation',
1308 'href' => $conv->uri
1314 $reply_ids = $this->getReplies();
1316 foreach ($reply_ids as $id) {
1317 $profile = Profile::staticGet('id', $id);
1318 if (!empty($profile)) {
1321 'rel' => 'ostatus:attention',
1322 'href' => $profile->getUri()
1328 $groups = $this->getGroups();
1330 foreach ($groups as $group) {
1333 'rel' => 'ostatus:attention',
1334 'href' => $group->permalink()
1339 if (!empty($this->repeat_of)) {
1340 $repeat = Notice::staticGet('id', $this->repeat_of);
1341 if (!empty($repeat)) {
1344 array('ref' => $repeat->uri, 'href' => $repeat->bestUrl())
1351 array('type' => 'html'),
1352 common_xml_safe_str($this->rendered)
1355 $tag = new Notice_tag();
1356 $tag->notice_id = $this->id;
1358 while ($tag->fetch()) {
1359 $xs->element('category', array('term' => $tag->tag));
1365 $attachments = $this->attachments();
1367 foreach($attachments as $attachment){
1368 $enclosure=$attachment->getEnclosure();
1370 $attributes = array('rel'=>'enclosure','href'=>$enclosure->url,'type'=>$enclosure->mimetype,'length'=>$enclosure->size);
1371 if($enclosure->title){
1372 $attributes['title']=$enclosure->title;
1374 $xs->element('link', $attributes, null);
1379 if (!empty($this->lat) && !empty($this->lon)) {
1380 $xs->element('georss:point', null, $this->lat . ' ' . $this->lon);
1383 $xs->elementEnd('entry');
1385 return $xs->getString();
1389 * Returns an XML string fragment with a reference to a notice as an
1390 * Activity Streams noun object with the given element type.
1392 * Assumes that 'activity' namespace has been previously defined.
1394 * @param string $element one of 'subject', 'object', 'target'
1397 function asActivityNoun($element)
1399 $noun = ActivityObject::fromNotice($this);
1400 return $noun->asString('activity:' . $element);
1405 if (!empty($this->url)) {
1407 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
1410 return common_local_url('shownotice',
1411 array('notice' => $this->id));
1415 function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $max_id=0)
1417 $cache = common_memcache();
1419 if (empty($cache) ||
1420 $since_id != 0 || $max_id != 0 ||
1422 ($offset + $limit) > NOTICE_CACHE_WINDOW) {
1423 return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id,
1427 $idkey = common_cache_key($cachekey);
1429 $idstr = $cache->get($idkey);
1431 if ($idstr !== false) {
1432 // Cache hit! Woohoo!
1433 $window = explode(',', $idstr);
1434 $ids = array_slice($window, $offset, $limit);
1438 $laststr = $cache->get($idkey.';last');
1440 if ($laststr !== false) {
1441 $window = explode(',', $laststr);
1442 $last_id = $window[0];
1443 $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1444 $last_id, 0, null)));
1446 $new_window = array_merge($new_ids, $window);
1448 $new_windowstr = implode(',', $new_window);
1450 $result = $cache->set($idkey, $new_windowstr);
1451 $result = $cache->set($idkey . ';last', $new_windowstr);
1453 $ids = array_slice($new_window, $offset, $limit);
1458 $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW,
1461 $windowstr = implode(',', $window);
1463 $result = $cache->set($idkey, $windowstr);
1464 $result = $cache->set($idkey . ';last', $windowstr);
1466 $ids = array_slice($window, $offset, $limit);
1472 * Determine which notice, if any, a new notice is in reply to.
1474 * For conversation tracking, we try to see where this notice fits
1475 * in the tree. Rough algorithm is:
1477 * if (reply_to is set and valid) {
1479 * } else if ((source not API or Web) and (content starts with "T NAME" or "@name ")) {
1480 * return ID of last notice by initial @name in content;
1483 * Note that all @nickname instances will still be used to save "reply" records,
1484 * so the notice shows up in the mentioned users' "replies" tab.
1486 * @param integer $reply_to ID passed in by Web or API
1487 * @param integer $profile_id ID of author
1488 * @param string $source Source tag, like 'web' or 'gwibber'
1489 * @param string $content Final notice content
1491 * @return integer ID of replied-to notice, or null for not a reply.
1494 static function getReplyTo($reply_to, $profile_id, $source, $content)
1496 static $lb = array('xmpp', 'mail', 'sms', 'omb');
1498 // If $reply_to is specified, we check that it exists, and then
1499 // return it if it does
1501 if (!empty($reply_to)) {
1502 $reply_notice = Notice::staticGet('id', $reply_to);
1503 if (!empty($reply_notice)) {
1508 // If it's not a "low bandwidth" source (one where you can't set
1509 // a reply_to argument), we return. This is mostly web and API
1512 if (!in_array($source, $lb)) {
1516 // Is there an initial @ or T?
1518 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $content, $match) ||
1519 preg_match('/^@([a-z0-9]{1,64})\s+/', $content, $match)) {
1520 $nickname = common_canonical_nickname($match[1]);
1525 // Figure out who that is.
1527 $sender = Profile::staticGet('id', $profile_id);
1528 if (empty($sender)) {
1532 $recipient = common_relative_profile($sender, $nickname, common_sql_now());
1534 if (empty($recipient)) {
1538 // Get their last notice
1540 $last = $recipient->getCurrentNotice();
1542 if (!empty($last)) {
1547 static function maxContent()
1549 $contentlimit = common_config('notice', 'contentlimit');
1550 // null => use global limit (distinct from 0!)
1551 if (is_null($contentlimit)) {
1552 $contentlimit = common_config('site', 'textlimit');
1554 return $contentlimit;
1557 static function contentTooLong($content)
1559 $contentlimit = self::maxContent();
1560 return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
1563 function getLocation()
1567 if (!empty($this->location_id) && !empty($this->location_ns)) {
1568 $location = Location::fromId($this->location_id, $this->location_ns);
1571 if (is_null($location)) { // no ID, or Location::fromId() failed
1572 if (!empty($this->lat) && !empty($this->lon)) {
1573 $location = Location::fromLatLon($this->lat, $this->lon);
1580 function repeat($repeater_id, $source)
1582 $author = Profile::staticGet('id', $this->profile_id);
1584 // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'.
1585 // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice.
1586 $content = sprintf(_('RT @%1$s %2$s'),
1590 $maxlen = common_config('site', 'textlimit');
1591 if ($maxlen > 0 && mb_strlen($content) > $maxlen) {
1592 // Web interface and current Twitter API clients will
1593 // pull the original notice's text, but some older
1594 // clients and RSS/Atom feeds will see this trimmed text.
1596 // Unfortunately this is likely to lose tags or URLs
1597 // at the end of long notices.
1598 $content = mb_substr($content, 0, $maxlen - 4) . ' ...';
1601 return self::saveNew($repeater_id, $content, $source,
1602 array('repeat_of' => $this->id));
1605 // These are supposed to be in chron order!
1607 function repeatStream($limit=100)
1609 $cache = common_memcache();
1611 if (empty($cache)) {
1612 $ids = $this->_repeatStreamDirect($limit);
1614 $idstr = $cache->get(common_cache_key('notice:repeats:'.$this->id));
1615 if ($idstr !== false) {
1616 $ids = explode(',', $idstr);
1618 $ids = $this->_repeatStreamDirect(100);
1619 $cache->set(common_cache_key('notice:repeats:'.$this->id), implode(',', $ids));
1622 // We do a max of 100, so slice down to limit
1623 $ids = array_slice($ids, 0, $limit);
1627 return Notice::getStreamByIds($ids);
1630 function _repeatStreamDirect($limit)
1632 $notice = new Notice();
1634 $notice->selectAdd(); // clears it
1635 $notice->selectAdd('id');
1637 $notice->repeat_of = $this->id;
1639 $notice->orderBy('created'); // NB: asc!
1641 if (!is_null($offset)) {
1642 $notice->limit($offset, $limit);
1647 if ($notice->find()) {
1648 while ($notice->fetch()) {
1649 $ids[] = $notice->id;
1659 function locationOptions($lat, $lon, $location_id, $location_ns, $profile = null)
1663 if (!empty($location_id) && !empty($location_ns)) {
1665 $options['location_id'] = $location_id;
1666 $options['location_ns'] = $location_ns;
1668 $location = Location::fromId($location_id, $location_ns);
1670 if (!empty($location)) {
1671 $options['lat'] = $location->lat;
1672 $options['lon'] = $location->lon;
1675 } else if (!empty($lat) && !empty($lon)) {
1677 $options['lat'] = $lat;
1678 $options['lon'] = $lon;
1680 $location = Location::fromLatLon($lat, $lon);
1682 if (!empty($location)) {
1683 $options['location_id'] = $location->location_id;
1684 $options['location_ns'] = $location->location_ns;
1686 } else if (!empty($profile)) {
1688 if (isset($profile->lat) && isset($profile->lon)) {
1689 $options['lat'] = $profile->lat;
1690 $options['lon'] = $profile->lon;
1693 if (isset($profile->location_id) && isset($profile->location_ns)) {
1694 $options['location_id'] = $profile->location_id;
1695 $options['location_ns'] = $profile->location_ns;
1702 function clearReplies()
1704 $replyNotice = new Notice();
1705 $replyNotice->reply_to = $this->id;
1707 //Null any notices that are replies to this notice
1709 if ($replyNotice->find()) {
1710 while ($replyNotice->fetch()) {
1711 $orig = clone($replyNotice);
1712 $replyNotice->reply_to = null;
1713 $replyNotice->update($orig);
1719 $reply = new Reply();
1720 $reply->notice_id = $this->id;
1722 if ($reply->find()) {
1723 while($reply->fetch()) {
1724 self::blow('reply:stream:%d', $reply->profile_id);
1732 function clearRepeats()
1734 $repeatNotice = new Notice();
1735 $repeatNotice->repeat_of = $this->id;
1737 //Null any notices that are repeats of this notice
1739 if ($repeatNotice->find()) {
1740 while ($repeatNotice->fetch()) {
1741 $orig = clone($repeatNotice);
1742 $repeatNotice->repeat_of = null;
1743 $repeatNotice->update($orig);
1748 function clearFaves()
1751 $fave->notice_id = $this->id;
1753 if ($fave->find()) {
1754 while ($fave->fetch()) {
1755 self::blow('fave:ids_by_user_own:%d', $fave->user_id);
1756 self::blow('fave:ids_by_user_own:%d;last', $fave->user_id);
1757 self::blow('fave:ids_by_user:%d', $fave->user_id);
1758 self::blow('fave:ids_by_user:%d;last', $fave->user_id);
1766 function clearTags()
1768 $tag = new Notice_tag();
1769 $tag->notice_id = $this->id;
1772 while ($tag->fetch()) {
1773 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, common_keyize($tag->tag));
1774 self::blow('profile:notice_ids_tagged:%d:%s;last', $this->profile_id, common_keyize($tag->tag));
1775 self::blow('notice_tag:notice_ids:%s', common_keyize($tag->tag));
1776 self::blow('notice_tag:notice_ids:%s;last', common_keyize($tag->tag));
1784 function clearGroupInboxes()
1786 $gi = new Group_inbox();
1788 $gi->notice_id = $this->id;
1791 while ($gi->fetch()) {
1792 self::blow('user_group:notice_ids:%d', $gi->group_id);
1800 function distribute()
1802 // We always insert for the author so they don't
1805 $user = User::staticGet('id', $this->profile_id);
1806 if (!empty($user)) {
1807 Inbox::insertNotice($user->id, $this->id);
1810 if (common_config('queue', 'inboxes')) {
1811 // If there's a failure, we want to _force_
1812 // distribution at this point.
1814 $qm = QueueManager::get();
1815 $qm->enqueue($this, 'distrib');
1816 } catch (Exception $e) {
1817 // If the exception isn't transient, this
1818 // may throw more exceptions as DQH does
1819 // its own enqueueing. So, we ignore them!
1821 $handler = new DistribQueueHandler();
1822 $handler->handle($this);
1823 } catch (Exception $e) {
1824 common_log(LOG_ERR, "emergency redistribution resulted in " . $e->getMessage());
1826 // Re-throw so somebody smarter can handle it.
1830 $handler = new DistribQueueHandler();
1831 $handler->handle($this);
1837 $result = parent::insert();
1840 // Profile::hasRepeated() abuses pkeyGet(), so we
1841 // have to clear manually
1842 if (!empty($this->repeat_of)) {
1843 $c = self::memcache();
1845 $ck = self::multicacheKey('Notice',
1846 array('profile_id' => $this->profile_id,
1847 'repeat_of' => $this->repeat_of));
1857 * Get the source of the notice
1859 * @return Notice_source $ns A notice source object. 'code' is the only attribute
1860 * guaranteed to be populated.
1862 function getSource()
1864 $ns = new Notice_source();
1865 if (!empty($this->source)) {
1866 switch ($this->source) {
1873 $ns->code = $this->source;
1876 $ns = Notice_source::staticGet($this->source);
1878 $ns = new Notice_source();
1879 $ns->code = $this->source;
1880 $app = Oauth_application::staticGet('name', $this->source);
1882 $ns->name = $app->name;
1883 $ns->url = $app->source_url;
1893 * Determine whether the notice was locally created
1895 * @return boolean locality
1898 public function isLocal()
1900 return ($this->is_local == Notice::LOCAL_PUBLIC ||
1901 $this->is_local == Notice::LOCAL_NONPUBLIC);