]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/User.php
a distributed -> the distributed
[quix0rs-gnu-social.git] / classes / User.php
index b76e45c33011e9f80e661dc33a816798922790c3..7df93b7c5a395679b975a23d2afff5017762b756 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
- * Laconica - a distributed open-source microblogging tool
- * Copyright (C) 2008, 2009, Control Yourself, Inc.
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2008, 2009, StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
@@ -62,14 +62,13 @@ class User extends Memcached_DataObject
     public $autosubscribe;                   // tinyint(1)
     public $urlshorteningservice;            // varchar(50)   default_ur1.ca
     public $inboxed;                         // tinyint(1)
+    public $design_id;                       // int(4)
+    public $viewdesigns;                     // tinyint(1)   default_1
     public $created;                         // datetime()   not_null
     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
 
     /* Static get */
-    function staticGet($k,$v=NULL)
-    {
-        return Memcached_DataObject::staticGet('User',$k,$v);
-    }
+    function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User',$k,$v); }
 
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
@@ -349,30 +348,31 @@ class User extends Memcached_DataObject
         $cache = common_memcache();
 
         // XXX: Kind of a hack.
+
         if ($cache) {
             // This is the stream of favorite notices, in rev chron
             // order. This forces it into cache.
-            $faves = $this->favoriteNotices(0, NOTICE_CACHE_WINDOW);
-            $cnt = 0;
-            while ($faves->fetch()) {
-                if ($faves->id < $notice->id) {
-                    // If we passed it, it's not a fave
-                    return false;
-                } else if ($faves->id == $notice->id) {
-                    // If it matches a cached notice, then it's a fave
-                    return true;
-                }
-                $cnt++;
+
+            $ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
+
+            // If it's in the list, then it's a fave
+
+            if (in_array($notice->id, $ids)) {
+                return true;
             }
+
             // If we're not past the end of the cache window,
             // then the cache has all available faves, so this one
             // is not a fave.
-            if ($cnt < NOTICE_CACHE_WINDOW) {
+
+            if (count($ids) < NOTICE_CACHE_WINDOW) {
                 return false;
             }
+
             // Otherwise, cache doesn't have all faves;
             // fall through to the default
         }
+
         $fave = Fave::pkeyGet(array('user_id' => $this->id,
                                     'notice_id' => $notice->id));
         return ((is_null($fave)) ? false : true);
@@ -402,29 +402,32 @@ class User extends Memcached_DataObject
     function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
     {
         $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id, $since);
-        common_debug("Ids = " . implode(',', $ids));
         return Notice::getStreamByIds($ids);
     }
 
+    function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) {
+        $profile = $this->getProfile();
+        if (!$profile) {
+            return null;
+        } else {
+            return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id, $since);
+        }
+    }
+
     function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
     {
         $profile = $this->getProfile();
         if (!$profile) {
             return null;
         } else {
-            return $profile->getNotices($offset, $limit, $since_id, $before_id);
+            return $profile->getNotices($offset, $limit, $since_id, $before_id, $since);
         }
     }
 
-    function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE)
+    function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE, $own=false)
     {
-        $qry =
-          'SELECT notice.* ' .
-          'FROM notice JOIN fave ON notice.id = fave.notice_id ' .
-          'WHERE fave.user_id = %d ';
-        return Notice::getStream(sprintf($qry, $this->id),
-                                 'user:faves:'.$this->id,
-                                 $offset, $limit);
+        $ids = Fave::stream($this->id, $offset, $limit, $own);
+        return Notice::getStreamByIds($ids);
     }
 
     function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null)
@@ -434,6 +437,33 @@ class User extends Memcached_DataObject
         // Complicated code, depending on whether we support inboxes yet
         // XXX: make this go away when inboxes become mandatory
 
