                               (set
                                (map :uuid
                                     (filter #(not= (:access %) "private") allowed-boards)))
-                              (set (map :uuid allowed-boards)))]
+                              (set (map :uuid allowed-boards)))
+        dir-filter (when start
+                     #(direction-filter direction start (r/get-field % :sort-value)))
+        uns-filter (when (and unseen container-last-seen-at)
+                      #(unseen-filter container-last-seen-at (r/get-field % :sort-value)))
+        filter? (or dir-filter uns-filter)]
     (db-common/with-timeout db-common/default-timeout
       (as-> (r/table table-name) query
         ;; Filter out non allowed boards
         (r/get-all query index-value {:index index-name})
         (r/merge query (r/fn [row]
           {:sort-value (row-order-val allowed-pins-boards row container-id)}))
+        (if filter?
+          (r/filter query (r/fn [row]
+            (cond (and dir-filter uns-filter)
+                  (r/and (dir-filter row) (uns-filter row))
+                  dir-filter
+                  (dir-filter row)
+                  :else
+                  (uns-filter row))))
+          query)
         ;; Order by home pinned-at/published-at
         (if-not count?
           (r/order-by query (order-fn :sort-value))
           query)
-        ;; Filter out:
-        (r/filter query (r/fn [row]
-          (r/and ;; Filter out seen entries if unseen flag is on
-                 (r/or (r/not unseen)
-                       (r/gt (r/get-field row [:published-at])
-                             container-last-seen-at))
-                 (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)))))))
            ;; Apply count if needed
         (if count? (r/count query) query)
            ;; Apply limit

            [oc.lib.db.common :as db-common]))

(def min-iso8601 (lib-time/to-iso (lib-time/from-millis 0)))

(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
                       ;; Add all the related interactions
                       :interactions (-> (r/table :interactions)
                                         (r/get-all [entry-uuid] {:index :resource-uuid})
