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 * @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', CachingNoticeStream::CACHE_WINDOW);
50 define('MAX_BOXCARS', 128);
52 class Notice extends Managed_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)
75 public $verb; // varchar(255)
76 public $object_type; // varchar(255)
77 public $scope; // int(4)
80 function staticGet($k,$v=NULL)
82 return Memcached_DataObject::staticGet('Notice',$k,$v);
85 /* the code above is auto generated do not remove the tag below */
88 public static function schemaDef()
92 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
93 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'who made the update'),
94 'uri' => array('type' => 'varchar', 'length' => 255, 'description' => 'universally unique identifier, usually a tag URI'),
95 'content' => array('type' => 'text', 'description' => 'update content', 'collate' => 'utf8_general_ci'),
96 'rendered' => array('type' => 'text', 'description' => 'HTML version of the content'),
97 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL of any attachment (image, video, bookmark, whatever)'),
98 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
99 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
100 'reply_to' => array('type' => 'int', 'description' => 'notice replied to (usually a guess)'),
101 'is_local' => array('type' => 'int', 'size' => 'tiny', 'default' => 0, 'description' => 'notice was generated by a user'),
102 'source' => array('type' => 'varchar', 'length' => 32, 'description' => 'source of comment, like "web", "im", or "clientname"'),
103 'conversation' => array('type' => 'int', 'description' => 'id of root notice in this conversation'),
104 'lat' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude'),
105 'lon' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'),
106 'location_id' => array('type' => 'int', 'description' => 'location id if possible'),
107 'location_ns' => array('type' => 'int', 'description' => 'namespace for location'),
108 'repeat_of' => array('type' => 'int', 'description' => 'notice this is a repeat of'),
109 'object_type' => array('type' => 'varchar', 'length' => 255, 'description' => 'URI representing activity streams object type', 'default' => 'http://activitystrea.ms/schema/1.0/note'),
110 'verb' => array('type' => 'varchar', 'length' => 255, 'description' => 'URI representing activity streams verb', 'default' => 'http://activitystrea.ms/schema/1.0/post'),
111 'scope' => array('type' => 'int',
112 'description' => 'bit map for distribution scope; 0 = everywhere; 1 = this server only; 2 = addressees; 4 = followers; null = default'),
114 'primary key' => array('id'),
115 'unique keys' => array(
116 'notice_uri_key' => array('uri'),
118 'foreign keys' => array(
119 'notice_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
120 'notice_reply_to_fkey' => array('notice', array('reply_to' => 'id')),
121 'notice_conversation_fkey' => array('conversation', array('conversation' => 'id')), # note... used to refer to notice.id
122 'notice_repeat_of_fkey' => array('notice', array('repeat_of' => 'id')), # @fixme: what about repeats of deleted notices?
125 'notice_created_id_is_local_idx' => array('created', 'id', 'is_local'),
126 'notice_profile_id_idx' => array('profile_id', 'created', 'id'),
127 'notice_repeat_of_created_id_idx' => array('repeat_of', 'created', 'id'),
128 'notice_conversation_created_id_idx' => array('conversation', 'created', 'id'),
129 'notice_replyto_idx' => array('reply_to')
133 if (common_config('search', 'type') == 'fulltext') {
134 $def['fulltext indexes'] = array('content' => array('content'));
140 function multiGet($kc, $kvs, $skipNulls=true)
142 return Memcached_DataObject::multiGet('Notice', $kc, $kvs, $skipNulls);
146 const LOCAL_PUBLIC = 1;
148 const LOCAL_NONPUBLIC = -1;
151 const PUBLIC_SCOPE = 0; // Useful fake constant
152 const SITE_SCOPE = 1;
153 const ADDRESSEE_SCOPE = 2;
154 const GROUP_SCOPE = 4;
155 const FOLLOWER_SCOPE = 8;
157 protected $_profile = -1;
159 function getProfile()
161 if (is_int($this->_profile) && $this->_profile == -1) {
162 $this->_setProfile(Profile::staticGet('id', $this->profile_id));
164 if (empty($this->_profile)) {
165 // TRANS: Server exception thrown when a user profile for a notice cannot be found.
166 // TRANS: %1$d is a profile ID (number), %2$d is a notice ID (number).
167 throw new ServerException(sprintf(_('No such profile (%1$d) for notice (%2$d).'), $this->profile_id, $this->id));
171 return $this->_profile;
174 function _setProfile($profile)
176 $this->_profile = $profile;
181 // For auditing purposes, save a record that the notice
184 // @fixme we have some cases where things get re-run and so the
186 $deleted = Deleted_notice::staticGet('id', $this->id);
189 $deleted = Deleted_notice::staticGet('uri', $this->uri);
193 $deleted = new Deleted_notice();
195 $deleted->id = $this->id;
196 $deleted->profile_id = $this->profile_id;
197 $deleted->uri = $this->uri;
198 $deleted->created = $this->created;
199 $deleted->deleted = common_sql_now();
204 if (Event::handle('NoticeDeleteRelated', array($this))) {
206 // Clear related records
208 $this->clearReplies();
209 $this->clearRepeats();
212 $this->clearGroupInboxes();
215 // NOTE: we don't clear inboxes
216 // NOTE: we don't clear queue items
219 $result = parent::delete();
221 $this->blowOnDelete();
226 * Extract #hashtags from this notice's content and save them to the database.
230 /* extract all #hastags */
231 $count = preg_match_all('/(?:^|\s)#([\pL\pN_\-\.]{1,64})/u', strtolower($this->content), $match);
236 /* Add them to the database */
237 return $this->saveKnownTags($match[1]);
241 * Record the given set of hash tags in the db for this notice.
242 * Given tag strings will be normalized and checked for dupes.
244 function saveKnownTags($hashtags)
246 //turn each into their canonical tag
247 //this is needed to remove dupes before saving e.g. #hash.tag = #hashtag
248 for($i=0; $i<count($hashtags); $i++) {
249 /* elide characters we don't want in the tag */
250 $hashtags[$i] = common_canonical_tag($hashtags[$i]);
253 foreach(array_unique($hashtags) as $hashtag) {
254 $this->saveTag($hashtag);
255 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, $hashtag);
261 * Record a single hash tag as associated with this notice.
262 * Tag format and uniqueness must be validated by caller.
264 function saveTag($hashtag)
266 $tag = new Notice_tag();
267 $tag->notice_id = $this->id;
268 $tag->tag = $hashtag;
269 $tag->created = $this->created;
270 $id = $tag->insert();
273 // TRANS: Server exception. %s are the error details.
274 throw new ServerException(sprintf(_('Database error inserting hashtag: %s.'),
275 $last_error->message));
279 // if it's saved, blow its cache
280 $tag->blowCache(false);
284 * Save a new notice and push it out to subscribers' inboxes.
285 * Poster's permissions are checked before sending.
287 * @param int $profile_id Profile ID of the poster
288 * @param string $content source message text; links may be shortened
289 * per current user's preference
290 * @param string $source source key ('web', 'api', etc)
291 * @param array $options Associative array of optional properties:
292 * string 'created' timestamp of notice; defaults to now
293 * int 'is_local' source/gateway ID, one of:
294 * Notice::LOCAL_PUBLIC - Local, ok to appear in public timeline
295 * Notice::REMOTE - Sent from a remote service;
296 * hide from public timeline but show in
297 * local "and friends" timelines
298 * Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
299 * Notice::GATEWAY - From another non-OStatus service;
300 * will not appear in public views
301 * float 'lat' decimal latitude for geolocation
302 * float 'lon' decimal longitude for geolocation
303 * int 'location_id' geoname identifier
304 * int 'location_ns' geoname namespace to interpret location_id
305 * int 'reply_to'; notice ID this is a reply to
306 * int 'repeat_of'; notice ID this is a repeat of
307 * string 'uri' unique ID for notice; defaults to local notice URL
308 * string 'url' permalink to notice; defaults to local notice URL
309 * string 'rendered' rendered HTML version of content
310 * array 'replies' list of profile URIs for reply delivery in
311 * place of extracting @-replies from content.
312 * array 'groups' list of group IDs to deliver to, in place of
313 * extracting ! tags from content
314 * array 'tags' list of hashtag strings to save with the notice
315 * in place of extracting # tags from content
316 * array 'urls' list of attached/referred URLs to save with the
317 * notice in place of extracting links from content
318 * boolean 'distribute' whether to distribute the notice, default true
319 * string 'object_type' URL of the associated object type (default ActivityObject::NOTE)
320 * string 'verb' URL of the associated verb (default ActivityVerb::POST)
321 * int 'scope' Scope bitmask; default to SITE_SCOPE on private sites, 0 otherwise
323 * @fixme tag override
326 * @throws ClientException
328 static function saveNew($profile_id, $content, $source, $options=null) {
329 $defaults = array('uri' => null,
334 'distribute' => true,
335 'object_type' => null,
338 if (!empty($options) && is_array($options)) {
339 $options = array_merge($defaults, $options);
345 if (!isset($is_local)) {
346 $is_local = Notice::LOCAL_PUBLIC;
349 $profile = Profile::staticGet('id', $profile_id);
350 $user = User::staticGet('id', $profile_id);
352 // Use the local user's shortening preferences, if applicable.
353 $final = $user->shortenLinks($content);
355 $final = common_shorten_links($content);
358 if (Notice::contentTooLong($final)) {
359 // TRANS: Client exception thrown if a notice contains too many characters.
360 throw new ClientException(_('Problem saving notice. Too long.'));
363 if (empty($profile)) {
364 // TRANS: Client exception thrown when trying to save a notice for an unknown user.
365 throw new ClientException(_('Problem saving notice. Unknown user.'));
368 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
369 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
370 // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
371 throw new ClientException(_('Too many notices too fast; take a breather '.
372 'and post again in a few minutes.'));
375 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
376 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
377 // TRANS: Client exception thrown when a user tries to post too many duplicate notices in a given time frame.
378 throw new ClientException(_('Too many duplicate messages too quickly;'.
379 ' take a breather and post again in a few minutes.'));
382 if (!$profile->hasRight(Right::NEWNOTICE)) {
383 common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
385 // TRANS: Client exception thrown when a user tries to post while being banned.
386 throw new ClientException(_('You are banned from posting notices on this site.'), 403);
389 $notice = new Notice();
390 $notice->profile_id = $profile_id;
392 $autosource = common_config('public', 'autosource');
394 // Sandboxed are non-false, but not 1, either
396 if (!$profile->hasRight(Right::PUBLICNOTICE) ||
397 ($source && $autosource && in_array($source, $autosource))) {
398 $notice->is_local = Notice::LOCAL_NONPUBLIC;
400 $notice->is_local = $is_local;
403 if (!empty($created)) {
404 $notice->created = $created;
406 $notice->created = common_sql_now();
409 $notice->content = $final;
411 $notice->source = $source;
415 // Get the groups here so we can figure out replies and such
417 if (!isset($groups)) {
418 $groups = self::groupsFromText($notice->content, $profile);
423 // Handle repeat case
425 if (isset($repeat_of)) {
427 // Check for a private one
429 $repeat = Notice::staticGet('id', $repeat_of);
431 if (empty($repeat)) {
432 // TRANS: Client exception thrown in notice when trying to repeat a missing or deleted notice.
433 throw new ClientException(_('Cannot repeat; original notice is missing or deleted.'));
436 if ($profile->id == $repeat->profile_id) {
437 // TRANS: Client error displayed when trying to repeat an own notice.
438 throw new ClientException(_('You cannot repeat your own notice.'));
441 if ($repeat->scope != Notice::SITE_SCOPE &&
442 $repeat->scope != Notice::PUBLIC_SCOPE) {
443 // TRANS: Client error displayed when trying to repeat a non-public notice.
444 throw new ClientException(_('Cannot repeat a private notice.'), 403);
447 if (!$repeat->inScope($profile)) {
448 // The generic checks above should cover this, but let's be sure!
449 // TRANS: Client error displayed when trying to repeat a notice you cannot access.
450 throw new ClientException(_('Cannot repeat a notice you cannot read.'), 403);
453 if ($profile->hasRepeated($repeat->id)) {
454 // TRANS: Client error displayed when trying to repeat an already repeated notice.
455 throw new ClientException(_('You already repeated that notice.'));
458 $notice->repeat_of = $repeat_of;
460 $reply = self::getReplyTo($reply_to, $profile_id, $source, $final);
462 if (!empty($reply)) {
464 if (!$reply->inScope($profile)) {
465 // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
466 // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
467 throw new ClientException(sprintf(_('%1$s has no access to notice %2$d.'),
468 $profile->nickname, $reply->id), 403);
471 $notice->reply_to = $reply->id;
472 $notice->conversation = $reply->conversation;
474 // If the original is private to a group, and notice has no group specified,
475 // make it to the same group(s)
477 if (empty($groups) && ($reply->scope | Notice::GROUP_SCOPE)) {
479 $replyGroups = $reply->getGroups();
480 foreach ($replyGroups as $group) {
481 if ($profile->isMember($group)) {
482 $groups[] = $group->id;
491 if (!empty($lat) && !empty($lon)) {
496 if (!empty($location_ns) && !empty($location_id)) {
497 $notice->location_id = $location_id;
498 $notice->location_ns = $location_ns;
501 if (!empty($rendered)) {
502 $notice->rendered = $rendered;
504 $notice->rendered = common_render_content($final, $notice);
508 if (!empty($notice->repeat_of)) {
509 $notice->verb = ActivityVerb::SHARE;
510 $notice->object_type = ActivityObject::ACTIVITY;
512 $notice->verb = ActivityVerb::POST;
515 $notice->verb = $verb;
518 if (empty($object_type)) {
519 $notice->object_type = (empty($notice->reply_to)) ? ActivityObject::NOTE : ActivityObject::COMMENT;
521 $notice->object_type = $object_type;
524 if (is_null($scope)) { // 0 is a valid value
525 if (!empty($reply)) {
526 $notice->scope = $reply->scope;
528 $notice->scope = self::defaultScope();
531 $notice->scope = $scope;
534 // For private streams
536 $user = $profile->getUser();
539 if ($user->private_stream &&
540 ($notice->scope == Notice::PUBLIC_SCOPE ||
541 $notice->scope == Notice::SITE_SCOPE)) {
542 $notice->scope |= Notice::FOLLOWER_SCOPE;
546 // Force the scope for private groups
548 foreach ($groups as $groupId) {
549 $group = User_group::staticGet('id', $groupId);
550 if (!empty($group)) {
551 if ($group->force_scope) {
552 $notice->scope |= Notice::GROUP_SCOPE;
558 if (Event::handle('StartNoticeSave', array(&$notice))) {
560 // XXX: some of these functions write to the DB
562 $id = $notice->insert();
565 common_log_db_error($notice, 'INSERT', __FILE__);
566 // TRANS: Server exception thrown when a notice cannot be saved.
567 throw new ServerException(_('Problem saving notice.'));
570 // Update ID-dependent columns: URI, conversation
572 $orig = clone($notice);
577 $notice->uri = common_notice_uri($notice);
581 // If it's not part of a conversation, it's
582 // the beginning of a new conversation.
584 if (empty($notice->conversation)) {
585 $conv = Conversation::create();
586 $notice->conversation = $conv->id;
591 if (!$notice->update($orig)) {
592 common_log_db_error($notice, 'UPDATE', __FILE__);
593 // TRANS: Server exception thrown when a notice cannot be updated.
594 throw new ServerException(_('Problem saving notice.'));
600 // Clear the cache for subscribed users, so they'll update at next request
601 // XXX: someone clever could prepend instead of clearing the cache
603 $notice->blowOnInsert();
605 // Save per-notice metadata...
607 if (isset($replies)) {
608 $notice->saveKnownReplies($replies);
610 $notice->saveReplies();
614 $notice->saveKnownTags($tags);
619 // Note: groups may save tags, so must be run after tags are saved
620 // to avoid errors on duplicates.
621 // Note: groups should always be set.
623 $notice->saveKnownGroups($groups);
626 $notice->saveKnownUrls($urls);
632 // Prepare inbox delivery, may be queued to background.
633 $notice->distribute();
639 function blowOnInsert($conversation = false)
641 $this->blowStream('profile:notice_ids:%d', $this->profile_id);
643 if ($this->isPublic()) {
644 $this->blowStream('public');
647 self::blow('notice:list-ids:conversation:%s', $this->conversation);
648 self::blow('conversation:notice_count:%d', $this->conversation);
650 if (!empty($this->repeat_of)) {
651 // XXX: we should probably only use one of these
652 $this->blowStream('notice:repeats:%d', $this->repeat_of);
653 self::blow('notice:list-ids:repeat_of:%d', $this->repeat_of);
656 $original = Notice::staticGet('id', $this->repeat_of);
658 if (!empty($original)) {
659 $originalUser = User::staticGet('id', $original->profile_id);
660 if (!empty($originalUser)) {
661 $this->blowStream('user:repeats_of_me:%d', $originalUser->id);
665 $profile = Profile::staticGet($this->profile_id);
667 if (!empty($profile)) {
668 $profile->blowNoticeCount();
671 $ptags = $this->getProfileTags();
672 foreach ($ptags as $ptag) {
673 $ptag->blowNoticeStreamCache();
678 * Clear cache entries related to this notice at delete time.
679 * Necessary to avoid breaking paging on public, profile timelines.
681 function blowOnDelete()
683 $this->blowOnInsert();
685 self::blow('profile:notice_ids:%d;last', $this->profile_id);
687 if ($this->isPublic()) {
688 self::blow('public;last');
691 self::blow('fave:by_notice', $this->id);
693 if ($this->conversation) {
694 // In case we're the first, will need to calc a new root.
695 self::blow('notice:conversation_root:%d', $this->conversation);
698 $ptags = $this->getProfileTags();
699 foreach ($ptags as $ptag) {
700 $ptag->blowNoticeStreamCache(true);
704 function blowStream()
706 $c = self::memcache();
712 $args = func_get_args();
714 $format = array_shift($args);
716 $keyPart = vsprintf($format, $args);
718 $cacheKey = Cache::key($keyPart);
720 $c->delete($cacheKey);
722 // delete the "last" stream, too, if this notice is
723 // older than the top of that stream
725 $lastKey = $cacheKey.';last';
727 $lastStr = $c->get($lastKey);
729 if ($lastStr !== false) {
730 $window = explode(',', $lastStr);
731 $lastID = $window[0];
732 $lastNotice = Notice::staticGet('id', $lastID);
733 if (empty($lastNotice) // just weird
734 || strtotime($lastNotice->created) >= strtotime($this->created)) {
735 $c->delete($lastKey);
740 /** save all urls in the notice to the db
742 * follow redirects and save all available file information
743 * (mimetype, date, size, oembed, etc.)
747 function saveUrls() {
748 if (common_config('attachments', 'process_links')) {
749 common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
754 * Save the given URLs as related links/attachments to the db
756 * follow redirects and save all available file information
757 * (mimetype, date, size, oembed, etc.)
761 function saveKnownUrls($urls)
763 if (common_config('attachments', 'process_links')) {
764 // @fixme validation?
765 foreach (array_unique($urls) as $url) {
766 File::processNew($url, $this->id);
774 function saveUrl($url, $notice_id) {
775 File::processNew($url, $notice_id);
778 static function checkDupes($profile_id, $content) {
779 $profile = Profile::staticGet($profile_id);
780 if (empty($profile)) {
783 $notice = $profile->getNotices(0, CachingNoticeStream::CACHE_WINDOW);
784 if (!empty($notice)) {
786 while ($notice->fetch()) {
787 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
789 } else if ($notice->content == $content) {
794 // If we get here, oldest item in cache window is not
795 // old enough for dupe limit; do direct check against DB
796 $notice = new Notice();
797 $notice->profile_id = $profile_id;
798 $notice->content = $content;
799 $threshold = common_sql_date(time() - common_config('site', 'dupelimit'));
800 $notice->whereAdd(sprintf("created > '%s'", $notice->escape($threshold)));
802 $cnt = $notice->count();
806 static function checkEditThrottle($profile_id) {
807 $profile = Profile::staticGet($profile_id);
808 if (empty($profile)) {
811 // Get the Nth notice
812 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
813 if ($notice && $notice->fetch()) {
814 // If the Nth notice was posted less than timespan seconds ago
815 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
820 // Either not N notices in the stream, OR the Nth was not posted within timespan seconds
824 protected $_attachments = -1;
826 function attachments() {
828 if ($this->_attachments != -1) {
829 return $this->_attachments;
832 $f2ps = Memcached_DataObject::listGet('File_to_post', 'post_id', array($this->id));
836 foreach ($f2ps[$this->id] as $f2p) {
837 $ids[] = $f2p->file_id;
840 $files = Memcached_DataObject::multiGet('File', 'id', $ids);
842 $this->_attachments = $files->fetchAll();
844 return $this->_attachments;
847 function _setAttachments($attachments)
849 $this->_attachments = $attachments;
852 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0)
854 $stream = new PublicNoticeStream();
855 return $stream->getNotices($offset, $limit, $since_id, $max_id);
859 function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0)
861 $stream = new ConversationNoticeStream($id);
863 return $stream->getNotices($offset, $limit, $since_id, $max_id);
867 * Is this notice part of an active conversation?
869 * @return boolean true if other messages exist in the same
870 * conversation, false if this is the only one
872 function hasConversation()
874 if (!empty($this->conversation)) {
875 $conversation = Notice::conversationStream(
881 if ($conversation->N > 0) {
889 * Grab the earliest notice from this conversation.
891 * @return Notice or null
893 function conversationRoot($profile=-1)
895 // XXX: can this happen?
897 if (empty($this->conversation)) {
901 // Get the current profile if not specified
903 if (is_int($profile) && $profile == -1) {
904 $profile = Profile::current();
907 // If this notice is out of scope, no root for you!
909 if (!$this->inScope($profile)) {
913 // If this isn't a reply to anything, then it's its own
916 if (empty($this->reply_to)) {
920 if (is_null($profile)) {
921 $keypart = sprintf('notice:conversation_root:%d:null', $this->id);
923 $keypart = sprintf('notice:conversation_root:%d:%d',
928 $root = self::cacheGet($keypart);
930 if ($root !== false && $root->inScope($profile)) {
936 $parent = $last->getOriginal();
937 if (!empty($parent) && $parent->inScope($profile)) {
944 } while (!empty($parent));
946 self::cacheSet($keypart, $root);
953 * Pull up a full list of local recipients who will be getting
954 * this notice in their inbox. Results will be cached, so don't
955 * change the input data wily-nilly!
957 * @param array $groups optional list of Group objects;
958 * if left empty, will be loaded from group_inbox records
959 * @param array $recipient optional list of reply profile ids
960 * if left empty, will be loaded from reply records
961 * @return array associating recipient user IDs with an inbox source constant
963 function whoGets($groups=null, $recipients=null)
965 $c = self::memcache();
968 $ni = $c->get(Cache::key('notice:who_gets:'.$this->id));
974 if (is_null($groups)) {
975 $groups = $this->getGroups();
978 if (is_null($recipients)) {
979 $recipients = $this->getReplies();
982 $users = $this->getSubscribedUsers();
983 $ptags = $this->getProfileTags();
985 // FIXME: kind of ignoring 'transitional'...
986 // we'll probably stop supporting inboxless mode
991 // Give plugins a chance to add folks in at start...
992 if (Event::handle('StartNoticeWhoGets', array($this, &$ni))) {
994 foreach ($users as $id) {
995 $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
998 foreach ($groups as $group) {
999 $users = $group->getUserMembers();
1000 foreach ($users as $id) {
1001 if (!array_key_exists($id, $ni)) {
1002 $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
1007 foreach ($ptags as $ptag) {
1008 $users = $ptag->getUserSubscribers();
1009 foreach ($users as $id) {
1010 if (!array_key_exists($id, $ni)) {
1011 $ni[$id] = NOTICE_INBOX_SOURCE_PROFILE_TAG;
1016 foreach ($recipients as $recipient) {
1017 if (!array_key_exists($recipient, $ni)) {
1018 $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
1022 // Exclude any deleted, non-local, or blocking recipients.
1023 $profile = $this->getProfile();
1024 $originalProfile = null;
1025 if ($this->repeat_of) {
1026 // Check blocks against the original notice's poster as well.
1027 $original = Notice::staticGet('id', $this->repeat_of);
1029 $originalProfile = $original->getProfile();
1033 foreach ($ni as $id => $source) {
1034 $user = User::staticGet('id', $id);
1035 if (empty($user) || $user->hasBlocked($profile) ||
1036 ($originalProfile && $user->hasBlocked($originalProfile))) {
1041 // Give plugins a chance to filter out...
1042 Event::handle('EndNoticeWhoGets', array($this, &$ni));
1046 // XXX: pack this data better
1047 $c->set(Cache::key('notice:who_gets:'.$this->id), $ni);
1054 * Adds this notice to the inboxes of each local user who should receive
1055 * it, based on author subscriptions, group memberships, and @-replies.
1057 * Warning: running a second time currently will make items appear
1058 * multiple times in users' inboxes.
1060 * @fixme make more robust against errors
1061 * @fixme break up massive deliveries to smaller background tasks
1063 * @param array $groups optional list of Group objects;
1064 * if left empty, will be loaded from group_inbox records
1065 * @param array $recipient optional list of reply profile ids
1066 * if left empty, will be loaded from reply records
1068 function addToInboxes($groups=null, $recipients=null)
1070 $ni = $this->whoGets($groups, $recipients);
1072 $ids = array_keys($ni);
1074 // We remove the author (if they're a local user),
1075 // since we'll have already done this in distribute()
1077 $i = array_search($this->profile_id, $ids);
1085 Inbox::bulkInsert($this->id, $ids);
1090 function getSubscribedUsers()
1094 if(common_config('db','quote_identifiers'))
1095 $user_table = '"user"';
1096 else $user_table = 'user';
1100 'FROM '. $user_table .' JOIN subscription '.
1101 'ON '. $user_table .'.id = subscription.subscriber ' .
1102 'WHERE subscription.subscribed = %d ';
1104 $user->query(sprintf($qry, $this->profile_id));
1108 while ($user->fetch()) {
1117 function getProfileTags()
1119 $profile = $this->getProfile();
1120 $list = $profile->getOtherTags($profile);
1123 while($list->fetch()) {
1124 $ptags[] = clone($list);
1131 * Record this notice to the given group inboxes for delivery.
1132 * Overrides the regular parsing of !group markup.
1134 * @param string $group_ids
1135 * @fixme might prefer URIs as identifiers, as for replies?
1136 * best with generalizations on user_group to support
1137 * remote groups better.
1139 function saveKnownGroups($group_ids)
1141 if (!is_array($group_ids)) {
1142 // TRANS: Server exception thrown when no array is provided to the method saveKnownGroups().
1143 throw new ServerException(_('Bad type provided to saveKnownGroups.'));
1147 foreach (array_unique($group_ids) as $id) {
1148 $group = User_group::staticGet('id', $id);
1150 common_log(LOG_ERR, "Local delivery to group id $id, $group->nickname");
1151 $result = $this->addToGroupInbox($group);
1153 common_log_db_error($gi, 'INSERT', __FILE__);
1156 if (common_config('group', 'addtag')) {
1157 // we automatically add a tag for every group name, too
1159 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($group->nickname),
1160 'notice_id' => $this->id));
1162 if (is_null($tag)) {
1163 $this->saveTag($group->nickname);
1167 $groups[] = clone($group);
1169 common_log(LOG_ERR, "Local delivery to group id $id skipped, doesn't exist");
1177 * Parse !group delivery and record targets into group_inbox.
1178 * @return array of Group objects
1180 function saveGroups()
1182 // Don't save groups for repeats
1184 if (!empty($this->repeat_of)) {
1188 $profile = $this->getProfile();
1190 $groups = self::groupsFromText($this->content, $profile);
1192 /* Add them to the database */
1194 foreach ($groups as $group) {
1195 /* XXX: remote groups. */
1197 if (empty($group)) {
1202 if ($profile->isMember($group)) {
1204 $result = $this->addToGroupInbox($group);
1207 common_log_db_error($gi, 'INSERT', __FILE__);
1210 $groups[] = clone($group);
1217 function addToGroupInbox($group)
1219 $gi = Group_inbox::pkeyGet(array('group_id' => $group->id,
1220 'notice_id' => $this->id));
1224 $gi = new Group_inbox();
1226 $gi->group_id = $group->id;
1227 $gi->notice_id = $this->id;
1228 $gi->created = $this->created;
1230 $result = $gi->insert();
1233 common_log_db_error($gi, 'INSERT', __FILE__);
1234 // TRANS: Server exception thrown when an update for a group inbox fails.
1235 throw new ServerException(_('Problem saving group inbox.'));
1238 self::blow('user_group:notice_ids:%d', $gi->group_id);
1245 * Save reply records indicating that this notice needs to be
1246 * delivered to the local users with the given URIs.
1248 * Since this is expected to be used when saving foreign-sourced
1249 * messages, we won't deliver to any remote targets as that's the
1250 * source service's responsibility.
1252 * Mail notifications etc will be handled later.
1254 * @param array of unique identifier URIs for recipients
1256 function saveKnownReplies($uris)
1262 $sender = Profile::staticGet($this->profile_id);
1264 foreach (array_unique($uris) as $uri) {
1266 $profile = Profile::fromURI($uri);
1268 if (empty($profile)) {
1269 common_log(LOG_WARNING, "Unable to determine profile for URI '$uri'");
1273 if ($profile->hasBlocked($sender)) {
1274 common_log(LOG_INFO, "Not saving reply to profile {$profile->id} ($uri) from sender {$sender->id} because of a block.");
1278 $this->saveReply($profile->id);
1279 self::blow('reply:stream:%d', $profile->id);
1286 * Pull @-replies from this message's content in StatusNet markup format
1287 * and save reply records indicating that this message needs to be
1288 * delivered to those users.
1290 * Mail notifications to local profiles will be sent later.
1292 * @return array of integer profile IDs
1295 function saveReplies()
1297 // Don't save reply data for repeats
1299 if (!empty($this->repeat_of)) {
1303 $sender = Profile::staticGet($this->profile_id);
1307 // If it's a reply, save for the replied-to author
1309 if (!empty($this->reply_to)) {
1310 $original = $this->getOriginal();
1311 if (!empty($original)) { // that'd be weird
1312 $author = $original->getProfile();
1313 if (!empty($author)) {
1314 $this->saveReply($author->id);
1315 $replied[$author->id] = 1;
1316 self::blow('reply:stream:%d', $author->id);
1321 // @todo ideally this parser information would only
1322 // be calculated once.
1324 $mentions = common_find_mentions($this->content, $this);
1326 // store replied only for first @ (what user/notice what the reply directed,
1327 // we assume first @ is it)
1329 foreach ($mentions as $mention) {
1331 foreach ($mention['mentioned'] as $mentioned) {
1333 // skip if they're already covered
1335 if (!empty($replied[$mentioned->id])) {
1339 // Don't save replies from blocked profile to local user
1341 $mentioned_user = User::staticGet('id', $mentioned->id);
1342 if (!empty($mentioned_user) && $mentioned_user->hasBlocked($sender)) {
1346 $this->saveReply($mentioned->id);
1347 $replied[$mentioned->id] = 1;
1348 self::blow('reply:stream:%d', $mentioned->id);
1352 $recipientIds = array_keys($replied);
1354 return $recipientIds;
1357 function saveReply($profileId)
1359 $reply = new Reply();
1361 $reply->notice_id = $this->id;
1362 $reply->profile_id = $profileId;
1363 $reply->modified = $this->created;
1370 protected $_replies = -1;
1373 * Pull the complete list of @-reply targets for this notice.
1375 * @return array of integer profile ids
1377 function getReplies()
1379 if ($this->_replies != -1) {
1380 return $this->_replies;
1383 $replyMap = Memcached_DataObject::listGet('Reply', 'notice_id', array($this->id));
1387 foreach ($replyMap[$this->id] as $reply) {
1388 $ids[] = $reply->profile_id;
1391 $this->_replies = $ids;
1396 function _setReplies($replies)
1398 $this->_replies = $replies;
1402 * Pull the complete list of @-reply targets for this notice.
1404 * @return array of Profiles
1406 function getReplyProfiles()
1408 $ids = $this->getReplies();
1410 $profiles = Profile::multiGet('id', $ids);
1412 return $profiles->fetchAll();
1416 * Send e-mail notifications to local @-reply targets.
1418 * Replies must already have been saved; this is expected to be run
1419 * from the distrib queue handler.
1421 function sendReplyNotifications()
1423 // Don't send reply notifications for repeats
1425 if (!empty($this->repeat_of)) {
1429 $recipientIds = $this->getReplies();
1431 foreach ($recipientIds as $recipientId) {
1432 $user = User::staticGet('id', $recipientId);
1433 if (!empty($user)) {
1434 mail_notify_attn($user, $this);
1440 * Pull list of groups this notice needs to be delivered to,
1441 * as previously recorded by saveGroups() or saveKnownGroups().
1443 * @return array of Group objects
1446 protected $_groups = -1;
1448 function getGroups()
1450 // Don't save groups for repeats
1452 if (!empty($this->repeat_of)) {
1456 if ($this->_groups != -1)
1458 return $this->_groups;
1461 $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', array($this->id));
1465 foreach ($gis[$this->id] as $gi)
1467 $ids[] = $gi->group_id;
1470 $groups = User_group::multiGet('id', $ids);
1472 $this->_groups = $groups->fetchAll();
1474 return $this->_groups;
1477 function _setGroups($groups)
1479 $this->_groups = $groups;
1483 * Convert a notice into an activity for export.
1485 * @param User $cur Current user
1487 * @return Activity activity object representing this Notice.
1490 function asActivity($cur)
1492 $act = self::cacheGet(Cache::codeKey('notice:as-activity:'.$this->id));
1497 $act = new Activity();
1499 if (Event::handle('StartNoticeAsActivity', array($this, &$act))) {
1501 $act->id = $this->uri;
1502 $act->time = strtotime($this->created);
1503 $act->link = $this->bestUrl();
1504 $act->content = common_xml_safe_str($this->rendered);
1506 $profile = $this->getProfile();
1508 $act->actor = ActivityObject::fromProfile($profile);
1509 $act->actor->extra[] = $profile->profileInfo($cur);
1511 $act->verb = $this->verb;
1513 if ($this->repeat_of) {
1514 $repeated = Notice::staticGet('id', $this->repeat_of);
1515 if (!empty($repeated)) {
1516 $act->objects[] = $repeated->asActivity($cur);
1519 $act->objects[] = ActivityObject::fromNotice($this);
1522 // XXX: should this be handled by default processing for object entry?
1526 $tags = $this->getTags();
1528 foreach ($tags as $tag) {
1529 $cat = new AtomCategory();
1532 $act->categories[] = $cat;
1536 // XXX: use Atom Media and/or File activity objects instead
1538 $attachments = $this->attachments();
1540 foreach ($attachments as $attachment) {
1541 // Save local attachments
1542 if (!empty($attachment->filename)) {
1543 $act->attachments[] = ActivityObject::fromFile($attachment);
1547 $ctx = new ActivityContext();
1549 if (!empty($this->reply_to)) {
1550 $reply = Notice::staticGet('id', $this->reply_to);
1551 if (!empty($reply)) {
1552 $ctx->replyToID = $reply->uri;
1553 $ctx->replyToUrl = $reply->bestUrl();
1557 $ctx->location = $this->getLocation();
1561 if (!empty($this->conversation)) {
1562 $conv = Conversation::staticGet('id', $this->conversation);
1563 if (!empty($conv)) {
1564 $ctx->conversation = $conv->uri;
1568 $reply_ids = $this->getReplies();
1570 foreach ($reply_ids as $id) {
1571 $rprofile = Profile::staticGet('id', $id);
1572 if (!empty($rprofile)) {
1573 $ctx->attention[] = $rprofile->getUri();
1574 $ctx->attentionType[$rprofile->getUri()] = ActivityObject::PERSON;
1578 $groups = $this->getGroups();
1580 foreach ($groups as $group) {
1581 $ctx->attention[] = $group->getUri();
1582 $ctx->attentionType[$group->getUri()] = ActivityObject::GROUP;
1585 switch ($this->scope) {
1586 case Notice::PUBLIC_SCOPE:
1587 $ctx->attention[] = "http://activityschema.org/collection/public";
1588 $ctx->attentionType["http://activityschema.org/collection/public"] = ActivityObject::COLLECTION;
1590 case Notice::FOLLOWER_SCOPE:
1591 $surl = common_local_url("subscribers", array('nickname' => $profile->nickname));
1592 $ctx->attention[] = $surl;
1593 $ctx->attentionType[$surl] = ActivityObject::COLLECTION;
1597 // XXX: deprecated; use ActivityVerb::SHARE instead
1601 if (!empty($this->repeat_of)) {
1602 $repeat = Notice::staticGet('id', $this->repeat_of);
1603 if (!empty($repeat)) {
1604 $ctx->forwardID = $repeat->uri;
1605 $ctx->forwardUrl = $repeat->bestUrl();
1609 $act->context = $ctx;
1613 $atom_feed = $profile->getAtomFeed();
1615 if (!empty($atom_feed)) {
1617 $act->source = new ActivitySource();
1619 // XXX: we should store the actual feed ID
1621 $act->source->id = $atom_feed;
1623 // XXX: we should store the actual feed title
1625 $act->source->title = $profile->getBestName();
1627 $act->source->links['alternate'] = $profile->profileurl;
1628 $act->source->links['self'] = $atom_feed;
1630 $act->source->icon = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
1632 $notice = $profile->getCurrentNotice();
1634 if (!empty($notice)) {
1635 $act->source->updated = self::utcDate($notice->created);
1638 $user = User::staticGet('id', $profile->id);
1640 if (!empty($user)) {
1641 $act->source->links['license'] = common_config('license', 'url');
1645 if ($this->isLocal()) {
1646 $act->selfLink = common_local_url('ApiStatusesShow', array('id' => $this->id,
1647 'format' => 'atom'));
1648 $act->editLink = $act->selfLink;
1651 Event::handle('EndNoticeAsActivity', array($this, &$act));
1654 self::cacheSet(Cache::codeKey('notice:as-activity:'.$this->id), $act);
1659 // This has gotten way too long. Needs to be sliced up into functional bits
1660 // or ideally exported to a utility class.
1662 function asAtomEntry($namespace=false,
1667 $act = $this->asActivity($cur);
1668 $act->extra[] = $this->noticeInfo($cur);
1669 return $act->asString($namespace, $author, $source);
1673 * Extra notice info for atom entries
1675 * Clients use some extra notice info in the atom stream.
1676 * This gives it to them.
1678 * @param User $cur Current user
1680 * @return array representation of <statusnet:notice_info> element
1683 function noticeInfo($cur)
1685 // local notice ID (useful to clients for ordering)
1687 $noticeInfoAttr = array('local_id' => $this->id);
1691 $ns = $this->getSource();
1694 $noticeInfoAttr['source'] = $ns->code;
1695 if (!empty($ns->url)) {
1696 $noticeInfoAttr['source_link'] = $ns->url;
1697 if (!empty($ns->name)) {
1698 $noticeInfoAttr['source'] = '<a href="'
1699 . htmlspecialchars($ns->url)
1700 . '" rel="nofollow">'
1701 . htmlspecialchars($ns->name)
1707 // favorite and repeated
1710 $noticeInfoAttr['favorite'] = ($cur->hasFave($this)) ? "true" : "false";
1711 $cp = $cur->getProfile();
1712 $noticeInfoAttr['repeated'] = ($cp->hasRepeated($this->id)) ? "true" : "false";
1715 if (!empty($this->repeat_of)) {
1716 $noticeInfoAttr['repeat_of'] = $this->repeat_of;
1719 return array('statusnet:notice_info', $noticeInfoAttr, null);
1723 * Returns an XML string fragment with a reference to a notice as an
1724 * Activity Streams noun object with the given element type.
1726 * Assumes that 'activity' namespace has been previously defined.
1728 * @param string $element one of 'subject', 'object', 'target'
1732 function asActivityNoun($element)
1734 $noun = ActivityObject::fromNotice($this);
1735 return $noun->asString('activity:' . $element);
1740 if (!empty($this->url)) {
1742 } else if (!empty($this->uri) && preg_match('/^https?:/', $this->uri)) {
1745 return common_local_url('shownotice',
1746 array('notice' => $this->id));
1752 * Determine which notice, if any, a new notice is in reply to.
1754 * For conversation tracking, we try to see where this notice fits
1755 * in the tree. Rough algorithm is:
1757 * if (reply_to is set and valid) {
1759 * } else if ((source not API or Web) and (content starts with "T NAME" or "@name ")) {
1760 * return ID of last notice by initial @name in content;
1763 * Note that all @nickname instances will still be used to save "reply" records,
1764 * so the notice shows up in the mentioned users' "replies" tab.
1766 * @param integer $reply_to ID passed in by Web or API
1767 * @param integer $profile_id ID of author
1768 * @param string $source Source tag, like 'web' or 'gwibber'
1769 * @param string $content Final notice content
1771 * @return integer ID of replied-to notice, or null for not a reply.
1774 static function getReplyTo($reply_to, $profile_id, $source, $content)
1776 static $lb = array('xmpp', 'mail', 'sms', 'omb');
1778 // If $reply_to is specified, we check that it exists, and then
1779 // return it if it does
1781 if (!empty($reply_to)) {
1782 $reply_notice = Notice::staticGet('id', $reply_to);
1783 if (!empty($reply_notice)) {
1784 return $reply_notice;
1788 // If it's not a "low bandwidth" source (one where you can't set
1789 // a reply_to argument), we return. This is mostly web and API
1792 if (!in_array($source, $lb)) {
1796 // Is there an initial @ or T?
1798 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $content, $match) ||
1799 preg_match('/^@([a-z0-9]{1,64})\s+/', $content, $match)) {
1800 $nickname = common_canonical_nickname($match[1]);
1805 // Figure out who that is.
1807 $sender = Profile::staticGet('id', $profile_id);
1808 if (empty($sender)) {
1812 $recipient = common_relative_profile($sender, $nickname, common_sql_now());
1814 if (empty($recipient)) {
1818 // Get their last notice
1820 $last = $recipient->getCurrentNotice();
1822 if (!empty($last)) {
1829 static function maxContent()
1831 $contentlimit = common_config('notice', 'contentlimit');
1832 // null => use global limit (distinct from 0!)
1833 if (is_null($contentlimit)) {
1834 $contentlimit = common_config('site', 'textlimit');
1836 return $contentlimit;
1839 static function contentTooLong($content)
1841 $contentlimit = self::maxContent();
1842 return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
1845 function getLocation()
1849 if (!empty($this->location_id) && !empty($this->location_ns)) {
1850 $location = Location::fromId($this->location_id, $this->location_ns);
1853 if (is_null($location)) { // no ID, or Location::fromId() failed
1854 if (!empty($this->lat) && !empty($this->lon)) {
1855 $location = Location::fromLatLon($this->lat, $this->lon);
1863 * Convenience function for posting a repeat of an existing message.
1865 * @param int $repeater_id: profile ID of user doing the repeat
1866 * @param string $source: posting source key, eg 'web', 'api', etc
1869 * @throws Exception on failure or permission problems
1871 function repeat($repeater_id, $source)
1873 $author = Profile::staticGet('id', $this->profile_id);
1875 // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'.
1876 // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice.
1877 $content = sprintf(_('RT @%1$s %2$s'),
1881 $maxlen = common_config('site', 'textlimit');
1882 if ($maxlen > 0 && mb_strlen($content) > $maxlen) {
1883 // Web interface and current Twitter API clients will
1884 // pull the original notice's text, but some older
1885 // clients and RSS/Atom feeds will see this trimmed text.
1887 // Unfortunately this is likely to lose tags or URLs
1888 // at the end of long notices.
1889 $content = mb_substr($content, 0, $maxlen - 4) . ' ...';
1892 // Scope is same as this one's
1894 return self::saveNew($repeater_id,
1897 array('repeat_of' => $this->id,
1898 'scope' => $this->scope));
1901 // These are supposed to be in chron order!
1903 function repeatStream($limit=100)
1905 $cache = Cache::instance();
1907 if (empty($cache)) {
1908 $ids = $this->_repeatStreamDirect($limit);
1910 $idstr = $cache->get(Cache::key('notice:repeats:'.$this->id));
1911 if ($idstr !== false) {
1912 if (empty($idstr)) {
1915 $ids = explode(',', $idstr);
1918 $ids = $this->_repeatStreamDirect(100);
1919 $cache->set(Cache::key('notice:repeats:'.$this->id), implode(',', $ids));
1922 // We do a max of 100, so slice down to limit
1923 $ids = array_slice($ids, 0, $limit);
1927 return NoticeStream::getStreamByIds($ids);
1930 function _repeatStreamDirect($limit)
1932 $notice = new Notice();
1934 $notice->selectAdd(); // clears it
1935 $notice->selectAdd('id');
1937 $notice->repeat_of = $this->id;
1939 $notice->orderBy('created, id'); // NB: asc!
1941 if (!is_null($limit)) {
1942 $notice->limit(0, $limit);
1945 return $notice->fetchAll('id');
1948 function locationOptions($lat, $lon, $location_id, $location_ns, $profile = null)
1952 if (!empty($location_id) && !empty($location_ns)) {
1953 $options['location_id'] = $location_id;
1954 $options['location_ns'] = $location_ns;
1956 $location = Location::fromId($location_id, $location_ns);
1958 if (!empty($location)) {
1959 $options['lat'] = $location->lat;
1960 $options['lon'] = $location->lon;
1963 } else if (!empty($lat) && !empty($lon)) {
1964 $options['lat'] = $lat;
1965 $options['lon'] = $lon;
1967 $location = Location::fromLatLon($lat, $lon);
1969 if (!empty($location)) {
1970 $options['location_id'] = $location->location_id;
1971 $options['location_ns'] = $location->location_ns;
1973 } else if (!empty($profile)) {
1974 if (isset($profile->lat) && isset($profile->lon)) {
1975 $options['lat'] = $profile->lat;
1976 $options['lon'] = $profile->lon;
1979 if (isset($profile->location_id) && isset($profile->location_ns)) {
1980 $options['location_id'] = $profile->location_id;
1981 $options['location_ns'] = $profile->location_ns;
1988 function clearReplies()
1990 $replyNotice = new Notice();
1991 $replyNotice->reply_to = $this->id;
1993 //Null any notices that are replies to this notice
1995 if ($replyNotice->find()) {
1996 while ($replyNotice->fetch()) {
1997 $orig = clone($replyNotice);
1998 $replyNotice->reply_to = null;
1999 $replyNotice->update($orig);
2005 $reply = new Reply();
2006 $reply->notice_id = $this->id;
2008 if ($reply->find()) {
2009 while($reply->fetch()) {
2010 self::blow('reply:stream:%d', $reply->profile_id);
2018 function clearFiles()
2020 $f2p = new File_to_post();
2022 $f2p->post_id = $this->id;
2025 while ($f2p->fetch()) {
2029 // FIXME: decide whether to delete File objects
2030 // ...and related (actual) files
2033 function clearRepeats()
2035 $repeatNotice = new Notice();
2036 $repeatNotice->repeat_of = $this->id;
2038 //Null any notices that are repeats of this notice
2040 if ($repeatNotice->find()) {
2041 while ($repeatNotice->fetch()) {
2042 $orig = clone($repeatNotice);
2043 $repeatNotice->repeat_of = null;
2044 $repeatNotice->update($orig);
2049 function clearFaves()
2052 $fave->notice_id = $this->id;
2054 if ($fave->find()) {
2055 while ($fave->fetch()) {
2056 self::blow('fave:ids_by_user_own:%d', $fave->user_id);
2057 self::blow('fave:ids_by_user_own:%d;last', $fave->user_id);
2058 self::blow('fave:ids_by_user:%d', $fave->user_id);
2059 self::blow('fave:ids_by_user:%d;last', $fave->user_id);
2067 function clearTags()
2069 $tag = new Notice_tag();
2070 $tag->notice_id = $this->id;
2073 while ($tag->fetch()) {
2074 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, Cache::keyize($tag->tag));
2075 self::blow('profile:notice_ids_tagged:%d:%s;last', $this->profile_id, Cache::keyize($tag->tag));
2076 self::blow('notice_tag:notice_ids:%s', Cache::keyize($tag->tag));
2077 self::blow('notice_tag:notice_ids:%s;last', Cache::keyize($tag->tag));
2085 function clearGroupInboxes()
2087 $gi = new Group_inbox();
2089 $gi->notice_id = $this->id;
2092 while ($gi->fetch()) {
2093 self::blow('user_group:notice_ids:%d', $gi->group_id);
2101 function distribute()
2103 // We always insert for the author so they don't
2105 Event::handle('StartNoticeDistribute', array($this));
2107 $user = User::staticGet('id', $this->profile_id);
2108 if (!empty($user)) {
2109 Inbox::insertNotice($user->id, $this->id);
2112 if (common_config('queue', 'inboxes')) {
2113 // If there's a failure, we want to _force_
2114 // distribution at this point.
2116 $qm = QueueManager::get();
2117 $qm->enqueue($this, 'distrib');
2118 } catch (Exception $e) {
2119 // If the exception isn't transient, this
2120 // may throw more exceptions as DQH does
2121 // its own enqueueing. So, we ignore them!
2123 $handler = new DistribQueueHandler();
2124 $handler->handle($this);
2125 } catch (Exception $e) {
2126 common_log(LOG_ERR, "emergency redistribution resulted in " . $e->getMessage());
2128 // Re-throw so somebody smarter can handle it.
2132 $handler = new DistribQueueHandler();
2133 $handler->handle($this);
2139 $result = parent::insert();
2142 // Profile::hasRepeated() abuses pkeyGet(), so we
2143 // have to clear manually
2144 if (!empty($this->repeat_of)) {
2145 $c = self::memcache();
2147 $ck = self::multicacheKey('Notice',
2148 array('profile_id' => $this->profile_id,
2149 'repeat_of' => $this->repeat_of));
2159 * Get the source of the notice
2161 * @return Notice_source $ns A notice source object. 'code' is the only attribute
2162 * guaranteed to be populated.
2164 function getSource()
2166 $ns = new Notice_source();
2167 if (!empty($this->source)) {
2168 switch ($this->source) {
2175 $ns->code = $this->source;
2178 $ns = Notice_source::staticGet($this->source);
2180 $ns = new Notice_source();
2181 $ns->code = $this->source;
2182 $app = Oauth_application::staticGet('name', $this->source);
2184 $ns->name = $app->name;
2185 $ns->url = $app->source_url;
2195 * Determine whether the notice was locally created
2197 * @return boolean locality
2200 public function isLocal()
2202 return ($this->is_local == Notice::LOCAL_PUBLIC ||
2203 $this->is_local == Notice::LOCAL_NONPUBLIC);
2207 * Get the list of hash tags saved with this notice.
2209 * @return array of strings
2211 public function getTags()
2215 $keypart = sprintf('notice:tags:%d', $this->id);
2217 $tagstr = self::cacheGet($keypart);
2219 if ($tagstr !== false) {
2220 $tags = explode(',', $tagstr);
2222 $tag = new Notice_tag();
2223 $tag->notice_id = $this->id;
2225 while ($tag->fetch()) {
2226 $tags[] = $tag->tag;
2229 self::cacheSet($keypart, implode(',', $tags));
2235 static private function utcDate($dt)
2237 $dateStr = date('d F Y H:i:s', strtotime($dt));
2238 $d = new DateTime($dateStr, new DateTimeZone('UTC'));
2239 return $d->format(DATE_W3C);
2243 * Look up the creation timestamp for a given notice ID, even
2244 * if it's been deleted.
2247 * @return mixed string recorded creation timestamp, or false if can't be found
2249 public static function getAsTimestamp($id)
2255 $notice = Notice::staticGet('id', $id);
2257 return $notice->created;
2260 $deleted = Deleted_notice::staticGet('id', $id);
2262 return $deleted->created;
2269 * Build an SQL 'where' fragment for timestamp-based sorting from a since_id
2270 * parameter, matching notices posted after the given one (exclusive).
2272 * If the referenced notice can't be found, will return false.
2275 * @param string $idField
2276 * @param string $createdField
2277 * @return mixed string or false if no match
2279 public static function whereSinceId($id, $idField='id', $createdField='created')
2281 $since = Notice::getAsTimestamp($id);
2283 return sprintf("($createdField = '%s' and $idField > %d) or ($createdField > '%s')", $since, $id, $since);
2289 * Build an SQL 'where' fragment for timestamp-based sorting from a since_id
2290 * parameter, matching notices posted after the given one (exclusive), and
2291 * if necessary add it to the data object's query.
2293 * @param DB_DataObject $obj
2295 * @param string $idField
2296 * @param string $createdField
2297 * @return mixed string or false if no match
2299 public static function addWhereSinceId(DB_DataObject $obj, $id, $idField='id', $createdField='created')
2301 $since = self::whereSinceId($id, $idField, $createdField);
2303 $obj->whereAdd($since);
2308 * Build an SQL 'where' fragment for timestamp-based sorting from a max_id
2309 * parameter, matching notices posted before the given one (inclusive).
2311 * If the referenced notice can't be found, will return false.
2314 * @param string $idField
2315 * @param string $createdField
2316 * @return mixed string or false if no match
2318 public static function whereMaxId($id, $idField='id', $createdField='created')
2320 $max = Notice::getAsTimestamp($id);
2322 return sprintf("($createdField < '%s') or ($createdField = '%s' and $idField <= %d)", $max, $max, $id);
2328 * Build an SQL 'where' fragment for timestamp-based sorting from a max_id
2329 * parameter, matching notices posted before the given one (inclusive), and
2330 * if necessary add it to the data object's query.
2332 * @param DB_DataObject $obj
2334 * @param string $idField
2335 * @param string $createdField
2336 * @return mixed string or false if no match
2338 public static function addWhereMaxId(DB_DataObject $obj, $id, $idField='id', $createdField='created')
2340 $max = self::whereMaxId($id, $idField, $createdField);
2342 $obj->whereAdd($max);
2348 if (common_config('public', 'localonly')) {
2349 return ($this->is_local == Notice::LOCAL_PUBLIC);
2351 return (($this->is_local != Notice::LOCAL_NONPUBLIC) &&
2352 ($this->is_local != Notice::GATEWAY));
2357 * Check that the given profile is allowed to read, respond to, or otherwise
2358 * act on this notice.
2360 * The $scope member is a bitmask of scopes, representing a logical AND of the
2361 * scope requirement. So, 0x03 (Notice::ADDRESSEE_SCOPE | Notice::SITE_SCOPE) means
2362 * "only visible to people who are mentioned in the notice AND are users on this site."
2363 * Users on the site who are not mentioned in the notice will not be able to see the
2366 * @param Profile $profile The profile to check; pass null to check for public/unauthenticated users.
2368 * @return boolean whether the profile is in the notice's scope
2370 function inScope($profile)
2372 if (is_null($profile)) {
2373 $keypart = sprintf('notice:in-scope-for:%d:null', $this->id);
2375 $keypart = sprintf('notice:in-scope-for:%d:%d', $this->id, $profile->id);
2378 $result = self::cacheGet($keypart);
2380 if ($result === false) {
2382 if (Event::handle('StartNoticeInScope', array($this, $profile, &$bResult))) {
2383 $bResult = $this->_inScope($profile);
2384 Event::handle('EndNoticeInScope', array($this, $profile, &$bResult));
2386 $result = ($bResult) ? 1 : 0;
2387 self::cacheSet($keypart, $result, 0, 300);
2390 return ($result == 1) ? true : false;
2393 protected function _inScope($profile)
2395 if (!is_null($this->scope)) {
2396 $scope = $this->scope;
2398 $scope = self::defaultScope();
2401 // If there's no scope, anyone (even anon) is in scope.
2403 if ($scope == 0) { // Not private
2405 return !$this->isHiddenSpam($profile);
2407 } else { // Private, somehow
2409 // If there's scope, anon cannot be in scope
2411 if (empty($profile)) {
2415 // Author is always in scope
2417 if ($this->profile_id == $profile->id) {
2421 // Only for users on this site
2423 if ($scope & Notice::SITE_SCOPE) {
2424 $user = $profile->getUser();
2430 // Only for users mentioned in the notice
2432 if ($scope & Notice::ADDRESSEE_SCOPE) {
2434 $repl = Reply::pkeyGet(array('notice_id' => $this->id,
2435 'profile_id' => $profile->id));
2442 // Only for members of the given group
2444 if ($scope & Notice::GROUP_SCOPE) {
2446 // XXX: just query for the single membership
2448 $groups = $this->getGroups();
2452 foreach ($groups as $group) {
2453 if ($profile->isMember($group)) {
2464 // Only for followers of the author
2468 if ($scope & Notice::FOLLOWER_SCOPE) {
2471 $author = $this->getProfile();
2472 } catch (Exception $e) {
2476 if (!Subscription::exists($profile, $author)) {
2481 return !$this->isHiddenSpam($profile);
2485 function isHiddenSpam($profile) {
2487 // Hide posts by silenced users from everyone but moderators.
2489 if (common_config('notice', 'hidespam')) {
2492 $author = $this->getProfile();
2493 } catch(Exception $e) {
2494 // If we can't get an author, keep it hidden.
2495 // XXX: technically not spam, but, whatever.
2499 if ($author->hasRole(Profile_role::SILENCED)) {
2500 if (empty($profile) || (($profile->id !== $author->id) && (!$profile->hasRight(Right::REVIEWSPAM)))) {
2509 static function groupsFromText($text, $profile)
2513 /* extract all !group */
2514 $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
2522 foreach (array_unique($match[1]) as $nickname) {
2523 $group = User_group::getForNickname($nickname, $profile);
2524 if (!empty($group) && $profile->isMember($group)) {
2525 $groups[] = $group->id;
2532 protected $_original = -1;
2534 function getOriginal()
2536 if (is_int($this->_original) && $this->_original == -1) {
2537 if (empty($this->reply_to)) {
2538 $this->_original = null;
2540 $this->_original = Notice::staticGet('id', $this->reply_to);
2543 return $this->_original;
2547 * Magic function called at serialize() time.
2549 * We use this to drop a couple process-specific references
2550 * from DB_DataObject which can cause trouble in future
2553 * @return array of variable names to include in serialization.
2558 $vars = parent::__sleep();
2559 $skip = array('_original', '_profile', '_groups', '_attachments', '_faves', '_replies', '_repeats');
2560 return array_diff($vars, $skip);
2563 static function defaultScope()
2565 $scope = common_config('notice', 'defaultscope');
2566 if (is_null($scope)) {
2567 if (common_config('site', 'private')) {
2576 static function fillProfiles($notices)
2578 $map = self::getProfiles($notices);
2580 foreach ($notices as $notice) {
2581 if (array_key_exists($notice->profile_id, $map)) {
2582 $notice->_setProfile($map[$notice->profile_id]);
2586 return array_values($map);
2589 static function getProfiles(&$notices)
2592 foreach ($notices as $notice) {
2593 $ids[] = $notice->profile_id;
2596 $ids = array_unique($ids);
2598 return Memcached_DataObject::pivotGet('Profile', 'id', $ids);
2601 static function fillGroups(&$notices)
2603 $ids = self::_idsOf($notices);
2605 $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', $ids);
2609 foreach ($gis as $id => $gi)
2613 $gids[] = $g->group_id;
2617 $gids = array_unique($gids);
2619 $group = Memcached_DataObject::pivotGet('User_group', 'id', $gids);
2621 foreach ($notices as $notice)
2624 $gi = $gis[$notice->id];
2625 foreach ($gi as $g) {
2626 $grps[] = $group[$g->group_id];
2628 $notice->_setGroups($grps);
2632 static function _idsOf(&$notices)
2635 foreach ($notices as $notice) {
2636 $ids[] = $notice->id;
2638 $ids = array_unique($ids);
2642 static function fillAttachments(&$notices)
2644 $ids = self::_idsOf($notices);
2646 $f2pMap = Memcached_DataObject::listGet('File_to_post', 'post_id', $ids);
2650 foreach ($f2pMap as $noticeId => $f2ps) {
2651 foreach ($f2ps as $f2p) {
2652 $fileIds[] = $f2p->file_id;
2656 $fileIds = array_unique($fileIds);
2658 $fileMap = Memcached_DataObject::pivotGet('File', 'id', $fileIds);
2660 foreach ($notices as $notice)
2663 $f2ps = $f2pMap[$notice->id];
2664 foreach ($f2ps as $f2p) {
2665 $files[] = $fileMap[$f2p->file_id];
2667 $notice->_setAttachments($files);
2674 * All faves of this notice
2676 * @return array Array of Fave objects
2681 if (isset($this->_faves) && is_array($this->_faves)) {
2682 return $this->_faves;
2684 $faveMap = Memcached_DataObject::listGet('Fave', 'notice_id', array($this->id));
2685 $this->_faves = $faveMap[$this->id];
2686 return $this->_faves;
2689 function _setFaves($faves)
2691 $this->_faves = $faves;
2694 static function fillFaves(&$notices)
2696 $ids = self::_idsOf($notices);
2697 $faveMap = Memcached_DataObject::listGet('Fave', 'notice_id', $ids);
2700 foreach ($faveMap as $id => $faves) {
2701 $cnt += count($faves);
2702 if (count($faves) > 0) {
2706 foreach ($notices as $notice) {
2707 $faves = $faveMap[$notice->id];
2708 $notice->_setFaves($faves);
2712 static function fillReplies(&$notices)
2714 $ids = self::_idsOf($notices);
2715 $replyMap = Memcached_DataObject::listGet('Reply', 'notice_id', $ids);
2716 foreach ($notices as $notice) {
2717 $replies = $replyMap[$notice->id];
2719 foreach ($replies as $reply) {
2720 $ids[] = $reply->profile_id;
2722 $notice->_setReplies($ids);
2726 protected $_repeats;
2728 function getRepeats()
2730 if (isset($this->_repeats) && is_array($this->_repeats)) {
2731 return $this->_repeats;
2733 $repeatMap = Memcached_DataObject::listGet('Notice', 'repeat_of', array($this->id));
2734 $this->_repeats = $repeatMap[$this->id];
2735 return $this->_repeats;
2738 function _setRepeats($repeats)
2740 $this->_repeats = $repeats;
2743 static function fillRepeats(&$notices)
2745 $ids = self::_idsOf($notices);
2746 $repeatMap = Memcached_DataObject::listGet('Notice', 'repeat_of', $ids);
2747 foreach ($notices as $notice) {
2748 $repeats = $repeatMap[$notice->id];
2749 $notice->_setRepeats($repeats);