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