                        :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})

                           (r/get-field :bookmarked-at)
                           (r/default nil))}))
        ;; Filter out:
        (r/filter query (r/fn [row]
          (r/or (r/not start)
                (r/and (= direction :before)
                       (r/gt start (r/get-field row :sort-value)))
                (r/and (= direction :after)
                       (r/le start (r/get-field row :sort-value))))))
        (if-not count?
          (r/order-by query (order-fn :sort-value))
          query)

            [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))
