]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/User.php
- Fix cache handling in TwitterStatusFetcher
[quix0rs-gnu-social.git] / classes / User.php
index 34151778c5c3274b89cf6f6562334e11c29249a7..0ab816b57ee301f730f4c31f18a53e9572c14fe5 100644 (file)
@@ -209,8 +209,6 @@ class User extends Memcached_DataObject
 
         $profile = new Profile();
 
-        $profile->query('BEGIN');
-
         if(!empty($email))
         {
             $email = common_canonical_email($email);
@@ -220,7 +218,7 @@ class User extends Memcached_DataObject
         $profile->nickname = $nickname;
         if(! User::allowed_nickname($nickname)){
             common_log(LOG_WARNING, sprintf("Attempted to register a nickname that is not allowed: %s", $profile->nickname),
-                           __FILE__);
+                       __FILE__);
         }
         $profile->profileurl = common_profile_url($nickname);
 
@@ -248,22 +246,10 @@ class User extends Memcached_DataObject
 
         $profile->created = common_sql_now();
 
-        $id = $profile->insert();
-
-        if (empty($id)) {
-            common_log_db_error($profile, 'INSERT', __FILE__);
-            return false;
-        }
-
         $user = new User();
 
-        $user->id = $id;
         $user->nickname = $nickname;
 
