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 = -1;
146 public function getProfile()
148 if ($this->_profile === -1) {
149 $this->_setProfile(Profile::getKV('id', $this->profile_id));
151 return $this->_profile;
154 public function _setProfile(Profile $profile=null)
156 if (!$profile instanceof Profile) {
157 throw new NoProfileException($this->profile_id);
159 $this->_profile = $profile;
162 function delete($useWhere=false)
164 // For auditing purposes, save a record that the notice
167 // @fixme we have some cases where things get re-run and so the
169 $deleted = Deleted_notice::getKV('id', $this->id);
171 if (!$deleted instanceof Deleted_notice) {
172 $deleted = Deleted_notice::getKV('uri', $this->uri);
175 if (!$deleted instanceof Deleted_notice) {
176 $deleted = new Deleted_notice();
178 $deleted->id = $this->id;
179 $deleted->profile_id = $this->profile_id;
180 $deleted->uri = $this->uri;
181 $deleted->created = $this->created;
182 $deleted->deleted = common_sql_now();
187 if (Event::handle('NoticeDeleteRelated', array($this))) {
189 // Clear related records
191 $this->clearReplies();
192 $this->clearRepeats();
195 $this->clearGroupInboxes();
197 $this->clearAttentions();
199 // NOTE: we don't clear queue items
202 $result = parent::delete($useWhere);
204 $this->blowOnDelete();
208 public function getUri()
213 public function getLocalUrl()
215 return common_local_url('shownotice', array('notice' => $this->id), null, null, false);
218 public function getUrl()
220 // The risk is we start having empty urls and non-http uris...
221 // and we can't really handle any other protocol right now.
223 case common_valid_http_url($this->url): // should we allow non-http/https URLs?
225 case $this->isLocal():
226 // let's generate a valid link to our locally available notice on demand
227 return common_local_url('shownotice', array('notice' => $this->id), null, null, false);
228 case common_valid_http_url($this->uri):
231 common_debug('No URL available for notice: id='.$this->id);
232 throw new InvalidUrlException($this->url);
236 public function get_object_type($canonical=false) {
238 ? ActivityObject::canonicalType($this->object_type)
239 : $this->object_type;
242 public static function getByUri($uri)
244 $notice = new Notice();
246 if (!$notice->find(true)) {
247 throw new NoResultException($notice);
253 * Extract #hashtags from this notice's content and save them to the database.
257 /* extract all #hastags */
258 $count = preg_match_all('/(?:^|\s)#([\pL\pN_\-\.]{1,64})/u', strtolower($this->content), $match);
263 /* Add them to the database */
264 return $this->saveKnownTags($match[1]);
268 * Record the given set of hash tags in the db for this notice.
269 * Given tag strings will be normalized and checked for dupes.
271 function saveKnownTags($hashtags)
273 //turn each into their canonical tag
274 //this is needed to remove dupes before saving e.g. #hash.tag = #hashtag
275 for($i=0; $i<count($hashtags); $i++) {
276 /* elide characters we don't want in the tag */
277 $hashtags[$i] = common_canonical_tag($hashtags[$i]);
280 foreach(array_unique($hashtags) as $hashtag) {
281 $this->saveTag($hashtag);
282 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, $hashtag);
288 * Record a single hash tag as associated with this notice.
289 * Tag format and uniqueness must be validated by caller.
291 function saveTag($hashtag)
293 $tag = new Notice_tag();
294 $tag->notice_id = $this->id;
295 $tag->tag = $hashtag;
296 $tag->created = $this->created;
297 $id = $tag->insert();
300 // TRANS: Server exception. %s are the error details.
301 throw new ServerException(sprintf(_('Database error inserting hashtag: %s.'),
302 $last_error->message));
306 // if it's saved, blow its cache
307 $tag->blowCache(false);
311 * Save a new notice and push it out to subscribers' inboxes.
312 * Poster's permissions are checked before sending.
314 * @param int $profile_id Profile ID of the poster
315 * @param string $content source message text; links may be shortened
316 * per current user's preference
317 * @param string $source source key ('web', 'api', etc)
318 * @param array $options Associative array of optional properties:
319 * string 'created' timestamp of notice; defaults to now
320 * int 'is_local' source/gateway ID, one of:
321 * Notice::LOCAL_PUBLIC - Local, ok to appear in public timeline
322 * Notice::REMOTE - Sent from a remote service;
323 * hide from public timeline but show in
324 * local "and friends" timelines
325 * Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
326 * Notice::GATEWAY - From another non-OStatus service;
327 * will not appear in public views
328 * float 'lat' decimal latitude for geolocation
329 * float 'lon' decimal longitude for geolocation
330 * int 'location_id' geoname identifier
331 * int 'location_ns' geoname namespace to interpret location_id
332 * int 'reply_to'; notice ID this is a reply to
333 * int 'repeat_of'; notice ID this is a repeat of
334 * string 'uri' unique ID for notice; a unique tag uri (can be url or anything too)
335 * string 'url' permalink to notice; defaults to local notice URL
336 * string 'rendered' rendered HTML version of content
337 * array 'replies' list of profile URIs for reply delivery in
338 * place of extracting @-replies from content.
339 * array 'groups' list of group IDs to deliver to, in place of
340 * extracting ! tags from content
341 * array 'tags' list of hashtag strings to save with the notice
342 * in place of extracting # tags from content
343 * array 'urls' list of attached/referred URLs to save with the
344 * notice in place of extracting links from content
345 * boolean 'distribute' whether to distribute the notice, default true
346 * string 'object_type' URL of the associated object type (default ActivityObject::NOTE)
347 * string 'verb' URL of the associated verb (default ActivityVerb::POST)
348 * int 'scope' Scope bitmask; default to SITE_SCOPE on private sites, 0 otherwise
350 * @fixme tag override
353 * @throws ClientException
355 static function saveNew($profile_id, $content, $source, array $options=null) {
356 $defaults = array('uri' => null,
361 'distribute' => true,
362 'object_type' => null,
365 if (!empty($options) && is_array($options)) {
366 $options = array_merge($defaults, $options);
372 if (!isset($is_local)) {
373 $is_local = Notice::LOCAL_PUBLIC;
376 $profile = Profile::getKV('id', $profile_id);
377 if (!$profile instanceof Profile) {
378 // TRANS: Client exception thrown when trying to save a notice for an unknown user.
379 throw new ClientException(_('Problem saving notice. Unknown user.'));
382 $user = User::getKV('id', $profile_id);
383 if ($user instanceof User) {
384 // Use the local user's shortening preferences, if applicable.
385 $final = $user->shortenLinks($content);
387 $final = common_shorten_links($content);
390 if (Notice::contentTooLong($final)) {
391 // TRANS: Client exception thrown if a notice contains too many characters.
392 throw new ClientException(_('Problem saving notice. Too long.'));
395 if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
396 common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
397 // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
398 throw new ClientException(_('Too many notices too fast; take a breather '.
399 'and post again in a few minutes.'));
402 if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
403 common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
404 // TRANS: Client exception thrown when a user tries to post too many duplicate notices in a given time frame.
405 throw new ClientException(_('Too many duplicate messages too quickly;'.
406 ' take a breather and post again in a few minutes.'));
409 if (!$profile->hasRight(Right::NEWNOTICE)) {
410 common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
412 // TRANS: Client exception thrown when a user tries to post while being banned.
413 throw new ClientException(_('You are banned from posting notices on this site.'), 403);
416 $notice = new Notice();
417 $notice->profile_id = $profile_id;
419 $autosource = common_config('public', 'autosource');
421 // Sandboxed are non-false, but not 1, either
423 if (!$profile->hasRight(Right::PUBLICNOTICE) ||
424 ($source && $autosource && in_array($source, $autosource))) {
425 $notice->is_local = Notice::LOCAL_NONPUBLIC;
427 $notice->is_local = $is_local;
430 if (!empty($created)) {
431 $notice->created = $created;
433 $notice->created = common_sql_now();
436 if (!$notice->isLocal()) {
437 // Only do these checks for non-local notices. Local notices will generate these values later.
438 if (!common_valid_http_url($url)) {
439 common_debug('Bad notice URL: ['.$url.'], URI: ['.$uri.']. Cannot link back to original! This is normal for shared notices etc.');
442 throw new ServerException('No URI for remote notice. Cannot accept that.');
446 $notice->content = $final;
448 $notice->source = $source;
452 // Get the groups here so we can figure out replies and such
453 if (!isset($groups)) {
454 $groups = User_group::idsFromText($notice->content, $profile);
459 // Handle repeat case
461 if (isset($repeat_of)) {
463 // Check for a private one
465 $repeat = Notice::getKV('id', $repeat_of);
467 if (!($repeat instanceof Notice)) {
468 // TRANS: Client exception thrown in notice when trying to repeat a missing or deleted notice.
469 throw new ClientException(_('Cannot repeat; original notice is missing or deleted.'));
472 if ($profile->id == $repeat->profile_id) {
473 // TRANS: Client error displayed when trying to repeat an own notice.
474 throw new ClientException(_('You cannot repeat your own notice.'));
477 if ($repeat->scope != Notice::SITE_SCOPE &&
478 $repeat->scope != Notice::PUBLIC_SCOPE) {
479 // TRANS: Client error displayed when trying to repeat a non-public notice.
480 throw new ClientException(_('Cannot repeat a private notice.'), 403);
483 if (!$repeat->inScope($profile)) {
484 // The generic checks above should cover this, but let's be sure!
485 // TRANS: Client error displayed when trying to repeat a notice you cannot access.
486 throw new ClientException(_('Cannot repeat a notice you cannot read.'), 403);
489 if ($profile->hasRepeated($repeat)) {
490 // TRANS: Client error displayed when trying to repeat an already repeated notice.
491 throw new ClientException(_('You already repeated that notice.'));
494 $notice->repeat_of = $repeat_of;
496 $reply = self::getReplyTo($reply_to, $profile_id, $source, $final);
498 if (!empty($reply)) {
500 if (!$reply->inScope($profile)) {
501 // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
502 // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
503 throw new ClientException(sprintf(_('%1$s has no access to notice %2$d.'),
504 $profile->nickname, $reply->id), 403);
507 $notice->reply_to = $reply->id;
508 $notice->conversation = $reply->conversation;
510 // If the original is private to a group, and notice has no group specified,
511 // make it to the same group(s)
513 if (empty($groups) && ($reply->scope & Notice::GROUP_SCOPE)) {
515 $replyGroups = $reply->getGroups();
516 foreach ($replyGroups as $group) {
517 if ($profile->isMember($group)) {
518 $groups[] = $group->id;
527 if (!empty($lat) && !empty($lon)) {
532 if (!empty($location_ns) && !empty($location_id)) {
533 $notice->location_id = $location_id;
534 $notice->location_ns = $location_ns;
537 if (!empty($rendered)) {
538 $notice->rendered = $rendered;
540 $notice->rendered = common_render_content($final, $notice);
544 if (!empty($notice->repeat_of)) {
545 $notice->verb = ActivityVerb::SHARE;
546 $notice->object_type = ActivityObject::ACTIVITY;
548 $notice->verb = ActivityVerb::POST;
551 $notice->verb = $verb;
554 if (empty($object_type)) {
555 $notice->object_type = (empty($notice->reply_to)) ? ActivityObject::NOTE : ActivityObject::COMMENT;
557 $notice->object_type = $object_type;
560 if (is_null($scope)) { // 0 is a valid value
561 if (!empty($reply)) {
562 $notice->scope = $reply->scope;
564 $notice->scope = self::defaultScope();
567 $notice->scope = $scope;
570 // For private streams
573 $user = $profile->getUser();
575 if ($user->private_stream &&
576 ($notice->scope == Notice::PUBLIC_SCOPE ||
577 $notice->scope == Notice::SITE_SCOPE)) {
578 $notice->scope |= Notice::FOLLOWER_SCOPE;
580 } catch (NoSuchUserException $e) {
581 // Cannot handle private streams for remote profiles
584 // Force the scope for private groups
586 foreach ($groups as $groupId) {
587 $group = User_group::getKV('id', $groupId);
588 if ($group instanceof User_group) {
589 if ($group->force_scope) {
590 $notice->scope |= Notice::GROUP_SCOPE;
596 if (Event::handle('StartNoticeSave', array(&$notice))) {
598 // XXX: some of these functions write to the DB
600 $id = $notice->insert();
603 common_log_db_error($notice, 'INSERT', __FILE__);
604 // TRANS: Server exception thrown when a notice cannot be saved.
605 throw new ServerException(_('Problem saving notice.'));
608 // Update ID-dependent columns: URI, conversation
610 $orig = clone($notice);
614 // We can only get here if it's a local notice, since remote notices
615 // should've bailed out earlier due to lacking a URI.
616 if (empty($notice->uri)) {
617 $notice->uri = sprintf('%s%s=%d:%s=%s',
619 'noticeId', $notice->id,
620 'objectType', $notice->get_object_type(true));
624 // If it's not part of a conversation, it's
625 // the beginning of a new conversation.
627 if (empty($notice->conversation)) {
628 $conv = Conversation::create($notice);
629 $notice->conversation = $conv->id;
634 if ($notice->update($orig) === false) {
635 common_log_db_error($notice, 'UPDATE', __FILE__);
636 // TRANS: Server exception thrown when a notice cannot be updated.
637 throw new ServerException(_('Problem saving notice.'));
643 // Clear the cache for subscribed users, so they'll update at next request
644 // XXX: someone clever could prepend instead of clearing the cache
646 $notice->blowOnInsert();
648 // Save per-notice metadata...
650 if (isset($replies)) {
651 $notice->saveKnownReplies($replies);
653 $notice->saveReplies();
657 $notice->saveKnownTags($tags);
662 // Note: groups may save tags, so must be run after tags are saved
663 // to avoid errors on duplicates.
664 // Note: groups should always be set.
666 $notice->saveKnownGroups($groups);
669 $notice->saveKnownUrls($urls);
675 // Prepare inbox delivery, may be queued to background.
676 $notice->distribute();
682 function blowOnInsert($conversation = false)
684 $this->blowStream('profile:notice_ids:%d', $this->profile_id);
686 if ($this->isPublic()) {
687 $this->blowStream('public');
690 self::blow('notice:list-ids:conversation:%s', $this->conversation);
691 self::blow('conversation:notice_count:%d', $this->conversation);
693 if (!empty($this->repeat_of)) {
694 // XXX: we should probably only use one of these
695 $this->blowStream('notice:repeats:%d', $this->repeat_of);
696 self::blow('notice:list-ids:repeat_of:%d', $this->repeat_of);
699 $original = Notice::getKV('id', $this->repeat_of);
701 if ($original instanceof Notice) {
702 $originalUser = User::getKV('id', $original->profile_id);
703 if ($originalUser instanceof User) {
704 $this->blowStream('user:repeats_of_me:%d', $originalUser->id);
708 $profile = Profile::getKV($this->profile_id);
710 if ($profile instanceof Profile) {
711 $profile->blowNoticeCount();
714 $ptags = $this->getProfileTags();
715 foreach ($ptags as $ptag) {
716 $ptag->blowNoticeStreamCache();
721 * Clear cache entries related to this notice at delete time.
722 * Necessary to avoid breaking paging on public, profile timelines.
724 function blowOnDelete()
726 $this->blowOnInsert();
728 self::blow('profile:notice_ids:%d;last', $this->profile_id);
730 if ($this->isPublic()) {
731 self::blow('public;last');
734 self::blow('fave:by_notice', $this->id);
736 if ($this->conversation) {
737 // In case we're the first, will need to calc a new root.
738 self::blow('notice:conversation_root:%d', $this->conversation);
741 $ptags = $this->getProfileTags();
742 foreach ($ptags as $ptag) {
743 $ptag->blowNoticeStreamCache(true);
747 function blowStream()
749 $c = self::memcache();
755 $args = func_get_args();
757 $format = array_shift($args);
759 $keyPart = vsprintf($format, $args);
761 $cacheKey = Cache::key($keyPart);
763 $c->delete($cacheKey);
765 // delete the "last" stream, too, if this notice is
766 // older than the top of that stream
768 $lastKey = $cacheKey.';last';
770 $lastStr = $c->get($lastKey);
772 if ($lastStr !== false) {
773 $window = explode(',', $lastStr);
774 $lastID = $window[0];
775 $lastNotice = Notice::getKV('id', $lastID);
776 if (!$lastNotice instanceof Notice // just weird
777 || strtotime($lastNotice->created) >= strtotime($this->created)) {
778 $c->delete($lastKey);
783 /** save all urls in the notice to the db
785 * follow redirects and save all available file information
786 * (mimetype, date, size, oembed, etc.)
790 function saveUrls() {
791 if (common_config('attachments', 'process_links')) {
792 common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
797 * Save the given URLs as related links/attachments to the db
799 * follow redirects and save all available file information
800 * (mimetype, date, size, oembed, etc.)
804 function saveKnownUrls($urls)
806 if (common_config('attachments', 'process_links')) {
807 // @fixme validation?
808 foreach (array_unique($urls) as $url) {
809 File::processNew($url, $this->id);
817 function saveUrl($url, $notice_id) {
818 File::processNew($url, $notice_id);
821 static function checkDupes($profile_id, $content) {
822 $profile = Profile::getKV($profile_id);
823 if (!$profile instanceof Profile) {
826 $notice = $profile->getNotices(0, CachingNoticeStream::CACHE_WINDOW);
827 if (!empty($notice)) {
829 while ($notice->fetch()) {
830 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
832 } else if ($notice->content == $content) {
837 // If we get here, oldest item in cache window is not
838 // old enough for dupe limit; do direct check against DB
839 $notice = new Notice();
840 $notice->profile_id = $profile_id;
841 $notice->content = $content;
842 $threshold = common_sql_date(time() - common_config('site', 'dupelimit'));
843 $notice->whereAdd(sprintf("created > '%s'", $notice->escape($threshold)));
845 $cnt = $notice->count();
849 static function checkEditThrottle($profile_id) {
850 $profile = Profile::getKV($profile_id);
851 if (!$profile instanceof Profile) {
854 // Get the Nth notice
855 $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
856 if ($notice && $notice->fetch()) {
857 // If the Nth notice was posted less than timespan seconds ago
858 if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
863 // Either not N notices in the stream, OR the Nth was not posted within timespan seconds
867 protected $_attachments = -1;
869 function attachments() {
871 if ($this->_attachments != -1) {
872 return $this->_attachments;
875 $f2ps = File_to_post::listGet('post_id', array($this->id));
879 foreach ($f2ps[$this->id] as $f2p) {
880 $ids[] = $f2p->file_id;
883 $files = File::multiGet('id', $ids);
885 $this->_attachments = $files->fetchAll();
887 return $this->_attachments;
890 function _setAttachments($attachments)
892 $this->_attachments = $attachments;
895 function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0)
897 $stream = new PublicNoticeStream();
898 return $stream->getNotices($offset, $limit, $since_id, $max_id);
902 function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0)
904 $stream = new ConversationNoticeStream($id);
906 return $stream->getNotices($offset, $limit, $since_id, $max_id);
910 * Is this notice part of an active conversation?
912 * @return boolean true if other messages exist in the same
913 * conversation, false if this is the only one
915 function hasConversation()
917 if (!empty($this->conversation)) {
918 $conversation = Notice::conversationStream(
924 if ($conversation->N > 0) {
932 * Grab the earliest notice from this conversation.
934 * @return Notice or null
936 function conversationRoot($profile=-1)
938 // XXX: can this happen?
940 if (empty($this->conversation)) {
944 // Get the current profile if not specified
946 if (is_int($profile) && $profile == -1) {
947 $profile = Profile::current();
950 // If this notice is out of scope, no root for you!
952 if (!$this->inScope($profile)) {
956 // If this isn't a reply to anything, then it's its own
959 if (empty($this->reply_to)) {
963 if (is_null($profile)) {
964 $keypart = sprintf('notice:conversation_root:%d:null', $this->id);
966 $keypart = sprintf('notice:conversation_root:%d:%d',
971 $root = self::cacheGet($keypart);
973 if ($root !== false && $root->inScope($profile)) {
980 $parent = $last->getParent();
981 if ($parent->inScope($profile)) {
985 } catch (Exception $e) {
986 // Latest notice has no parent
988 // No parent, or parent out of scope
993 self::cacheSet($keypart, $root);
999 * Pull up a full list of local recipients who will be getting
1000 * this notice in their inbox. Results will be cached, so don't
1001 * change the input data wily-nilly!
1003 * @param array $groups optional list of Group objects;
1004 * if left empty, will be loaded from group_inbox records
1005 * @param array $recipient optional list of reply profile ids
1006 * if left empty, will be loaded from reply records
1007 * @return array associating recipient user IDs with an inbox source constant
1009 function whoGets(array $groups=null, array $recipients=null)
1011 $c = self::memcache();
1014 $ni = $c->get(Cache::key('notice:who_gets:'.$this->id));
1015 if ($ni !== false) {
1020 if (is_null($recipients)) {
1021 $recipients = $this->getReplies();
1026 // Give plugins a chance to add folks in at start...
1027 if (Event::handle('StartNoticeWhoGets', array($this, &$ni))) {
1029 $users = $this->getSubscribedUsers();
1030 foreach ($users as $id) {
1031 $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
1034 if (is_null($groups)) {
1035 $groups = $this->getGroups();
1037 foreach ($groups as $group) {
1038 $users = $group->getUserMembers();
1039 foreach ($users as $id) {
1040 if (!array_key_exists($id, $ni)) {
1041 $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
1046 $ptAtts = $this->getAttentionsFromProfileTags();
1047 foreach ($ptAtts as $key=>$val) {
1048 if (!array_key_exists($key, $ni)) {
1053 foreach ($recipients as $recipient) {
1054 if (!array_key_exists($recipient, $ni)) {
1055 $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
1059 // Exclude any deleted, non-local, or blocking recipients.
1060 $profile = $this->getProfile();
1061 $originalProfile = null;
1062 if ($this->repeat_of) {
1063 // Check blocks against the original notice's poster as well.
1064 $original = Notice::getKV('id', $this->repeat_of);
1065 if ($original instanceof Notice) {
1066 $originalProfile = $original->getProfile();
1070 foreach ($ni as $id => $source) {
1072 $user = User::getKV('id', $id);
1073 if (!$user instanceof User ||
1074 $user->hasBlocked($profile) ||
1075 ($originalProfile && $user->hasBlocked($originalProfile))) {
1078 } catch (UserNoProfileException $e) {
1079 // User doesn't have a profile; invalid; skip them.
1084 // Give plugins a chance to filter out...
1085 Event::handle('EndNoticeWhoGets', array($this, &$ni));
1089 // XXX: pack this data better
1090 $c->set(Cache::key('notice:who_gets:'.$this->id), $ni);
1096 function getSubscribedUsers()
1100 if(common_config('db','quote_identifiers'))
1101 $user_table = '"user"';
1102 else $user_table = 'user';
1106 'FROM '. $user_table .' JOIN subscription '.
1107 'ON '. $user_table .'.id = subscription.subscriber ' .
1108 'WHERE subscription.subscribed = %d ';
1110 $user->query(sprintf($qry, $this->profile_id));
1114 while ($user->fetch()) {
1123 function getProfileTags()
1125 $profile = $this->getProfile();
1126 $list = $profile->getOtherTags($profile);
1129 while($list->fetch()) {
1130 $ptags[] = clone($list);
1136 public function getAttentionsFromProfileTags()
1139 $ptags = $this->getProfileTags();
1140 foreach ($ptags as $ptag) {
1141 $users = $ptag->getUserSubscribers();
1142 foreach ($users as $id) {
1143 $ni[$id] = NOTICE_INBOX_SOURCE_PROFILE_TAG;
1150 * Record this notice to the given group inboxes for delivery.
1151 * Overrides the regular parsing of !group markup.
1153 * @param string $group_ids
1154 * @fixme might prefer URIs as identifiers, as for replies?
1155 * best with generalizations on user_group to support
1156 * remote groups better.
1158 function saveKnownGroups($group_ids)
1160 if (!is_array($group_ids)) {
1161 // TRANS: Server exception thrown when no array is provided to the method saveKnownGroups().
1162 throw new ServerException(_('Bad type provided to saveKnownGroups.'));
1166 foreach (array_unique($group_ids) as $id) {
1167 $group = User_group::getKV('id', $id);
1168 if ($group instanceof User_group) {
1169 common_log(LOG_ERR, "Local delivery to group id $id, $group->nickname");
1170 $result = $this->addToGroupInbox($group);
1172 common_log_db_error($gi, 'INSERT', __FILE__);
1175 if (common_config('group', 'addtag')) {
1176 // we automatically add a tag for every group name, too
1178 $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($group->nickname),
1179 'notice_id' => $this->id));
1181 if (is_null($tag)) {
1182 $this->saveTag($group->nickname);
1186 $groups[] = clone($group);
1188 common_log(LOG_ERR, "Local delivery to group id $id skipped, doesn't exist");
1195 function addToGroupInbox(User_group $group)
1197 $gi = Group_inbox::pkeyGet(array('group_id' => $group->id,
1198 'notice_id' => $this->id));
1200 if (!$gi instanceof Group_inbox) {
1202 $gi = new Group_inbox();
1204 $gi->group_id = $group->id;
1205 $gi->notice_id = $this->id;
1206 $gi->created = $this->created;
1208 $result = $gi->insert();
1211 common_log_db_error($gi, 'INSERT', __FILE__);
1212 // TRANS: Server exception thrown when an update for a group inbox fails.
1213 throw new ServerException(_('Problem saving group inbox.'));
1216 self::blow('user_group:notice_ids:%d', $gi->group_id);
1223 * Save reply records indicating that this notice needs to be
1224 * delivered to the local users with the given URIs.
1226 * Since this is expected to be used when saving foreign-sourced
1227 * messages, we won't deliver to any remote targets as that's the
1228 * source service's responsibility.
1230 * Mail notifications etc will be handled later.
1232 * @param array $uris Array of unique identifier URIs for recipients
1234 function saveKnownReplies(array $uris)
1240 $sender = Profile::getKV($this->profile_id);
1242 foreach (array_unique($uris) as $uri) {
1244 $profile = Profile::fromURI($uri);
1246 if (!$profile instanceof Profile) {
1247 common_log(LOG_WARNING, "Unable to determine profile for URI '$uri'");
1251 if ($profile->hasBlocked($sender)) {
1252 common_log(LOG_INFO, "Not saving reply to profile {$profile->id} ($uri) from sender {$sender->id} because of a block.");
1256 $this->saveReply($profile->id);
1257 self::blow('reply:stream:%d', $profile->id);
1264 * Pull @-replies from this message's content in StatusNet markup format
1265 * and save reply records indicating that this message needs to be
1266 * delivered to those users.
1268 * Mail notifications to local profiles will be sent later.
1270 * @return array of integer profile IDs
1273 function saveReplies()
1275 // Don't save reply data for repeats
1277 if (!empty($this->repeat_of)) {
1281 $sender = Profile::getKV($this->profile_id);
1285 // If it's a reply, save for the replied-to author
1287 $parent = $this->getParent();
1288 $author = $parent->getProfile();
1289 if ($author instanceof Profile) {
1290 $this->saveReply($author->id);
1291 $replied[$author->id] = 1;
1292 self::blow('reply:stream:%d', $author->id);
1294 } catch (Exception $e) {
1295 // Not a reply, since it has no parent!
1298 // @todo ideally this parser information would only
1299 // be calculated once.
1301 $mentions = common_find_mentions($this->content, $this);
1303 // store replied only for first @ (what user/notice what the reply directed,
1304 // we assume first @ is it)
1306 foreach ($mentions as $mention) {
1308 foreach ($mention['mentioned'] as $mentioned) {
1310 // skip if they're already covered
1312 if (!empty($replied[$mentioned->id])) {
1316 // Don't save replies from blocked profile to local user
1318 $mentioned_user = User::getKV('id', $mentioned->id);
1319 if ($mentioned_user instanceof User && $mentioned_user->hasBlocked($sender)) {
1323 $this->saveReply($mentioned->id);
1324 $replied[$mentioned->id] = 1;
1325 self::blow('reply:stream:%d', $mentioned->id);
1329 $recipientIds = array_keys($replied);
1331 return $recipientIds;
1334 function saveReply($profileId)
1336 $reply = new Reply();
1338 $reply->notice_id = $this->id;
1339 $reply->profile_id = $profileId;
1340 $reply->modified = $this->created;
1347 protected $_replies = -1;
1350 * Pull the complete list of @-reply targets for this notice.
1352 * @return array of integer profile ids
1354 function getReplies()
1356 if ($this->_replies != -1) {
1357 return $this->_replies;
1360 $replyMap = Reply::listGet('notice_id', array($this->id));
1364 foreach ($replyMap[$this->id] as $reply) {
1365 $ids[] = $reply->profile_id;
1368 $this->_replies = $ids;
1373 function _setReplies($replies)
1375 $this->_replies = $replies;
1379 * Pull the complete list of @-reply targets for this notice.
1381 * @return array of Profiles
1383 function getReplyProfiles()
1385 $ids = $this->getReplies();
1387 $profiles = Profile::multiGet('id', $ids);
1389 return $profiles->fetchAll();
1393 * Send e-mail notifications to local @-reply targets.
1395 * Replies must already have been saved; this is expected to be run
1396 * from the distrib queue handler.
1398 function sendReplyNotifications()
1400 // Don't send reply notifications for repeats
1402 if (!empty($this->repeat_of)) {
1406 $recipientIds = $this->getReplies();
1408 foreach ($recipientIds as $recipientId) {
1409 $user = User::getKV('id', $recipientId);
1410 if ($user instanceof User) {
1411 mail_notify_attn($user, $this);
1417 * Pull list of groups this notice needs to be delivered to,
1418 * as previously recorded by saveKnownGroups().
1420 * @return array of Group objects
1423 protected $_groups = -1;
1425 function getGroups()
1427 // Don't save groups for repeats
1429 if (!empty($this->repeat_of)) {
1433 if ($this->_groups != -1)
1435 return $this->_groups;
1438 $gis = Group_inbox::listGet('notice_id', array($this->id));
1442 foreach ($gis[$this->id] as $gi)
1444 $ids[] = $gi->group_id;
1447 $groups = User_group::multiGet('id', $ids);
1449 $this->_groups = $groups->fetchAll();
1451 return $this->_groups;
1454 function _setGroups($groups)
1456 $this->_groups = $groups;
1460 * Convert a notice into an activity for export.
1462 * @param User $cur Current user
1464 * @return Activity activity object representing this Notice.
1467 function asActivity($cur=null)
1469 $act = self::cacheGet(Cache::codeKey('notice:as-activity:'.$this->id));
1471 if ($act instanceof Activity) {
1474 $act = new Activity();
1476 if (Event::handle('StartNoticeAsActivity', array($this, &$act))) {
1478 $act->id = $this->uri;
1479 $act->time = strtotime($this->created);
1480 $act->link = $this->getUrl();
1481 $act->content = common_xml_safe_str($this->rendered);
1483 $profile = $this->getProfile();
1485 $act->actor = ActivityObject::fromProfile($profile);
1486 $act->actor->extra[] = $profile->profileInfo($cur);
1488 $act->verb = $this->verb;
1490 if ($this->repeat_of) {
1491 $repeated = Notice::getKV('id', $this->repeat_of);
1492 if ($repeated instanceof Notice) {
1493 $act->objects[] = $repeated->asActivity($cur);
1496 $act->objects[] = ActivityObject::fromNotice($this);
1499 // XXX: should this be handled by default processing for object entry?
1503 $tags = $this->getTags();
1505 foreach ($tags as $tag) {
1506 $cat = new AtomCategory();
1509 $act->categories[] = $cat;
1513 // XXX: use Atom Media and/or File activity objects instead
1515 $attachments = $this->attachments();
1517 foreach ($attachments as $attachment) {
1518 // Save local attachments
1519 if (!empty($attachment->filename)) {
1520 $act->attachments[] = ActivityObject::fromFile($attachment);
1524 $ctx = new ActivityContext();
1527 $reply = $this->getParent();
1528 $ctx->replyToID = $reply->getUri();
1529 $ctx->replyToUrl = $reply->getUrl();
1530 } catch (Exception $e) {
1531 // This is not a reply to something
1534 $ctx->location = $this->getLocation();
1538 if (!empty($this->conversation)) {
1539 $conv = Conversation::getKV('id', $this->conversation);
1540 if ($conv instanceof Conversation) {
1541 $ctx->conversation = $conv->uri;
1545 $reply_ids = $this->getReplies();
1547 foreach ($reply_ids as $id) {
1548 $rprofile = Profile::getKV('id', $id);
1549 if ($rprofile instanceof Profile) {
1550 $ctx->attention[$rprofile->getUri()] = ActivityObject::PERSON;
1554 $groups = $this->getGroups();
1556 foreach ($groups as $group) {
1557 $ctx->attention[$group->getUri()] = ActivityObject::GROUP;
1560 switch ($this->scope) {
1561 case Notice::PUBLIC_SCOPE:
1562 $ctx->attention[ActivityContext::ATTN_PUBLIC] = ActivityObject::COLLECTION;
1564 case Notice::FOLLOWER_SCOPE:
1565 $surl = common_local_url("subscribers", array('nickname' => $profile->nickname));
1566 $ctx->attention[$surl] = ActivityObject::COLLECTION;
1570 $act->context = $ctx;
1572 $source = $this->getSource();
1574 if ($source instanceof Notice_source) {
1575 $act->generator = ActivityObject::fromNoticeSource($source);
1580 $atom_feed = $profile->getAtomFeed();
1582 if (!empty($atom_feed)) {
1584 $act->source = new ActivitySource();
1586 // XXX: we should store the actual feed ID
1588 $act->source->id = $atom_feed;
1590 // XXX: we should store the actual feed title
1592 $act->source->title = $profile->getBestName();
1594 $act->source->links['alternate'] = $profile->profileurl;
1595 $act->source->links['self'] = $atom_feed;
1597 $act->source->icon = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
1599 $notice = $profile->getCurrentNotice();
1601 if ($notice instanceof Notice) {
1602 $act->source->updated = self::utcDate($notice->created);
1605 $user = User::getKV('id', $profile->id);
1607 if ($user instanceof User) {
1608 $act->source->links['license'] = common_config('license', 'url');
1612 if ($this->isLocal()) {
1613 $act->selfLink = common_local_url('ApiStatusesShow', array('id' => $this->id,
1614 'format' => 'atom'));
1615 $act->editLink = $act->selfLink;
1618 Event::handle('EndNoticeAsActivity', array($this, &$act));
1621 self::cacheSet(Cache::codeKey('notice:as-activity:'.$this->id), $act);
1626 // This has gotten way too long. Needs to be sliced up into functional bits
1627 // or ideally exported to a utility class.
1629 function asAtomEntry($namespace=false,
1634 $act = $this->asActivity($cur);
1635 $act->extra[] = $this->noticeInfo($cur);
1636 return $act->asString($namespace, $author, $source);
1640 * Extra notice info for atom entries
1642 * Clients use some extra notice info in the atom stream.
1643 * This gives it to them.
1645 * @param User $cur Current user
1647 * @return array representation of <statusnet:notice_info> element
1650 function noticeInfo($cur)
1652 // local notice ID (useful to clients for ordering)
1654 $noticeInfoAttr = array('local_id' => $this->id);
1658 $ns = $this->getSource();
1660 if ($ns instanceof Notice_source) {
1661 $noticeInfoAttr['source'] = $ns->code;
1662 if (!empty($ns->url)) {
1663 $noticeInfoAttr['source_link'] = $ns->url;
1664 if (!empty($ns->name)) {
1665 $noticeInfoAttr['source'] = '<a href="'
1666 . htmlspecialchars($ns->url)
1667 . '" rel="nofollow">'
1668 . htmlspecialchars($ns->name)
1674 // favorite and repeated
1677 $cp = $cur->getProfile();
1678 $noticeInfoAttr['favorite'] = ($cp->hasFave($this)) ? "true" : "false";
1679 $noticeInfoAttr['repeated'] = ($cp->hasRepeated($this)) ? "true" : "false";
1682 if (!empty($this->repeat_of)) {
1683 $noticeInfoAttr['repeat_of'] = $this->repeat_of;
1686 return array('statusnet:notice_info', $noticeInfoAttr, null);
1690 * Returns an XML string fragment with a reference to a notice as an
1691 * Activity Streams noun object with the given element type.
1693 * Assumes that 'activity' namespace has been previously defined.
1695 * @param string $element one of 'subject', 'object', 'target'
1699 function asActivityNoun($element)
1701 $noun = ActivityObject::fromNotice($this);
1702 return $noun->asString('activity:' . $element);
1706 * Determine which notice, if any, a new notice is in reply to.
1708 * For conversation tracking, we try to see where this notice fits
1709 * in the tree. Rough algorithm is:
1711 * if (reply_to is set and valid) {
1713 * } else if ((source not API or Web) and (content starts with "T NAME" or "@name ")) {
1714 * return ID of last notice by initial @name in content;
1717 * Note that all @nickname instances will still be used to save "reply" records,
1718 * so the notice shows up in the mentioned users' "replies" tab.
1720 * @param integer $reply_to ID passed in by Web or API
1721 * @param integer $profile_id ID of author
1722 * @param string $source Source tag, like 'web' or 'gwibber'
1723 * @param string $content Final notice content
1725 * @return integer ID of replied-to notice, or null for not a reply.
1728 static function getReplyTo($reply_to, $profile_id, $source, $content)
1730 static $lb = array('xmpp', 'mail', 'sms', 'omb');
1732 // If $reply_to is specified, we check that it exists, and then
1733 // return it if it does
1735 if (!empty($reply_to)) {
1736 $reply_notice = Notice::getKV('id', $reply_to);
1737 if ($reply_notice instanceof Notice) {
1738 return $reply_notice;
1742 // If it's not a "low bandwidth" source (one where you can't set
1743 // a reply_to argument), we return. This is mostly web and API
1746 if (!in_array($source, $lb)) {
1750 // Is there an initial @ or T?
1752 if (preg_match('/^T ([A-Z0-9]{1,64}) /', $content, $match) ||
1753 preg_match('/^@([a-z0-9]{1,64})\s+/', $content, $match)) {
1754 $nickname = common_canonical_nickname($match[1]);
1759 // Figure out who that is.
1761 $sender = Profile::getKV('id', $profile_id);
1762 if (!$sender instanceof Profile) {
1766 $recipient = common_relative_profile($sender, $nickname, common_sql_now());
1768 if (!$recipient instanceof Profile) {
1772 // Get their last notice
1774 $last = $recipient->getCurrentNotice();
1776 if ($last instanceof Notice) {
1783 static function maxContent()
1785 $contentlimit = common_config('notice', 'contentlimit');
1786 // null => use global limit (distinct from 0!)
1787 if (is_null($contentlimit)) {
1788 $contentlimit = common_config('site', 'textlimit');
1790 return $contentlimit;
1793 static function contentTooLong($content)
1795 $contentlimit = self::maxContent();
1796 return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
1799 function getLocation()
1803 if (!empty($this->location_id) && !empty($this->location_ns)) {
1804 $location = Location::fromId($this->location_id, $this->location_ns);
1807 if (is_null($location)) { // no ID, or Location::fromId() failed
1808 if (!empty($this->lat) && !empty($this->lon)) {
1809 $location = Location::fromLatLon($this->lat, $this->lon);
1817 * Convenience function for posting a repeat of an existing message.
1819 * @param int $repeater_id: profile ID of user doing the repeat
1820 * @param string $source: posting source key, eg 'web', 'api', etc
1823 * @throws Exception on failure or permission problems
1825 function repeat($repeater_id, $source)
1827 $author = Profile::getKV('id', $this->profile_id);
1829 // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'.
1830 // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice.
1831 $content = sprintf(_('RT @%1$s %2$s'),
1835 $maxlen = common_config('site', 'textlimit');
1836 if ($maxlen > 0 && mb_strlen($content) > $maxlen) {
1837 // Web interface and current Twitter API clients will
1838 // pull the original notice's text, but some older
1839 // clients and RSS/Atom feeds will see this trimmed text.
1841 // Unfortunately this is likely to lose tags or URLs
1842 // at the end of long notices.
1843 $content = mb_substr($content, 0, $maxlen - 4) . ' ...';
1846 // Scope is same as this one's
1848 return self::saveNew($repeater_id,
1851 array('repeat_of' => $this->id,
1852 'scope' => $this->scope));
1855 // These are supposed to be in chron order!
1857 function repeatStream($limit=100)
1859 $cache = Cache::instance();
1861 if (empty($cache)) {
1862 $ids = $this->_repeatStreamDirect($limit);
1864 $idstr = $cache->get(Cache::key('notice:repeats:'.$this->id));
1865 if ($idstr !== false) {
1866 if (empty($idstr)) {
1869 $ids = explode(',', $idstr);
1872 $ids = $this->_repeatStreamDirect(100);
1873 $cache->set(Cache::key('notice:repeats:'.$this->id), implode(',', $ids));
1876 // We do a max of 100, so slice down to limit
1877 $ids = array_slice($ids, 0, $limit);
1881 return NoticeStream::getStreamByIds($ids);
1884 function _repeatStreamDirect($limit)
1886 $notice = new Notice();
1888 $notice->selectAdd(); // clears it
1889 $notice->selectAdd('id');
1891 $notice->repeat_of = $this->id;
1893 $notice->orderBy('created, id'); // NB: asc!
1895 if (!is_null($limit)) {
1896 $notice->limit(0, $limit);
1899 return $notice->fetchAll('id');
1902 function locationOptions($lat, $lon, $location_id, $location_ns, $profile = null)
1906 if (!empty($location_id) && !empty($location_ns)) {
1907 $options['location_id'] = $location_id;
1908 $options['location_ns'] = $location_ns;
1910 $location = Location::fromId($location_id, $location_ns);
1912 if ($location instanceof Location) {
1913 $options['lat'] = $location->lat;
1914 $options['lon'] = $location->lon;
1917 } else if (!empty($lat) && !empty($lon)) {
1918 $options['lat'] = $lat;
1919 $options['lon'] = $lon;
1921 $location = Location::fromLatLon($lat, $lon);
1923 if ($location instanceof Location) {
1924 $options['location_id'] = $location->location_id;
1925 $options['location_ns'] = $location->location_ns;
1927 } else if (!empty($profile)) {
1928 if (isset($profile->lat) && isset($profile->lon)) {
1929 $options['lat'] = $profile->lat;
1930 $options['lon'] = $profile->lon;
1933 if (isset($profile->location_id) && isset($profile->location_ns)) {
1934 $options['location_id'] = $profile->location_id;
1935 $options['location_ns'] = $profile->location_ns;
1942 function clearAttentions()
1944 $att = new Attention();
1945 $att->notice_id = $this->getID();
1948 while ($att->fetch()) {
1949 // Can't do delete() on the object directly since it won't remove all of it
1950 $other = clone($att);
1956 function clearReplies()
1958 $replyNotice = new Notice();
1959 $replyNotice->reply_to = $this->id;
1961 //Null any notices that are replies to this notice
1963 if ($replyNotice->find()) {
1964 while ($replyNotice->fetch()) {
1965 $orig = clone($replyNotice);
1966 $replyNotice->reply_to = null;
1967 $replyNotice->update($orig);
1973 $reply = new Reply();
1974 $reply->notice_id = $this->id;
1976 if ($reply->find()) {
1977 while($reply->fetch()) {
1978 self::blow('reply:stream:%d', $reply->profile_id);
1986 function clearFiles()
1988 $f2p = new File_to_post();
1990 $f2p->post_id = $this->id;
1993 while ($f2p->fetch()) {
1997 // FIXME: decide whether to delete File objects
1998 // ...and related (actual) files
2001 function clearRepeats()
2003 $repeatNotice = new Notice();
2004 $repeatNotice->repeat_of = $this->id;
2006 //Null any notices that are repeats of this notice
2008 if ($repeatNotice->find()) {
2009 while ($repeatNotice->fetch()) {
2010 $orig = clone($repeatNotice);
2011 $repeatNotice->repeat_of = null;
2012 $repeatNotice->update($orig);
2017 function clearFaves()
2020 $fave->notice_id = $this->id;
2022 if ($fave->find()) {
2023 while ($fave->fetch()) {
2024 self::blow('fave:ids_by_user_own:%d', $fave->user_id);
2025 self::blow('fave:ids_by_user_own:%d;last', $fave->user_id);
2026 self::blow('fave:ids_by_user:%d', $fave->user_id);
2027 self::blow('fave:ids_by_user:%d;last', $fave->user_id);
2035 function clearTags()
2037 $tag = new Notice_tag();
2038 $tag->notice_id = $this->id;
2041 while ($tag->fetch()) {
2042 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, Cache::keyize($tag->tag));
2043 self::blow('profile:notice_ids_tagged:%d:%s;last', $this->profile_id, Cache::keyize($tag->tag));
2044 self::blow('notice_tag:notice_ids:%s', Cache::keyize($tag->tag));
2045 self::blow('notice_tag:notice_ids:%s;last', Cache::keyize($tag->tag));
2053 function clearGroupInboxes()
2055 $gi = new Group_inbox();
2057 $gi->notice_id = $this->id;
2060 while ($gi->fetch()) {
2061 self::blow('user_group:notice_ids:%d', $gi->group_id);
2069 function distribute()
2071 // We always insert for the author so they don't
2073 Event::handle('StartNoticeDistribute', array($this));
2075 // If there's a failure, we want to _force_
2076 // distribution at this point.
2078 $qm = QueueManager::get();
2079 $qm->enqueue($this, 'distrib');
2080 } catch (Exception $e) {
2081 // If the exception isn't transient, this
2082 // may throw more exceptions as DQH does
2083 // its own enqueueing. So, we ignore them!
2085 $handler = new DistribQueueHandler();
2086 $handler->handle($this);
2087 } catch (Exception $e) {
2088 common_log(LOG_ERR, "emergency redistribution resulted in " . $e->getMessage());
2090 // Re-throw so somebody smarter can handle it.
2097 $result = parent::insert();
2099 if ($result !== false) {
2100 // Profile::hasRepeated() abuses pkeyGet(), so we
2101 // have to clear manually
2102 if (!empty($this->repeat_of)) {
2103 $c = self::memcache();
2105 $ck = self::multicacheKey('Notice',
2106 array('profile_id' => $this->profile_id,
2107 'repeat_of' => $this->repeat_of));
2117 * Get the source of the notice
2119 * @return Notice_source $ns A notice source object. 'code' is the only attribute
2120 * guaranteed to be populated.
2122 function getSource()
2124 $ns = new Notice_source();
2125 if (!empty($this->source)) {
2126 switch ($this->source) {
2133 $ns->code = $this->source;
2136 $ns = Notice_source::getKV($this->source);
2138 $ns = new Notice_source();
2139 $ns->code = $this->source;
2140 $app = Oauth_application::getKV('name', $this->source);
2142 $ns->name = $app->name;
2143 $ns->url = $app->source_url;
2153 * Determine whether the notice was locally created
2155 * @return boolean locality
2158 public function isLocal()
2160 return ($this->is_local == Notice::LOCAL_PUBLIC ||
2161 $this->is_local == Notice::LOCAL_NONPUBLIC);
2165 * Get the list of hash tags saved with this notice.
2167 * @return array of strings
2169 public function getTags()
2173 $keypart = sprintf('notice:tags:%d', $this->id);
2175 $tagstr = self::cacheGet($keypart);
2177 if ($tagstr !== false) {
2178 $tags = explode(',', $tagstr);
2180 $tag = new Notice_tag();
2181 $tag->notice_id = $this->id;
2183 while ($tag->fetch()) {
2184 $tags[] = $tag->tag;
2187 self::cacheSet($keypart, implode(',', $tags));
2193 static private function utcDate($dt)
2195 $dateStr = date('d F Y H:i:s', strtotime($dt));
2196 $d = new DateTime($dateStr, new DateTimeZone('UTC'));
2197 return $d->format(DATE_W3C);
2201 * Look up the creation timestamp for a given notice ID, even
2202 * if it's been deleted.
2205 * @return mixed string recorded creation timestamp, or false if can't be found
2207 public static function getAsTimestamp($id)
2213 $notice = Notice::getKV('id', $id);
2215 return $notice->created;
2218 $deleted = Deleted_notice::getKV('id', $id);
2220 return $deleted->created;
2227 * Build an SQL 'where' fragment for timestamp-based sorting from a since_id
2228 * parameter, matching notices posted after the given one (exclusive).
2230 * If the referenced notice can't be found, will return false.
2233 * @param string $idField
2234 * @param string $createdField
2235 * @return mixed string or false if no match
2237 public static function whereSinceId($id, $idField='id', $createdField='created')
2239 $since = Notice::getAsTimestamp($id);
2241 return sprintf("($createdField = '%s' and $idField > %d) or ($createdField > '%s')", $since, $id, $since);
2247 * Build an SQL 'where' fragment for timestamp-based sorting from a since_id
2248 * parameter, matching notices posted after the given one (exclusive), and
2249 * if necessary add it to the data object's query.
2251 * @param DB_DataObject $obj
2253 * @param string $idField
2254 * @param string $createdField
2255 * @return mixed string or false if no match
2257 public static function addWhereSinceId(DB_DataObject $obj, $id, $idField='id', $createdField='created')
2259 $since = self::whereSinceId($id, $idField, $createdField);
2261 $obj->whereAdd($since);
2266 * Build an SQL 'where' fragment for timestamp-based sorting from a max_id
2267 * parameter, matching notices posted before the given one (inclusive).
2269 * If the referenced notice can't be found, will return false.
2272 * @param string $idField
2273 * @param string $createdField
2274 * @return mixed string or false if no match
2276 public static function whereMaxId($id, $idField='id', $createdField='created')
2278 $max = Notice::getAsTimestamp($id);
2280 return sprintf("($createdField < '%s') or ($createdField = '%s' and $idField <= %d)", $max, $max, $id);
2286 * Build an SQL 'where' fragment for timestamp-based sorting from a max_id
2287 * parameter, matching notices posted before the given one (inclusive), and
2288 * if necessary add it to the data object's query.
2290 * @param DB_DataObject $obj
2292 * @param string $idField
2293 * @param string $createdField
2294 * @return mixed string or false if no match
2296 public static function addWhereMaxId(DB_DataObject $obj, $id, $idField='id', $createdField='created')
2298 $max = self::whereMaxId($id, $idField, $createdField);
2300 $obj->whereAdd($max);
2306 if (common_config('public', 'localonly')) {
2307 return ($this->is_local == Notice::LOCAL_PUBLIC);
2309 return (($this->is_local != Notice::LOCAL_NONPUBLIC) &&
2310 ($this->is_local != Notice::GATEWAY));
2315 * Check that the given profile is allowed to read, respond to, or otherwise
2316 * act on this notice.
2318 * The $scope member is a bitmask of scopes, representing a logical AND of the
2319 * scope requirement. So, 0x03 (Notice::ADDRESSEE_SCOPE | Notice::SITE_SCOPE) means
2320 * "only visible to people who are mentioned in the notice AND are users on this site."
2321 * Users on the site who are not mentioned in the notice will not be able to see the
2324 * @param Profile $profile The profile to check; pass null to check for public/unauthenticated users.
2326 * @return boolean whether the profile is in the notice's scope
2328 function inScope($profile)
2330 if (is_null($profile)) {
2331 $keypart = sprintf('notice:in-scope-for:%d:null', $this->id);
2333 $keypart = sprintf('notice:in-scope-for:%d:%d', $this->id, $profile->id);
2336 $result = self::cacheGet($keypart);
2338 if ($result === false) {
2340 if (Event::handle('StartNoticeInScope', array($this, $profile, &$bResult))) {
2341 $bResult = $this->_inScope($profile);
2342 Event::handle('EndNoticeInScope', array($this, $profile, &$bResult));
2344 $result = ($bResult) ? 1 : 0;
2345 self::cacheSet($keypart, $result, 0, 300);
2348 return ($result == 1) ? true : false;
2351 protected function _inScope($profile)
2353 if (!is_null($this->scope)) {
2354 $scope = $this->scope;
2356 $scope = self::defaultScope();
2359 // If there's no scope, anyone (even anon) is in scope.
2361 if ($scope == 0) { // Not private
2363 return !$this->isHiddenSpam($profile);
2365 } else { // Private, somehow
2367 // If there's scope, anon cannot be in scope
2369 if (empty($profile)) {
2373 // Author is always in scope
2375 if ($this->profile_id == $profile->id) {
2379 // Only for users on this site
2381 if (($scope & Notice::SITE_SCOPE) && !$profile->isLocal()) {
2385 // Only for users mentioned in the notice
2387 if ($scope & Notice::ADDRESSEE_SCOPE) {
2389 $reply = Reply::pkeyGet(array('notice_id' => $this->id,
2390 'profile_id' => $profile->id));
2392 if (!$reply instanceof Reply) {
2397 // Only for members of the given group
2399 if ($scope & Notice::GROUP_SCOPE) {
2401 // XXX: just query for the single membership
2403 $groups = $this->getGroups();
2407 foreach ($groups as $group) {
2408 if ($profile->isMember($group)) {
2419 // Only for followers of the author
2423 if ($scope & Notice::FOLLOWER_SCOPE) {
2426 $author = $this->getProfile();
2427 } catch (Exception $e) {
2431 if (!Subscription::exists($profile, $author)) {
2436 return !$this->isHiddenSpam($profile);
2440 function isHiddenSpam($profile) {
2442 // Hide posts by silenced users from everyone but moderators.
2444 if (common_config('notice', 'hidespam')) {
2447 $author = $this->getProfile();
2448 } catch(Exception $e) {
2449 // If we can't get an author, keep it hidden.
2450 // XXX: technically not spam, but, whatever.
2454 if ($author->hasRole(Profile_role::SILENCED)) {
2455 if (!$profile instanceof Profile || (($profile->id !== $author->id) && (!$profile->hasRight(Right::REVIEWSPAM)))) {
2464 public function getParent()
2466 $parent = Notice::getKV('id', $this->reply_to);
2468 if (!$parent instanceof Notice) {
2469 throw new ServerException('Notice has no parent');
2476 * Magic function called at serialize() time.
2478 * We use this to drop a couple process-specific references
2479 * from DB_DataObject which can cause trouble in future
2482 * @return array of variable names to include in serialization.
2487 $vars = parent::__sleep();
2488 $skip = array('_profile', '_groups', '_attachments', '_faves', '_replies', '_repeats');
2489 return array_diff($vars, $skip);
2492 static function defaultScope()
2494 $scope = common_config('notice', 'defaultscope');
2495 if (is_null($scope)) {
2496 if (common_config('site', 'private')) {
2505 static function fillProfiles($notices)
2507 $map = self::getProfiles($notices);
2509 foreach ($notices as $entry=>$notice) {
2511 if (array_key_exists($notice->profile_id, $map)) {
2512 $notice->_setProfile($map[$notice->profile_id]);
2514 } catch (NoProfileException $e) {
2515 common_log(LOG_WARNING, "Failed to fill profile in Notice with non-existing entry for profile_id: {$e->id}");
2516 unset($notices[$entry]);
2520 return array_values($map);
2523 static function getProfiles(&$notices)
2526 foreach ($notices as $notice) {
2527 $ids[] = $notice->profile_id;
2530 $ids = array_unique($ids);
2532 return Profile::pivotGet('id', $ids);
2535 static function fillGroups(&$notices)
2537 $ids = self::_idsOf($notices);
2539 $gis = Group_inbox::listGet('notice_id', $ids);
2543 foreach ($gis as $id => $gi)
2547 $gids[] = $g->group_id;
2551 $gids = array_unique($gids);
2553 $group = User_group::pivotGet('id', $gids);
2555 foreach ($notices as $notice)
2558 $gi = $gis[$notice->id];
2559 foreach ($gi as $g) {
2560 $grps[] = $group[$g->group_id];
2562 $notice->_setGroups($grps);
2566 static function _idsOf(&$notices)
2569 foreach ($notices as $notice) {
2570 $ids[] = $notice->id;
2572 $ids = array_unique($ids);
2576 static function fillAttachments(&$notices)
2578 $ids = self::_idsOf($notices);
2580 $f2pMap = File_to_post::listGet('post_id', $ids);
2584 foreach ($f2pMap as $noticeId => $f2ps) {
2585 foreach ($f2ps as $f2p) {
2586 $fileIds[] = $f2p->file_id;
2590 $fileIds = array_unique($fileIds);
2592 $fileMap = File::pivotGet('id', $fileIds);
2594 foreach ($notices as $notice)
2597 $f2ps = $f2pMap[$notice->id];
2598 foreach ($f2ps as $f2p) {
2599 $files[] = $fileMap[$f2p->file_id];
2601 $notice->_setAttachments($files);
2608 * All faves of this notice
2610 * @return array Array of Fave objects
2615 if (isset($this->_faves) && is_array($this->_faves)) {
2616 return $this->_faves;
2618 $faveMap = Fave::listGet('notice_id', array($this->id));
2619 $this->_faves = $faveMap[$this->id];
2620 return $this->_faves;
2623 function _setFaves($faves)
2625 $this->_faves = $faves;
2628 static function fillFaves(&$notices)
2630 $ids = self::_idsOf($notices);
2631 $faveMap = Fave::listGet('notice_id', $ids);
2634 foreach ($faveMap as $id => $faves) {
2635 $cnt += count($faves);
2636 if (count($faves) > 0) {
2640 foreach ($notices as $notice) {
2641 $faves = $faveMap[$notice->id];
2642 $notice->_setFaves($faves);
2646 static function fillReplies(&$notices)
2648 $ids = self::_idsOf($notices);
2649 $replyMap = Reply::listGet('notice_id', $ids);
2650 foreach ($notices as $notice) {
2651 $replies = $replyMap[$notice->id];
2653 foreach ($replies as $reply) {
2654 $ids[] = $reply->profile_id;
2656 $notice->_setReplies($ids);
2660 protected $_repeats;
2662 function getRepeats()
2664 if (isset($this->_repeats) && is_array($this->_repeats)) {
2665 return $this->_repeats;
2667 $repeatMap = Notice::listGet('repeat_of', array($this->id));
2668 $this->_repeats = $repeatMap[$this->id];
2669 return $this->_repeats;
2672 function _setRepeats($repeats)
2674 $this->_repeats = $repeats;
2677 static function fillRepeats(&$notices)
2679 $ids = self::_idsOf($notices);
2680 $repeatMap = Notice::listGet('repeat_of', $ids);
2681 foreach ($notices as $notice) {
2682 $repeats = $repeatMap[$notice->id];
2683 $notice->_setRepeats($repeats);