(ns computersandblues.lodestone.match (:require [applied-science.js-interop :as j])) (defn- ->regex "Does its best to compila a stirng into a regular expression; will fall back to return a RegExp that matches verbatim." [s] (try (js/RegExp. s "i") (catch js/Error _ (js/RegExp. (js/RegExp.escape s) "i")))) (defn query->tokens [query] (->> (re-seq #"\"([^\"]+)\"|([^\s]+)" query) (mapv (fn [[_ a b]] (or a b))))) (defn text-matcher [token] (let [match? (partial re-find (->regex token))] (filter (fn [post] (or (match? (j/get post :content)) (match? (j/get-in post [:account :acct])) ; search for url + username of poster (some #(match? (j/get % :username)) (j/get post :mentions)) (some #(some-> (j/get % :description) match?) (j/get post :media_attachments))))))) ; search in alt text (defn query->matching-xform [query] (if query (->> (query->tokens query) (mapv #(text-matcher %)) (reduce comp)) (filter (constantly true))))