-        if (!empty($password)) { // may not have a password for OpenID users
-            $user->password = common_munge_password($password, $id);
-        }
-
         // Users who respond to invite email have proven their ownership of that address
 
         if (!empty($code)) {
@@ -282,95 +268,129 @@ class User extends Memcached_DataObject
         $user->inboxed = 1;
 
         $user->created = common_sql_now();
-        $user->uri = common_user_uri($user);
 
-        $result = $user->insert();
+        if (Event::handle('StartUserRegister', array(&$user, &$profile))) {
 
-        if (!$result) {
-            common_log_db_error($user, 'INSERT', __FILE__);
-            return false;
-        }
+            $profile->query('BEGIN');
 
-        // Everyone is subscribed to themself
+            $id = $profile->insert();
 
-        $subscription = new Subscription();
-        $subscription->subscriber = $user->id;
-        $subscription->subscribed = $user->id;
-        $subscription->created = $user->created;
+            if (empty($id)) {
+                common_log_db_error($profile, 'INSERT', __FILE__);
+                return false;
+            }
 
-        $result = $subscription->insert();
+            $user->id = $id;
+            $user->uri = common_user_uri($user);
+            if (!empty($password)) { // may not have a password for OpenID users
+                $user->password = common_munge_password($password, $id);
+            }
 
-        if (!$result) {
-            common_log_db_error($subscription, 'INSERT', __FILE__);
-            return false;
-        }
+            $result = $user->insert();
+
+            if (!$result) {
+                common_log_db_error($user, 'INSERT', __FILE__);
+                return false;
+            }
+
+            // Everyone gets an inbox
 
-        if (!empty($email) && !$user->email) {
+            $inbox = new Inbox();
 
-            $confirm = new Confirm_address();
-            $confirm->code = common_confirmation_code(128);
-            $confirm->user_id = $user->id;
-            $confirm->address = $email;
-            $confirm->address_type = 'email';
+            $inbox->user_id = $user->id;
+            $inbox->notice_ids = '';
+
+            $result = $inbox->insert();
 
-            $result = $confirm->insert();
             if (!$result) {
-                common_log_db_error($confirm, 'INSERT', __FILE__);
+                common_log_db_error($inbox, 'INSERT', __FILE__);
                 return false;
             }
-        }
 
-        if (!empty($code) && $user->email) {
-            $user->emailChanged();
-        }
+            // Everyone is subscribed to themself
 
-        // Default system subscription
+            $subscription = new Subscription();
+            $subscription->subscriber = $user->id;
+            $subscription->subscribed = $user->id;
+            $subscription->created = $user->created;
 
-        $defnick = common_config('newuser', 'default');
+            $result = $subscription->insert();
 
-        if (!empty($defnick)) {
-            $defuser = User::staticGet('nickname', $defnick);
-            if (empty($defuser)) {
-                common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
-                           __FILE__);
-            } else {
-                $defsub = new Subscription();
-                $defsub->subscriber = $user->id;
-                $defsub->subscribed = $defuser->id;
-                $defsub->created = $user->created;
+            if (!$result) {
+                common_log_db_error($subscription, 'INSERT', __FILE__);
+                return false;
+            }
+
+            if (!empty($email) && !$user->email) {
+
+                $confirm = new Confirm_address();
+                $confirm->code = common_confirmation_code(128);
+                $confirm->user_id = $user->id;
+                $confirm->address = $email;
+                $confirm->address_type = 'email';
 
-                $result = $defsub->insert();
+                $result = $confirm->insert();
 
                 if (!$result) {
-                    common_log_db_error($defsub, 'INSERT', __FILE__);
+                    common_log_db_error($confirm, 'INSERT', __FILE__);
                     return false;
                 }
             }
-        }
 
-        $profile->query('COMMIT');
+            if (!empty($code) && $user->email) {
+                $user->emailChanged();
+            }
 
-        if (!empty($email) && !$user->email) {
-            mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
-        }
+            // Default system subscription
+
+            $defnick = common_config('newuser', 'default');
 
-        // Welcome message
+            if (!empty($defnick)) {
+                $defuser = User::staticGet('nickname', $defnick);
+                if (empty($defuser)) {
+                    common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick),
+                               __FILE__);
+                } else {
+                    $defsub = new Subscription();
+                    $defsub->subscriber = $user->id;
+                    $defsub->subscribed = $defuser->id;
+                    $defsub->created = $user->created;
 
-        $welcome = common_config('newuser', 'welcome');
+                    $result = $defsub->insert();
 
-        if (!empty($welcome)) {
-            $welcomeuser = User::staticGet('nickname', $welcome);
-            if (empty($welcomeuser)) {
-                common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick),
-                           __FILE__);
-            } else {
-                $notice = Notice::saveNew($welcomeuser->id,
-                                          sprintf(_('Welcome to %1$s, @%2$s!'),
-                                                  common_config('site', 'name'),
-                                                  $user->nickname),
-                                          'system');
-                common_broadcast_notice($notice);
+                    if (!$result) {
+                        common_log_db_error($defsub, 'INSERT', __FILE__);
+                        return false;
+                    }
+                }
+            }
+
+            $profile->query('COMMIT');
+
+            if (!empty($email) && !$user->email) {
+                mail_confirm_address($user, $confirm->code, $profile->nickname, $email);
+            }
+
+            // Welcome message
+
+            $welcome = common_config('newuser', 'welcome');
+
+            if (!empty($welcome)) {
+                $welcomeuser = User::staticGet('nickname', $welcome);
+                if (empty($welcomeuser)) {
+                    common_log(LOG_WARNING, sprintf("Welcome user %s does not exist.", $defnick),
+                               __FILE__);
+                } else {
+                    $notice = Notice::saveNew($welcomeuser->id,
+                                              sprintf(_('Welcome to %1$s, @%2$s!'),
+                                                      common_config('site', 'name'),
+                                                      $user->nickname),
+                                              'system');
+
+                }
             }
+
+            Event::handle('EndUserRegister', array(&$profile, &$user));
         }
 
         return $user;
@@ -482,89 +502,30 @@ class User extends Memcached_DataObject
 
     function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
     {
-        $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, false);
-
+        $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, false);
         return Notice::getStreamByIds($ids);
     }
 
     function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
     {
-        $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, true);
-
+        $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, true);
         return Notice::getStreamByIds($ids);
     }
 
     function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
     {
-        $ids = Notice::stream(array($this, '_friendsTimelineDirect'),
-                              array(false),
-                              'user:friends_timeline:'.$this->id,
-                              $offset, $limit, $since_id, $before_id, $since);
+        $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, false);
 
         return Notice::getStreamByIds($ids);
     }
 
     function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
     {
-        $ids = Notice::stream(array($this, '_friendsTimelineDirect'),
-                              array(true),
-                              'user:friends_timeline_own:'.$this->id,
-                              $offset, $limit, $since_id, $before_id, $since);
+        $ids = Inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, true);
 
         return Notice::getStreamByIds($ids);
     }
 
