             [oc.lib.db.common :as db-common]))
 
 (def min-iso8601 (lib-time/to-iso (lib-time/from-millis 0)))
+
+(defn- direction-filter [direction start row-field]
+  (if (= direction :before)
+    (r/gt start row-field)
+    (r/le start row-field)))
+
+(defn- unseen-filter [container-last-seen-at row-field]
+  (r/gt container-last-seen-at row-field))
+
+(defn- user-visibility-filter [user-id entry-row]
+  (r/or ;; has :follow true
+        (-> (r/get-field entry-row [:user-visibility (keyword user-id) :follow])
+            (r/default false))
+        (-> (r/get-field entry-row [:user-visibility (keyword user-id) :unfollow])
+            (r/default true)
+            (r/not))))
 
 (defn row-pinned-at [allowed-board-uuids row container-id]
   (r/branch (r/contains allowed-board-uuids (r/get-field row :board-uuid))

                       :last-activity-at last-activity-at
                       ;; Add sort-value for pagination
                       :sort-value last-activity-at
                       ;; Count unseen comments
                       :unseen-comments (-> (r/table :interactions)
                                            (r/between [true entry-uuid container-last-seen-at]
                                                       [true entry-uuid r/maxval]
                                                       {:index :comment-resource-uuid-created-at})
                                            (r/count))
                       ;; Add all the related interactions
                       :interactions (-> (r/table :interactions)
                                         (r/get-all [entry-uuid] {:index :resource-uuid})

             (coll? relation-fields))
         (boolean? count?)
         (boolean? unseen)]}
  (let [order-fn (if (= order :desc) r/desc r/asc)
        dir-filter (when start
                     #(direction-filter direction start (r/get-field % [:reduction :created-at])))
        uns-filter (when (and unseen container-last-seen-at)
                      #(unseen-filter container-last-seen-at (r/get-field % [:reduction :created-at])))
        filter? (or dir-filter uns-filter)]
    (db-common/with-timeout db-common/default-timeout
      (as-> (r/table :interactions) query
        (r/get-all query index-value {:index index-name})
        (r/group query :resource-uuid)
        (r/max query :created-at)
        (r/ungroup query)
        (if filter?
          (r/filter query (r/fn [row]
            ;; All records after/before the start
            (cond (and dir-filter uns-filter)
                  (r/and (dir-filter row)
                         (uns-filter row))
                  dir-filter
                  (dir-filter row)
                  uns-filter
                  (uns-filter row))))
          query)
        ;; Join with the relative entry
        (r/eq-join query :group  ;; {:left {:group "resource-uuid" :reduction comment-map} :right entry-map}
          (r/table :entries))
        ;; Filter out items the user is not following
        (r/filter query (r/fn [row]
          ;; Filter on the user's visibility map:
          (user-visibility-filter user-id (r/get-field row :right))))
        ;; Sort
        (if-not count?
          (r/order-by query (order-fn (r/fn [row] (r/get-field row [:left :reduction :created-at]))))
          query)
        ;; Apply limit
        (if (pos? limit)
          (r/limit query limit)
