]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Get conversation root visible to this user in threadednoticelist
authorEvan Prodromou <evan@status.net>
Mon, 11 Apr 2011 15:19:11 +0000 (11:19 -0400)
committerEvan Prodromou <evan@status.net>
Mon, 11 Apr 2011 15:19:11 +0000 (11:19 -0400)
classes/Notice.php
lib/threadednoticelist.php

index de45bdaae180b017f90359031cdb3948a7035e82..70036fa8fbaeb1cfb80cc3516a8776873fb576e4 100644 (file)
@@ -802,30 +802,65 @@ class Notice extends Memcached_DataObject
      *
      * @return Notice or null
      */
-    function conversationRoot()
+    function conversationRoot($profile=-1)
     {
-        if (!empty($this->conversation)) {
-            $c = self::memcache();
+        // XXX: can this happen?
 
-            $key = Cache::key('notice:conversation_root:' . $this->conversation);
-            $notice = $c->get($key);
-            if ($notice) {
-                return $notice;
-            }
+        if (empty($this->conversation)) {
+            return null;
+        }
 
-            $notice = new Notice();
-            $notice->conversation = $this->conversation;
-            $notice->orderBy('CREATED');
-            $notice->limit(1);
-            $notice->find(true);
+        // Get the current profile if not specified
 
-            if ($notice->N) {
-                $c->set($key, $notice);
-                return $notice;
-            }
+        if (is_int($profile) && $profile == -1) {
+            $profile = Profile::current();
         }
-        return null;
+
+        // If this notice is out of scope, no root for you!
+
+        if (!$this->inScope($profile)) {
+            return null;
+        }
+
+        // If this isn't a reply to anything, then it's its own
+        // root.
+
+        if (empty($this->reply_to)) {
+            return $this;
+        }
+        
+        if (is_null($profile)) {
+            $keypart = sprintf('notice:conversation_root:%d:null', $this->id);
+        } else {
+            $keypart = sprintf('notice:conversation_root:%d:%d',
+                               $this->id,
+                               $profile->id);
+        }
+            
+        $root = self::cacheGet($keypart);
+
+        if ($root !== false && $root->inScope($profile)) {
+            return $root;
+        } else {
+            $last = $this;
+
+            do {
+                $parent = $last->getOriginal();
+                if (!empty($parent) && $parent->inScope($profile)) {
+                    $last = $parent;
+                    continue;
+                } else {
+                    $root = $last;
+                    break;
+                }
+            } while (!empty($parent));
+
+            self::cacheSet($keypart, $root);
+        }
+
+        return $root;
     }
+
     /**
      * Pull up a full list of local recipients who will be getting
      * this notice in their inbox. Results will be cached, so don't
@@ -2420,4 +2455,18 @@ class Notice extends Memcached_DataObject
 
         return $groups;
     }
+
+    protected $_original = -1;
+
+    function getOriginal()
+    {
+        if (is_int($this->_original) && $this->_original == -1) {
+            if (empty($this->reply_to)) {
+                $this->_original = null;
+            } else {
+                $this->_original = Notice::staticGet('id', $this->reply_to);
+            }
+        }
+        return $this->_original;
+    }
 }
index 372791c3e5492da532796cf75b3221eb81de10bd..3725ff0535c214a015aff6647c494431cd2cbbf9 100644 (file)
@@ -52,9 +52,12 @@ class ThreadedNoticeList extends NoticeList
 {
     protected $userProfile;
 
-    function __construct($notice, $out=null, $profile=null)
+    function __construct($notice, $out=null, $profile=-1)
     {
         parent::__construct($notice, $out);
+        if (is_int($profile) && $profile == -1) {
+            $profile = Profile::current();
+        }
         $this->userProfile = $profile;
     }
 
@@ -98,7 +101,7 @@ class ThreadedNoticeList extends NoticeList
             $conversations[$convo] = true;
 
             // Get the convo's root notice
-            $root = $notice->conversationRoot();
+            $root = $notice->conversationRoot($this->userProfile);
             if ($root) {
                 $notice = $root;
             }