]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Notice.php
Occasionally we'd get a false from Profile::getKV
[quix0rs-gnu-social.git] / classes / Notice.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008-2011 StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  * @category Notices
20  * @package  StatusNet
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/
35  */
36
37 if (!defined('GNUSOCIAL')) { exit(1); }
38
39 /**
40  * Table Definition for notice
41  */
42
43 /* We keep 200 notices, the max number of notices available per API request,
44  * in the memcached cache. */
45
46 define('NOTICE_CACHE_WINDOW', CachingNoticeStream::CACHE_WINDOW);
47
48 define('MAX_BOXCARS', 128);
49
50 class Notice extends Managed_DataObject
51 {
52     ###START_AUTOCODE
53     /* the code below is auto generated do not remove the above tag */
54
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)
76
77     /* the code above is auto generated do not remove the tag below */
78     ###END_AUTOCODE
79
80     public static function schemaDef()
81     {
82         $def = array(
83             'fields' => array(
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'),
105             ),
106             'primary key' => array('id'),
107             'unique keys' => array(
108                 'notice_uri_key' => array('uri'),
109             ),
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?
115             ),
116             'indexes' => array(
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')
122             )
123         );
124
125         if (common_config('search', 'type') == 'fulltext') {
126             $def['fulltext indexes'] = array('content' => array('content'));
127         }
128
129         return $def;
130     }
131         
132     /* Notice types */
133     const LOCAL_PUBLIC    =  1;
134     const REMOTE          =  0;
135     const LOCAL_NONPUBLIC = -1;
136     const GATEWAY         = -2;
137
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;
143
144     protected $_profile = array();
145     
146     /**
147      * Will always return a profile, if anything fails it will
148      * (through _setProfile) throw a NoProfileException.
149      */
150     public function getProfile()
151     {
152         if (!isset($this->_profile[$this->profile_id])) {
153             // We could've sent getKV directly to _setProfile, but occasionally we get
154             // a "false" (instead of null), likely because it indicates a cache miss.
155             $profile = Profile::getKV('id', $this->profile_id);
156             $this->_setProfile($profile instanceof Profile ? $profile : null);
157         }
158         return $this->_profile[$this->profile_id];
159     }
160     
161     public function _setProfile(Profile $profile=null)
162     {
163         if (!$profile instanceof Profile) {
164             throw new NoProfileException($this->profile_id);
165         }
166         $this->_profile[$this->profile_id] = $profile;
167     }
168
169     function delete($useWhere=false)
170     {
171         // For auditing purposes, save a record that the notice
172         // was deleted.
173
174         // @fixme we have some cases where things get re-run and so the
175         // insert fails.
176         $deleted = Deleted_notice::getKV('id', $this->id);
177
178         if (!$deleted instanceof Deleted_notice) {
179             $deleted = Deleted_notice::getKV('uri', $this->uri);
180         }
181
182         if (!$deleted instanceof Deleted_notice) {
183             $deleted = new Deleted_notice();
184
185             $deleted->id         = $this->id;
186             $deleted->profile_id = $this->profile_id;
187             $deleted->uri        = $this->uri;
188             $deleted->created    = $this->created;
189             $deleted->deleted    = common_sql_now();
190
191             $deleted->insert();
192         }
193
194         if (Event::handle('NoticeDeleteRelated', array($this))) {
195
196             // Clear related records
197
198             $this->clearReplies();
199             $this->clearRepeats();
200             $this->clearTags();
201             $this->clearGroupInboxes();
202             $this->clearFiles();
203             $this->clearAttentions();
204
205             // NOTE: we don't clear queue items
206         }
207
208         $result = parent::delete($useWhere);
209
210         $this->blowOnDelete();
211         return $result;
212     }
213
214     public function getUri()
215     {
216         return $this->uri;
217     }
218
219     /*
220      * Get a Notice object by URI. Will call external plugins for help
221      * using the event StartGetNoticeFromURI.
222      *
223      * @param string $uri A unique identifier for a resource (notice in this case)
224      */
225     static function fromUri($uri)
226     {
227         $notice = null;
228
229         if (Event::handle('StartGetNoticeFromUri', array($uri, &$notice))) {
230             $notice = Notice::getKV('uri', $uri);
231             Event::handle('EndGetNoticeFromUri', array($uri, $notice));
232         }
233
234         if (!$notice instanceof Notice) {
235             throw new UnknownUriException($uri);
236         }
237
238         return $notice;
239     }
240
241     /*
242      * @param $root boolean If true, link to just the conversation root.
243      *
244      * @return URL to conversation
245      */
246     public function getConversationUrl($anchor=true)
247     {
248         return Conversation::getUrlFromNotice($this, $anchor);
249     }
250
251     /*
252      * Get the local representation URL of this notice.
253      */
254     public function getLocalUrl()
255     {
256         return common_local_url('shownotice', array('notice' => $this->id), null, null, false);
257     }
258
259     public function getTitle()
260     {
261         $title = null;
262         if (Event::handle('GetNoticeTitle', array($this, &$title))) {
263             // TRANS: Title of a notice posted without a title value.
264             // TRANS: %1$s is a user name, %2$s is the notice creation date/time.
265             $title = sprintf(_('%1$s\'s status on %2$s'),
266                              $this->getProfile()->getFancyName(),
267                              common_exact_date($this->created));
268         }
269         return $title;
270     }
271
272     /*
273      * Get the original representation URL of this notice.
274      */
275     public function getUrl()
276     {
277         // The risk is we start having empty urls and non-http uris...
278         // and we can't really handle any other protocol right now.
279         switch (true) {
280         case common_valid_http_url($this->url): // should we allow non-http/https URLs?
281             return $this->url;
282         case $this->isLocal():
283             // let's generate a valid link to our locally available notice on demand
284             return common_local_url('shownotice', array('notice' => $this->id), null, null, false);
285         case common_valid_http_url($this->uri):
286             return $this->uri;
287         default:
288             common_debug('No URL available for notice: id='.$this->id);
289             throw new InvalidUrlException($this->url);
290         }
291     }
292
293     public function get_object_type($canonical=false) {
294         return $canonical
295                 ? ActivityObject::canonicalType($this->object_type)
296                 : $this->object_type;
297     }
298
299     public static function getByUri($uri)
300     {
301         $notice = new Notice();
302         $notice->uri = $uri;
303         if (!$notice->find(true)) {
304             throw new NoResultException($notice);
305         }
306         return $notice;
307     }
308
309     /**
310      * Extract #hashtags from this notice's content and save them to the database.
311      */
312     function saveTags()
313     {
314         /* extract all #hastags */
315         $count = preg_match_all('/(?:^|\s)#([\pL\pN_\-\.]{1,64})/u', strtolower($this->content), $match);
316         if (!$count) {
317             return true;
318         }
319
320         /* Add them to the database */
321         return $this->saveKnownTags($match[1]);
322     }
323
324     /**
325      * Record the given set of hash tags in the db for this notice.
326      * Given tag strings will be normalized and checked for dupes.
327      */
328     function saveKnownTags($hashtags)
329     {
330         //turn each into their canonical tag
331         //this is needed to remove dupes before saving e.g. #hash.tag = #hashtag
332         for($i=0; $i<count($hashtags); $i++) {
333             /* elide characters we don't want in the tag */
334             $hashtags[$i] = common_canonical_tag($hashtags[$i]);
335         }
336
337         foreach(array_unique($hashtags) as $hashtag) {
338             $this->saveTag($hashtag);
339             self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, $hashtag);
340         }
341         return true;
342     }
343
344     /**
345      * Record a single hash tag as associated with this notice.
346      * Tag format and uniqueness must be validated by caller.
347      */
348     function saveTag($hashtag)
349     {
350         $tag = new Notice_tag();
351         $tag->notice_id = $this->id;
352         $tag->tag = $hashtag;
353         $tag->created = $this->created;
354         $id = $tag->insert();
355
356         if (!$id) {
357             // TRANS: Server exception. %s are the error details.
358             throw new ServerException(sprintf(_('Database error inserting hashtag: %s.'),
359                                               $last_error->message));
360             return;
361         }
362
363         // if it's saved, blow its cache
364         $tag->blowCache(false);
365     }
366
367     /**
368      * Save a new notice and push it out to subscribers' inboxes.
369      * Poster's permissions are checked before sending.
370      *
371      * @param int $profile_id Profile ID of the poster
372      * @param string $content source message text; links may be shortened
373      *                        per current user's preference
374      * @param string $source source key ('web', 'api', etc)
375      * @param array $options Associative array of optional properties:
376      *              string 'created' timestamp of notice; defaults to now
377      *              int 'is_local' source/gateway ID, one of:
378      *                  Notice::LOCAL_PUBLIC    - Local, ok to appear in public timeline
379      *                  Notice::REMOTE          - Sent from a remote service;
380      *                                            hide from public timeline but show in
381      *                                            local "and friends" timelines
382      *                  Notice::LOCAL_NONPUBLIC - Local, but hide from public timeline
383      *                  Notice::GATEWAY         - From another non-OStatus service;
384      *                                            will not appear in public views
385      *              float 'lat' decimal latitude for geolocation
386      *              float 'lon' decimal longitude for geolocation
387      *              int 'location_id' geoname identifier
388      *              int 'location_ns' geoname namespace to interpret location_id
389      *              int 'reply_to'; notice ID this is a reply to
390      *              int 'repeat_of'; notice ID this is a repeat of
391      *              string 'uri' unique ID for notice; a unique tag uri (can be url or anything too)
392      *              string 'url' permalink to notice; defaults to local notice URL
393      *              string 'rendered' rendered HTML version of content
394      *              array 'replies' list of profile URIs for reply delivery in
395      *                              place of extracting @-replies from content.
396      *              array 'groups' list of group IDs to deliver to, in place of
397      *                              extracting ! tags from content
398      *              array 'tags' list of hashtag strings to save with the notice
399      *                           in place of extracting # tags from content
400      *              array 'urls' list of attached/referred URLs to save with the
401      *                           notice in place of extracting links from content
402      *              boolean 'distribute' whether to distribute the notice, default true
403      *              string 'object_type' URL of the associated object type (default ActivityObject::NOTE)
404      *              string 'verb' URL of the associated verb (default ActivityVerb::POST)
405      *              int 'scope' Scope bitmask; default to SITE_SCOPE on private sites, 0 otherwise
406      *
407      * @fixme tag override
408      *
409      * @return Notice
410      * @throws ClientException
411      */
412     static function saveNew($profile_id, $content, $source, array $options=null) {
413         $defaults = array('uri' => null,
414                           'url' => null,
415                           'reply_to' => null,
416                           'repeat_of' => null,
417                           'scope' => null,
418                           'distribute' => true,
419                           'object_type' => null,
420                           'verb' => null);
421
422         if (!empty($options) && is_array($options)) {
423             $options = array_merge($defaults, $options);
424             extract($options);
425         } else {
426             extract($defaults);
427         }
428
429         if (!isset($is_local)) {
430             $is_local = Notice::LOCAL_PUBLIC;
431         }
432
433         $profile = Profile::getKV('id', $profile_id);
434         if (!$profile instanceof Profile) {
435             // TRANS: Client exception thrown when trying to save a notice for an unknown user.
436             throw new ClientException(_('Problem saving notice. Unknown user.'));
437         }
438
439         $user = User::getKV('id', $profile_id);
440         if ($user instanceof User) {
441             // Use the local user's shortening preferences, if applicable.
442             $final = $user->shortenLinks($content);
443         } else {
444             $final = common_shorten_links($content);
445         }
446
447         if (Notice::contentTooLong($final)) {
448             // TRANS: Client exception thrown if a notice contains too many characters.
449             throw new ClientException(_('Problem saving notice. Too long.'));
450         }
451
452         if (common_config('throttle', 'enabled') && !Notice::checkEditThrottle($profile_id)) {
453             common_log(LOG_WARNING, 'Excessive posting by profile #' . $profile_id . '; throttled.');
454             // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
455             throw new ClientException(_('Too many notices too fast; take a breather '.
456                                         'and post again in a few minutes.'));
457         }
458
459         if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) {
460             common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.');
461             // TRANS: Client exception thrown when a user tries to post too many duplicate notices in a given time frame.
462             throw new ClientException(_('Too many duplicate messages too quickly;'.
463                                         ' take a breather and post again in a few minutes.'));
464         }
465
466         if (!$profile->hasRight(Right::NEWNOTICE)) {
467             common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $profile->nickname);
468
469             // TRANS: Client exception thrown when a user tries to post while being banned.
470             throw new ClientException(_('You are banned from posting notices on this site.'), 403);
471         }
472
473         $notice = new Notice();
474         $notice->profile_id = $profile_id;
475
476         $autosource = common_config('public', 'autosource');
477
478         // Sandboxed are non-false, but not 1, either
479
480         if (!$profile->hasRight(Right::PUBLICNOTICE) ||
481             ($source && $autosource && in_array($source, $autosource))) {
482             $notice->is_local = Notice::LOCAL_NONPUBLIC;
483         } else {
484             $notice->is_local = $is_local;
485         }
486
487         if (!empty($created)) {
488             $notice->created = $created;
489         } else {
490             $notice->created = common_sql_now();
491         }
492
493         if (!$notice->isLocal()) {
494             // Only do these checks for non-local notices. Local notices will generate these values later.
495             if (!common_valid_http_url($url)) {
496                 common_debug('Bad notice URL: ['.$url.'], URI: ['.$uri.']. Cannot link back to original! This is normal for shared notices etc.');
497             }
498             if (empty($uri)) {
499                 throw new ServerException('No URI for remote notice. Cannot accept that.');
500             }
501         }
502
503         $notice->content = $final;
504
505         $notice->source = $source;
506         $notice->uri = $uri;
507         $notice->url = $url;
508
509         // Get the groups here so we can figure out replies and such
510         if (!isset($groups)) {
511             $groups = User_group::idsFromText($notice->content, $profile);
512         }
513
514         $reply = null;
515
516         // Handle repeat case
517
518         if (isset($repeat_of)) {
519
520             // Check for a private one
521
522             $repeat = Notice::getKV('id', $repeat_of);
523
524             if (!($repeat instanceof Notice)) {
525                 // TRANS: Client exception thrown in notice when trying to repeat a missing or deleted notice.
526                 throw new ClientException(_('Cannot repeat; original notice is missing or deleted.'));
527             }
528
529             if ($profile->id == $repeat->profile_id) {
530                 // TRANS: Client error displayed when trying to repeat an own notice.
531                 throw new ClientException(_('You cannot repeat your own notice.'));
532             }
533
534             if ($repeat->scope != Notice::SITE_SCOPE &&
535                 $repeat->scope != Notice::PUBLIC_SCOPE) {
536                 // TRANS: Client error displayed when trying to repeat a non-public notice.
537                 throw new ClientException(_('Cannot repeat a private notice.'), 403);
538             }
539
540             if (!$repeat->inScope($profile)) {
541                 // The generic checks above should cover this, but let's be sure!
542                 // TRANS: Client error displayed when trying to repeat a notice you cannot access.
543                 throw new ClientException(_('Cannot repeat a notice you cannot read.'), 403);
544             }
545
546             if ($profile->hasRepeated($repeat)) {
547                 // TRANS: Client error displayed when trying to repeat an already repeated notice.
548                 throw new ClientException(_('You already repeated that notice.'));
549             }
550
551             $notice->repeat_of = $repeat->id;
552             $notice->conversation = $repeat->conversation;
553         } else {
554             $reply = null;
555
556             // If $reply_to is specified, we check that it exists, and then
557             // return it if it does
558             if (!empty($reply_to)) {
559                 $reply = Notice::getKV('id', $reply_to);
560             } elseif (in_array($source, array('xmpp', 'mail', 'sms'))) {
561                 // If the source lacks capability of sending the "reply_to"
562                 // metadata, let's try to find an inline replyto-reference.
563                 $reply = self::getInlineReplyTo($profile, $final);
564             }
565
566             if ($reply instanceof Notice) {
567                 if (!$reply->inScope($profile)) {
568                     // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
569                     // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
570                     throw new ClientException(sprintf(_('%1$s has no access to notice %2$d.'),
571                                                       $profile->nickname, $reply->id), 403);
572                 }
573
574                 // If it's a repeat, the reply_to should be to the original
575                 if (!empty($reply->repeat_of)) {
576                     $notice->reply_to = $reply->repeat_of;
577                 } else {
578                     $notice->reply_to = $reply->id;
579                 }
580                 // But the conversation ought to be the same :)
581                 $notice->conversation = $reply->conversation;
582
583                 // If the original is private to a group, and notice has
584                 // no group specified, make it to the same group(s)
585
586                 if (empty($groups) && ($reply->scope & Notice::GROUP_SCOPE)) {
587                     $groups = array();
588                     $replyGroups = $reply->getGroups();
589                     foreach ($replyGroups as $group) {
590                         if ($profile->isMember($group)) {
591                             $groups[] = $group->id;
592                         }
593                     }
594                 }
595
596                 // Scope set below
597             }
598         }
599
600         if (!empty($lat) && !empty($lon)) {
601             $notice->lat = $lat;
602             $notice->lon = $lon;
603         }
604
605         if (!empty($location_ns) && !empty($location_id)) {
606             $notice->location_id = $location_id;
607             $notice->location_ns = $location_ns;
608         }
609
610         if (!empty($rendered)) {
611             $notice->rendered = $rendered;
612         } else {
613             $notice->rendered = common_render_content($final, $notice);
614         }
615
616         if (empty($verb)) {
617             if (!empty($notice->repeat_of)) {
618                 $notice->verb        = ActivityVerb::SHARE;
619                 $notice->object_type = ActivityObject::ACTIVITY;
620             } else {
621                 $notice->verb        = ActivityVerb::POST;
622             }
623         } else {
624             $notice->verb = $verb;
625         }
626
627         if (empty($object_type)) {
628             $notice->object_type = (empty($notice->reply_to)) ? ActivityObject::NOTE : ActivityObject::COMMENT;
629         } else {
630             $notice->object_type = $object_type;
631         }
632
633         if (is_null($scope) && $reply instanceof Notice) {
634             $notice->scope = $reply->scope;
635         } else {
636             $notice->scope = $scope;
637         }
638
639         $notice->scope = self::figureOutScope($profile, $groups, $notice->scope);
640
641         if (Event::handle('StartNoticeSave', array(&$notice))) {
642
643             // XXX: some of these functions write to the DB
644
645             $id = $notice->insert();
646
647             if (!$id) {
648                 common_log_db_error($notice, 'INSERT', __FILE__);
649                 // TRANS: Server exception thrown when a notice cannot be saved.
650                 throw new ServerException(_('Problem saving notice.'));
651             }
652
653             // Update ID-dependent columns: URI, conversation
654
655             $orig = clone($notice);
656
657             $changed = false;
658
659             // We can only get here if it's a local notice, since remote notices
660             // should've bailed out earlier due to lacking a URI.
661             if (empty($notice->uri)) {
662                 $notice->uri = sprintf('%s%s=%d:%s=%s',
663                                     TagURI::mint(),
664                                     'noticeId', $notice->id,
665                                     'objectType', $notice->get_object_type(true));
666                 $changed = true;
667             }
668
669             // If it's not part of a conversation, it's
670             // the beginning of a new conversation.
671
672             if (empty($notice->conversation)) {
673                 $conv = Conversation::create($notice);
674                 $notice->conversation = $conv->id;
675                 $changed = true;
676             }
677
678             if ($changed) {
679                 if ($notice->update($orig) === false) {
680                     common_log_db_error($notice, 'UPDATE', __FILE__);
681                     // TRANS: Server exception thrown when a notice cannot be updated.
682                     throw new ServerException(_('Problem saving notice.'));
683                 }
684             }
685
686         }
687
688         // Clear the cache for subscribed users, so they'll update at next request
689         // XXX: someone clever could prepend instead of clearing the cache
690
691         $notice->blowOnInsert();
692
693         // Save per-notice metadata...
694
695         if (isset($replies)) {
696             $notice->saveKnownReplies($replies);
697         } else {
698             $notice->saveReplies();
699         }
700
701         if (isset($tags)) {
702             $notice->saveKnownTags($tags);
703         } else {
704             $notice->saveTags();
705         }
706
707         // Note: groups may save tags, so must be run after tags are saved
708         // to avoid errors on duplicates.
709         // Note: groups should always be set.
710
711         $notice->saveKnownGroups($groups);
712
713         if (isset($urls)) {
714             $notice->saveKnownUrls($urls);
715         } else {
716             $notice->saveUrls();
717         }
718
719         if ($distribute) {
720             // Prepare inbox delivery, may be queued to background.
721             $notice->distribute();
722         }
723
724         return $notice;
725     }
726
727     static function saveActivity(Activity $act, Profile $actor, array $options=array())
728     {
729         // First check if we're going to let this Activity through from the specific actor
730         if (!$actor->hasRight(Right::NEWNOTICE)) {
731             common_log(LOG_WARNING, "Attempted post from user disallowed to post: " . $actor->getNickname());
732
733             // TRANS: Client exception thrown when a user tries to post while being banned.
734             throw new ClientException(_m('You are banned from posting notices on this site.'), 403);
735         }
736         if (common_config('throttle', 'enabled') && !self::checkEditThrottle($actor->id)) {
737             common_log(LOG_WARNING, 'Excessive posting by profile #' . $actor->id . '; throttled.');
738             // TRANS: Client exception thrown when a user tries to post too many notices in a given time frame.
739             throw new ClientException(_m('Too many notices too fast; take a breather '.
740                                         'and post again in a few minutes.'));
741         }
742
743 /* This interferes with stuff like Favorites from old StatusNet installations (first object in objects is the favored notice)
744         // Get ActivityObject properties
745         $actobj = count($act->objects)==1 ? $act->objects[0] : null;
746         if (!is_null($actobj) && $actobj->id) {
747             $options['uri'] = $actobj->id;
748             if ($actobj->link) {
749                 $options['url'] = $actobj->link;
750             } elseif ($act->link) {
751                 $options['url'] = $act->link;
752             } elseif (preg_match('!^https?://!', $actobj->id)) {
753                 $options['url'] = $actobj->id;
754             }
755         } else {
756             // implied object
757             $options['uri'] = $act->id;
758             $options['url'] = $act->link;
759         }
760 */
761
762         $defaults = array(
763                           'groups'   => array(),
764                           'is_local' => self::LOCAL_PUBLIC,
765                           'mentions' => array(),
766                           'reply_to' => null,
767                           'repeat_of' => null,
768                           'scope' => null,
769                           'source' => 'unknown',
770                           'tags' => array(),
771                           'uri' => null,
772                           'url' => null,
773                           'urls' => array(),
774                           'distribute' => true);
775
776         // options will have default values when nothing has been supplied
777         $options = array_merge($defaults, $options); 
778         foreach (array_keys($defaults) as $key) {
779             // Only convert the keynames we specify ourselves from 'defaults' array into variables
780             $$key = $options[$key];
781         }
782         extract($options, EXTR_SKIP);
783
784         $stored = new Notice();
785         if (!empty($uri)) {
786             $stored->uri = $uri;
787             if ($stored->find()) {
788                 common_debug('cannot create duplicate Notice URI: '.$stored->uri);
789                 throw new Exception('Notice URI already exists');
790             }
791         }
792
793         $stored->profile_id = $actor->id;
794         $stored->source = $source;
795         $stored->uri = $uri;
796         $stored->url = $url;
797         $stored->verb = $act->verb;
798
799         $autosource = common_config('public', 'autosource');
800
801         // Sandboxed are non-false, but not 1, either
802         if (!$actor->hasRight(Right::PUBLICNOTICE) ||
803             ($source && $autosource && in_array($source, $autosource))) {
804             $stored->is_local = Notice::LOCAL_NONPUBLIC;
805         }
806
807         // Maybe a missing act-time should be fatal if the actor is not local?
808         if (!empty($act->time)) {
809             $stored->created = common_sql_date($act->time);
810         } else {
811             $stored->created = common_sql_now();
812         }
813
814         $reply = null;
815         if ($act->context instanceof ActivityContext && !empty($act->context->replyToID)) {
816             $reply = self::getKV('uri', $act->context->replyToID);
817         }
818         if (!$reply instanceof Notice && $act->target instanceof ActivityObject) {
819             $reply = self::getKV('uri', $act->target->id);
820         }
821
822         if ($reply instanceof Notice) {
823             if (!$reply->inScope($actor)) {
824                 // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
825                 // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
826                 throw new ClientException(sprintf(_m('%1$s has no right to reply to notice %2$d.'), $actor->getNickname(), $reply->id), 403);
827             }
828
829             $stored->reply_to     = $reply->id;
830             $stored->conversation = $reply->conversation;
831
832             // If the original is private to a group, and notice has no group specified,
833             // make it to the same group(s)
834             if (empty($groups) && ($reply->scope & Notice::GROUP_SCOPE)) {
835                 $groups = array();
836                 $replyGroups = $reply->getGroups();
837                 foreach ($replyGroups as $group) {
838                     if ($actor->isMember($group)) {
839                         $groups[] = $group->id;
840                     }
841                 }
842             }
843
844             if (is_null($scope)) {
845                 $scope = $reply->scope;
846             }
847         }
848
849         if ($act->context instanceof ActivityContext) {
850             $location = $act->context->location;
851             if ($location) {
852                 $stored->lat = $location->lat;
853                 $stored->lon = $location->lon;
854                 if ($location->location_id) {
855                     $stored->location_ns = $location->location_ns;
856                     $stored->location_id = $location->location_id;
857                 }
858             }
859         } else {
860             $act->context = new ActivityContext();
861         }
862
863         $stored->scope = self::figureOutScope($actor, $groups, $scope);
864
865         foreach ($act->categories as $cat) {
866             if ($cat->term) {
867                 $term = common_canonical_tag($cat->term);
868                 if (!empty($term)) {
869                     $tags[] = $term;
870                 }
871             }
872         }
873
874         foreach ($act->enclosures as $href) {
875             // @todo FIXME: Save these locally or....?
876             $urls[] = $href;
877         }
878
879         if (Event::handle('StartNoticeSave', array(&$stored))) {
880             // XXX: some of these functions write to the DB
881
882             try {
883                 $stored->insert();    // throws exception on error
884
885                 $object = null;
886                 Event::handle('StoreActivityObject', array($act, $stored, $options, &$object));
887                 if (empty($object)) {
888                     throw new ServerException('No object from StoreActivityObject '.$stored->uri . ': '.$act->asString());
889                 }
890                 $orig = clone($stored);
891                 $stored->object_type = ActivityUtils::resolveUri($object->getObjectType(), true);
892                 $stored->update($orig);
893             } catch (Exception $e) {
894                 if (empty($stored->id)) {
895                     common_debug('Failed to save stored object entry in database ('.$e->getMessage().')');
896                 } else {
897                     common_debug('Failed to store activity object in database ('.$e->getMessage().'), deleting notice id '.$stored->id);
898                     $stored->delete();
899                 }
900                 throw $e;
901             }
902         }
903
904         common_debug(get_called_class().' looking up mentions metadata etc.');
905         // Save per-notice metadata...
906         $mentions = array();
907         $groups   = array();
908
909         // This event lets plugins filter out non-local recipients (attentions we don't care about)
910         // Used primarily for OStatus (and if we don't federate, all attentions would be local anyway)
911         Event::handle('GetLocalAttentions', array($actor, $act->context->attention, &$mentions, &$groups));
912
913         if (!empty($mentions)) {
914             $stored->saveKnownReplies($mentions);
915         } else {
916             $stored->saveReplies();
917         }
918
919         if (!empty($tags)) {
920             $stored->saveKnownTags($tags);
921         } else {
922             $stored->saveTags();
923         }
924
925         // Note: groups may save tags, so must be run after tags are saved
926         // to avoid errors on duplicates.
927         // Note: groups should always be set.
928
929         $stored->saveKnownGroups($groups);
930
931         if (!empty($urls)) {
932             $stored->saveKnownUrls($urls);
933         } else {
934             $stored->saveUrls();
935         }
936
937         if ($distribute) {
938             // Prepare inbox delivery, may be queued to background.
939             $stored->distribute();
940         }
941         
942         common_debug(get_called_class().' returning stored activity - success!');
943
944         return $stored;
945     }
946
947     static public function figureOutScope(Profile $actor, array $groups, $scope=null) {
948         if (is_null($scope)) {
949             $scope = self::defaultScope();
950         }
951
952         // For private streams
953         try {
954             $user = $actor->getUser();
955             // FIXME: We can't do bit comparison with == (Legacy StatusNet thing. Let's keep it for now.)
956             if ($user->private_stream && ($scope == Notice::PUBLIC_SCOPE || $scope == Notice::SITE_SCOPE)) {
957                 $scope |= Notice::FOLLOWER_SCOPE;
958             }
959         } catch (NoSuchUserException $e) {
960             // TODO: Not a local user, so we don't know about scope preferences... yet!
961         }
962
963         // Force the scope for private groups
964         foreach ($groups as $group_id) {
965             $group = User_group::staticGet('id', $group_id);
966             if ($group instanceof User_group) {
967                 if ($group->force_scope) {
968                     $scope |= Notice::GROUP_SCOPE;
969                     break;
970                 }
971             }
972         }
973
974         return $scope;
975     }
976
977     function blowOnInsert($conversation = false)
978     {
979         $this->blowStream('profile:notice_ids:%d', $this->profile_id);
980
981         if ($this->isPublic()) {
982             $this->blowStream('public');
983         }
984
985         self::blow('notice:list-ids:conversation:%s', $this->conversation);
986         self::blow('conversation:notice_count:%d', $this->conversation);
987
988         if (!empty($this->repeat_of)) {
989             // XXX: we should probably only use one of these
990             $this->blowStream('notice:repeats:%d', $this->repeat_of);
991             self::blow('notice:list-ids:repeat_of:%d', $this->repeat_of);
992         }
993
994         $original = Notice::getKV('id', $this->repeat_of);
995
996         if ($original instanceof Notice) {
997             $originalUser = User::getKV('id', $original->profile_id);
998             if ($originalUser instanceof User) {
999                 $this->blowStream('user:repeats_of_me:%d', $originalUser->id);
1000             }
1001         }
1002
1003         $profile = Profile::getKV($this->profile_id);
1004
1005         if ($profile instanceof Profile) {
1006             $profile->blowNoticeCount();
1007         }
1008
1009         $ptags = $this->getProfileTags();
1010         foreach ($ptags as $ptag) {
1011             $ptag->blowNoticeStreamCache();
1012         }
1013     }
1014
1015     /**
1016      * Clear cache entries related to this notice at delete time.
1017      * Necessary to avoid breaking paging on public, profile timelines.
1018      */
1019     function blowOnDelete()
1020     {
1021         $this->blowOnInsert();
1022
1023         self::blow('profile:notice_ids:%d;last', $this->profile_id);
1024
1025         if ($this->isPublic()) {
1026             self::blow('public;last');
1027         }
1028
1029         self::blow('fave:by_notice', $this->id);
1030
1031         if ($this->conversation) {
1032             // In case we're the first, will need to calc a new root.
1033             self::blow('notice:conversation_root:%d', $this->conversation);
1034         }
1035
1036         $ptags = $this->getProfileTags();
1037         foreach ($ptags as $ptag) {
1038             $ptag->blowNoticeStreamCache(true);
1039         }
1040     }
1041
1042     function blowStream()
1043     {
1044         $c = self::memcache();
1045
1046         if (empty($c)) {
1047             return false;
1048         }
1049
1050         $args = func_get_args();
1051
1052         $format = array_shift($args);
1053
1054         $keyPart = vsprintf($format, $args);
1055
1056         $cacheKey = Cache::key($keyPart);
1057         
1058         $c->delete($cacheKey);
1059
1060         // delete the "last" stream, too, if this notice is
1061         // older than the top of that stream
1062
1063         $lastKey = $cacheKey.';last';
1064
1065         $lastStr = $c->get($lastKey);
1066
1067         if ($lastStr !== false) {
1068             $window     = explode(',', $lastStr);
1069             $lastID     = $window[0];
1070             $lastNotice = Notice::getKV('id', $lastID);
1071             if (!$lastNotice instanceof Notice // just weird
1072                 || strtotime($lastNotice->created) >= strtotime($this->created)) {
1073                 $c->delete($lastKey);
1074             }
1075         }
1076     }
1077
1078     /** save all urls in the notice to the db
1079      *
1080      * follow redirects and save all available file information
1081      * (mimetype, date, size, oembed, etc.)
1082      *
1083      * @return void
1084      */
1085     function saveUrls() {
1086         if (common_config('attachments', 'process_links')) {
1087             common_replace_urls_callback($this->content, array($this, 'saveUrl'), $this->id);
1088         }
1089     }
1090
1091     /**
1092      * Save the given URLs as related links/attachments to the db
1093      *
1094      * follow redirects and save all available file information
1095      * (mimetype, date, size, oembed, etc.)
1096      *
1097      * @return void
1098      */
1099     function saveKnownUrls($urls)
1100     {
1101         if (common_config('attachments', 'process_links')) {
1102             // @fixme validation?
1103             foreach (array_unique($urls) as $url) {
1104                 try {
1105                     File::processNew($url, $this->id);
1106                 } catch (ServerException $e) {
1107                     // Could not save URL. Log it?
1108                 }
1109             }
1110         }
1111     }
1112
1113     /**
1114      * @private callback
1115      */
1116     function saveUrl($url, $notice_id) {
1117         try {
1118             File::processNew($url, $notice_id);
1119         } catch (ServerException $e) {
1120             // Could not save URL. Log it?
1121         }
1122     }
1123
1124     static function checkDupes($profile_id, $content) {
1125         $profile = Profile::getKV($profile_id);
1126         if (!$profile instanceof Profile) {
1127             return false;
1128         }
1129         $notice = $profile->getNotices(0, CachingNoticeStream::CACHE_WINDOW);
1130         if (!empty($notice)) {
1131             $last = 0;
1132             while ($notice->fetch()) {
1133                 if (time() - strtotime($notice->created) >= common_config('site', 'dupelimit')) {
1134                     return true;
1135                 } else if ($notice->content == $content) {
1136                     return false;
1137                 }
1138             }
1139         }
1140         // If we get here, oldest item in cache window is not
1141         // old enough for dupe limit; do direct check against DB
1142         $notice = new Notice();
1143         $notice->profile_id = $profile_id;
1144         $notice->content = $content;
1145         $threshold = common_sql_date(time() - common_config('site', 'dupelimit'));
1146         $notice->whereAdd(sprintf("created > '%s'", $notice->escape($threshold)));
1147
1148         $cnt = $notice->count();
1149         return ($cnt == 0);
1150     }
1151
1152     static function checkEditThrottle($profile_id) {
1153         $profile = Profile::getKV($profile_id);
1154         if (!$profile instanceof Profile) {
1155             return false;
1156         }
1157         // Get the Nth notice
1158         $notice = $profile->getNotices(common_config('throttle', 'count') - 1, 1);
1159         if ($notice && $notice->fetch()) {
1160             // If the Nth notice was posted less than timespan seconds ago
1161             if (time() - strtotime($notice->created) <= common_config('throttle', 'timespan')) {
1162                 // Then we throttle
1163                 return false;
1164             }
1165         }
1166         // Either not N notices in the stream, OR the Nth was not posted within timespan seconds
1167         return true;
1168     }
1169
1170         protected $_attachments = array();
1171         
1172     function attachments() {
1173                 if (isset($this->_attachments[$this->id])) {
1174             return $this->_attachments[$this->id];
1175         }
1176                 
1177         $f2ps = File_to_post::listGet('post_id', array($this->id));
1178                 
1179                 $ids = array();
1180                 
1181                 foreach ($f2ps[$this->id] as $f2p) {
1182             $ids[] = $f2p->file_id;    
1183         }
1184                 
1185                 $files = File::multiGet('id', $ids);
1186
1187                 $this->_attachments[$this->id] = $files->fetchAll();
1188                 
1189         return $this->_attachments[$this->id];
1190     }
1191
1192         function _setAttachments($attachments)
1193         {
1194             $this->_attachments[$this->id] = $attachments;
1195         }
1196
1197     function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0)
1198     {
1199         $stream = new PublicNoticeStream();
1200         return $stream->getNotices($offset, $limit, $since_id, $max_id);
1201     }
1202
1203
1204     function conversationStream($id, $offset=0, $limit=20, $since_id=0, $max_id=0)
1205     {
1206         $stream = new ConversationNoticeStream($id);
1207
1208         return $stream->getNotices($offset, $limit, $since_id, $max_id);
1209     }
1210
1211     /**
1212      * Is this notice part of an active conversation?
1213      *
1214      * @return boolean true if other messages exist in the same
1215      *                 conversation, false if this is the only one
1216      */
1217     function hasConversation()
1218     {
1219         if (!empty($this->conversation)) {
1220             $conversation = Notice::conversationStream(
1221                 $this->conversation,
1222                 1,
1223                 1
1224             );
1225
1226             if ($conversation->N > 0) {
1227                 return true;
1228             }
1229         }
1230         return false;
1231     }
1232
1233     /**
1234      * Grab the earliest notice from this conversation.
1235      *
1236      * @return Notice or null
1237      */
1238     function conversationRoot($profile=-1)
1239     {
1240         // XXX: can this happen?
1241
1242         if (empty($this->conversation)) {
1243             return null;
1244         }
1245
1246         // Get the current profile if not specified
1247
1248         if (is_int($profile) && $profile == -1) {
1249             $profile = Profile::current();
1250         }
1251
1252         // If this notice is out of scope, no root for you!
1253
1254         if (!$this->inScope($profile)) {
1255             return null;
1256         }
1257
1258         // If this isn't a reply to anything, then it's its own
1259         // root.
1260
1261         if (empty($this->reply_to)) {
1262             return $this;
1263         }
1264         
1265         if (is_null($profile)) {
1266             $keypart = sprintf('notice:conversation_root:%d:null', $this->id);
1267         } else {
1268             $keypart = sprintf('notice:conversation_root:%d:%d',
1269                                $this->id,
1270                                $profile->id);
1271         }
1272             
1273         $root = self::cacheGet($keypart);
1274
1275         if ($root !== false && $root->inScope($profile)) {
1276             return $root;
1277         }
1278
1279         $last = $this;
1280         while (true) {
1281             try {
1282                 $parent = $last->getParent();
1283                 if ($parent->inScope($profile)) {
1284                     $last = $parent;
1285                     continue;
1286                 }
1287             } catch (Exception $e) {
1288                 // Latest notice has no parent
1289             }
1290             // No parent, or parent out of scope
1291             $root = $last;
1292             break;
1293         }
1294
1295         self::cacheSet($keypart, $root);
1296
1297         return $root;
1298     }
1299
1300     /**
1301      * Pull up a full list of local recipients who will be getting
1302      * this notice in their inbox. Results will be cached, so don't
1303      * change the input data wily-nilly!
1304      *
1305      * @param array $groups optional list of Group objects;
1306      *              if left empty, will be loaded from group_inbox records
1307      * @param array $recipient optional list of reply profile ids
1308      *              if left empty, will be loaded from reply records
1309      * @return array associating recipient user IDs with an inbox source constant
1310      */
1311     function whoGets(array $groups=null, array $recipients=null)
1312     {
1313         $c = self::memcache();
1314
1315         if (!empty($c)) {
1316             $ni = $c->get(Cache::key('notice:who_gets:'.$this->id));
1317             if ($ni !== false) {
1318                 return $ni;
1319             }
1320         }
1321
1322         if (is_null($recipients)) {
1323             $recipients = $this->getReplies();
1324         }
1325
1326         $ni = array();
1327
1328         // Give plugins a chance to add folks in at start...
1329         if (Event::handle('StartNoticeWhoGets', array($this, &$ni))) {
1330
1331             $users = $this->getSubscribedUsers();
1332             foreach ($users as $id) {
1333                 $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
1334             }
1335
1336             if (is_null($groups)) {
1337                 $groups = $this->getGroups();
1338             }
1339             foreach ($groups as $group) {
1340                 $users = $group->getUserMembers();
1341                 foreach ($users as $id) {
1342                     if (!array_key_exists($id, $ni)) {
1343                         $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
1344                     }
1345                 }
1346             }
1347
1348             $ptAtts = $this->getAttentionsFromProfileTags();
1349             foreach ($ptAtts as $key=>$val) {
1350                 if (!array_key_exists($key, $ni)) {
1351                     $ni[$key] = $val;
1352                 }
1353             }
1354
1355             foreach ($recipients as $recipient) {
1356                 if (!array_key_exists($recipient, $ni)) {
1357                     $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
1358                 }
1359             }
1360
1361             // Exclude any deleted, non-local, or blocking recipients.
1362             $profile = $this->getProfile();
1363             $originalProfile = null;
1364             if ($this->repeat_of) {
1365                 // Check blocks against the original notice's poster as well.
1366                 $original = Notice::getKV('id', $this->repeat_of);
1367                 if ($original instanceof Notice) {
1368                     $originalProfile = $original->getProfile();
1369                 }
1370             }
1371
1372             foreach ($ni as $id => $source) {
1373                 try {
1374                     $user = User::getKV('id', $id);
1375                     if (!$user instanceof User ||
1376                         $user->hasBlocked($profile) ||
1377                         ($originalProfile && $user->hasBlocked($originalProfile))) {
1378                         unset($ni[$id]);
1379                     }
1380                 } catch (UserNoProfileException $e) {
1381                     // User doesn't have a profile; invalid; skip them.
1382                     unset($ni[$id]);
1383                 }
1384             }
1385
1386             // Give plugins a chance to filter out...
1387             Event::handle('EndNoticeWhoGets', array($this, &$ni));
1388         }
1389
1390         if (!empty($c)) {
1391             // XXX: pack this data better
1392             $c->set(Cache::key('notice:who_gets:'.$this->id), $ni);
1393         }
1394
1395         return $ni;
1396     }
1397
1398     function getSubscribedUsers()
1399     {
1400         $user = new User();
1401
1402         if(common_config('db','quote_identifiers'))
1403           $user_table = '"user"';
1404         else $user_table = 'user';
1405
1406         $qry =
1407           'SELECT id ' .
1408           'FROM '. $user_table .' JOIN subscription '.
1409           'ON '. $user_table .'.id = subscription.subscriber ' .
1410           'WHERE subscription.subscribed = %d ';
1411
1412         $user->query(sprintf($qry, $this->profile_id));
1413
1414         $ids = array();
1415
1416         while ($user->fetch()) {
1417             $ids[] = $user->id;
1418         }
1419
1420         $user->free();
1421
1422         return $ids;
1423     }
1424
1425     function getProfileTags()
1426     {
1427         $profile = $this->getProfile();
1428         $list    = $profile->getOtherTags($profile);
1429         $ptags   = array();
1430
1431         while($list->fetch()) {
1432             $ptags[] = clone($list);
1433         }
1434
1435         return $ptags;
1436     }
1437
1438     public function getAttentionsFromProfileTags()
1439     {
1440         $ni = array();
1441         $ptags = $this->getProfileTags();
1442         foreach ($ptags as $ptag) {
1443             $users = $ptag->getUserSubscribers();
1444             foreach ($users as $id) {
1445                 $ni[$id] = NOTICE_INBOX_SOURCE_PROFILE_TAG;
1446             }
1447         }
1448         return $ni;
1449     }
1450
1451     /**
1452      * Record this notice to the given group inboxes for delivery.
1453      * Overrides the regular parsing of !group markup.
1454      *
1455      * @param string $group_ids
1456      * @fixme might prefer URIs as identifiers, as for replies?
1457      *        best with generalizations on user_group to support
1458      *        remote groups better.
1459      */
1460     function saveKnownGroups($group_ids)
1461     {
1462         if (!is_array($group_ids)) {
1463             // TRANS: Server exception thrown when no array is provided to the method saveKnownGroups().
1464             throw new ServerException(_('Bad type provided to saveKnownGroups.'));
1465         }
1466
1467         $groups = array();
1468         foreach (array_unique($group_ids) as $id) {
1469             $group = User_group::getKV('id', $id);
1470             if ($group instanceof User_group) {
1471                 common_log(LOG_ERR, "Local delivery to group id $id, $group->nickname");
1472                 $result = $this->addToGroupInbox($group);
1473                 if (!$result) {
1474                     common_log_db_error($gi, 'INSERT', __FILE__);
1475                 }
1476
1477                 if (common_config('group', 'addtag')) {
1478                     // we automatically add a tag for every group name, too
1479
1480                     $tag = Notice_tag::pkeyGet(array('tag' => common_canonical_tag($group->nickname),
1481                                                      'notice_id' => $this->id));
1482
1483                     if (is_null($tag)) {
1484                         $this->saveTag($group->nickname);
1485                     }
1486                 }
1487
1488                 $groups[] = clone($group);
1489             } else {
1490                 common_log(LOG_ERR, "Local delivery to group id $id skipped, doesn't exist");
1491             }
1492         }
1493
1494         return $groups;
1495     }
1496
1497     function addToGroupInbox(User_group $group)
1498     {
1499         $gi = Group_inbox::pkeyGet(array('group_id' => $group->id,
1500                                          'notice_id' => $this->id));
1501
1502         if (!$gi instanceof Group_inbox) {
1503
1504             $gi = new Group_inbox();
1505
1506             $gi->group_id  = $group->id;
1507             $gi->notice_id = $this->id;
1508             $gi->created   = $this->created;
1509
1510             $result = $gi->insert();
1511
1512             if (!$result) {
1513                 common_log_db_error($gi, 'INSERT', __FILE__);
1514                 // TRANS: Server exception thrown when an update for a group inbox fails.
1515                 throw new ServerException(_('Problem saving group inbox.'));
1516             }
1517
1518             self::blow('user_group:notice_ids:%d', $gi->group_id);
1519         }
1520
1521         return true;
1522     }
1523
1524     /**
1525      * Save reply records indicating that this notice needs to be
1526      * delivered to the local users with the given URIs.
1527      *
1528      * Since this is expected to be used when saving foreign-sourced
1529      * messages, we won't deliver to any remote targets as that's the
1530      * source service's responsibility.
1531      *
1532      * Mail notifications etc will be handled later.
1533      *
1534      * @param array  $uris   Array of unique identifier URIs for recipients
1535      */
1536     function saveKnownReplies(array $uris)
1537     {
1538         if (empty($uris)) {
1539             return;
1540         }
1541
1542         $sender = Profile::getKV($this->profile_id);
1543
1544         foreach (array_unique($uris) as $uri) {
1545             try {
1546                 $profile = Profile::fromUri($uri);
1547             } catch (UnknownUriException $e) {
1548                 common_log(LOG_WARNING, "Unable to determine profile for URI '$uri'");
1549                 continue;
1550             }
1551
1552             if ($profile->hasBlocked($sender)) {
1553                 common_log(LOG_INFO, "Not saving reply to profile {$profile->id} ($uri) from sender {$sender->id} because of a block.");
1554                 continue;
1555             }
1556
1557             $this->saveReply($profile->id);
1558             self::blow('reply:stream:%d', $profile->id);
1559         }
1560
1561         return;
1562     }
1563
1564     /**
1565      * Pull @-replies from this message's content in StatusNet markup format
1566      * and save reply records indicating that this message needs to be
1567      * delivered to those users.
1568      *
1569      * Mail notifications to local profiles will be sent later.
1570      *
1571      * @return array of integer profile IDs
1572      */
1573
1574     function saveReplies()
1575     {
1576         // Don't save reply data for repeats
1577
1578         if (!empty($this->repeat_of)) {
1579             return array();
1580         }
1581
1582         $sender = $this->getProfile();
1583
1584         $replied = array();
1585
1586         // If it's a reply, save for the replied-to author
1587         try {
1588             $parent = $this->getParent();
1589             $parentauthor = $parent->getProfile();
1590             $this->saveReply($parentauthor->id);
1591             $replied[$parentauthor->id] = 1;
1592             self::blow('reply:stream:%d', $parentauthor->id);
1593         } catch (Exception $e) {
1594             // Not a reply, since it has no parent!
1595         }
1596
1597         // @todo ideally this parser information would only
1598         // be calculated once.
1599
1600         $mentions = common_find_mentions($this->content, $this);
1601
1602         // store replied only for first @ (what user/notice what the reply directed,
1603         // we assume first @ is it)
1604
1605         foreach ($mentions as $mention) {
1606
1607             foreach ($mention['mentioned'] as $mentioned) {
1608
1609                 // skip if they're already covered
1610
1611                 if (!empty($replied[$mentioned->id])) {
1612                     continue;
1613                 }
1614
1615                 // Don't save replies from blocked profile to local user
1616
1617                 $mentioned_user = User::getKV('id', $mentioned->id);
1618                 if ($mentioned_user instanceof User && $mentioned_user->hasBlocked($sender)) {
1619                     continue;
1620                 }
1621
1622                 $this->saveReply($mentioned->id);
1623                 $replied[$mentioned->id] = 1;
1624                 self::blow('reply:stream:%d', $mentioned->id);
1625             }
1626         }
1627
1628         $recipientIds = array_keys($replied);
1629
1630         return $recipientIds;
1631     }
1632
1633     function saveReply($profileId)
1634     {
1635         $reply = new Reply();
1636
1637         $reply->notice_id  = $this->id;
1638         $reply->profile_id = $profileId;
1639         $reply->modified   = $this->created;
1640
1641         $reply->insert();
1642
1643         return $reply;
1644     }
1645
1646     protected $_replies = array();
1647
1648     /**
1649      * Pull the complete list of @-reply targets for this notice.
1650      *
1651      * @return array of integer profile ids
1652      */
1653     function getReplies()
1654     {
1655         if (isset($this->_replies[$this->id])) {
1656             return $this->_replies[$this->id];
1657         }
1658
1659         $replyMap = Reply::listGet('notice_id', array($this->id));
1660
1661         $ids = array();
1662
1663         foreach ($replyMap[$this->id] as $reply) {
1664             $ids[] = $reply->profile_id;
1665         }
1666
1667         $this->_replies[$this->id] = $ids;
1668
1669         return $ids;
1670     }
1671
1672     function _setReplies($replies)
1673     {
1674         $this->_replies[$this->id] = $replies;
1675     }
1676
1677     /**
1678      * Pull the complete list of @-reply targets for this notice.
1679      *
1680      * @return array of Profiles
1681      */
1682     function getReplyProfiles()
1683     {
1684         $ids = $this->getReplies();
1685         
1686         $profiles = Profile::multiGet('id', $ids);
1687         
1688         return $profiles->fetchAll();
1689     }
1690
1691     /**
1692      * Send e-mail notifications to local @-reply targets.
1693      *
1694      * Replies must already have been saved; this is expected to be run
1695      * from the distrib queue handler.
1696      */
1697     function sendReplyNotifications()
1698     {
1699         // Don't send reply notifications for repeats
1700
1701         if (!empty($this->repeat_of)) {
1702             return array();
1703         }
1704
1705         $recipientIds = $this->getReplies();
1706
1707         foreach ($recipientIds as $recipientId) {
1708             $user = User::getKV('id', $recipientId);
1709             if ($user instanceof User) {
1710                 mail_notify_attn($user, $this);
1711             }
1712         }
1713     }
1714
1715     /**
1716      * Pull list of groups this notice needs to be delivered to,
1717      * as previously recorded by saveKnownGroups().
1718      *
1719      * @return array of Group objects
1720      */
1721     
1722     protected $_groups = array();
1723     
1724     function getGroups()
1725     {
1726         // Don't save groups for repeats
1727
1728         if (!empty($this->repeat_of)) {
1729             return array();
1730         }
1731         
1732         if (isset($this->_groups[$this->id])) {
1733             return $this->_groups[$this->id];
1734         }
1735         
1736         $gis = Group_inbox::listGet('notice_id', array($this->id));
1737
1738         $ids = array();
1739
1740                 foreach ($gis[$this->id] as $gi)
1741                 {
1742                     $ids[] = $gi->group_id;
1743                 }
1744                 
1745                 $groups = User_group::multiGet('id', $ids);
1746                 
1747                 $this->_groups[$this->id] = $groups->fetchAll();
1748                 
1749                 return $this->_groups[$this->id];
1750     }
1751     
1752     function _setGroups($groups)
1753     {
1754         $this->_groups[$this->id] = $groups;
1755     }
1756
1757     /**
1758      * Convert a notice into an activity for export.
1759      *
1760      * @param User $cur Current user
1761      *
1762      * @return Activity activity object representing this Notice.
1763      */
1764
1765     function asActivity($cur=null)
1766     {
1767         $act = self::cacheGet(Cache::codeKey('notice:as-activity:'.$this->id));
1768
1769         if ($act instanceof Activity) {
1770             return $act;
1771         }
1772         $act = new Activity();
1773
1774         if (Event::handle('StartNoticeAsActivity', array($this, &$act))) {
1775
1776             $act->id      = $this->uri;
1777             $act->time    = strtotime($this->created);
1778             try {
1779                 $act->link    = $this->getUrl();
1780             } catch (InvalidUrlException $e) {
1781                 // The notice is probably a share or similar, which don't
1782                 // have a representational URL of their own.
1783             }
1784             $act->content = common_xml_safe_str($this->rendered);
1785
1786             $profile = $this->getProfile();
1787
1788             $act->actor            = $profile->asActivityObject();
1789             $act->actor->extra[]   = $profile->profileInfo($cur);
1790
1791             $act->verb = $this->verb;
1792
1793             if ($this->repeat_of) {
1794                 $repeated = Notice::getKV('id', $this->repeat_of);
1795                 if ($repeated instanceof Notice) {
1796                     $act->objects[] = $repeated->asActivity($cur);
1797                 }
1798             } else {
1799                 $act->objects[] = ActivityObject::fromNotice($this);
1800             }
1801
1802             // XXX: should this be handled by default processing for object entry?
1803
1804             // Categories
1805
1806             $tags = $this->getTags();
1807
1808             foreach ($tags as $tag) {
1809                 $cat       = new AtomCategory();
1810                 $cat->term = $tag;
1811
1812                 $act->categories[] = $cat;
1813             }
1814
1815             // Enclosures
1816             // XXX: use Atom Media and/or File activity objects instead
1817
1818             $attachments = $this->attachments();
1819
1820             foreach ($attachments as $attachment) {
1821                 // Save local attachments
1822                 if (!empty($attachment->filename)) {
1823                     $act->attachments[] = ActivityObject::fromFile($attachment);
1824                 }
1825             }
1826
1827             $ctx = new ActivityContext();
1828
1829             try {
1830                 $reply = $this->getParent();
1831                 $ctx->replyToID  = $reply->getUri();
1832                 $ctx->replyToUrl = $reply->getUrl();
1833             } catch (Exception $e) {
1834                 // This is not a reply to something
1835             }
1836
1837             $ctx->location = $this->getLocation();
1838
1839             $conv = null;
1840
1841             if (!empty($this->conversation)) {
1842                 $conv = Conversation::getKV('id', $this->conversation);
1843                 if ($conv instanceof Conversation) {
1844                     $ctx->conversation = $conv->uri;
1845                 }
1846             }
1847
1848             $reply_ids = $this->getReplies();
1849
1850             foreach ($reply_ids as $id) {
1851                 $rprofile = Profile::getKV('id', $id);
1852                 if ($rprofile instanceof Profile) {
1853                     $ctx->attention[$rprofile->getUri()] = ActivityObject::PERSON;
1854                 }
1855             }
1856
1857             $groups = $this->getGroups();
1858
1859             foreach ($groups as $group) {
1860                 $ctx->attention[$group->getUri()] = ActivityObject::GROUP;
1861             }
1862
1863             switch ($this->scope) {
1864             case Notice::PUBLIC_SCOPE:
1865                 $ctx->attention[ActivityContext::ATTN_PUBLIC] = ActivityObject::COLLECTION;
1866                 break;
1867             case Notice::FOLLOWER_SCOPE:
1868                 $surl = common_local_url("subscribers", array('nickname' => $profile->nickname));
1869                 $ctx->attention[$surl] = ActivityObject::COLLECTION;
1870                 break;
1871             }
1872
1873             $act->context = $ctx;
1874
1875             $source = $this->getSource();
1876
1877             if ($source instanceof Notice_source) {
1878                 $act->generator = ActivityObject::fromNoticeSource($source);
1879             }
1880
1881             // Source
1882
1883             $atom_feed = $profile->getAtomFeed();
1884
1885             if (!empty($atom_feed)) {
1886
1887                 $act->source = new ActivitySource();
1888
1889                 // XXX: we should store the actual feed ID
1890
1891                 $act->source->id = $atom_feed;
1892
1893                 // XXX: we should store the actual feed title
1894
1895                 $act->source->title = $profile->getBestName();
1896
1897                 $act->source->links['alternate'] = $profile->profileurl;
1898                 $act->source->links['self']      = $atom_feed;
1899
1900                 $act->source->icon = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
1901
1902                 $notice = $profile->getCurrentNotice();
1903
1904                 if ($notice instanceof Notice) {
1905                     $act->source->updated = self::utcDate($notice->created);
1906                 }
1907
1908                 $user = User::getKV('id', $profile->id);
1909
1910                 if ($user instanceof User) {
1911                     $act->source->links['license'] = common_config('license', 'url');
1912                 }
1913             }
1914
1915             if ($this->isLocal()) {
1916                 $act->selfLink = common_local_url('ApiStatusesShow', array('id' => $this->id,
1917                                                                            'format' => 'atom'));
1918                 $act->editLink = $act->selfLink;
1919             }
1920
1921             Event::handle('EndNoticeAsActivity', array($this, &$act));
1922         }
1923
1924         self::cacheSet(Cache::codeKey('notice:as-activity:'.$this->id), $act);
1925
1926         return $act;
1927     }
1928
1929     // This has gotten way too long. Needs to be sliced up into functional bits
1930     // or ideally exported to a utility class.
1931
1932     function asAtomEntry($namespace=false,
1933                          $source=false,
1934                          $author=true,
1935                          $cur=null)
1936     {
1937         $act = $this->asActivity($cur);
1938         $act->extra[] = $this->noticeInfo($cur);
1939         return $act->asString($namespace, $author, $source);
1940     }
1941
1942     /**
1943      * Extra notice info for atom entries
1944      *
1945      * Clients use some extra notice info in the atom stream.
1946      * This gives it to them.
1947      *
1948      * @param User $cur Current user
1949      *
1950      * @return array representation of <statusnet:notice_info> element
1951      */
1952
1953     function noticeInfo($cur)
1954     {
1955         // local notice ID (useful to clients for ordering)
1956
1957         $noticeInfoAttr = array('local_id' => $this->id);
1958
1959         // notice source
1960
1961         $ns = $this->getSource();
1962
1963         if ($ns instanceof Notice_source) {
1964             $noticeInfoAttr['source'] =  $ns->code;
1965             if (!empty($ns->url)) {
1966                 $noticeInfoAttr['source_link'] = $ns->url;
1967                 if (!empty($ns->name)) {
1968                     $noticeInfoAttr['source'] =  '<a href="'
1969                         . htmlspecialchars($ns->url)
1970                         . '" rel="nofollow">'
1971                         . htmlspecialchars($ns->name)
1972                         . '</a>';
1973                 }
1974             }
1975         }
1976
1977         // favorite and repeated
1978
1979         $scoped = null;
1980         if (!empty($cur)) {
1981             $scoped = $cur->getProfile();
1982             $noticeInfoAttr['repeated'] = ($scoped->hasRepeated($this)) ? "true" : "false";
1983         }
1984
1985         if (!empty($this->repeat_of)) {
1986             $noticeInfoAttr['repeat_of'] = $this->repeat_of;
1987         }
1988
1989         Event::handle('StatusNetApiNoticeInfo', array($this, &$noticeInfoAttr, $scoped));
1990
1991         return array('statusnet:notice_info', $noticeInfoAttr, null);
1992     }
1993
1994     /**
1995      * Returns an XML string fragment with a reference to a notice as an
1996      * Activity Streams noun object with the given element type.
1997      *
1998      * Assumes that 'activity' namespace has been previously defined.
1999      *
2000      * @param string $element one of 'subject', 'object', 'target'
2001      * @return string
2002      */
2003
2004     function asActivityNoun($element)
2005     {
2006         $noun = ActivityObject::fromNotice($this);
2007         return $noun->asString('activity:' . $element);
2008     }
2009
2010     /**
2011      * Determine which notice, if any, a new notice is in reply to.
2012      *
2013      * For conversation tracking, we try to see where this notice fits
2014      * in the tree. Beware that this may very well give false positives
2015      * and add replies to wrong threads (if there have been newer posts
2016      * by the same user as we're replying to).
2017      *
2018      * @param Profile $sender     Author profile
2019      * @param string  $content    Final notice content
2020      *
2021      * @return integer ID of replied-to notice, or null for not a reply.
2022      */
2023
2024     static function getInlineReplyTo(Profile $sender, $content)
2025     {
2026         // Is there an initial @ or T?
2027         if (preg_match('/^T ([A-Z0-9]{1,64}) /', $content, $match)
2028                 || preg_match('/^@([a-z0-9]{1,64})\s+/', $content, $match)) {
2029             $nickname = common_canonical_nickname($match[1]);
2030         } else {
2031             return null;
2032         }
2033
2034         // Figure out who that is.
2035         $recipient = common_relative_profile($sender, $nickname, common_sql_now());
2036
2037         if ($recipient instanceof Profile) {
2038             // Get their last notice
2039             $last = $recipient->getCurrentNotice();
2040             if ($last instanceof Notice) {
2041                 return $last;
2042             }
2043             // Maybe in the future we want to handle something else below
2044             // so don't return getCurrentNotice() immediately.
2045         }
2046
2047         return null;
2048     }
2049
2050     static function maxContent()
2051     {
2052         $contentlimit = common_config('notice', 'contentlimit');
2053         // null => use global limit (distinct from 0!)
2054         if (is_null($contentlimit)) {
2055             $contentlimit = common_config('site', 'textlimit');
2056         }
2057         return $contentlimit;
2058     }
2059
2060     static function contentTooLong($content)
2061     {
2062         $contentlimit = self::maxContent();
2063         return ($contentlimit > 0 && !empty($content) && (mb_strlen($content) > $contentlimit));
2064     }
2065
2066     function getLocation()
2067     {
2068         $location = null;
2069
2070         if (!empty($this->location_id) && !empty($this->location_ns)) {
2071             $location = Location::fromId($this->location_id, $this->location_ns);
2072         }
2073
2074         if (is_null($location)) { // no ID, or Location::fromId() failed
2075             if (!empty($this->lat) && !empty($this->lon)) {
2076                 $location = Location::fromLatLon($this->lat, $this->lon);
2077             }
2078         }
2079
2080         return $location;
2081     }
2082
2083     /**
2084      * Convenience function for posting a repeat of an existing message.
2085      *
2086      * @param Profile $repeater Profile which is doing the repeat
2087      * @param string $source: posting source key, eg 'web', 'api', etc
2088      * @return Notice
2089      *
2090      * @throws Exception on failure or permission problems
2091      */
2092     function repeat(Profile $repeater, $source)
2093     {
2094         $author = $this->getProfile();
2095
2096         // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'.
2097         // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice.
2098         $content = sprintf(_('RT @%1$s %2$s'),
2099                            $author->getNickname(),
2100                            $this->content);
2101
2102         // Scope is same as this one's
2103         return self::saveNew($repeater->id,
2104                              $content,
2105                              $source,
2106                              array('repeat_of' => $this->id,
2107                                    'scope' => $this->scope));
2108     }
2109
2110     // These are supposed to be in chron order!
2111
2112     function repeatStream($limit=100)
2113     {
2114         $cache = Cache::instance();
2115
2116         if (empty($cache)) {
2117             $ids = $this->_repeatStreamDirect($limit);
2118         } else {
2119             $idstr = $cache->get(Cache::key('notice:repeats:'.$this->id));
2120             if ($idstr !== false) {
2121                 if (empty($idstr)) {
2122                         $ids = array();
2123                 } else {
2124                         $ids = explode(',', $idstr);
2125                 }
2126             } else {
2127                 $ids = $this->_repeatStreamDirect(100);
2128                 $cache->set(Cache::key('notice:repeats:'.$this->id), implode(',', $ids));
2129             }
2130             if ($limit < 100) {
2131                 // We do a max of 100, so slice down to limit
2132                 $ids = array_slice($ids, 0, $limit);
2133             }
2134         }
2135
2136         return NoticeStream::getStreamByIds($ids);
2137     }
2138
2139     function _repeatStreamDirect($limit)
2140     {
2141         $notice = new Notice();
2142
2143         $notice->selectAdd(); // clears it
2144         $notice->selectAdd('id');
2145
2146         $notice->repeat_of = $this->id;
2147
2148         $notice->orderBy('created, id'); // NB: asc!
2149
2150         if (!is_null($limit)) {
2151             $notice->limit(0, $limit);
2152         }
2153
2154         return $notice->fetchAll('id');
2155     }
2156
2157     function locationOptions($lat, $lon, $location_id, $location_ns, $profile = null)
2158     {
2159         $options = array();
2160
2161         if (!empty($location_id) && !empty($location_ns)) {
2162             $options['location_id'] = $location_id;
2163             $options['location_ns'] = $location_ns;
2164
2165             $location = Location::fromId($location_id, $location_ns);
2166
2167             if ($location instanceof Location) {
2168                 $options['lat'] = $location->lat;
2169                 $options['lon'] = $location->lon;
2170             }
2171
2172         } else if (!empty($lat) && !empty($lon)) {
2173             $options['lat'] = $lat;
2174             $options['lon'] = $lon;
2175
2176             $location = Location::fromLatLon($lat, $lon);
2177
2178             if ($location instanceof Location) {
2179                 $options['location_id'] = $location->location_id;
2180                 $options['location_ns'] = $location->location_ns;
2181             }
2182         } else if (!empty($profile)) {
2183             if (isset($profile->lat) && isset($profile->lon)) {
2184                 $options['lat'] = $profile->lat;
2185                 $options['lon'] = $profile->lon;
2186             }
2187
2188             if (isset($profile->location_id) && isset($profile->location_ns)) {
2189                 $options['location_id'] = $profile->location_id;
2190                 $options['location_ns'] = $profile->location_ns;
2191             }
2192         }
2193
2194         return $options;
2195     }
2196
2197     function clearAttentions()
2198     {
2199         $att = new Attention();
2200         $att->notice_id = $this->getID();
2201
2202         if ($att->find()) {
2203             while ($att->fetch()) {
2204                 // Can't do delete() on the object directly since it won't remove all of it
2205                 $other = clone($att);
2206                 $other->delete();
2207             }
2208         }
2209     }
2210
2211     function clearReplies()
2212     {
2213         $replyNotice = new Notice();
2214         $replyNotice->reply_to = $this->id;
2215
2216         //Null any notices that are replies to this notice
2217
2218         if ($replyNotice->find()) {
2219             while ($replyNotice->fetch()) {
2220                 $orig = clone($replyNotice);
2221                 $replyNotice->reply_to = null;
2222                 $replyNotice->update($orig);
2223             }
2224         }
2225
2226         // Reply records
2227
2228         $reply = new Reply();
2229         $reply->notice_id = $this->id;
2230
2231         if ($reply->find()) {
2232             while($reply->fetch()) {
2233                 self::blow('reply:stream:%d', $reply->profile_id);
2234                 $reply->delete();
2235             }
2236         }
2237
2238         $reply->free();
2239     }
2240
2241     function clearFiles()
2242     {
2243         $f2p = new File_to_post();
2244
2245         $f2p->post_id = $this->id;
2246
2247         if ($f2p->find()) {
2248             while ($f2p->fetch()) {
2249                 $f2p->delete();
2250             }
2251         }
2252         // FIXME: decide whether to delete File objects
2253         // ...and related (actual) files
2254     }
2255
2256     function clearRepeats()
2257     {
2258         $repeatNotice = new Notice();
2259         $repeatNotice->repeat_of = $this->id;
2260
2261         //Null any notices that are repeats of this notice
2262
2263         if ($repeatNotice->find()) {
2264             while ($repeatNotice->fetch()) {
2265                 $orig = clone($repeatNotice);
2266                 $repeatNotice->repeat_of = null;
2267                 $repeatNotice->update($orig);
2268             }
2269         }
2270     }
2271
2272     function clearTags()
2273     {
2274         $tag = new Notice_tag();
2275         $tag->notice_id = $this->id;
2276
2277         if ($tag->find()) {
2278             while ($tag->fetch()) {
2279                 self::blow('profile:notice_ids_tagged:%d:%s', $this->profile_id, Cache::keyize($tag->tag));
2280                 self::blow('profile:notice_ids_tagged:%d:%s;last', $this->profile_id, Cache::keyize($tag->tag));
2281                 self::blow('notice_tag:notice_ids:%s', Cache::keyize($tag->tag));
2282                 self::blow('notice_tag:notice_ids:%s;last', Cache::keyize($tag->tag));
2283                 $tag->delete();
2284             }
2285         }
2286
2287         $tag->free();
2288     }
2289
2290     function clearGroupInboxes()
2291     {
2292         $gi = new Group_inbox();
2293
2294         $gi->notice_id = $this->id;
2295
2296         if ($gi->find()) {
2297             while ($gi->fetch()) {
2298                 self::blow('user_group:notice_ids:%d', $gi->group_id);
2299                 $gi->delete();
2300             }
2301         }
2302
2303         $gi->free();
2304     }
2305
2306     function distribute()
2307     {
2308         // We always insert for the author so they don't
2309         // have to wait
2310         Event::handle('StartNoticeDistribute', array($this));
2311
2312         // If there's a failure, we want to _force_
2313         // distribution at this point.
2314         try {
2315             $qm = QueueManager::get();
2316             $qm->enqueue($this, 'distrib');
2317         } catch (Exception $e) {
2318             // If the exception isn't transient, this
2319             // may throw more exceptions as DQH does
2320             // its own enqueueing. So, we ignore them!
2321             try {
2322                 $handler = new DistribQueueHandler();
2323                 $handler->handle($this);
2324             } catch (Exception $e) {
2325                 common_log(LOG_ERR, "emergency redistribution resulted in " . $e->getMessage());
2326             }
2327             // Re-throw so somebody smarter can handle it.
2328             throw $e;
2329         }
2330     }
2331
2332     function insert()
2333     {
2334         $result = parent::insert();
2335
2336         if ($result !== false) {
2337             // Profile::hasRepeated() abuses pkeyGet(), so we
2338             // have to clear manually
2339             if (!empty($this->repeat_of)) {
2340                 $c = self::memcache();
2341                 if (!empty($c)) {
2342                     $ck = self::multicacheKey('Notice',
2343                                               array('profile_id' => $this->profile_id,
2344                                                     'repeat_of' => $this->repeat_of));
2345                     $c->delete($ck);
2346                 }
2347             }
2348         }
2349
2350         return $result;
2351     }
2352
2353     /**
2354      * Get the source of the notice
2355      *
2356      * @return Notice_source $ns A notice source object. 'code' is the only attribute
2357      *                           guaranteed to be populated.
2358      */
2359     function getSource()
2360     {
2361         $ns = new Notice_source();
2362         if (!empty($this->source)) {
2363             switch ($this->source) {
2364             case 'web':
2365             case 'xmpp':
2366             case 'mail':
2367             case 'omb':
2368             case 'system':
2369             case 'api':
2370                 $ns->code = $this->source;
2371                 break;
2372             default:
2373                 $ns = Notice_source::getKV($this->source);
2374                 if (!$ns) {
2375                     $ns = new Notice_source();
2376                     $ns->code = $this->source;
2377                     $app = Oauth_application::getKV('name', $this->source);
2378                     if ($app) {
2379                         $ns->name = $app->name;
2380                         $ns->url  = $app->source_url;
2381                     }
2382                 }
2383                 break;
2384             }
2385         }
2386         return $ns;
2387     }
2388
2389     /**
2390      * Determine whether the notice was locally created
2391      *
2392      * @return boolean locality
2393      */
2394
2395     public function isLocal()
2396     {
2397         return ($this->is_local == Notice::LOCAL_PUBLIC ||
2398                 $this->is_local == Notice::LOCAL_NONPUBLIC);
2399     }
2400
2401     /**
2402      * Get the list of hash tags saved with this notice.
2403      *
2404      * @return array of strings
2405      */
2406     public function getTags()
2407     {
2408         $tags = array();
2409
2410         $keypart = sprintf('notice:tags:%d', $this->id);
2411
2412         $tagstr = self::cacheGet($keypart);
2413
2414         if ($tagstr !== false) {
2415             $tags = explode(',', $tagstr);
2416         } else {
2417             $tag = new Notice_tag();
2418             $tag->notice_id = $this->id;
2419             if ($tag->find()) {
2420                 while ($tag->fetch()) {
2421                     $tags[] = $tag->tag;
2422                 }
2423             }
2424             self::cacheSet($keypart, implode(',', $tags));
2425         }
2426
2427         return $tags;
2428     }
2429
2430     static private function utcDate($dt)
2431     {
2432         $dateStr = date('d F Y H:i:s', strtotime($dt));
2433         $d = new DateTime($dateStr, new DateTimeZone('UTC'));
2434         return $d->format(DATE_W3C);
2435     }
2436
2437     /**
2438      * Look up the creation timestamp for a given notice ID, even
2439      * if it's been deleted.
2440      *
2441      * @param int $id
2442      * @return mixed string recorded creation timestamp, or false if can't be found
2443      */
2444     public static function getAsTimestamp($id)
2445     {
2446         if (!$id) {
2447             return false;
2448         }
2449
2450         $notice = Notice::getKV('id', $id);
2451         if ($notice) {
2452             return $notice->created;
2453         }
2454
2455         $deleted = Deleted_notice::getKV('id', $id);
2456         if ($deleted) {
2457             return $deleted->created;
2458         }
2459
2460         return false;
2461     }
2462
2463     /**
2464      * Build an SQL 'where' fragment for timestamp-based sorting from a since_id
2465      * parameter, matching notices posted after the given one (exclusive).
2466      *
2467      * If the referenced notice can't be found, will return false.
2468      *
2469      * @param int $id
2470      * @param string $idField
2471      * @param string $createdField
2472      * @return mixed string or false if no match
2473      */
2474     public static function whereSinceId($id, $idField='id', $createdField='created')
2475     {
2476         $since = Notice::getAsTimestamp($id);
2477         if ($since) {
2478             return sprintf("($createdField = '%s' and $idField > %d) or ($createdField > '%s')", $since, $id, $since);
2479         }
2480         return false;
2481     }
2482
2483     /**
2484      * Build an SQL 'where' fragment for timestamp-based sorting from a since_id
2485      * parameter, matching notices posted after the given one (exclusive), and
2486      * if necessary add it to the data object's query.
2487      *
2488      * @param DB_DataObject $obj
2489      * @param int $id
2490      * @param string $idField
2491      * @param string $createdField
2492      * @return mixed string or false if no match
2493      */
2494     public static function addWhereSinceId(DB_DataObject $obj, $id, $idField='id', $createdField='created')
2495     {
2496         $since = self::whereSinceId($id, $idField, $createdField);
2497         if ($since) {
2498             $obj->whereAdd($since);
2499         }
2500     }
2501
2502     /**
2503      * Build an SQL 'where' fragment for timestamp-based sorting from a max_id
2504      * parameter, matching notices posted before the given one (inclusive).
2505      *
2506      * If the referenced notice can't be found, will return false.
2507      *
2508      * @param int $id
2509      * @param string $idField
2510      * @param string $createdField
2511      * @return mixed string or false if no match
2512      */
2513     public static function whereMaxId($id, $idField='id', $createdField='created')
2514     {
2515         $max = Notice::getAsTimestamp($id);
2516         if ($max) {
2517             return sprintf("($createdField < '%s') or ($createdField = '%s' and $idField <= %d)", $max, $max, $id);
2518         }
2519         return false;
2520     }
2521
2522     /**
2523      * Build an SQL 'where' fragment for timestamp-based sorting from a max_id
2524      * parameter, matching notices posted before the given one (inclusive), and
2525      * if necessary add it to the data object's query.
2526      *
2527      * @param DB_DataObject $obj
2528      * @param int $id
2529      * @param string $idField
2530      * @param string $createdField
2531      * @return mixed string or false if no match
2532      */
2533     public static function addWhereMaxId(DB_DataObject $obj, $id, $idField='id', $createdField='created')
2534     {
2535         $max = self::whereMaxId($id, $idField, $createdField);
2536         if ($max) {
2537             $obj->whereAdd($max);
2538         }
2539     }
2540
2541     function isPublic()
2542     {
2543         if (common_config('public', 'localonly')) {
2544             return ($this->is_local == Notice::LOCAL_PUBLIC);
2545         } else {
2546             return (($this->is_local != Notice::LOCAL_NONPUBLIC) &&
2547                     ($this->is_local != Notice::GATEWAY));
2548         }
2549     }
2550
2551     /**
2552      * Check that the given profile is allowed to read, respond to, or otherwise
2553      * act on this notice.
2554      *
2555      * The $scope member is a bitmask of scopes, representing a logical AND of the
2556      * scope requirement. So, 0x03 (Notice::ADDRESSEE_SCOPE | Notice::SITE_SCOPE) means
2557      * "only visible to people who are mentioned in the notice AND are users on this site."
2558      * Users on the site who are not mentioned in the notice will not be able to see the
2559      * notice.
2560      *
2561      * @param Profile $profile The profile to check; pass null to check for public/unauthenticated users.
2562      *
2563      * @return boolean whether the profile is in the notice's scope
2564      */
2565     function inScope($profile)
2566     {
2567         if (is_null($profile)) {
2568             $keypart = sprintf('notice:in-scope-for:%d:null', $this->id);
2569         } else {
2570             $keypart = sprintf('notice:in-scope-for:%d:%d', $this->id, $profile->id);
2571         }
2572
2573         $result = self::cacheGet($keypart);
2574
2575         if ($result === false) {
2576             $bResult = false;
2577             if (Event::handle('StartNoticeInScope', array($this, $profile, &$bResult))) {
2578                 $bResult = $this->_inScope($profile);
2579                 Event::handle('EndNoticeInScope', array($this, $profile, &$bResult));
2580             }
2581             $result = ($bResult) ? 1 : 0;
2582             self::cacheSet($keypart, $result, 0, 300);
2583         }
2584
2585         return ($result == 1) ? true : false;
2586     }
2587
2588     protected function _inScope($profile)
2589     {
2590         if (!is_null($this->scope)) {
2591             $scope = $this->scope;
2592         } else {
2593             $scope = self::defaultScope();
2594         }
2595
2596         // If there's no scope, anyone (even anon) is in scope.
2597
2598         if ($scope == 0) { // Not private
2599
2600             return !$this->isHiddenSpam($profile);
2601
2602         } else { // Private, somehow
2603
2604             // If there's scope, anon cannot be in scope
2605
2606             if (empty($profile)) {
2607                 return false;
2608             }
2609
2610             // Author is always in scope
2611
2612             if ($this->profile_id == $profile->id) {
2613                 return true;
2614             }
2615
2616             // Only for users on this site
2617
2618             if (($scope & Notice::SITE_SCOPE) && !$profile->isLocal()) {
2619                 return false;
2620             }
2621
2622             // Only for users mentioned in the notice
2623
2624             if ($scope & Notice::ADDRESSEE_SCOPE) {
2625
2626                 $reply = Reply::pkeyGet(array('notice_id' => $this->id,
2627                                              'profile_id' => $profile->id));
2628                                                                                  
2629                 if (!$reply instanceof Reply) {
2630                     return false;
2631                 }
2632             }
2633
2634             // Only for members of the given group
2635
2636             if ($scope & Notice::GROUP_SCOPE) {
2637
2638                 // XXX: just query for the single membership
2639
2640                 $groups = $this->getGroups();
2641
2642                 $foundOne = false;
2643
2644                 foreach ($groups as $group) {
2645                     if ($profile->isMember($group)) {
2646                         $foundOne = true;
2647                         break;
2648                     }
2649                 }
2650
2651                 if (!$foundOne) {
2652                     return false;
2653                 }
2654             }
2655
2656             // Only for followers of the author
2657
2658             $author = null;
2659
2660             if ($scope & Notice::FOLLOWER_SCOPE) {
2661
2662                 try {
2663                     $author = $this->getProfile();
2664                 } catch (Exception $e) {
2665                     return false;
2666                 }
2667         
2668                 if (!Subscription::exists($profile, $author)) {
2669                     return false;
2670                 }
2671             }
2672
2673             return !$this->isHiddenSpam($profile);
2674         }
2675     }
2676
2677     function isHiddenSpam($profile) {
2678         
2679         // Hide posts by silenced users from everyone but moderators.
2680
2681         if (common_config('notice', 'hidespam')) {
2682
2683             try {
2684                 $author = $this->getProfile();
2685             } catch(Exception $e) {
2686                 // If we can't get an author, keep it hidden.
2687                 // XXX: technically not spam, but, whatever.
2688                 return true;
2689             }
2690
2691             if ($author->hasRole(Profile_role::SILENCED)) {
2692                 if (!$profile instanceof Profile || (($profile->id !== $author->id) && (!$profile->hasRight(Right::REVIEWSPAM)))) {
2693                     return true;
2694                 }
2695             }
2696         }
2697
2698         return false;
2699     }
2700
2701     public function getParent()
2702     {
2703         $parent = Notice::getKV('id', $this->reply_to);
2704
2705         if (!$parent instanceof Notice) {
2706             throw new ServerException('Notice has no parent');
2707         }
2708
2709         return $parent;
2710     }
2711
2712     /**
2713      * Magic function called at serialize() time.
2714      *
2715      * We use this to drop a couple process-specific references
2716      * from DB_DataObject which can cause trouble in future
2717      * processes.
2718      *
2719      * @return array of variable names to include in serialization.
2720      */
2721
2722     function __sleep()
2723     {
2724         $vars = parent::__sleep();
2725         $skip = array('_profile', '_groups', '_attachments', '_faves', '_replies', '_repeats');
2726         return array_diff($vars, $skip);
2727     }
2728     
2729     static function defaultScope()
2730     {
2731         $scope = common_config('notice', 'defaultscope');
2732         if (is_null($scope)) {
2733                 if (common_config('site', 'private')) {
2734                         $scope = 1;
2735                 } else {
2736                         $scope = 0;
2737                 }
2738         }
2739         return $scope;
2740     }
2741
2742         static function fillProfiles($notices)
2743         {
2744                 $map = self::getProfiles($notices);
2745                 
2746                 foreach ($notices as $entry=>$notice) {
2747             try {
2748                         if (array_key_exists($notice->profile_id, $map)) {
2749                                 $notice->_setProfile($map[$notice->profile_id]);
2750                         }
2751             } catch (NoProfileException $e) {
2752                 common_log(LOG_WARNING, "Failed to fill profile in Notice with non-existing entry for profile_id: {$e->profile_id}");
2753                 unset($notices[$entry]);
2754             }
2755                 }
2756                 
2757                 return array_values($map);
2758         }
2759         
2760         static function getProfiles(&$notices)
2761         {
2762                 $ids = array();
2763                 foreach ($notices as $notice) {
2764                         $ids[] = $notice->profile_id;
2765                 }
2766                 
2767                 $ids = array_unique($ids);
2768                 
2769                 return Profile::pivotGet('id', $ids); 
2770         }
2771         
2772         static function fillGroups(&$notices)
2773         {
2774         $ids = self::_idsOf($notices);
2775                 
2776         $gis = Group_inbox::listGet('notice_id', $ids);
2777                 
2778         $gids = array();
2779
2780                 foreach ($gis as $id => $gi)
2781                 {
2782                     foreach ($gi as $g)
2783                     {
2784                         $gids[] = $g->group_id;
2785                     }
2786                 }
2787                 
2788                 $gids = array_unique($gids);
2789                 
2790                 $group = User_group::pivotGet('id', $gids);
2791                 
2792                 foreach ($notices as $notice)
2793                 {
2794                         $grps = array();
2795                         $gi = $gis[$notice->id];
2796                         foreach ($gi as $g) {
2797                             $grps[] = $group[$g->group_id];
2798                         }
2799                     $notice->_setGroups($grps);
2800                 }
2801         }
2802
2803     static function _idsOf(array &$notices)
2804     {
2805                 $ids = array();
2806                 foreach ($notices as $notice) {
2807                         $ids[$notice->id] = true;
2808                 }
2809                 return array_keys($ids);
2810     }
2811
2812     static function fillAttachments(&$notices)
2813     {
2814         $ids = self::_idsOf($notices);
2815
2816         $f2pMap = File_to_post::listGet('post_id', $ids);
2817                 
2818                 $fileIds = array();
2819                 
2820                 foreach ($f2pMap as $noticeId => $f2ps) {
2821             foreach ($f2ps as $f2p) {
2822                 $fileIds[] = $f2p->file_id;    
2823             }
2824         }
2825
2826         $fileIds = array_unique($fileIds);
2827
2828                 $fileMap = File::pivotGet('id', $fileIds);
2829
2830                 foreach ($notices as $notice)
2831                 {
2832                         $files = array();
2833                         $f2ps = $f2pMap[$notice->id];
2834                         foreach ($f2ps as $f2p) {
2835                             $files[] = $fileMap[$f2p->file_id];
2836                         }
2837                     $notice->_setAttachments($files);
2838                 }
2839     }
2840
2841     static function fillReplies(&$notices)
2842     {
2843         $ids = self::_idsOf($notices);
2844         $replyMap = Reply::listGet('notice_id', $ids);
2845         foreach ($notices as $notice) {
2846             $replies = $replyMap[$notice->id];
2847             $ids = array();
2848             foreach ($replies as $reply) {
2849                 $ids[] = $reply->profile_id;
2850             }
2851             $notice->_setReplies($ids);
2852         }
2853     }
2854
2855     protected $_repeats = array();
2856
2857     function getRepeats()
2858     {
2859         if (isset($this->_repeats[$this->id])) {
2860             return $this->_repeats[$this->id];
2861         }
2862         $repeatMap = Notice::listGet('repeat_of', array($this->id));
2863         $this->_repeats[$this->id] = $repeatMap[$this->id];
2864         return $this->_repeats[$this->id];
2865     }
2866
2867     function _setRepeats($repeats)
2868     {
2869         $this->_repeats[$this->id] = $repeats;
2870     }
2871
2872     static function fillRepeats(&$notices)
2873     {
2874         $ids = self::_idsOf($notices);
2875         $repeatMap = Notice::listGet('repeat_of', $ids);
2876         foreach ($notices as $notice) {
2877                 $repeats = $repeatMap[$notice->id];
2878             $notice->_setRepeats($repeats);
2879         }
2880     }
2881 }