From bf4c5cb41a179439d5d3d26b3cc42fd255b6418f Mon Sep 17 00:00:00 2001
From: Evan Prodromou <evan@status.net>
Date: Tue, 28 Dec 2010 11:58:55 -0800
Subject: [PATCH] Stream of notices linking to an URL

---
 classes/File.php         | 65 ++++++++++++++++++++++++++++++++++++++++
 classes/File_to_post.php | 15 ++++++++++
 2 files changed, 80 insertions(+)

diff --git a/classes/File.php b/classes/File.php
index ef9dbf14ab..d25c7529f0 100644
--- a/classes/File.php
+++ b/classes/File.php
@@ -412,4 +412,69 @@ class File extends Memcached_DataObject
     {
         return File_thumbnail::staticGet('file_id', $this->id);
     }
+
+    function blowCache($last=false)
+    {
+        self::blow('file:notice-ids:%s', $this->url);
+        if ($last) {
+            self::blow('file:notice-ids:%s;last', $this->url);
+        }
+    }
+
+    /**
+     * Stream of notices linking to this URL
+     *
+     * @param integer $offset   Offset to show; default is 0
+     * @param integer $limit    Limit of notices to show
+     * @param integer $since_id Since this notice
+     * @param integer $max_id   Before this notice
+     *
+     * @return array ids of notices that link to this file
+     */
+
+    function stream($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
+    {
+        $ids = Notice::stream(array($this, '_streamDirect'),
+                              null,
+                              'file:notice-ids:'.$this->url,
+                              $offset, $limit, $since_id, $max_id);
+        return $ids;
+    }
+
+    /**
+     * Stream of notices linking to this URL
+     *
+     * @param integer $offset   Offset to show; default is 0
+     * @param integer $limit    Limit of notices to show
+     * @param integer $since_id Since this notice
+     * @param integer $max_id   Before this notice
+     *
+     * @return array ids of notices that link to this file
+     */
+
+    function _streamDirect($offset, $limit, $since_id, $max_id)
+    {
+        $f2p = new File_to_post();
+
+        $f2p->file_id = $this->id;
+
+        Notice::addWhereSinceId($f2p, $since_id, 'post_id', 'modified');
+        Notice::addWhereMaxId($f2p, $max_id, 'post_id', 'modified');
+
+        $f2p->orderBy('modified DESC, notice_id DESC');
+
+        if (!is_null($offset)) {
+            $reply->limit($offset, $limit);
+        }
+
+        $ids = array();
+
+        if ($f2p->find()) {
+            while ($f2p->fetch()) {
+                $ids[] = $f2p->notice_id;
+            }
+        }
+
+        return $ids;
+    }
 }
diff --git a/classes/File_to_post.php b/classes/File_to_post.php
index 530921adcb..7d417d58b7 100644
--- a/classes/File_to_post.php
+++ b/classes/File_to_post.php
@@ -52,6 +52,12 @@ class File_to_post extends Memcached_DataObject
                 $f2p->file_id = $file_id;
                 $f2p->post_id = $notice_id;
                 $f2p->insert();
+                
+                $f = File::staticGet($file_id);
+
+                if (!empty($f)) {
+                    $f->blowNoticeCache();
+                }
             }
 
             if (empty($seen[$notice_id])) {
@@ -66,4 +72,13 @@ class File_to_post extends Memcached_DataObject
     {
         return Memcached_DataObject::pkeyGet('File_to_post', $kv);
     }
+
+    function delete()
+    {
+        $f = File::staticGet('id', $this->file_id);
+        if (!empty($f)) {
+            $f->blowNoticeCache();
+        }
+        return parent::delete();
+    }
 }
-- 
2.39.5