activity-pub-poc/src/main/clj/dda/activity_pub_poc/json_ld.clj

54 lines
1.5 KiB
Clojure
Raw Normal View History

2023-06-16 15:38:51 +00:00
(ns dda.activity-pub-poc.json-ld
2023-06-15 13:00:46 +00:00
(:require
2023-06-15 13:18:50 +00:00
[hato.client :as http]
2023-06-16 15:38:51 +00:00
[clojure.string :as str]
2023-06-16 14:06:28 +00:00
[clojure.inspector :as ins]
2023-06-15 13:00:46 +00:00
))
2023-06-16 14:06:28 +00:00
(def team-url "https://social.meissa-gmbh.de/users/team")
2023-06-16 15:38:51 +00:00
(def context-cache (atom {}))
2023-06-15 13:00:46 +00:00
(defn json-get [url]
2023-06-16 14:06:28 +00:00
(:body
(http/get url
{:headers {"Accept" "application/json"}
:http-client {:redirect-policy :normal}
:as :json-string-keys})))
2023-06-16 15:38:51 +00:00
(defn fetch-context [url]
(if-let [ctx (get @context-cache url)]
ctx
(get
(swap! context-cache
(fn [cache]
(assoc cache url (get (json-get url) "@context"))))
url)))
(defn expand-context
([new-context]
(expand-context {} new-context))
([current-context new-context]
(cond
(string? new-context)
(expand-context current-context (fetch-context new-context))
(sequential? new-context)
(reduce expand-context current-context new-context)
(map? new-context)
(into current-context
(map
(fn [[k v]]
(let [id (if (map? v) (get v "@id" v) v)
[prefix suffix] (str/split id #":")]
(if-let [base (get (merge current-context new-context) prefix)]
[k (assoc (if (map? v) v {})
"@id" (str (if (map? base) (get base "@id") base)
suffix))]
[k (if (map? v) v {"@id" v})]))))
new-context))))
(expand-context (get (json-get team-url) "@context"))