]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/User.php
Drop timestamp cutoff parameter from User::getCurrentNotice() and Profile::getCurrent...
[quix0rs-gnu-social.git] / classes / User.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, 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
20 if (!defined('STATUSNET') && !defined('LACONICA')) {
21     exit(1);
22 }
23
24 /**
25  * Table Definition for user
26  */
27
28 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
29 require_once 'Validate.php';
30
31 class User extends Memcached_DataObject
32 {
33     ###START_AUTOCODE
34     /* the code below is auto generated do not remove the above tag */
35
36     public $__table = 'user';                            // table name
37     public $id;                              // int(4)  primary_key not_null
38     public $nickname;                        // varchar(64)  unique_key
39     public $password;                        // varchar(255)
40     public $email;                           // varchar(255)  unique_key
41     public $incomingemail;                   // varchar(255)  unique_key
42     public $emailnotifysub;                  // tinyint(1)   default_1
43     public $emailnotifyfav;                  // tinyint(1)   default_1
44     public $emailnotifynudge;                // tinyint(1)   default_1
45     public $emailnotifymsg;                  // tinyint(1)   default_1
46     public $emailnotifyattn;                 // tinyint(1)   default_1
47     public $emailmicroid;                    // tinyint(1)   default_1
48     public $language;                        // varchar(50)
49     public $timezone;                        // varchar(50)
50     public $emailpost;                       // tinyint(1)   default_1
51     public $jabber;                          // varchar(255)  unique_key
52     public $jabbernotify;                    // tinyint(1)
53     public $jabberreplies;                   // tinyint(1)
54     public $jabbermicroid;                   // tinyint(1)   default_1
55     public $updatefrompresence;              // tinyint(1)
56     public $sms;                             // varchar(64)  unique_key
57     public $carrier;                         // int(4)
58     public $smsnotify;                       // tinyint(1)
59     public $smsreplies;                      // tinyint(1)
60     public $smsemail;                        // varchar(255)
61     public $uri;                             // varchar(255)  unique_key
62     public $autosubscribe;                   // tinyint(1)
63     public $urlshorteningservice;            // varchar(50)   default_ur1.ca
64     public $inboxed;                         // tinyint(1)
65     public $design_id;                       // int(4)
66     public $viewdesigns;                     // tinyint(1)   default_1
67     public $created;                         // datetime()   not_null
68     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
69
70     /* Static get */
71     function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User',$k,$v); }
72
73     /* the code above is auto generated do not remove the tag below */
74     ###END_AUTOCODE
75
76     function getProfile()
77     {
78         return Profile::staticGet('id', $this->id);
79     }
80
81     function isSubscribed($other)
82     {
83         return Subscription::exists($this->getProfile(), $other);
84     }
85
86     // 'update' won't write key columns, so we have to do it ourselves.
87
88     function updateKeys(&$orig)
89     {
90         $parts = array();
91         foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) {
92             if (strcmp($this->$k, $orig->$k) != 0) {
93                 $parts[] = $k . ' = ' . $this->_quote($this->$k);
94             }
95         }
96         if (count($parts) == 0) {
97             // No changes
98             return true;
99         }
100         $toupdate = implode(', ', $parts);
101
102         $table = common_database_tablename($this->tableName());
103         $qry = 'UPDATE ' . $table . ' SET ' . $toupdate .
104           ' WHERE id = ' . $this->id;
105         $orig->decache();
106         $result = $this->query($qry);
107         if ($result) {
108             $this->encache();
109         }
110         return $result;
111     }
112
113     static function allowed_nickname($nickname)
114     {
115         // XXX: should already be validated for size, content, etc.
116         $blacklist = common_config('nickname', 'blacklist');
117
118         //all directory and file names should be blacklisted
119         $d = dir(INSTALLDIR);
120         while (false !== ($entry = $d->read())) {
121             $blacklist[]=$entry;
122         }
123         $d->close();
124
125         //all top level names in the router should be blacklisted
126         $router = Router::get();
127         foreach(array_keys($router->m->getPaths()) as $path){
128             if(preg_match('/^\/(.*?)[\/\?]/',$path,$matches)){
129                 $blacklist[]=$matches[1];
130             }
131         }
132         return !in_array($nickname, $blacklist);
133     }
134
135     /**
136      * Get the most recent notice posted by this user, if any.
137      *
138      * @return mixed Notice or null
139      */
140     function getCurrentNotice()
141     {
142         $profile = $this->getProfile();
143         if (!$profile) {
144             return null;
145         }
146         return $profile->getCurrentNotice();
147     }
148
149     function getCarrier()
150     {
151         return Sms_carrier::staticGet('id', $this->carrier);
152     }
153
154     function subscribeTo($other)
155     {
156         $sub = new Subscription();
157         $sub->subscriber = $this->id;
158         $sub->subscribed = $other->id;
159
160         $sub->created = common_sql_now(); // current time
161
162         if (!$sub->insert()) {
163             return false;
164         }
165
166         return true;
167     }
168
169     function hasBlocked($other)
170     {
171         $profile = $this->getProfile();
172         return $profile->hasBlocked($other);
173     }
174
175     /**
176      * Register a new user account and profile and set up default subscriptions.
177      * If a new-user welcome message is configured, this will be sent.
178      *
179      * @param array $fields associative array of optional properties
180      *              string 'bio'
181      *              string 'email'
182      *              bool 'email_confirmed' pass true to mark email as pre-confirmed
183      *              string 'fullname'
184      *              string 'homepage'
185      *              string 'location' informal string description of geolocation
186      *              float 'lat' decimal latitude for geolocation
187      *              float 'lon' decimal longitude for geolocation
188      *              int 'location_id' geoname identifier
189      *              int 'location_ns' geoname namespace to interpret location_id
190      *              string 'nickname' REQUIRED
191      *              string 'password' (may be missing for eg OpenID registrations)
192      *              string 'code' invite code
193      *              ?string 'uri' permalink to notice; defaults to local notice URL
194      * @return mixed User object or false on failure
195      */
196     static function register($fields) {
197
198         // MAGICALLY put fields into current scope
199
200         extract($fields);
201
202         $profile = new Profile();
203
204         if(!empty($email))
205         {
206             $email = common_canonical_email($email);
207         }
208
209         $nickname = common_canonical_nickname($nickname);
210         $profile->nickname = $nickname;
211         if(! User::allowed_nickname($nickname)){
212             common_log(LOG_WARNING, sprintf("Attempted to register a nickname that is not allowed: %s", $profile->nickname),
213                        __FILE__);
214         }
215         $profile->profileurl = common_profile_url($nickname);
216
217         if (!empty($fullname)) {
218             $profile->fullname = $fullname;
219         }
220         if (!empty($homepage)) {
221             $profile->homepage = $homepage;
222         }
223         if (!empty($bio)) {
224             $profile->bio = $bio;
225         }
226         if (!empty($location)) {
227             $profile->location = $location;
228
229             $loc = Location::fromName($location);
230
231             if (!empty($loc)) {
232                 $profile->lat         = $loc->lat;
233                 $profile->lon         = $loc->lon;
234                 $profile->location_id = $loc->location_id;
235                 $profile->location_ns = $loc->location_ns;
236             }
237         }
238
239         $profile->created = common_sql_now();
240
241         $user = new User();
242
243         $user->nickname = $nickname;
244
245         // Users who respond to invite email have proven their ownership of that address
246
247         if (!empty($code)) {
248             $invite = Invitation::staticGet($code);
249             if ($invite && $invite->address && $invite->address_type == 'email' && $invite->address == $email) {
250                 $user->email = $invite->address;
251             }
252         }
253
254         if(isset($email_confirmed) && $email_confirmed) {
255             $user->email = $email;
256         }
257
258         // This flag is ignored but still set to 1
259
260         $user->inboxed = 1;
261
262         $user->created = common_sql_now();
263
264         if (Event::handle('StartUserRegister', array(&$user, &$profile))) {
265
266             $profile->query('BEGIN');
267
268             $id = $profile->insert();
269
270             if (empty($id)) {
271                 common_log_db_error($profile, 'INSERT', __FILE__);
272                 return false;
273             }
274
275             $user->id = $id;
276             $user->uri = common_user_uri($user);
277             if (!empty($password)) { // may not have a password for OpenID users
278                 $user->password = common_munge_password($password, $id);
279             }
280
281             $result = $user->insert();
282
283             if (!$result) {
284                 common_log_db_error($user, 'INSERT', __FILE__);
285                 return false;
286             }
287
288             // Everyone gets an inbox
289
290             $inbox = new Inbox();
291
292             $inbox->user_id = $user->id;
293             $inbox->notice_ids = '';
294
295             $result = $inbox->insert();
296
297             if (!$result) {
298                 common_log_db_error($inbox, 'INSERT', __FILE__);
299                 return false;
300             }
301
302             // Everyone is subscribed to themself
303
304             $subscription = new Subscription();
305             $subscription->subscriber = $user->id;
306             $subscription->subscribed = $user->id;
307             $subscription->created = $user->created;
308
309             $result = $subscription->insert();
310
311             if (!$result) {
312                 common_log_db_error($subscription, 'INSERT', __FILE__);
313                 return false;
314             }
315
316             if (!empty($email) && !$user->email) {
317
318                 $confirm = new Confirm_address();
319                 $confirm->code = common_confirmation_code(128);
320                 $confirm->user_id = $user->id;
321                 $confirm->address = $email;
322                 $confirm->address_type = 'email';
323
324                 $result = $confirm->insert();
325
326                 if (!$result) {
327                     common_log_db_error($confirm, 'INSERT', __FILE__);
328                     return false;
329                 }
330             }
331
332             if (!empty($code) && $user->email) {
333                 $user->emailChanged();
334             }
335
336             // Default system subscription
337
338             $defnick = common_config('newuser', 'default');
339
340             if (!empty($defnick)) {
341                 $defuser = User::staticGet('nickname', $defnick);
342                 if (empty($defuser)) {
343                     common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
344                                __FILE__);
345                 } else {
346                     $defsub = new Subscription();
347                     $defsub->subscriber = $user->id;
348                     $defsub->subscribed = $defuser->id;
349                     $defsub->created = $user->created;
350
351                     $result = $defsub->insert();
352
353                     if (!$result) {
354                         common_log_db_error($defsub, 'INSERT', __FILE__);
355                         return false;
356                     }
357                 }
358             }
359
360             $profile->query('COMMIT');
361
362             if (!empty($email) && !$user->email) {
363                 mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
364             }
365
366             // Welcome message
367
368             $welcome = common_config('newuser', 'welcome');
369
370             if (!empty($welcome)) {
371                 $welcomeuser = User::staticGet('nickname', $welcome);
372                 if (empty($welcomeuser)) {
373                     common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick),
374                                __FILE__);
375                 } else {
376                     $notice = Notice::saveNew($welcomeuser->id,
377                                               sprintf(_('Welcome to %1$s, @%2$s!'),
378                                                       common_config('site', 'name'),
379                                                       $user->nickname),
380                                               'system');
381
382                 }
383             }
384
385             Event::handle('EndUserRegister', array(&$profile, &$user));
386         }
387
388         return $user;
389     }
390
391     // Things we do when the email changes
392
393     function emailChanged()
394     {
395
396         $invites = new Invitation();
397         $invites->address = $this->email;
398         $invites->address_type = 'email';
399
400         if ($invites->find()) {
401             while ($invites->fetch()) {
402                 $other = User::staticGet($invites->user_id);
403                 subs_subscribe_to($other, $this);
404             }
405         }
406     }
407
408     function hasFave($notice)
409     {
410         $cache = common_memcache();
411
412         // XXX: Kind of a hack.
413
414         if ($cache) {
415             // This is the stream of favorite notices, in rev chron
416             // order. This forces it into cache.
417
418             $ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
419
420             // If it's in the list, then it's a fave
421
422             if (in_array($notice->id, $ids)) {
423                 return true;
424             }
425
426             // If we're not past the end of the cache window,
427             // then the cache has all available faves, so this one
428             // is not a fave.
429
430             if (count($ids) < NOTICE_CACHE_WINDOW) {
431                 return false;
432             }
433
434             // Otherwise, cache doesn't have all faves;
435             // fall through to the default
436         }
437
438         $fave = Fave::pkeyGet(array('user_id' => $this->id,
439                                     'notice_id' => $notice->id));
440         return ((is_null($fave)) ? false : true);
441     }
442
443     function mutuallySubscribed($other)
444     {
445         return $this->isSubscribed($other) &&
446           $other->isSubscribed($this);
447     }
448
449     function mutuallySubscribedUsers()
450     {
451         // 3-way join; probably should get cached
452         $UT = common_config('db','type')=='pgsql'?'"user"':'user';
453         $qry = "SELECT $UT.* " .
454           "FROM subscription sub1 JOIN $UT ON sub1.subscribed = $UT.id " .
455           "JOIN subscription sub2 ON $UT.id = sub2.subscriber " .
456           'WHERE sub1.subscriber = %d and sub2.subscribed = %d ' .
457           "ORDER BY $UT.nickname";
458         $user = new User();
459         $user->query(sprintf($qry, $this->id, $this->id));
460
461         return $user;
462     }
463
464     function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
465     {
466         $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id);
467         return Notice::getStreamByIds($ids);
468     }
469
470     function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) {
471         $profile = $this->getProfile();
472         if (!$profile) {
473             return null;
474         } else {
475             return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id);
476         }
477     }
478
479     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
480     {
481         $profile = $this->getProfile();
482         if (!$profile) {
483             return null;
484         } else {
485             return $profile->getNotices($offset, $limit, $since_id, $before_id);
486         }
487     }
488
489     function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE, $own=false)
490     {
491         $ids = Fave::stream($this->id, $offset, $limit, $own);
492         return Notice::getStreamByIds($ids);
493     }
494
495     function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
496     {
497         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, false);
498     }
499
500     function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
501     {
502         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, true);
503     }
504
505     function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
506     {
507         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, false);
508     }
509
510     function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0)
511     {
512         return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, true);
513     }
514
515     function blowFavesCache()
516     {
517         $cache = common_memcache();
518         if ($cache) {
519             // Faves don't happen chronologically, so we need to blow
520             // ;last cache, too
521             $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
522             $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
523             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id));
524             $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id.';last'));
525         }
526         $profile = $this->getProfile();
527         $profile->blowFaveCount();
528     }
529
530     function getSelfTags()
531     {
532         return Profile_tag::getTags($this->id, $this->id);
533     }
534
535     function setSelfTags($newtags)
536     {
537         return Profile_tag::setTags($this->id, $this->id, $newtags);
538     }
539
540     function block($other)
541     {
542         // Add a new block record
543
544         // no blocking (and thus unsubbing from) yourself
545
546         if ($this->id == $other->id) {
547             common_log(LOG_WARNING,
548                 sprintf(
549                     "Profile ID %d (%s) tried to block his or herself.",
550                     $profile->id,
551                     $profile->nickname
552                 )
553             );
554             return false;
555         }
556
557         $block = new Profile_block();
558
559         // Begin a transaction
560
561         $block->query('BEGIN');
562
563         $block->blocker = $this->id;
564         $block->blocked = $other->id;
565
566         $result = $block->insert();
567
568         if (!$result) {
569             common_log_db_error($block, 'INSERT', __FILE__);
570             return false;
571         }
572
573         // Cancel their subscription, if it exists
574
575         $otherUser = User::staticGet('id', $other->id);
576
577         if (!empty($otherUser)) {
578             subs_unsubscribe_to($otherUser, $this->getProfile());
579         }
580
581         $block->query('COMMIT');
582
583         return true;
584     }
585
586     function unblock($other)
587     {
588         // Get the block record
589
590         $block = Profile_block::get($this->id, $other->id);
591
592         if (!$block) {
593             return false;
594         }
595
596         $result = $block->delete();
597
598         if (!$result) {
599             common_log_db_error($block, 'DELETE', __FILE__);
600             return false;
601         }
602
603         return true;
604     }
605
606     function isMember($group)
607     {
608         $profile = $this->getProfile();
609         return $profile->isMember($group);
610     }
611
612     function isAdmin($group)
613     {
614         $profile = $this->getProfile();
615         return $profile->isAdmin($group);
616     }
617
618     function getGroups($offset=0, $limit=null)
619     {
620         $profile = $this->getProfile();
621         return $profile->getGroups($offset, $limit);
622     }
623
624     function getSubscriptions($offset=0, $limit=null)
625     {
626         $profile = $this->getProfile();
627         assert(!empty($profile));
628         return $profile->getSubscriptions($offset, $limit);
629     }
630
631     function getSubscribers($offset=0, $limit=null)
632     {
633         $profile = $this->getProfile();
634         assert(!empty($profile));
635         return $profile->getSubscribers($offset, $limit);
636     }
637
638     function getTaggedSubscribers($tag, $offset=0, $limit=null)
639     {
640         $qry =
641           'SELECT profile.* ' .
642           'FROM profile JOIN subscription ' .
643           'ON profile.id = subscription.subscriber ' .
644           'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
645           'AND profile_tag.tagger = subscription.subscribed) ' .
646           'WHERE subscription.subscribed = %d ' .
647           "AND profile_tag.tag = '%s' " .
648           'AND subscription.subscribed != subscription.subscriber ' .
649           'ORDER BY subscription.created DESC ';
650
651         if ($offset) {
652             $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
653         }
654
655         $profile = new Profile();
656
657         $cnt = $profile->query(sprintf($qry, $this->id, $tag));
658
659         return $profile;
660     }
661
662     function getTaggedSubscriptions($tag, $offset=0, $limit=null)
663     {
664         $qry =
665           'SELECT profile.* ' .
666           'FROM profile JOIN subscription ' .
667           'ON profile.id = subscription.subscribed ' .
668           'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
669           'AND profile_tag.tagger = subscription.subscriber) ' .
670           'WHERE subscription.subscriber = %d ' .
671           "AND profile_tag.tag = '%s' " .
672           'AND subscription.subscribed != subscription.subscriber ' .
673           'ORDER BY subscription.created DESC ';
674
675         $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
676
677         $profile = new Profile();
678
679         $profile->query(sprintf($qry, $this->id, $tag));
680
681         return $profile;
682     }
683
684     function getDesign()
685     {
686         return Design::staticGet('id', $this->design_id);
687     }
688
689     function hasRight($right)
690     {
691         $profile = $this->getProfile();
692         return $profile->hasRight($right);
693     }
694
695     function delete()
696     {
697         $profile = $this->getProfile();
698         if ($profile) {
699             $profile->delete();
700         }
701
702         $related = array('Fave',
703                          'Confirm_address',
704                          'Remember_me',
705                          'Foreign_link',
706                          'Invitation',
707                          );
708         Event::handle('UserDeleteRelated', array($this, &$related));
709
710         foreach ($related as $cls) {
711             $inst = new $cls();
712             $inst->user_id = $this->id;
713             $inst->delete();
714         }
715
716         $this->_deleteTags();
717         $this->_deleteBlocks();
718
719         parent::delete();
720     }
721
722     function _deleteTags()
723     {
724         $tag = new Profile_tag();
725         $tag->tagger = $this->id;
726         $tag->delete();
727     }
728
729     function _deleteBlocks()
730     {
731         $block = new Profile_block();
732         $block->blocker = $this->id;
733         $block->delete();
734         // XXX delete group block? Reset blocker?
735     }
736
737     function hasRole($name)
738     {
739         $profile = $this->getProfile();
740         return $profile->hasRole($name);
741     }
742
743     function grantRole($name)
744     {
745         $profile = $this->getProfile();
746         return $profile->grantRole($name);
747     }
748
749     function revokeRole($name)
750     {
751         $profile = $this->getProfile();
752         return $profile->revokeRole($name);
753     }
754
755     function isSandboxed()
756     {
757         $profile = $this->getProfile();
758         return $profile->isSandboxed();
759     }
760
761     function isSilenced()
762     {
763         $profile = $this->getProfile();
764         return $profile->isSilenced();
765     }
766
767     function repeatedByMe($offset=0, $limit=20, $since_id=null, $max_id=null)
768     {
769         $ids = Notice::stream(array($this, '_repeatedByMeDirect'),
770                               array(),
771                               'user:repeated_by_me:'.$this->id,
772                               $offset, $limit, $since_id, $max_id, null);
773
774         return Notice::getStreamByIds($ids);
775     }
776
777     function _repeatedByMeDirect($offset, $limit, $since_id, $max_id)
778     {
779         $notice = new Notice();
780
781         $notice->selectAdd(); // clears it
782         $notice->selectAdd('id');
783
784         $notice->profile_id = $this->id;
785         $notice->whereAdd('repeat_of IS NOT NULL');
786
787         $notice->orderBy('id DESC');
788
789         if (!is_null($offset)) {
790             $notice->limit($offset, $limit);
791         }
792
793         if ($since_id != 0) {
794             $notice->whereAdd('id > ' . $since_id);
795         }
796
797         if ($max_id != 0) {
798             $notice->whereAdd('id <= ' . $max_id);
799         }
800
801         $ids = array();
802
803         if ($notice->find()) {
804             while ($notice->fetch()) {
805                 $ids[] = $notice->id;
806             }
807         }
808
809         $notice->free();
810         $notice = NULL;
811
812         return $ids;
813     }
814
815     function repeatsOfMe($offset=0, $limit=20, $since_id=null, $max_id=null)
816     {
817         $ids = Notice::stream(array($this, '_repeatsOfMeDirect'),
818                               array(),
819                               'user:repeats_of_me:'.$this->id,
820                               $offset, $limit, $since_id, $max_id);
821
822         return Notice::getStreamByIds($ids);
823     }
824
825     function _repeatsOfMeDirect($offset, $limit, $since_id, $max_id)
826     {
827         $qry =
828           'SELECT DISTINCT original.id AS id ' .
829           'FROM notice original JOIN notice rept ON original.id = rept.repeat_of ' .
830           'WHERE original.profile_id = ' . $this->id . ' ';
831
832         if ($since_id != 0) {
833             $qry .= 'AND original.id > ' . $since_id . ' ';
834         }
835
836         if ($max_id != 0) {
837             $qry .= 'AND original.id <= ' . $max_id . ' ';
838         }
839
840         // NOTE: we sort by fave time, not by notice time!
841
842         $qry .= 'ORDER BY original.id DESC ';
843
844         if (!is_null($offset)) {
845             $qry .= "LIMIT $limit OFFSET $offset";
846         }
847
848         $ids = array();
849
850         $notice = new Notice();
851
852         $notice->query($qry);
853
854         while ($notice->fetch()) {
855             $ids[] = $notice->id;
856         }
857
858         $notice->free();
859         $notice = NULL;
860
861         return $ids;
862     }
863
864     function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
865     {
866         throw new Exception("Not implemented since inbox change.");
867     }
868
869     function shareLocation()
870     {
871         $cfg = common_config('location', 'share');
872
873         if ($cfg == 'always') {
874             return true;
875         } else if ($cfg == 'never') {
876             return false;
877         } else { // user
878             $share = true;
879
880             $prefs = User_location_prefs::staticGet('user_id', $this->id);
881
882             if (empty($prefs)) {
883                 $share = common_config('location', 'sharedefault');
884             } else {
885                 $share = $prefs->share_location;
886                 $prefs->free();
887             }
888
889             return $share;
890         }
891     }
892
893     static function siteOwner()
894     {
895         $owner = self::cacheGet('user:site_owner');
896
897         if ($owner === false) { // cache miss
898
899             $pr = new Profile_role();
900
901             $pr->role = Profile_role::OWNER;
902
903             $pr->orderBy('created');
904
905             $pr->limit(1);
906
907             if ($pr->find(true)) {
908                 $owner = User::staticGet('id', $pr->profile_id);
909             } else {
910                 $owner = null;
911             }
912
913             self::cacheSet('user:site_owner', $owner);
914         }
915
916         return $owner;
917     }
918 }