]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
More Favorite pluginification (favecount, cache, menus(favecount, cache, menus))
[quix0rs-gnu-social.git] / classes / Notice.php
index c6ae3c915517794607d102cba29d1a73c5d98052..4c831c7cc75645ccfdead4b2f1fc6cd5d8324e6d 100644 (file)
@@ -143,6 +143,10 @@ class Notice extends Managed_DataObject
 
     protected $_profile = array();
     
+    /**
+     * Will always return a profile, if anything fails it will
+     * (through _setProfile) throw a NoProfileException.
+     */
     public function getProfile()
     {
         if (!isset($this->_profile[$this->profile_id])) {
@@ -190,7 +194,6 @@ class Notice extends Managed_DataObject
 
             $this->clearReplies();
             $this->clearRepeats();
-            $this->clearFaves();
             $this->clearTags();
             $this->clearGroupInboxes();
             $this->clearFiles();
@@ -228,6 +231,19 @@ class Notice extends Managed_DataObject
         return common_local_url('shownotice', array('notice' => $this->id), null, null, false);
     }
 
+    public function getTitle()
+    {
+        $title = null;
+        if (Event::handle('GetNoticeTitle', array($this, &$title))) {
+            // TRANS: Title of a notice posted without a title value.
+            // TRANS: %1$s is a user name, %2$s is the notice creation date/time.
+            $title = sprintf(_('%1$s\'s status on %2$s'),
+                             $this->getProfile()->getFancyName(),
+                             common_exact_date($this->created));
+        }
+        return $title;
+    }
+
     /*
      * Get the original representation URL of this notice.
      */
@@ -530,7 +546,13 @@ class Notice extends Managed_DataObject
                                                       $profile->nickname, $reply->id), 403);
                 }
 
-                $notice->reply_to     = $reply->id;
+                // If it's a repeat, the reply_to should be to the original
+                if (!empty($reply->repeat_of)) {
+                    $notice->reply_to = $reply->repeat_of;
+                } else {
+                    $notice->reply_to = $reply->id;
+                }
+                // But the conversation ought to be the same :)
                 $notice->conversation = $reply->conversation;
 
                 // If the original is private to a group, and notice has
@@ -898,12 +920,11 @@ class Notice extends Managed_DataObject
         return true;
     }
 
-       protected $_attachments = -1;
+       protected $_attachments = array();
        
     function attachments() {
-
-               if ($this->_attachments != -1)  {
-            return $this->_attachments;
+               if (isset($this->_attachments[$this->id])) {
+            return $this->_attachments[$this->id];
         }
                
         $f2ps = File_to_post::listGet('post_id', array($this->id));
@@ -916,14 +937,14 @@ class Notice extends Managed_DataObject
                
                $files = File::multiGet('id', $ids);
 
-               $this->_attachments = $files->fetchAll();
+               $this->_attachments[$this->id] = $files->fetchAll();
                
-        return $this->_attachments;
+        return $this->_attachments[$this->id];
     }
 
        function _setAttachments($attachments)
        {
-           $this->_attachments = $attachments;
+           $this->_attachments[$this->id] = $attachments;
        }
 
     function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0)
@@ -1311,19 +1332,17 @@ class Notice extends Managed_DataObject
             return array();
         }
 
-        $sender = Profile::getKV($this->profile_id);
+        $sender = $this->getProfile();
 
         $replied = array();
 
         // If it's a reply, save for the replied-to author
         try {
             $parent = $this->getParent();
-            $author = $parent->getProfile();
-            if ($author instanceof Profile) {
-                $this->saveReply($author->id);
-                $replied[$author->id] = 1;
-                self::blow('reply:stream:%d', $author->id);
-            }
+            $parentauthor = $parent->getProfile();
+            $this->saveReply($parentauthor->id);
+            $replied[$parentauthor->id] = 1;
+            self::blow('reply:stream:%d', $parentauthor->id);
         } catch (Exception $e) {
             // Not a reply, since it has no parent!
         }
@@ -1377,7 +1396,7 @@ class Notice extends Managed_DataObject
         return $reply;
     }
 
-    protected $_replies = -1;
+    protected $_replies = array();
 
     /**
      * Pull the complete list of @-reply targets for this notice.
@@ -1386,8 +1405,8 @@ class Notice extends Managed_DataObject
      */
     function getReplies()
     {
-        if ($this->_replies != -1) {
-            return $this->_replies;
+        if (isset($this->_replies[$this->id])) {
+            return $this->_replies[$this->id];
         }
 
         $replyMap = Reply::listGet('notice_id', array($this->id));
@@ -1398,14 +1417,14 @@ class Notice extends Managed_DataObject
             $ids[] = $reply->profile_id;
         }
 
-        $this->_replies = $ids;
+        $this->_replies[$this->id] = $ids;
 
         return $ids;
     }
 
     function _setReplies($replies)
     {
-        $this->_replies = $replies;
+        $this->_replies[$this->id] = $replies;
     }
 
     /**
@@ -1453,7 +1472,7 @@ class Notice extends Managed_DataObject
      * @return array of Group objects
      */
     
