git.fiddlerwoaroof.com
Raw Blame History
(defpackage :activitypub-tools.webfinger-viewer
  (:use :cl )
  (:export
   #:main))
(in-package :activitypub-tools.webfinger-viewer)

(defclass handler (ningle:<app> araneus:mixin)
  ())

(defmethod araneus:controller ((name (eql 'root)) params &key)
  nil)
(defmethod araneus:view ((name (eql 'root)) (params null))
  `(200
    (:content-type "text/html" ,(format nil "/v/~a" params))
    (,(spinneret:with-html-string
        (:form :method :post
               :action "/"
               (:input :name "handle"
                       :placeholder "foo@example.com")
               (:button :type "submit" ">"))))))

(defmethod araneus:controller ((name (eql 'redirect)) params &key)
  (serapeum:assocdr "handle" params
                    :test 'equal))
(defmethod araneus:view ((name (eql 'redirect)) (params null))
  `(404 (:content-type "text/html") ("Not Found")))
(defmethod araneus:view ((name (eql 'redirect)) (params string))
  `(302
    (:content-type "text/plain"
     :location ,(format nil "/v/~a" params))
    ("Redirecting")))

(defmethod araneus:controller ((name (eql 'handle)) params &key)
  (activitypub-tools.domain:webfinger-data (serapeum:assocdr :handle params)))

(defmethod araneus:view ((name (eql 'handle)) (model hash-table))
  `(200
    (:content-type "text/html")
    (,(spinneret:with-html-string
        (:head
         (:style "body { padding: 1em; font-family: sans-serif; }"))
        (:h1 "Webfinger Information Viewer")
        (:section
         (:h* "subject")
         (gethash "subject" model))
        (:section
         (:h* "aka")
         (:ul
          (loop for alias in (gethash "aliases" model)
                do (:li alias))))
        (:section
         (:h* "links")
         (:ul
          (loop for link in (gethash "links" model)
                do (:li (loop for key being the hash-keys of link using (hash-value value)
                              do (if (equal key "href")
                                     (:a :class key :href value ("~a: ~a" key value))
                                     (:div :class key ("~a: ~a" key value))))))))


        (:details (:summary "Raw data")
                  (:pre (prin1-to-string model)))))))

(defmethod araneus:routes progn ((app handler))
  (araneus:defroutes app
    (("/v/:handle") (araneus:as-route 'handle))
    (("/" :method :post) (araneus:as-route 'redirect))
    (("/" :method :get) (araneus:as-route 'root))))

(defun main ()
  (uiop:setup-command-line-arguments)
  (activitypub-tools.pprint-setup:setup-pprint)
  (let* ((port (if (= (length (uiop:command-line-arguments)) 1)
                   (parse-integer (elt (uiop:command-line-arguments) 0))
                   9092))
         (handler (make-instance 'handler)))
    (clack:clackup handler
                   :address "0.0.0.0"
                   :port port
                   :debug nil
                   :use-thread nil)))