   (or (= :https (:scheme req))
       (= "https" (get-in req [:headers "x-forwarded-proto"]))))
 
+(def bucket-doc-ids (atom #{}))
+
+(defn clean-bucket-doc-ids []
+  (swap! bucket-doc-ids (fn [b]
+                          (set/intersection b (set (keys @sente/document-subs))))))
+
 (defn app [sente-state]
   (routes
    (POST "/api/entity-ids" request

          @(d/transact (pcd/conn) [{:db/id document-id :document/name "Untitled"}])
          (redirect (str "/document/" document-id))))
   ;; Group newcomers into buckets with bucket-count users in each bucket.
   ;; TODO: exclude documents that didn't start out as buckets.
   (GET ["/bucket/:bucket-count" :bucket-count #"[0-9]+"] [bucket-count]
        (let [bucket-count (Integer/parseInt bucket-count)]
          (if-let [eid (ffirst (sort-by (comp - count last)
                                        (filter (fn [[doc-id subs]]
                                                  (< (count subs) bucket-count))
                                                @sente/document-subs)))]
            (redirect (str "/document/" eid))
            (redirect "/"))))
   (GET "/interesting" []
        {:status 200
         :body (str

  (:require [cemerick.url :as url]
            [cheshire.core :as json]
            [clojure.core.async :as async]
            [clojure.set :as set]
            [clojure.tools.logging :as log]
            [clojure.tools.reader.edn :as edn]
            [clj-time.core :as time]