-    function _friendsTimelineDirect($own, $offset, $limit, $since_id, $max_id, $since)
-    {
-        $qry =
-          'SELECT notice.id AS id ' .
-          'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
-          'WHERE notice_inbox.user_id = ' . $this->id . ' ' .
-          'AND notice.repeat_of IS NULL ';
-
-        if (!$own) {
-            // XXX: autoload notice inbox for constant
-            $inbox = new Notice_inbox();
-
-            $qry .= 'AND notice_inbox.source != ' . NOTICE_INBOX_SOURCE_GATEWAY . ' ';
-        }
-
-        if ($since_id != 0) {
-            $qry .= 'AND notice.id > ' . $since_id . ' ';
-        }
-
-        if ($max_id != 0) {
-            $qry .= 'AND notice.id <= ' . $max_id . ' ';
-        }
-
-        if (!is_null($since)) {
-            $qry .= 'AND notice.modified > \'' . date('Y-m-d H:i:s', $since) . '\' ';
-        }
-
-        // NOTE: we sort by fave time, not by notice time!
-
-        $qry .= 'ORDER BY notice_id DESC ';
-
-        if (!is_null($offset)) {
-            $qry .= "LIMIT $limit OFFSET $offset";
-        }
-
-        $ids = array();
-
-        $notice = new Notice();
-
-        $notice->query($qry);
-
-        while ($notice->fetch()) {
-            $ids[] = $notice->id;
-        }
-
-        $notice->free();
-        $notice = NULL;
-
-        return $ids;
-    }
-
     function blowFavesCache()
     {
         $cache = common_memcache();
@@ -777,7 +738,6 @@ class User extends Memcached_DataObject
                          'Remember_me',
                          'Foreign_link',
                          'Invitation',
-                         'Notice_inbox',
                          );
         Event::handle('UserDeleteRelated', array($this, &$related));
 
@@ -945,56 +905,7 @@ class User extends Memcached_DataObject
 
     function repeatedToMe($offset=0, $limit=20, $since_id=null, $max_id=null)
     {
-        $ids = Notice::stream(array($this, '_repeatedToMeDirect'),
-                              array(),
-                              'user:repeated_to_me:'.$this->id,
-                              $offset, $limit, $since_id, $max_id, null);
-
-        return Notice::getStreamByIds($ids);
-    }
-
-    function _repeatedToMeDirect($offset, $limit, $since_id, $max_id, $since)
-    {
-        $qry =
-          'SELECT notice.id AS id ' .
-          'FROM notice JOIN notice_inbox ON notice.id = notice_inbox.notice_id ' .
-          'WHERE notice_inbox.user_id = ' . $this->id . ' ' .
-          'AND notice.repeat_of IS NOT NULL ';
-
-        if ($since_id != 0) {
-            $qry .= 'AND notice.id > ' . $since_id . ' ';
-        }
-
-        if ($max_id != 0) {
-            $qry .= 'AND notice.id <= ' . $max_id . ' ';
-        }
-
-        if (!is_null($since)) {
-            $qry .= 'AND notice.modified > \'' . date('Y-m-d H:i:s', $since) . '\' ';
-        }
-
-        // NOTE: we sort by fave time, not by notice time!
-
-        $qry .= 'ORDER BY notice.id DESC ';
-
-        if (!is_null($offset)) {
-            $qry .= "LIMIT $limit OFFSET $offset";
-        }
-
-        $ids = array();
-
-        $notice = new Notice();
-
-        $notice->query($qry);
-
-        while ($notice->fetch()) {
-            $ids[] = $notice->id;
-        }
-
-        $notice->free();
-        $notice = NULL;
-
-        return $ids;
+        throw new Exception("Not implemented since inbox change.");
     }
 
     function shareLocation()
@@ -1020,4 +931,30 @@ class User extends Memcached_DataObject
             return $share;
         }
     }
+
+    static function siteOwner()
+    {
+        $owner = self::cacheGet('user:site_owner');
+
+        if ($owner === false) { // cache miss
+
+            $pr = new Profile_role();
+
+            $pr->role = Profile_role::OWNER;
+
+            $pr->orderBy('created');
+
+            $pr->limit(1);
+
+            if ($pr->find(true)) {
+                $owner = User::staticGet('id', $pr->profile_id);
+            } else {
+                $owner = null;
+            }
+
+            self::cacheSet('user:site_owner', $owner);
+        }
+
+        return $owner;
+    }
 }