001 (ns clj-activitypub.webfinger
002 "copied from [Jahfer's clj-activitypub library](https://github.com/jahfer/clj-activitypub).
003 If and when Jahfer issues a release of that library, this directory will be deleted and a
004 dependency on that library will be added to the project."
005 (:require [clj-http.client :as client]
006 [clj-activitypub.internal.http-util :as http]
007 [clj-activitypub.internal.thread-cache :as thread-cache]))
008
009 (def remote-uri-path "/.well-known/webfinger")
010
011 (defn- resource-str [domain username]
012 (str "acct:" username "@" domain))
013
014 (defn resource-url
015 "Builds a URL pointing to the user's account on the remote server."
016 [domain username & [params]]
017 (let [resource (resource-str domain username)
018 query-str (http/encode-url-params (merge params {:resource resource}))]
019 (str "https://" domain remote-uri-path "?" query-str)))
020
021 (def ^:private user-id-cache
022 (thread-cache/make))
023
024 (defn fetch-user-id
025 "Follows the webfinger request to a remote domain, retrieving the ID of the requested
026 account. Typically returns a string in the form of a URL."
027 [domain username]
028 ((:get-v user-id-cache)
029 (str domain "@" username) ;; cache key
030 (fn []
031 (let [response (some-> (resource-url domain username {:rel "self"})
032 (client/get {:as :json :throw-exceptions false :ignore-unknown-host? true}))]
033 (some->> response :body :links
034 (some #(when (= (:type %) "application/activity+json") %))
035 :href)))))