3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008-2011 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 * @author Mikael Nordfeldth <mmn@hethane.se>
33 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
34 * @license GNU Affero General Public License http://www.gnu.org/licenses/
37 if (!defined('GNUSOCIAL')) { exit(1); }
40 * Table Definition for notice
43 /* We keep 200 notices, the max number of notices available per API request,
44 * in the memcached cache. */
46 define('NOTICE_CACHE_WINDOW', CachingNoticeStream::CACHE_WINDOW);
48 define('MAX_BOXCARS', 128);
50 class Notice extends Managed_DataObject
53 /* the code below is auto generated do not remove the above tag */
55 public $__table = 'notice'; // table name
56 public $id; // int(4) primary_key not_null
57 public $profile_id; // int(4) multiple_key not_null
58 public $uri; // varchar(255) unique_key
59 public $content; // text
60 public $rendered; // text
61 public $url; // varchar(255)
62 public $created; // datetime multiple_key not_null default_0000-00-00%2000%3A00%3A00
63 public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
64 public $reply_to; // int(4)
65 public $is_local; // int(4)
66 public $source; // varchar(32)
67 public $conversation; // int(4)
68 public $lat; // decimal(10,7)
69 public $lon; // decimal(10,7)
70 public $location_id; // int(4)
71 public $location_ns; // int(4)
72 public $repeat_of; // int(4)
73 public $verb; // varchar(255)
74 public $object_type; // varchar(255)
75 public $scope; // int(4)
77 /* the code above is auto generated do not remove the tag below */
80 public static function schemaDef()
84 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
85 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'who made the update'),
86 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universally unique identifier, usually a tag URI'),
87 'content' => array('type' => 'text', 'description' => 'update content', 'collate' => 'utf8_general_ci'),
88 'rendered' => array('type' => 'text', 'description' => 'HTML version of the content'),
89 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL of any attachment (image, video, bookmark, whatever)'),
90 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
91 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
92 'reply_to' => array('type' => 'int', 'description' => 'notice replied to (usually a guess)'),
93 'is_local' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'notice was generated by a user'),
94 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'),
95 'conversation' => array('type' => 'int', 'description' => 'id of root notice in this conversation'),
96 'lat' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude'),
97 'lon' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'),
98 'location_id' => array('type' => 'int', 'description' => 'location id if possible'),
99 'location_ns' => array('type' => 'int', 'description' => 'namespace for location'),
100 'repeat_of' => array('type' => 'int', 'description' => 'notice this is a repeat of'),
101 'object_type' => array('type' => 'varchar', 'length' => 255, 'description' => 'URI representing activity streams object type', 'default' => 'http://activitystrea.ms/schema/1.0/note'),
102 'verb' => array('type' => 'varchar', 'length' => 255, 'description' => 'URI representing activity streams verb', 'default' => 'http://activitystrea.ms/schema/1.0/post'),
103 'scope' => array('type' => 'int',
104 'description' => 'bit map for distribution scope; 0 = everywhere; 1 = this server only; 2 = addressees; 4 = followers; null = default'),
106 'primary key' => array('id'),
107 'unique keys' => array(
108 'notice_uri_key' => array('uri'),
110 'foreign keys' => array(
111 'notice_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
112 'notice_reply_to_fkey' => array('notice', array('reply_to' => 'id')),
113 'notice_conversation_fkey' => array('conversation', array('conversation' => 'id')), # note... used to refer to notice.id
114 'notice_repeat_of_fkey' => array('notice', array('repeat_of' => 'id')), # @fixme: what about repeats of deleted notices?
117 'notice_created_id_is_local_idx' => array('created', 'id', 'is_local'),
118 'notice_profile_id_idx' => array('profile_id', 'created', 'id'),
119 'notice_repeat_of_created_id_idx' => array('repeat_of', 'created', 'id'),
120 'notice_conversation_created_id_idx' => array('conversation', 'created', 'id'),
121 'notice_replyto_idx' => array('reply_to')
125 if (common_config('search', 'type') == 'fulltext') {
126 $def['fulltext indexes'] = array('content' => array('content'));
133 const LOCAL_PUBLIC = 1;
135 const LOCAL_NONPUBLIC = -1;
138 const PUBLIC_SCOPE = 0; // Useful fake constant
139 const SITE_SCOPE = 1;
140 const ADDRESSEE_SCOPE = 2;
141 const GROUP_SCOPE = 4;
142 const FOLLOWER_SCOPE = 8;
144 protected $_profile = array();
147 * Will always return a profile, if anything fails it will
148 * (through _setProfile) throw a NoProfileException.
150 public function getProfile()
152 if (!isset($this->_profile[$this->profile_id])) {
153 // We could've sent getKV directly to _setProfile, but occasionally we get
154 // a "false" (instead of null), likely because it indicates a cache miss.
155 $profile = Profile::getKV('id', $this->profile_id);
156 $this->_setProfile($profile instanceof Profile ? $profile : null);
158 return $this->_profile[$this->profile_id];
161 public function _setProfile(Profile $profile=null)
163 if (!$profile instanceof Profile) {
164 throw new NoProfileException($this->profile_id);
166 $this->_profile[$this->profile_id] = $profile;
169 function delete($useWhere=false)
171 // For auditing purposes, save a record that the notice
174 // @fixme we have some cases where things get re-run and so the
176 $deleted = Deleted_notice::getKV('id', $this->id);
178 if (!$deleted instanceof Deleted_notice) {
179 $deleted = Deleted_notice::getKV('uri', $this->uri);
182 if (!$deleted instanceof Deleted_notice) {
183 $deleted = new Deleted_notice();
185 $deleted->id = $this->id;
186 $deleted->profile_id = $this->profile_id;
187 $deleted->uri = $this->uri;
188 $deleted->created = $this->created;
189 $deleted->deleted = common_sql_now();
194 if (Event::handle('NoticeDeleteRelated', array($this))) {
196 // Clear related records
198 $this->clearReplies();
199 $this->clearRepeats();
201 $this->clearGroupInboxes();
203 $this->clearAttentions();
205 // NOTE: we don't clear queue items
208 $result = parent::delete($useWhere);
210 $this->blowOnDelete();
214 public function getUri()
220 * Get a Notice object by URI. Will call external plugins for help
221 * using the event StartGetNoticeFromURI.
223 * @param string $uri A unique identifier for a resource (notice in this case)
225 static function fromUri($uri)
229 if (Event::handle('StartGetNoticeFromUri', array($uri, &$notice))) {
230 $notice = Notice::getKV('uri', $uri);
231 Event::handle('EndGetNoticeFromUri', array($uri, $notice));
234 if (!$notice instanceof Notice) {
235 throw new UnknownUriException($uri);
242 * @param $root boolean If true, link to just the conversation root.
244 * @return URL to conversation
246 public function getConversationUrl($anchor=true)
248 return Conversation::getUrlFromNotice($this, $anchor);
252 * Get the local representation URL of this notice.
254 public function getLocalUrl()
256 return common_local_url('shownotice', array('notice' => $this->id), null, null, false);
259 public function getTitle()
262 if (Event::handle('GetNoticeTitle', array($this, &$title))) {
263 // TRANS: Title of a notice posted without a title value.
264 // TRANS: %1$s is a user name, %2$s is the notice creation date/time.
265 $title = sprintf(_('%1$s\'s status on %2$s'),
266 $this->getProfile()->getFancyName(),
267 common_exact_date($this->created));
273 * Get the original representation URL of this notice.
275 public function getUrl()
277 // The risk is we start having empty urls and non-http uris...
278 // and we can't really handle any other protocol right now.
280 case common_valid_http_url($this->url): // should we allow non-http/https URLs?
282 case $this->isLocal():
283 // let's generate a valid link to our locally available notice on demand
284 return common_local_url('shownotice', array('notice' => $this->id), null, null, false);
285 case common_valid_http_url($this->uri):
288 common_debug('No URL available for notice: id='.$this->id);
289 throw new InvalidUrlException($this->url);
293 public function get_object_type($canonical=false) {
295 ? ActivityObject::canonicalType($this->object_type)
296 : $this->object_type;
299 public static function getByUri($uri)
301 $notice = new Notice();
303 if (!$notice->find(true)) {
304 throw new NoResultException($notice);
310 * Extract #hashtags from this notice's content and save them to the database.
314 /* extract all #hastags */
315 $count = preg_match_all('/(?:^|\s)#([\pL\pN_\-\.]{1,64})/u', strtolower($this->content), $match);
320 /* Add them to the database */
321 return $this->saveKnownTags($match[1]);
325 * Record the given set of hash tags in the db for this notice.
326 * Given tag strings will be normalized and checked for dupes.
328 function saveKnownTags($hashtags)
330 //turn each into their canonical tag
331 //this is needed to remove dupes before saving e.g. #hash.tag = #hashtag
332 for($i=0; $i<count($hashtags); $i++) {
333 /* elide characters we don't want in the tag */
334 $hashtags[$i] = common_canonical_tag($hashtags[$i]);
337 foreach(array_unique($hashtags) as $hashtag) {
338 $this->saveTag($hashtag);
339 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, $hashtag);
345 * Record a single hash tag as associated with this notice.
346 * Tag format and uniqueness must be validated by caller.
348 function saveTag($hashtag)
350 $tag = new Notice_tag();
351 $tag->notice_id = $this->id;
352 $tag->tag = $hashtag;
353 $tag->created = $this->created;
354 $id = $tag->insert();
357 // TRANS: Server exception. %s are the error details.
358 throw new ServerException(sprintf(_('Database error inserting hashtag: %s.'),
359 $last_error->message));
363 // if it's saved, blow its cache
364 $tag->blowCache(false);
368 * Save a new notice and push it out to subscribers' inboxes.
369 * Poster's permissions are checked before sending.
371 * @param int $profile_id Profile ID of the poster
372 * @param string $content source message text; links may be shortened
373 * per current user's preference
374 * @param string $source source key ('web', 'api', etc)
375 * @param array $options Associative array of optional properties:
376 * string 'created' timestamp of notice; defaults to now
377 * int 'is_local' source/gateway ID, one of:
378 * Notice::LOCAL_PUBLIC - Local, ok to appear in public timeline
379 * Notice::REMOTE - Sent from a remote service;
380 * hide from public timeline but show in
381 * local "and friends" timelines
382 * Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
383 * Notice::GATEWAY - From another non-OStatus service;
384 * will not appear in public views
385 * float 'lat' decimal latitude for geolocation
386 * float 'lon' decimal longitude for geolocation
387 * int 'location_id' geoname identifier
388 * int 'location_ns' geoname namespace to interpret location_id
389 * int 'reply_to'; notice ID this is a reply to
390 * int 'repeat_of'; notice ID this is a repeat of
391 * string 'uri' unique ID for notice; a unique tag uri (can be url or anything too)
392 * string 'url' permalink to notice; defaults to local notice URL
393 * string 'rendered' rendered HTML version of content
394 * array 'replies' list of profile URIs for reply delivery in
395 * place of extracting @-replies from content.
396 * array 'groups' list of group IDs to deliver to, in place of
397 * extracting ! tags from content
398 * array 'tags' list of hashtag strings to save with the notice
399 * in place of extracting # tags from content
400 * array 'urls' list of attached/referred URLs to save with the
401 * notice in place of extracting links from content
402 * boolean 'distribute' whether to distribute the notice, default true
403 * string 'object_type' URL of the associated object type (default ActivityObject::NOTE)
404 * string 'verb' URL of the associated verb (default ActivityVerb::POST)
405 * int 'scope' Scope bitmask; default to SITE_SCOPE on private sites, 0 otherwise
407 * @fixme tag override
410 * @throws ClientException
412 static function saveNew($profile_id, $content, $source, array $options=null) {
413 $defaults = array('uri' => null,
418 'distribute' => true,
419 'object_type' => null,
422 if (!empty($options) && is_array($options)) {
423 $options = array_merge($defaults, $options);
429 if (!isset($is_local)) {
430 $is_local = Notice::LOCAL_PUBLIC;
433 $profile = Profile::getKV('id', $profile_id);
434 if (!$profile instanceof Profile) {
435 // TRANS: Client exception thrown when trying to save a notice for an unknown user.
436 throw new ClientException(_('Problem saving notice. Unknown user.'));
439 $user = User::getKV('id', $profile_id);
440 if ($user instanceof User) {
441 // Use the local user's shortening preferences, if applicable.
442 $final = $user->shortenLinks($content);
444 $final = common_shorten_links($content);
447 if (Notice::contentTooLong($final)) {
448 // TRANS: Client exception thrown if a notice contains too many characters.
449 throw new ClientException(_('Problem saving notice. Too long.'));
452 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
453 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
454 // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
455 throw new ClientException(_('Too many notices too fast; take a breather '.
456 'and post again in a few minutes.'));
459 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
460 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
461 // TRANS: Client exception thrown when a user tries to post too many duplicate notices in a given time frame.
462 throw new ClientException(_('Too many duplicate messages too quickly;'.
463 ' take a breather and post again in a few minutes.'));
466 if (!$profile->hasRight(Right::NEWNOTICE)) {
467 common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
469 // TRANS: Client exception thrown when a user tries to post while being banned.
470 throw new ClientException(_('You are banned from posting notices on this site.'), 403);
473 $notice = new Notice();
474 $notice->profile_id = $profile_id;
476 $autosource = common_config('public', 'autosource');
478 // Sandboxed are non-false, but not 1, either
480 if (!$profile->hasRight(Right::PUBLICNOTICE) ||
481 ($source && $autosource && in_array($source, $autosource))) {
482 $notice->is_local = Notice::LOCAL_NONPUBLIC;
484 $notice->is_local = $is_local;
487 if (!empty($created)) {
488 $notice->created = $created;
490 $notice->created = common_sql_now();
493 if (!$notice->isLocal()) {
494 // Only do these checks for non-local notices. Local notices will generate these values later.
495 if (!common_valid_http_url($url)) {
496 common_debug('Bad notice URL: ['.$url.'], URI: ['.$uri.']. Cannot link back to original! This is normal for shared notices etc.');
499 throw new ServerException('No URI for remote notice. Cannot accept that.');
503 $notice->content = $final;
505 $notice->source = $source;
509 // Get the groups here so we can figure out replies and such
510 if (!isset($groups)) {
511 $groups = User_group::idsFromText($notice->content, $profile);
516 // Handle repeat case
518 if (isset($repeat_of)) {
520 // Check for a private one
522 $repeat = Notice::getKV('id', $repeat_of);
524 if (!($repeat instanceof Notice)) {
525 // TRANS: Client exception thrown in notice when trying to repeat a missing or deleted notice.
526 throw new ClientException(_('Cannot repeat; original notice is missing or deleted.'));
529 if ($profile->id == $repeat->profile_id) {
530 // TRANS: Client error displayed when trying to repeat an own notice.
531 throw new ClientException(_('You cannot repeat your own notice.'));
534 if ($repeat->scope != Notice::SITE_SCOPE &&
535 $repeat->scope != Notice::PUBLIC_SCOPE) {
536 // TRANS: Client error displayed when trying to repeat a non-public notice.
537 throw new ClientException(_('Cannot repeat a private notice.'), 403);
540 if (!$repeat->inScope($profile)) {
541 // The generic checks above should cover this, but let's be sure!
542 // TRANS: Client error displayed when trying to repeat a notice you cannot access.
543 throw new ClientException(_('Cannot repeat a notice you cannot read.'), 403);
546 if ($profile->hasRepeated($repeat)) {
547 // TRANS: Client error displayed when trying to repeat an already repeated notice.
548 throw new ClientException(_('You already repeated that notice.'));
551 $notice->repeat_of = $repeat->id;
552 $notice->conversation = $repeat->conversation;
556 // If $reply_to is specified, we check that it exists, and then
557 // return it if it does
558 if (!empty($reply_to)) {
559 $reply = Notice::getKV('id', $reply_to);
560 } elseif (in_array($source, array('xmpp', 'mail', 'sms'))) {
561 // If the source lacks capability of sending the "reply_to"
562 // metadata, let's try to find an inline replyto-reference.
563 $reply = self::getInlineReplyTo($profile, $final);
566 if ($reply instanceof Notice) {
567 if (!$reply->inScope($profile)) {
568 // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
569 // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
570 throw new ClientException(sprintf(_('%1$s has no access to notice %2$d.'),
571 $profile->nickname, $reply->id), 403);
574 // If it's a repeat, the reply_to should be to the original
575 if (!empty($reply->repeat_of)) {
576 $notice->reply_to = $reply->repeat_of;
578 $notice->reply_to = $reply->id;
580 // But the conversation ought to be the same :)
581 $notice->conversation = $reply->conversation;
583 // If the original is private to a group, and notice has
584 // no group specified, make it to the same group(s)
586 if (empty($groups) && ($reply->scope & Notice::GROUP_SCOPE)) {
588 $replyGroups = $reply->getGroups();
589 foreach ($replyGroups as $group) {
590 if ($profile->isMember($group)) {
591 $groups[] = $group->id;
600 if (!empty($lat) && !empty($lon)) {
605 if (!empty($location_ns) && !empty($location_id)) {
606 $notice->location_id = $location_id;
607 $notice->location_ns = $location_ns;
610 if (!empty($rendered)) {
611 $notice->rendered = $rendered;
613 $notice->rendered = common_render_content($final, $notice);
617 if (!empty($notice->repeat_of)) {
618 $notice->verb = ActivityVerb::SHARE;
619 $notice->object_type = ActivityObject::ACTIVITY;
621 $notice->verb = ActivityVerb::POST;
624 $notice->verb = $verb;
627 if (empty($object_type)) {
628 $notice->object_type = (empty($notice->reply_to)) ? ActivityObject::NOTE : ActivityObject::COMMENT;
630 $notice->object_type = $object_type;
633 if (is_null($scope) && $reply instanceof Notice) {
634 $notice->scope = $reply->scope;
636 $notice->scope = $scope;
639 $notice->scope = self::figureOutScope($profile, $groups, $notice->scope);
641 if (Event::handle('StartNoticeSave', array(&$notice))) {
643 // XXX: some of these functions write to the DB
645 $id = $notice->insert();
648 common_log_db_error($notice, 'INSERT', __FILE__);
649 // TRANS: Server exception thrown when a notice cannot be saved.
650 throw new ServerException(_('Problem saving notice.'));
653 // Update ID-dependent columns: URI, conversation
655 $orig = clone($notice);
659 // We can only get here if it's a local notice, since remote notices
660 // should've bailed out earlier due to lacking a URI.
661 if (empty($notice->uri)) {
662 $notice->uri = sprintf('%s%s=%d:%s=%s',
664 'noticeId', $notice->id,
665 'objectType', $notice->get_object_type(true));
669 // If it's not part of a conversation, it's
670 // the beginning of a new conversation.
672 if (empty($notice->conversation)) {
673 $conv = Conversation::create($notice);
674 $notice->conversation = $conv->id;
679 if ($notice->update($orig) === false) {
680 common_log_db_error($notice, 'UPDATE', __FILE__);
681 // TRANS: Server exception thrown when a notice cannot be updated.
682 throw new ServerException(_('Problem saving notice.'));
688 // Clear the cache for subscribed users, so they'll update at next request
689 // XXX: someone clever could prepend instead of clearing the cache
691 $notice->blowOnInsert();
693 // Save per-notice metadata...
695 if (isset($replies)) {
696 $notice->saveKnownReplies($replies);
698 $notice->saveReplies();
702 $notice->saveKnownTags($tags);
707 // Note: groups may save tags, so must be run after tags are saved
708 // to avoid errors on duplicates.
709 // Note: groups should always be set.
711 $notice->saveKnownGroups($groups);
714 $notice->saveKnownUrls($urls);
720 // Prepare inbox delivery, may be queued to background.
721 $notice->distribute();
727 static function saveActivity(Activity $act, Profile $actor, array $options=array())
729 // First check if we're going to let this Activity through from the specific actor
730 if (!$actor->hasRight(Right::NEWNOTICE)) {
731 common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $actor->getNickname());
733 // TRANS: Client exception thrown when a user tries to post while being banned.
734 throw new ClientException(_m('You are banned from posting notices on this site.'), 403);
736 if (common_config('throttle', 'enabled') && !self::checkEditThrottle($actor->id)) {
737 common_log(LOG_WARNING, 'Excessive posting by profile #' . $actor->id . '; throttled.');
738 // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
739 throw new ClientException(_m('Too many notices too fast; take a breather '.
740 'and post again in a few minutes.'));
743 /* This interferes with stuff like Favorites from old StatusNet installations (first object in objects is the favored notice)
744 // Get ActivityObject properties
745 $actobj = count($act->objects)==1 ? $act->objects[0] : null;
746 if (!is_null($actobj) && $actobj->id) {
747 $options['uri'] = $actobj->id;
749 $options['url'] = $actobj->link;
750 } elseif ($act->link) {
751 $options['url'] = $act->link;
752 } elseif (preg_match('!^https?://!', $actobj->id)) {
753 $options['url'] = $actobj->id;
757 $options['uri'] = $act->id;
758 $options['url'] = $act->link;
764 'is_local' => self::LOCAL_PUBLIC,
765 'mentions' => array(),
769 'source' => 'unknown',
774 'distribute' => true);
776 // options will have default values when nothing has been supplied
777 $options = array_merge($defaults, $options);
778 foreach (array_keys($defaults) as $key) {
779 // Only convert the keynames we specify ourselves from 'defaults' array into variables
780 $$key = $options[$key];
782 extract($options, EXTR_SKIP);
784 $stored = new Notice();
787 if ($stored->find()) {
788 common_debug('cannot create duplicate Notice URI: '.$stored->uri);
789 throw new Exception('Notice URI already exists');
793 $stored->profile_id = $actor->id;
794 $stored->source = $source;
797 $stored->verb = $act->verb;
799 $autosource = common_config('public', 'autosource');
801 // Sandboxed are non-false, but not 1, either
802 if (!$actor->hasRight(Right::PUBLICNOTICE) ||
803 ($source && $autosource && in_array($source, $autosource))) {
804 $stored->is_local = Notice::LOCAL_NONPUBLIC;
807 // Maybe a missing act-time should be fatal if the actor is not local?
808 if (!empty($act->time)) {
809 $stored->created = common_sql_date($act->time);
811 $stored->created = common_sql_now();
815 if ($act->context instanceof ActivityContext && !empty($act->context->replyToID)) {
816 $reply = self::getKV('uri', $act->context->replyToID);
818 if (!$reply instanceof Notice && $act->target instanceof ActivityObject) {
819 $reply = self::getKV('uri', $act->target->id);
822 if ($reply instanceof Notice) {
823 if (!$reply->inScope($actor)) {
824 // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
825 // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
826 throw new ClientException(sprintf(_m('%1$s has no right to reply to notice %2$d.'), $actor->getNickname(), $reply->id), 403);
829 $stored->reply_to = $reply->id;
830 $stored->conversation = $reply->conversation;
832 // If the original is private to a group, and notice has no group specified,
833 // make it to the same group(s)
834 if (empty($groups) && ($reply->scope & Notice::GROUP_SCOPE)) {
836 $replyGroups = $reply->getGroups();
837 foreach ($replyGroups as $group) {
838 if ($actor->isMember($group)) {
839 $groups[] = $group->id;
844 if (is_null($scope)) {
845 $scope = $reply->scope;
849 if ($act->context instanceof ActivityContext) {
850 $location = $act->context->location;
852 $stored->lat = $location->lat;
853 $stored->lon = $location->lon;
854 if ($location->location_id) {
855 $stored->location_ns = $location->location_ns;
856 $stored->location_id = $location->location_id;
860 $act->context = new ActivityContext();
863 $stored->scope = self::figureOutScope($actor, $groups, $scope);
865 foreach ($act->categories as $cat) {
867 $term = common_canonical_tag($cat->term);
874 foreach ($act->enclosures as $href) {
875 // @todo FIXME: Save these locally or....?
879 if (Event::handle('StartNoticeSave', array(&$stored))) {
880 // XXX: some of these functions write to the DB
883 $stored->insert(); // throws exception on error
886 Event::handle('StoreActivityObject', array($act, $stored, $options, &$object));
887 if (empty($object)) {
888 throw new ServerException('No object from StoreActivityObject '.$stored->uri . ': '.$act->asString());
890 $orig = clone($stored);
891 $stored->object_type = ActivityUtils::resolveUri($object->getObjectType(), true);
892 $stored->update($orig);
893 } catch (Exception $e) {
894 if (empty($stored->id)) {
895 common_debug('Failed to save stored object entry in database ('.$e->getMessage().')');
897 common_debug('Failed to store activity object in database ('.$e->getMessage().'), deleting notice id '.$stored->id);
904 common_debug(get_called_class().' looking up mentions metadata etc.');
905 // Save per-notice metadata...
909 // This event lets plugins filter out non-local recipients (attentions we don't care about)
910 // Used primarily for OStatus (and if we don't federate, all attentions would be local anyway)
911 Event::handle('GetLocalAttentions', array($actor, $act->context->attention, &$mentions, &$groups));
913 if (!empty($mentions)) {
914 $stored->saveKnownReplies($mentions);
916 $stored->saveReplies();
920 $stored->saveKnownTags($tags);
925 // Note: groups may save tags, so must be run after tags are saved
926 // to avoid errors on duplicates.
927 // Note: groups should always be set.
929 $stored->saveKnownGroups($groups);
932 $stored->saveKnownUrls($urls);
938 // Prepare inbox delivery, may be queued to background.
939 $stored->distribute();
942 common_debug(get_called_class().' returning stored activity - success!');
947 static public function figureOutScope(Profile $actor, array $groups, $scope=null) {
948 if (is_null($scope)) {
949 $scope = self::defaultScope();
952 // For private streams
954 $user = $actor->getUser();
955 // FIXME: We can't do bit comparison with == (Legacy StatusNet thing. Let's keep it for now.)
956 if ($user->private_stream && ($scope == Notice::PUBLIC_SCOPE || $scope == Notice::SITE_SCOPE)) {
957 $scope |= Notice::FOLLOWER_SCOPE;
959 } catch (NoSuchUserException $e) {
960 // TODO: Not a local user, so we don't know about scope preferences... yet!
963 // Force the scope for private groups
964 foreach ($groups as $group_id) {
965 $group = User_group::staticGet('id', $group_id);
966 if ($group instanceof User_group) {
967 if ($group->force_scope) {
968 $scope |= Notice::GROUP_SCOPE;
977 function blowOnInsert($conversation = false)
979 $this->blowStream('profile:notice_ids:%d', $this->profile_id);
981 if ($this->isPublic()) {
982 $this->blowStream('public');
985 self::blow('notice:list-ids:conversation:%s', $this->conversation);
986 self::blow('conversation:notice_count:%d', $this->conversation);
988 if (!empty($this->repeat_of)) {
989 // XXX: we should probably only use one of these
990 $this->blowStream('notice:repeats:%d', $this->repeat_of);
991 self::blow('notice:list-ids:repeat_of:%d', $this->repeat_of);
994 $original = Notice::getKV('id', $this->repeat_of);
996 if ($original instanceof Notice) {
997 $originalUser = User::getKV('id', $original->profile_id);
998 if ($originalUser instanceof User) {
999 $this->blowStream('user:repeats_of_me:%d', $originalUser->id);
1003 $profile = Profile::getKV($this->profile_id);
1005 if ($profile instanceof Profile) {
1006 $profile->blowNoticeCount();
1009 $ptags = $this->getProfileTags();
1010 foreach ($ptags as $ptag) {
1011 $ptag->blowNoticeStreamCache();
1016 * Clear cache entries related to this notice at delete time.
1017 * Necessary to avoid breaking paging on public, profile timelines.
1019 function blowOnDelete()
1021 $this->blowOnInsert();
1023 self::blow('profile:notice_ids:%d;last', $this->profile_id);
1025 if ($this->isPublic()) {
1026 self::blow('public;last');
1029 self::blow('fave:by_notice', $this->id);
1031 if ($this->conversation) {
1032 // In case we're the first, will need to calc a new root.
1033 self::blow('notice:conversation_root:%d', $this->conversation);
1036 $ptags = $this->getProfileTags();
1037 foreach ($ptags as $ptag) {
1038 $ptag->blowNoticeStreamCache(true);
1042 function blowStream()
1044 $c = self::memcache();
1050 $args = func_get_args();
1052 $format = array_shift($args);
1054 $keyPart = vsprintf($format, $args);
1056 $cacheKey = Cache::key($keyPart);
1058 $c->delete($cacheKey);
1060 // delete the "last" stream, too, if this notice is
1061 // older than the top of that stream
1063 $lastKey = $cacheKey.';last';
1065 $lastStr = $c->get($lastKey);
1067 if ($lastStr !== false) {
1068 $window = explode(',', $lastStr);
1069 $lastID = $window[0];
1070 $lastNotice = Notice::getKV('id', $lastID);
1071 if (!$lastNotice instanceof Notice // just weird
1072 || strtotime($lastNotice->created) >= strtotime($this->created)) {
1073 $c->delete($lastKey);
1078 /** save all urls in the notice to the db
1080 * follow redirects and save all available file information
1081 * (mimetype, date, size, oembed, etc.)
1085 function saveUrls() {
1086 if (common_config('attachments', 'process_links')) {
1087 common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
1092 * Save the given URLs as related links/attachments to the db
1094 * follow redirects and save all available file information
1095 * (mimetype, date, size, oembed, etc.)
1099 function saveKnownUrls($urls)
1101 if (common_config('attachments', 'process_links')) {
1102 // @fixme validation?
1103 foreach (array_unique($urls) as $url) {
1105 File::processNew($url, $this->id);
1106 } catch (ServerException $e) {
1107 // Could not save URL. Log it?
1116 function saveUrl($url, $notice_id) {
1118 File::processNew($url, $notice_id);
1119 } catch (ServerException $e) {
1120 // Could not save URL. Log it?
1124 static function checkDupes($profile_id, $content) {
1125 $profile = Profile::getKV($profile_id);
1126 if (!$profile instanceof Profile) {
1129 $notice = $profile->getNotices(0, CachingNoticeStream::CACHE_WINDOW);
1130 if (!empty($notice)) {
1132 while ($notice->fetch()) {
1133 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
1135 } else if ($notice->content == $content) {
1140 // If we get here, oldest item in cache window is not
1141 // old enough for dupe limit; do direct check against DB
1142 $notice = new Notice();
1143 $notice->profile_id = $profile_id;
1144 $notice->content = $content;
1145 $threshold = common_sql_date(time() - common_config('site', 'dupelimit'));
1146 $notice->whereAdd(sprintf("created > '%s'", $notice->escape($threshold)));
1148 $cnt = $notice->count();
1152 static function checkEditThrottle($profile_id) {
1153 $profile = Profile::getKV($profile_id);
1154 if (!$profile instanceof Profile) {
1157 // Get the Nth notice
1158 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
1159 if ($notice && $notice->fetch()) {
1160 // If the Nth notice was posted less than timespan seconds ago
1161 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
1166 // Either not N notices in the stream, OR the Nth was not posted within timespan seconds
1170 protected $_attachments = array();
1172 function attachments() {
1173 if (isset($this->_attachments[$this->id])) {
1174 return $this->_attachments[$this->id];
1177 $f2ps = File_to_post::listGet('post_id', array($this->id));
1181 foreach ($f2ps[$this->id] as $f2p) {
1182 $ids[] = $f2p->file_id;
1185 $files = File::multiGet('id', $ids);
1187 $this->_attachments[$this->id] = $files->fetchAll();
1189 return $this->_attachments[$this->id];
1192 function _setAttachments($attachments)
1194 $this->_attachments[$this->id] = $attachments;
1197 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0)
1199 $stream = new PublicNoticeStream();
1200 return $stream->getNotices($offset, $limit, $since_id, $max_id);
1204 function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0)
1206 $stream = new ConversationNoticeStream($id);
1208 return $stream->getNotices($offset, $limit, $since_id, $max_id);
1212 * Is this notice part of an active conversation?
1214 * @return boolean true if other messages exist in the same
1215 * conversation, false if this is the only one
1217 function hasConversation()
1219 if (!empty($this->conversation)) {
1220 $conversation = Notice::conversationStream(
1221 $this->conversation,
1226 if ($conversation->N > 0) {
1234 * Grab the earliest notice from this conversation.
1236 * @return Notice or null
1238 function conversationRoot($profile=-1)
1240 // XXX: can this happen?
1242 if (empty($this->conversation)) {
1246 // Get the current profile if not specified
1248 if (is_int($profile) && $profile == -1) {
1249 $profile = Profile::current();
1252 // If this notice is out of scope, no root for you!
1254 if (!$this->inScope($profile)) {
1258 // If this isn't a reply to anything, then it's its own
1261 if (empty($this->reply_to)) {
1265 if (is_null($profile)) {
1266 $keypart = sprintf('notice:conversation_root:%d:null', $this->id);
1268 $keypart = sprintf('notice:conversation_root:%d:%d',
1273 $root = self::cacheGet($keypart);
1275 if ($root !== false && $root->inScope($profile)) {
1282 $parent = $last->getParent();
1283 if ($parent->inScope($profile)) {
1287 } catch (Exception $e) {
1288 // Latest notice has no parent
1290 // No parent, or parent out of scope
1295 self::cacheSet($keypart, $root);
1301 * Pull up a full list of local recipients who will be getting
1302 * this notice in their inbox. Results will be cached, so don't
1303 * change the input data wily-nilly!
1305 * @param array $groups optional list of Group objects;
1306 * if left empty, will be loaded from group_inbox records
1307 * @param array $recipient optional list of reply profile ids
1308 * if left empty, will be loaded from reply records
1309 * @return array associating recipient user IDs with an inbox source constant
1311 function whoGets(array $groups=null, array $recipients=null)
1313 $c = self::memcache();
1316 $ni = $c->get(Cache::key('notice:who_gets:'.$this->id));
1317 if ($ni !== false) {
1322 if (is_null($recipients)) {
1323 $recipients = $this->getReplies();
1328 // Give plugins a chance to add folks in at start...
1329 if (Event::handle('StartNoticeWhoGets', array($this, &$ni))) {
1331 $users = $this->getSubscribedUsers();
1332 foreach ($users as $id) {
1333 $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
1336 if (is_null($groups)) {
1337 $groups = $this->getGroups();
1339 foreach ($groups as $group) {
1340 $users = $group->getUserMembers();
1341 foreach ($users as $id) {
1342 if (!array_key_exists($id, $ni)) {
1343 $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
1348 $ptAtts = $this->getAttentionsFromProfileTags();
1349 foreach ($ptAtts as $key=>$val) {
1350 if (!array_key_exists($key, $ni)) {
1355 foreach ($recipients as $recipient) {
1356 if (!array_key_exists($recipient, $ni)) {
1357 $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
1361 // Exclude any deleted, non-local, or blocking recipients.
1362 $profile = $this->getProfile();
1363 $originalProfile = null;
1364 if ($this->repeat_of) {
1365 // Check blocks against the original notice's poster as well.
1366 $original = Notice::getKV('id', $this->repeat_of);
1367 if ($original instanceof Notice) {
1368 $originalProfile = $original->getProfile();
1372 foreach ($ni as $id => $source) {
1374 $user = User::getKV('id', $id);
1375 if (!$user instanceof User ||
1376 $user->hasBlocked($profile) ||
1377 ($originalProfile && $user->hasBlocked($originalProfile))) {
1380 } catch (UserNoProfileException $e) {
1381 // User doesn't have a profile; invalid; skip them.
1386 // Give plugins a chance to filter out...
1387 Event::handle('EndNoticeWhoGets', array($this, &$ni));
1391 // XXX: pack this data better
1392 $c->set(Cache::key('notice:who_gets:'.$this->id), $ni);
1398 function getSubscribedUsers()
1402 if(common_config('db','quote_identifiers'))
1403 $user_table = '"user"';
1404 else $user_table = 'user';
1408 'FROM '. $user_table .' JOIN subscription '.
1409 'ON '. $user_table .'.id = subscription.subscriber ' .
1410 'WHERE subscription.subscribed = %d ';
1412 $user->query(sprintf($qry, $this->profile_id));
1416 while ($user->fetch()) {
1425 function getProfileTags()
1427 $profile = $this->getProfile();
1428 $list = $profile->getOtherTags($profile);
1431 while($list->fetch()) {
1432 $ptags[] = clone($list);
1438 public function getAttentionsFromProfileTags()
1441 $ptags = $this->getProfileTags();
1442 foreach ($ptags as $ptag) {
1443 $users = $ptag->getUserSubscribers();
1444 foreach ($users as $id) {
1445 $ni[$id] = NOTICE_INBOX_SOURCE_PROFILE_TAG;
1452 * Record this notice to the given group inboxes for delivery.
1453 * Overrides the regular parsing of !group markup.
1455 * @param string $group_ids
1456 * @fixme might prefer URIs as identifiers, as for replies?
1457 * best with generalizations on user_group to support
1458 * remote groups better.
1460 function saveKnownGroups($group_ids)
1462 if (!is_array($group_ids)) {
1463 // TRANS: Server exception thrown when no array is provided to the method saveKnownGroups().
1464 throw new ServerException(_('Bad type provided to saveKnownGroups.'));
1468 foreach (array_unique($group_ids) as $id) {
1469 $group = User_group::getKV('id', $id);
1470 if ($group instanceof User_group) {
1471 common_log(LOG_ERR, "Local delivery to group id $id, $group->nickname");
1472 $result = $this->addToGroupInbox($group);
1474 common_log_db_error($gi, 'INSERT', __FILE__);
1477 if (common_config('group', 'addtag')) {
1478 // we automatically add a tag for every group name, too
1480 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($group->nickname),
1481 'notice_id' => $this->id));
1483 if (is_null($tag)) {
1484 $this->saveTag($group->nickname);
1488 $groups[] = clone($group);
1490 common_log(LOG_ERR, "Local delivery to group id $id skipped, doesn't exist");
1497 function addToGroupInbox(User_group $group)
1499 $gi = Group_inbox::pkeyGet(array('group_id' => $group->id,
1500 'notice_id' => $this->id));
1502 if (!$gi instanceof Group_inbox) {
1504 $gi = new Group_inbox();
1506 $gi->group_id = $group->id;
1507 $gi->notice_id = $this->id;
1508 $gi->created = $this->created;
1510 $result = $gi->insert();
1513 common_log_db_error($gi, 'INSERT', __FILE__);
1514 // TRANS: Server exception thrown when an update for a group inbox fails.
1515 throw new ServerException(_('Problem saving group inbox.'));
1518 self::blow('user_group:notice_ids:%d', $gi->group_id);
1525 * Save reply records indicating that this notice needs to be
1526 * delivered to the local users with the given URIs.
1528 * Since this is expected to be used when saving foreign-sourced
1529 * messages, we won't deliver to any remote targets as that's the
1530 * source service's responsibility.
1532 * Mail notifications etc will be handled later.
1534 * @param array $uris Array of unique identifier URIs for recipients
1536 function saveKnownReplies(array $uris)
1542 $sender = Profile::getKV($this->profile_id);
1544 foreach (array_unique($uris) as $uri) {
1546 $profile = Profile::fromUri($uri);
1547 } catch (UnknownUriException $e) {
1548 common_log(LOG_WARNING, "Unable to determine profile for URI '$uri'");
1552 if ($profile->hasBlocked($sender)) {
1553 common_log(LOG_INFO, "Not saving reply to profile {$profile->id} ($uri) from sender {$sender->id} because of a block.");
1557 $this->saveReply($profile->id);
1558 self::blow('reply:stream:%d', $profile->id);
1565 * Pull @-replies from this message's content in StatusNet markup format
1566 * and save reply records indicating that this message needs to be
1567 * delivered to those users.
1569 * Mail notifications to local profiles will be sent later.
1571 * @return array of integer profile IDs
1574 function saveReplies()
1576 // Don't save reply data for repeats
1578 if (!empty($this->repeat_of)) {
1582 $sender = $this->getProfile();
1586 // If it's a reply, save for the replied-to author
1588 $parent = $this->getParent();
1589 $parentauthor = $parent->getProfile();
1590 $this->saveReply($parentauthor->id);
1591 $replied[$parentauthor->id] = 1;
1592 self::blow('reply:stream:%d', $parentauthor->id);
1593 } catch (Exception $e) {
1594 // Not a reply, since it has no parent!
1597 // @todo ideally this parser information would only
1598 // be calculated once.
1600 $mentions = common_find_mentions($this->content, $this);
1602 // store replied only for first @ (what user/notice what the reply directed,
1603 // we assume first @ is it)
1605 foreach ($mentions as $mention) {
1607 foreach ($mention['mentioned'] as $mentioned) {
1609 // skip if they're already covered
1611 if (!empty($replied[$mentioned->id])) {
1615 // Don't save replies from blocked profile to local user
1617 $mentioned_user = User::getKV('id', $mentioned->id);
1618 if ($mentioned_user instanceof User && $mentioned_user->hasBlocked($sender)) {
1622 $this->saveReply($mentioned->id);
1623 $replied[$mentioned->id] = 1;
1624 self::blow('reply:stream:%d', $mentioned->id);
1628 $recipientIds = array_keys($replied);
1630 return $recipientIds;
1633 function saveReply($profileId)
1635 $reply = new Reply();
1637 $reply->notice_id = $this->id;
1638 $reply->profile_id = $profileId;
1639 $reply->modified = $this->created;
1646 protected $_replies = array();
1649 * Pull the complete list of @-reply targets for this notice.
1651 * @return array of integer profile ids
1653 function getReplies()
1655 if (isset($this->_replies[$this->id])) {
1656 return $this->_replies[$this->id];
1659 $replyMap = Reply::listGet('notice_id', array($this->id));
1663 foreach ($replyMap[$this->id] as $reply) {
1664 $ids[] = $reply->profile_id;
1667 $this->_replies[$this->id] = $ids;
1672 function _setReplies($replies)
1674 $this->_replies[$this->id] = $replies;
1678 * Pull the complete list of @-reply targets for this notice.
1680 * @return array of Profiles
1682 function getReplyProfiles()
1684 $ids = $this->getReplies();
1686 $profiles = Profile::multiGet('id', $ids);
1688 return $profiles->fetchAll();
1692 * Send e-mail notifications to local @-reply targets.
1694 * Replies must already have been saved; this is expected to be run
1695 * from the distrib queue handler.
1697 function sendReplyNotifications()
1699 // Don't send reply notifications for repeats
1701 if (!empty($this->repeat_of)) {
1705 $recipientIds = $this->getReplies();
1707 foreach ($recipientIds as $recipientId) {
1708 $user = User::getKV('id', $recipientId);
1709 if ($user instanceof User) {
1710 mail_notify_attn($user, $this);
1716 * Pull list of groups this notice needs to be delivered to,
1717 * as previously recorded by saveKnownGroups().
1719 * @return array of Group objects
1722 protected $_groups = array();
1724 function getGroups()
1726 // Don't save groups for repeats
1728 if (!empty($this->repeat_of)) {
1732 if (isset($this->_groups[$this->id])) {
1733 return $this->_groups[$this->id];
1736 $gis = Group_inbox::listGet('notice_id', array($this->id));
1740 foreach ($gis[$this->id] as $gi)
1742 $ids[] = $gi->group_id;
1745 $groups = User_group::multiGet('id', $ids);
1747 $this->_groups[$this->id] = $groups->fetchAll();
1749 return $this->_groups[$this->id];
1752 function _setGroups($groups)
1754 $this->_groups[$this->id] = $groups;
1758 * Convert a notice into an activity for export.
1760 * @param User $cur Current user
1762 * @return Activity activity object representing this Notice.
1765 function asActivity($cur=null)
1767 $act = self::cacheGet(Cache::codeKey('notice:as-activity:'.$this->id));
1769 if ($act instanceof Activity) {
1772 $act = new Activity();
1774 if (Event::handle('StartNoticeAsActivity', array($this, &$act))) {
1776 $act->id = $this->uri;
1777 $act->time = strtotime($this->created);
1779 $act->link = $this->getUrl();
1780 } catch (InvalidUrlException $e) {
1781 // The notice is probably a share or similar, which don't
1782 // have a representational URL of their own.
1784 $act->content = common_xml_safe_str($this->rendered);
1786 $profile = $this->getProfile();
1788 $act->actor = $profile->asActivityObject();
1789 $act->actor->extra[] = $profile->profileInfo($cur);
1791 $act->verb = $this->verb;
1793 if ($this->repeat_of) {
1794 $repeated = Notice::getKV('id', $this->repeat_of);
1795 if ($repeated instanceof Notice) {
1796 $act->objects[] = $repeated->asActivity($cur);
1799 $act->objects[] = ActivityObject::fromNotice($this);
1802 // XXX: should this be handled by default processing for object entry?
1806 $tags = $this->getTags();
1808 foreach ($tags as $tag) {
1809 $cat = new AtomCategory();
1812 $act->categories[] = $cat;
1816 // XXX: use Atom Media and/or File activity objects instead
1818 $attachments = $this->attachments();
1820 foreach ($attachments as $attachment) {
1821 // Save local attachments
1822 if (!empty($attachment->filename)) {
1823 $act->attachments[] = ActivityObject::fromFile($attachment);
1827 $ctx = new ActivityContext();
1830 $reply = $this->getParent();
1831 $ctx->replyToID = $reply->getUri();
1832 $ctx->replyToUrl = $reply->getUrl();
1833 } catch (Exception $e) {
1834 // This is not a reply to something
1837 $ctx->location = $this->getLocation();
1841 if (!empty($this->conversation)) {
1842 $conv = Conversation::getKV('id', $this->conversation);
1843 if ($conv instanceof Conversation) {
1844 $ctx->conversation = $conv->uri;
1848 $reply_ids = $this->getReplies();
1850 foreach ($reply_ids as $id) {
1851 $rprofile = Profile::getKV('id', $id);
1852 if ($rprofile instanceof Profile) {
1853 $ctx->attention[$rprofile->getUri()] = ActivityObject::PERSON;
1857 $groups = $this->getGroups();
1859 foreach ($groups as $group) {
1860 $ctx->attention[$group->getUri()] = ActivityObject::GROUP;
1863 switch ($this->scope) {
1864 case Notice::PUBLIC_SCOPE:
1865 $ctx->attention[ActivityContext::ATTN_PUBLIC] = ActivityObject::COLLECTION;
1867 case Notice::FOLLOWER_SCOPE:
1868 $surl = common_local_url("subscribers", array('nickname' => $profile->nickname));
1869 $ctx->attention[$surl] = ActivityObject::COLLECTION;
1873 $act->context = $ctx;
1875 $source = $this->getSource();
1877 if ($source instanceof Notice_source) {
1878 $act->generator = ActivityObject::fromNoticeSource($source);
1883 $atom_feed = $profile->getAtomFeed();
1885 if (!empty($atom_feed)) {
1887 $act->source = new ActivitySource();
1889 // XXX: we should store the actual feed ID
1891 $act->source->id = $atom_feed;
1893 // XXX: we should store the actual feed title
1895 $act->source->title = $profile->getBestName();
1897 $act->source->links['alternate'] = $profile->profileurl;
1898 $act->source->links['self'] = $atom_feed;
1900 $act->source->icon = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
1902 $notice = $profile->getCurrentNotice();
1904 if ($notice instanceof Notice) {
1905 $act->source->updated = self::utcDate($notice->created);
1908 $user = User::getKV('id', $profile->id);
1910 if ($user instanceof User) {
1911 $act->source->links['license'] = common_config('license', 'url');
1915 if ($this->isLocal()) {
1916 $act->selfLink = common_local_url('ApiStatusesShow', array('id' => $this->id,
1917 'format' => 'atom'));
1918 $act->editLink = $act->selfLink;
1921 Event::handle('EndNoticeAsActivity', array($this, &$act));
1924 self::cacheSet(Cache::codeKey('notice:as-activity:'.$this->id), $act);
1929 // This has gotten way too long. Needs to be sliced up into functional bits
1930 // or ideally exported to a utility class.
1932 function asAtomEntry($namespace=false,
1937 $act = $this->asActivity($cur);
1938 $act->extra[] = $this->noticeInfo($cur);
1939 return $act->asString($namespace, $author, $source);
1943 * Extra notice info for atom entries
1945 * Clients use some extra notice info in the atom stream.
1946 * This gives it to them.
1948 * @param User $cur Current user
1950 * @return array representation of <statusnet:notice_info> element
1953 function noticeInfo($cur)
1955 // local notice ID (useful to clients for ordering)
1957 $noticeInfoAttr = array('local_id' => $this->id);
1961 $ns = $this->getSource();
1963 if ($ns instanceof Notice_source) {
1964 $noticeInfoAttr['source'] = $ns->code;
1965 if (!empty($ns->url)) {
1966 $noticeInfoAttr['source_link'] = $ns->url;
1967 if (!empty($ns->name)) {
1968 $noticeInfoAttr['source'] = '<a href="'
1969 . htmlspecialchars($ns->url)
1970 . '" rel="nofollow">'
1971 . htmlspecialchars($ns->name)
1977 // favorite and repeated
1981 $scoped = $cur->getProfile();
1982 $noticeInfoAttr['repeated'] = ($scoped->hasRepeated($this)) ? "true" : "false";
1985 if (!empty($this->repeat_of)) {
1986 $noticeInfoAttr['repeat_of'] = $this->repeat_of;
1989 Event::handle('StatusNetApiNoticeInfo', array($this, &$noticeInfoAttr, $scoped));
1991 return array('statusnet:notice_info', $noticeInfoAttr, null);
1995 * Returns an XML string fragment with a reference to a notice as an
1996 * Activity Streams noun object with the given element type.
1998 * Assumes that 'activity' namespace has been previously defined.
2000 * @param string $element one of 'subject', 'object', 'target'
2004 function asActivityNoun($element)
2006 $noun = ActivityObject::fromNotice($this);
2007 return $noun->asString('activity:' . $element);
2011 * Determine which notice, if any, a new notice is in reply to.
2013 * For conversation tracking, we try to see where this notice fits
2014 * in the tree. Beware that this may very well give false positives
2015 * and add replies to wrong threads (if there have been newer posts
2016 * by the same user as we're replying to).
2018 * @param Profile $sender Author profile
2019 * @param string $content Final notice content
2021 * @return integer ID of replied-to notice, or null for not a reply.
2024 static function getInlineReplyTo(Profile $sender, $content)
2026 // Is there an initial @ or T?
2027 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $content, $match)
2028 || preg_match('/^@([a-z0-9]{1,64})\s+/', $content, $match)) {
2029 $nickname = common_canonical_nickname($match[1]);
2034 // Figure out who that is.
2035 $recipient = common_relative_profile($sender, $nickname, common_sql_now());
2037 if ($recipient instanceof Profile) {
2038 // Get their last notice
2039 $last = $recipient->getCurrentNotice();
2040 if ($last instanceof Notice) {
2043 // Maybe in the future we want to handle something else below
2044 // so don't return getCurrentNotice() immediately.
2050 static function maxContent()
2052 $contentlimit = common_config('notice', 'contentlimit');
2053 // null => use global limit (distinct from 0!)
2054 if (is_null($contentlimit)) {
2055 $contentlimit = common_config('site', 'textlimit');
2057 return $contentlimit;
2060 static function contentTooLong($content)
2062 $contentlimit = self::maxContent();
2063 return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
2066 function getLocation()
2070 if (!empty($this->location_id) && !empty($this->location_ns)) {
2071 $location = Location::fromId($this->location_id, $this->location_ns);
2074 if (is_null($location)) { // no ID, or Location::fromId() failed
2075 if (!empty($this->lat) && !empty($this->lon)) {
2076 $location = Location::fromLatLon($this->lat, $this->lon);
2084 * Convenience function for posting a repeat of an existing message.
2086 * @param Profile $repeater Profile which is doing the repeat
2087 * @param string $source: posting source key, eg 'web', 'api', etc
2090 * @throws Exception on failure or permission problems
2092 function repeat(Profile $repeater, $source)
2094 $author = $this->getProfile();
2096 // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'.
2097 // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice.
2098 $content = sprintf(_('RT @%1$s %2$s'),
2099 $author->getNickname(),
2102 // Scope is same as this one's
2103 return self::saveNew($repeater->id,
2106 array('repeat_of' => $this->id,
2107 'scope' => $this->scope));
2110 // These are supposed to be in chron order!
2112 function repeatStream($limit=100)
2114 $cache = Cache::instance();
2116 if (empty($cache)) {
2117 $ids = $this->_repeatStreamDirect($limit);
2119 $idstr = $cache->get(Cache::key('notice:repeats:'.$this->id));
2120 if ($idstr !== false) {
2121 if (empty($idstr)) {
2124 $ids = explode(',', $idstr);
2127 $ids = $this->_repeatStreamDirect(100);
2128 $cache->set(Cache::key('notice:repeats:'.$this->id), implode(',', $ids));
2131 // We do a max of 100, so slice down to limit
2132 $ids = array_slice($ids, 0, $limit);
2136 return NoticeStream::getStreamByIds($ids);
2139 function _repeatStreamDirect($limit)
2141 $notice = new Notice();
2143 $notice->selectAdd(); // clears it
2144 $notice->selectAdd('id');
2146 $notice->repeat_of = $this->id;
2148 $notice->orderBy('created, id'); // NB: asc!
2150 if (!is_null($limit)) {
2151 $notice->limit(0, $limit);
2154 return $notice->fetchAll('id');
2157 function locationOptions($lat, $lon, $location_id, $location_ns, $profile = null)
2161 if (!empty($location_id) && !empty($location_ns)) {
2162 $options['location_id'] = $location_id;
2163 $options['location_ns'] = $location_ns;
2165 $location = Location::fromId($location_id, $location_ns);
2167 if ($location instanceof Location) {
2168 $options['lat'] = $location->lat;
2169 $options['lon'] = $location->lon;
2172 } else if (!empty($lat) && !empty($lon)) {
2173 $options['lat'] = $lat;
2174 $options['lon'] = $lon;
2176 $location = Location::fromLatLon($lat, $lon);
2178 if ($location instanceof Location) {
2179 $options['location_id'] = $location->location_id;
2180 $options['location_ns'] = $location->location_ns;
2182 } else if (!empty($profile)) {
2183 if (isset($profile->lat) && isset($profile->lon)) {
2184 $options['lat'] = $profile->lat;
2185 $options['lon'] = $profile->lon;
2188 if (isset($profile->location_id) && isset($profile->location_ns)) {
2189 $options['location_id'] = $profile->location_id;
2190 $options['location_ns'] = $profile->location_ns;
2197 function clearAttentions()
2199 $att = new Attention();
2200 $att->notice_id = $this->getID();
2203 while ($att->fetch()) {
2204 // Can't do delete() on the object directly since it won't remove all of it
2205 $other = clone($att);
2211 function clearReplies()
2213 $replyNotice = new Notice();
2214 $replyNotice->reply_to = $this->id;
2216 //Null any notices that are replies to this notice
2218 if ($replyNotice->find()) {
2219 while ($replyNotice->fetch()) {
2220 $orig = clone($replyNotice);
2221 $replyNotice->reply_to = null;
2222 $replyNotice->update($orig);
2228 $reply = new Reply();
2229 $reply->notice_id = $this->id;
2231 if ($reply->find()) {
2232 while($reply->fetch()) {
2233 self::blow('reply:stream:%d', $reply->profile_id);
2241 function clearFiles()
2243 $f2p = new File_to_post();
2245 $f2p->post_id = $this->id;
2248 while ($f2p->fetch()) {
2252 // FIXME: decide whether to delete File objects
2253 // ...and related (actual) files
2256 function clearRepeats()
2258 $repeatNotice = new Notice();
2259 $repeatNotice->repeat_of = $this->id;
2261 //Null any notices that are repeats of this notice
2263 if ($repeatNotice->find()) {
2264 while ($repeatNotice->fetch()) {
2265 $orig = clone($repeatNotice);
2266 $repeatNotice->repeat_of = null;
2267 $repeatNotice->update($orig);
2272 function clearTags()
2274 $tag = new Notice_tag();
2275 $tag->notice_id = $this->id;
2278 while ($tag->fetch()) {
2279 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, Cache::keyize($tag->tag));
2280 self::blow('profile:notice_ids_tagged:%d:%s;last', $this->profile_id, Cache::keyize($tag->tag));
2281 self::blow('notice_tag:notice_ids:%s', Cache::keyize($tag->tag));
2282 self::blow('notice_tag:notice_ids:%s;last', Cache::keyize($tag->tag));
2290 function clearGroupInboxes()
2292 $gi = new Group_inbox();
2294 $gi->notice_id = $this->id;
2297 while ($gi->fetch()) {
2298 self::blow('user_group:notice_ids:%d', $gi->group_id);
2306 function distribute()
2308 // We always insert for the author so they don't
2310 Event::handle('StartNoticeDistribute', array($this));
2312 // If there's a failure, we want to _force_
2313 // distribution at this point.
2315 $qm = QueueManager::get();
2316 $qm->enqueue($this, 'distrib');
2317 } catch (Exception $e) {
2318 // If the exception isn't transient, this
2319 // may throw more exceptions as DQH does
2320 // its own enqueueing. So, we ignore them!
2322 $handler = new DistribQueueHandler();
2323 $handler->handle($this);
2324 } catch (Exception $e) {
2325 common_log(LOG_ERR, "emergency redistribution resulted in " . $e->getMessage());
2327 // Re-throw so somebody smarter can handle it.
2334 $result = parent::insert();
2336 if ($result !== false) {
2337 // Profile::hasRepeated() abuses pkeyGet(), so we
2338 // have to clear manually
2339 if (!empty($this->repeat_of)) {
2340 $c = self::memcache();
2342 $ck = self::multicacheKey('Notice',
2343 array('profile_id' => $this->profile_id,
2344 'repeat_of' => $this->repeat_of));
2354 * Get the source of the notice
2356 * @return Notice_source $ns A notice source object. 'code' is the only attribute
2357 * guaranteed to be populated.
2359 function getSource()
2361 $ns = new Notice_source();
2362 if (!empty($this->source)) {
2363 switch ($this->source) {
2370 $ns->code = $this->source;
2373 $ns = Notice_source::getKV($this->source);
2375 $ns = new Notice_source();
2376 $ns->code = $this->source;
2377 $app = Oauth_application::getKV('name', $this->source);
2379 $ns->name = $app->name;
2380 $ns->url = $app->source_url;
2390 * Determine whether the notice was locally created
2392 * @return boolean locality
2395 public function isLocal()
2397 return ($this->is_local == Notice::LOCAL_PUBLIC ||
2398 $this->is_local == Notice::LOCAL_NONPUBLIC);
2402 * Get the list of hash tags saved with this notice.
2404 * @return array of strings
2406 public function getTags()
2410 $keypart = sprintf('notice:tags:%d', $this->id);
2412 $tagstr = self::cacheGet($keypart);
2414 if ($tagstr !== false) {
2415 $tags = explode(',', $tagstr);
2417 $tag = new Notice_tag();
2418 $tag->notice_id = $this->id;
2420 while ($tag->fetch()) {
2421 $tags[] = $tag->tag;
2424 self::cacheSet($keypart, implode(',', $tags));
2430 static private function utcDate($dt)
2432 $dateStr = date('d F Y H:i:s', strtotime($dt));
2433 $d = new DateTime($dateStr, new DateTimeZone('UTC'));
2434 return $d->format(DATE_W3C);
2438 * Look up the creation timestamp for a given notice ID, even
2439 * if it's been deleted.
2442 * @return mixed string recorded creation timestamp, or false if can't be found
2444 public static function getAsTimestamp($id)
2450 $notice = Notice::getKV('id', $id);
2452 return $notice->created;
2455 $deleted = Deleted_notice::getKV('id', $id);
2457 return $deleted->created;
2464 * Build an SQL 'where' fragment for timestamp-based sorting from a since_id
2465 * parameter, matching notices posted after the given one (exclusive).
2467 * If the referenced notice can't be found, will return false.
2470 * @param string $idField
2471 * @param string $createdField
2472 * @return mixed string or false if no match
2474 public static function whereSinceId($id, $idField='id', $createdField='created')
2476 $since = Notice::getAsTimestamp($id);
2478 return sprintf("($createdField = '%s' and $idField > %d) or ($createdField > '%s')", $since, $id, $since);
2484 * Build an SQL 'where' fragment for timestamp-based sorting from a since_id
2485 * parameter, matching notices posted after the given one (exclusive), and
2486 * if necessary add it to the data object's query.
2488 * @param DB_DataObject $obj
2490 * @param string $idField
2491 * @param string $createdField
2492 * @return mixed string or false if no match
2494 public static function addWhereSinceId(DB_DataObject $obj, $id, $idField='id', $createdField='created')
2496 $since = self::whereSinceId($id, $idField, $createdField);
2498 $obj->whereAdd($since);
2503 * Build an SQL 'where' fragment for timestamp-based sorting from a max_id
2504 * parameter, matching notices posted before the given one (inclusive).
2506 * If the referenced notice can't be found, will return false.
2509 * @param string $idField
2510 * @param string $createdField
2511 * @return mixed string or false if no match
2513 public static function whereMaxId($id, $idField='id', $createdField='created')
2515 $max = Notice::getAsTimestamp($id);
2517 return sprintf("($createdField < '%s') or ($createdField = '%s' and $idField <= %d)", $max, $max, $id);
2523 * Build an SQL 'where' fragment for timestamp-based sorting from a max_id
2524 * parameter, matching notices posted before the given one (inclusive), and
2525 * if necessary add it to the data object's query.
2527 * @param DB_DataObject $obj
2529 * @param string $idField
2530 * @param string $createdField
2531 * @return mixed string or false if no match
2533 public static function addWhereMaxId(DB_DataObject $obj, $id, $idField='id', $createdField='created')
2535 $max = self::whereMaxId($id, $idField, $createdField);
2537 $obj->whereAdd($max);
2543 if (common_config('public', 'localonly')) {
2544 return ($this->is_local == Notice::LOCAL_PUBLIC);
2546 return (($this->is_local != Notice::LOCAL_NONPUBLIC) &&
2547 ($this->is_local != Notice::GATEWAY));
2552 * Check that the given profile is allowed to read, respond to, or otherwise
2553 * act on this notice.
2555 * The $scope member is a bitmask of scopes, representing a logical AND of the
2556 * scope requirement. So, 0x03 (Notice::ADDRESSEE_SCOPE | Notice::SITE_SCOPE) means
2557 * "only visible to people who are mentioned in the notice AND are users on this site."
2558 * Users on the site who are not mentioned in the notice will not be able to see the
2561 * @param Profile $profile The profile to check; pass null to check for public/unauthenticated users.
2563 * @return boolean whether the profile is in the notice's scope
2565 function inScope($profile)
2567 if (is_null($profile)) {
2568 $keypart = sprintf('notice:in-scope-for:%d:null', $this->id);
2570 $keypart = sprintf('notice:in-scope-for:%d:%d', $this->id, $profile->id);
2573 $result = self::cacheGet($keypart);
2575 if ($result === false) {
2577 if (Event::handle('StartNoticeInScope', array($this, $profile, &$bResult))) {
2578 $bResult = $this->_inScope($profile);
2579 Event::handle('EndNoticeInScope', array($this, $profile, &$bResult));
2581 $result = ($bResult) ? 1 : 0;
2582 self::cacheSet($keypart, $result, 0, 300);
2585 return ($result == 1) ? true : false;
2588 protected function _inScope($profile)
2590 if (!is_null($this->scope)) {
2591 $scope = $this->scope;
2593 $scope = self::defaultScope();
2596 // If there's no scope, anyone (even anon) is in scope.
2598 if ($scope == 0) { // Not private
2600 return !$this->isHiddenSpam($profile);
2602 } else { // Private, somehow
2604 // If there's scope, anon cannot be in scope
2606 if (empty($profile)) {
2610 // Author is always in scope
2612 if ($this->profile_id == $profile->id) {
2616 // Only for users on this site
2618 if (($scope & Notice::SITE_SCOPE) && !$profile->isLocal()) {
2622 // Only for users mentioned in the notice
2624 if ($scope & Notice::ADDRESSEE_SCOPE) {
2626 $reply = Reply::pkeyGet(array('notice_id' => $this->id,
2627 'profile_id' => $profile->id));
2629 if (!$reply instanceof Reply) {
2634 // Only for members of the given group
2636 if ($scope & Notice::GROUP_SCOPE) {
2638 // XXX: just query for the single membership
2640 $groups = $this->getGroups();
2644 foreach ($groups as $group) {
2645 if ($profile->isMember($group)) {
2656 // Only for followers of the author
2660 if ($scope & Notice::FOLLOWER_SCOPE) {
2663 $author = $this->getProfile();
2664 } catch (Exception $e) {
2668 if (!Subscription::exists($profile, $author)) {
2673 return !$this->isHiddenSpam($profile);
2677 function isHiddenSpam($profile) {
2679 // Hide posts by silenced users from everyone but moderators.
2681 if (common_config('notice', 'hidespam')) {
2684 $author = $this->getProfile();
2685 } catch(Exception $e) {
2686 // If we can't get an author, keep it hidden.
2687 // XXX: technically not spam, but, whatever.
2691 if ($author->hasRole(Profile_role::SILENCED)) {
2692 if (!$profile instanceof Profile || (($profile->id !== $author->id) && (!$profile->hasRight(Right::REVIEWSPAM)))) {
2701 public function getParent()
2703 $parent = Notice::getKV('id', $this->reply_to);
2705 if (!$parent instanceof Notice) {
2706 throw new ServerException('Notice has no parent');
2713 * Magic function called at serialize() time.
2715 * We use this to drop a couple process-specific references
2716 * from DB_DataObject which can cause trouble in future
2719 * @return array of variable names to include in serialization.
2724 $vars = parent::__sleep();
2725 $skip = array('_profile', '_groups', '_attachments', '_faves', '_replies', '_repeats');
2726 return array_diff($vars, $skip);
2729 static function defaultScope()
2731 $scope = common_config('notice', 'defaultscope');
2732 if (is_null($scope)) {
2733 if (common_config('site', 'private')) {
2742 static function fillProfiles($notices)
2744 $map = self::getProfiles($notices);
2746 foreach ($notices as $entry=>$notice) {
2748 if (array_key_exists($notice->profile_id, $map)) {
2749 $notice->_setProfile($map[$notice->profile_id]);
2751 } catch (NoProfileException $e) {
2752 common_log(LOG_WARNING, "Failed to fill profile in Notice with non-existing entry for profile_id: {$e->profile_id}");
2753 unset($notices[$entry]);
2757 return array_values($map);
2760 static function getProfiles(&$notices)
2763 foreach ($notices as $notice) {
2764 $ids[] = $notice->profile_id;
2767 $ids = array_unique($ids);
2769 return Profile::pivotGet('id', $ids);
2772 static function fillGroups(&$notices)
2774 $ids = self::_idsOf($notices);
2776 $gis = Group_inbox::listGet('notice_id', $ids);
2780 foreach ($gis as $id => $gi)
2784 $gids[] = $g->group_id;
2788 $gids = array_unique($gids);
2790 $group = User_group::pivotGet('id', $gids);
2792 foreach ($notices as $notice)
2795 $gi = $gis[$notice->id];
2796 foreach ($gi as $g) {
2797 $grps[] = $group[$g->group_id];
2799 $notice->_setGroups($grps);
2803 static function _idsOf(array &$notices)
2806 foreach ($notices as $notice) {
2807 $ids[$notice->id] = true;
2809 return array_keys($ids);
2812 static function fillAttachments(&$notices)
2814 $ids = self::_idsOf($notices);
2816 $f2pMap = File_to_post::listGet('post_id', $ids);
2820 foreach ($f2pMap as $noticeId => $f2ps) {
2821 foreach ($f2ps as $f2p) {
2822 $fileIds[] = $f2p->file_id;
2826 $fileIds = array_unique($fileIds);
2828 $fileMap = File::pivotGet('id', $fileIds);
2830 foreach ($notices as $notice)
2833 $f2ps = $f2pMap[$notice->id];
2834 foreach ($f2ps as $f2p) {
2835 $files[] = $fileMap[$f2p->file_id];
2837 $notice->_setAttachments($files);
2841 static function fillReplies(&$notices)
2843 $ids = self::_idsOf($notices);
2844 $replyMap = Reply::listGet('notice_id', $ids);
2845 foreach ($notices as $notice) {
2846 $replies = $replyMap[$notice->id];
2848 foreach ($replies as $reply) {
2849 $ids[] = $reply->profile_id;
2851 $notice->_setReplies($ids);
2855 protected $_repeats = array();
2857 function getRepeats()
2859 if (isset($this->_repeats[$this->id])) {
2860 return $this->_repeats[$this->id];
2862 $repeatMap = Notice::listGet('repeat_of', array($this->id));
2863 $this->_repeats[$this->id] = $repeatMap[$this->id];
2864 return $this->_repeats[$this->id];
2867 function _setRepeats($repeats)
2869 $this->_repeats[$this->id] = $repeats;
2872 static function fillRepeats(&$notices)
2874 $ids = self::_idsOf($notices);
2875 $repeatMap = Notice::listGet('repeat_of', $ids);
2876 foreach ($notices as $notice) {
2877 $repeats = $repeatMap[$notice->id];
2878 $notice->_setRepeats($repeats);