+        if ($enabled === false ||
+            ($enabled == 'transitional' && $this->inboxed == 0)) {
+            $qry =
+              'SELECT notice.* ' .
+              'FROM notice JOIN subscription ON notice.profile_id = subscription.subscribed ' .
+              'WHERE subscription.subscriber = %d ' .
+              'AND notice.is_local != ' . Notice::GATEWAY;
+            return Notice::getStream(sprintf($qry, $this->id),
+                                     'user:notices_with_friends:' . $this->id,
+                                     $offset, $limit, $since_id, $before_id,
+                                     $order, $since);
+        } else if ($enabled === true ||
+                   ($enabled == 'transitional' && $this->inboxed == 1)) {
+
+            $ids = Notice_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)
+    {
+        $enabled = common_config('inboxes', 'enabled');
+
+        // Complicated code, depending on whether we support inboxes yet
+        // XXX: make this go away when inboxes become mandatory
+
         if ($enabled === false ||
             ($enabled == 'transitional' && $this->inboxed == 0)) {
             $qry =
@@ -447,7 +477,7 @@ class User extends Memcached_DataObject
         } else if ($enabled === true ||
                    ($enabled == 'transitional' && $this->inboxed == 1)) {
 
-            $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since);
+            $ids = Notice_inbox::stream($this->id, $offset, $limit, $since_id, $before_id, $since, true);
 
             return Notice::getStreamByIds($ids);
         }
@@ -459,9 +489,13 @@ class User extends Memcached_DataObject
         if ($cache) {
             // Faves don't happen chronologically, so we need to blow
             // ;last cache, too
-            $cache->delete(common_cache_key('user:faves:'.$this->id));
-            $cache->delete(common_cache_key('user:faves:'.$this->id).';last');
+            $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
+            $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
+            $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id));
+            $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id.';last'));
         }
+        $profile = $this->getProfile();
+        $profile->blowFaveCount();
     }
 
     function getSelfTags()
@@ -570,50 +604,16 @@ class User extends Memcached_DataObject
 
     function getSubscriptions($offset=0, $limit=null)
     {
-        $qry =
-          'SELECT profile.* ' .
-          'FROM profile JOIN subscription ' .
-          'ON profile.id = subscription.subscribed ' .
-          'WHERE subscription.subscriber = %d ' .
-          'AND subscription.subscribed != subscription.subscriber ' .
-          'ORDER BY subscription.created DESC ';
-
-        if (common_config('db','type') == 'pgsql') {
-            $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
-        } else {
-            $qry .= ' LIMIT ' . $offset . ', ' . $limit;
-        }
-
-        $profile = new Profile();
-
-        $profile->query(sprintf($qry, $this->id));
-
-        return $profile;
+        $profile = $this->getProfile();
+        assert(!empty($profile));
+        return $profile->getSubscriptions($offset, $limit);
     }
 
     function getSubscribers($offset=0, $limit=null)
     {
-        $qry =
-          'SELECT profile.* ' .
-          'FROM profile JOIN subscription ' .
-          'ON profile.id = subscription.subscriber ' .
-          'WHERE subscription.subscribed = %d ' .
-          'AND subscription.subscribed != subscription.subscriber ' .
-          'ORDER BY subscription.created DESC ';
-
-        if ($offset) {
-            if (common_config('db','type') == 'pgsql') {
-                $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
-            } else {
-                $qry .= ' LIMIT ' . $offset . ', ' . $limit;
-            }
-        }
-
-        $profile = new Profile();
-
-        $cnt = $profile->query(sprintf($qry, $this->id));
-
-        return $profile;
+        $profile = $this->getProfile();
+        assert(!empty($profile));
+        return $profile->getSubscribers($offset, $limit);
     }
 
     function getTaggedSubscribers($tag, $offset=0, $limit=null)
@@ -680,4 +680,9 @@ class User extends Memcached_DataObject
 
         return ($cnt > 0);
     }
+
+    function getDesign()
+    {
+        return Design::staticGet('id', $this->design_id);
+    }
 }