-    protected $_groups = -1;
+    protected $_groups = array();
     
     function getGroups()
     {
@@ -1463,9 +1482,8 @@ class Notice extends Managed_DataObject
             return array();
         }
         
-        if ($this->_groups != -1)
-        {
-            return $this->_groups;
+        if (isset($this->_groups[$this->id])) {
+            return $this->_groups[$this->id];
         }
         
         $gis = Group_inbox::listGet('notice_id', array($this->id));
@@ -1479,14 +1497,14 @@ class Notice extends Managed_DataObject
                
                $groups = User_group::multiGet('id', $ids);
                
-               $this->_groups = $groups->fetchAll();
+               $this->_groups[$this->id] = $groups->fetchAll();
                
-               return $this->_groups;
+               return $this->_groups[$this->id];
     }
     
     function _setGroups($groups)
     {
-        $this->_groups = $groups;
+        $this->_groups[$this->id] = $groups;
     }
 
     /**
@@ -1711,16 +1729,18 @@ class Notice extends Managed_DataObject
 
         // favorite and repeated
 
+        $scoped = null;
         if (!empty($cur)) {
-            $cp = $cur->getProfile();
-            $noticeInfoAttr['favorite'] = ($cp->hasFave($this)) ? "true" : "false";
-            $noticeInfoAttr['repeated'] = ($cp->hasRepeated($this)) ? "true" : "false";
+            $scoped = $cur->getProfile();
+            $noticeInfoAttr['repeated'] = ($scoped->hasRepeated($this)) ? "true" : "false";
         }
 
         if (!empty($this->repeat_of)) {
             $noticeInfoAttr['repeat_of'] = $this->repeat_of;
         }
 
+        Event::handle('StatusNetApiNoticeInfo', array($this, &$noticeInfoAttr, $scoped));
+
         return array('statusnet:notice_info', $noticeInfoAttr, null);
     }
 
@@ -2002,24 +2022,6 @@ class Notice extends Managed_DataObject
         }
     }
 
-    function clearFaves()
-    {
-        $fave = new Fave();
-        $fave->notice_id = $this->id;
-
-        if ($fave->find()) {
-            while ($fave->fetch()) {
-                self::blow('fave:ids_by_user_own:%d', $fave->user_id);
-                self::blow('fave:ids_by_user_own:%d;last', $fave->user_id);
-                self::blow('fave:ids_by_user:%d', $fave->user_id);
-                self::blow('fave:ids_by_user:%d;last', $fave->user_id);
-                $fave->delete();
-            }
-        }
-
-        $fave->free();
-    }
-
     function clearTags()
     {
         $tag = new Notice_tag();
@@ -2551,14 +2553,13 @@ class Notice extends Managed_DataObject
                }
        }
 
-    static function _idsOf(&$notices)
+    static function _idsOf(array &$notices)
     {
                $ids = array();
                foreach ($notices as $notice) {
-                       $ids[] = $notice->id;
+                       $ids[$notice->id] = true;
                }
-               $ids = array_unique($ids);
-        return $ids;
+               return array_keys($ids);
     }
 
     static function fillAttachments(&$notices)
@@ -2590,47 +2591,6 @@ class Notice extends Managed_DataObject
                }
     }
 
-    protected $_faves;
-
-    /**
-     * All faves of this notice
-     *
-     * @return array Array of Fave objects
-     */
-
-    function getFaves()
-    {
-        if (isset($this->_faves) && is_array($this->_faves)) {
-            return $this->_faves;
-        }
-        $faveMap = Fave::listGet('notice_id', array($this->id));
-        $this->_faves = $faveMap[$this->id];
-        return $this->_faves;
-    }
-
-    function _setFaves($faves)
-    {
-        $this->_faves = $faves;
-    }
-
-    static function fillFaves(&$notices)
-    {
-        $ids = self::_idsOf($notices);
-        $faveMap = Fave::listGet('notice_id', $ids);
-        $cnt = 0;
-        $faved = array();
-        foreach ($faveMap as $id => $faves) {
-            $cnt += count($faves);
-            if (count($faves) > 0) {
-                $faved[] = $id;
-            }
-        }
-        foreach ($notices as $notice) {
-               $faves = $faveMap[$notice->id];
-            $notice->_setFaves($faves);
-        }
-    }
-
     static function fillReplies(&$notices)
     {
         $ids = self::_idsOf($notices);
@@ -2645,21 +2605,21 @@ class Notice extends Managed_DataObject
         }
     }
 
-    protected $_repeats;
+    protected $_repeats = array();
 
     function getRepeats()
     {
-        if (isset($this->_repeats) && is_array($this->_repeats)) {
-            return $this->_repeats;
+        if (isset($this->_repeats[$this->id])) {
+            return $this->_repeats[$this->id];
         }
         $repeatMap = Notice::listGet('repeat_of', array($this->id));
-        $this->_repeats = $repeatMap[$this->id];
-        return $this->_repeats;
+        $this->_repeats[$this->id] = $repeatMap[$this->id];
+        return $this->_repeats[$this->id];
     }
 
     function _setRepeats($repeats)
     {
-        $this->_repeats = $repeats;
+        $this->_repeats[$this->id] = $repeats;
     }
 
     static function fillRepeats(&$notices)