Fix white archive page.
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Oliver 2026-11-24 13:33:30 +00:00
parent d5a96e3190
commit 8a3ad00f78
5 changed files with 175 additions and 10 deletions

View File

@ -10,7 +10,8 @@
:refer [state load-index! latest-article org->document]]
[com.oly.static-sites.do-blog.pages.chess-board :as chess]
[com.oly.static-sites.do-blog.pages.chess.state :as chess-state]
[com.oly.static-sites.do-blog.pages.chess.vstate :as viking-state])
[com.oly.static-sites.do-blog.pages.chess.vstate :as viking-state]
[com.oly.static-sites.do-blog.pages.matrix-widget :as widget])
(:require-macros [cl-eorg.macros :refer [org-doc->move-process]]))
;; Copy org documents into public/ and write org-meta-index.edn at compile time.
@ -40,6 +41,8 @@
(defn handle-action [[action & args]]
(case action
:search/set-terms (swap! state assoc-in [:search :terms] (first args))
:widget/set-draft (swap! widget/widget-state assoc :draft (first args))
:widget/send (widget/send-message! (:draft @widget/widget-state))
(js/console.warn "Unknown action" (pr-str action))))
(defn dispatch [{:replicant/keys [dom-event]} actions]
@ -47,12 +50,14 @@
;; Navigation
(defn fetch-text [path on-success on-error]
(defn fetch-text
([path on-success] (fetch-text path on-success nil))
([path on-success on-error]
(GET path {:response-format (text-response-format)
:handler on-success
:error-handler (fn [error]
(js/console.error "Failed to fetch" path (clj->js error))
(on-error error))}))
(when on-error (on-error error)))})))
(defn show-route! [match & {:as extra}]
(swap! state merge
@ -97,6 +102,7 @@
(add-watch state ::render (fn [_ _ _ _] (render!)))
(add-watch chess-state/game-state ::render (fn [_ _ _ _] (render!)))
(add-watch viking-state/game-state ::render (fn [_ _ _ _] (render!)))
(add-watch widget/widget-state ::render (fn [_ _ _ _] (render!)))
(fetch-text "/documents/org-meta-index.edn"
(fn [response]
(load-index! (reader/read-string response))

View File

@ -0,0 +1,150 @@
(ns com.oly.static-sites.do-blog.pages.matrix-widget
"Minimal Matrix widget, written against the postMessage protocol directly.
Add it to a room in Element with:
/addwidget https://<site>/widget/
The conversation with the client (Element) is:
1. client -> widget toWidget capabilities we reply with what we want
2. client -> widget toWidget notify_capabilities we reply {} and are ready
3. widget -> client fromWidget send_event post a message to the room
4. client -> widget toWidget send_event a new event in the room
Replies echo the request object back with an added :response key.
Requests we send need a unique :requestId and the :widgetId the client gave us."
(:require [clojure.string :as str]
[com.oly.static-sites.do-blog.views :as views]))
(def capabilities
["org.matrix.msc2762.send.event:m.room.message#m.text"
"org.matrix.msc2762.receive.event:m.room.message"])
(defonce widget-state
(atom {:status :waiting ;; :waiting -> :negotiating -> :ready
:widget-id nil
:approved []
:draft ""
:log []}))
(defn log! [direction text]
(swap! widget-state update :log
#(->> (conj % {:id (random-uuid) :direction direction :text text})
(take-last 50)
vec)))
;; Sending
(defn post! [message]
(.postMessage js/window.parent (clj->js message) "*"))
(defn reply!
"Answer a toWidget request: the same object back, plus :response."
[request response]
(post! (assoc request :response response)))
(defn request!
"Send a fromWidget request to the client."
[action data]
(post! {:api "fromWidget"
:widgetId (:widget-id @widget-state)
:requestId (str (random-uuid))
:action action
:data data}))
(defn send-message! [body]
(when-not (str/blank? body)
(request! "send_event" {:type "m.room.message"
:content {:msgtype "m.text" :body body}})
(log! :out body)
(swap! widget-state assoc :draft "")))
;; Receiving
(defn handle-request
"Requests the client sends to us (api toWidget)."
[{:keys [action data widgetId] :as request}]
(case action
"capabilities"
(do (swap! widget-state assoc :status :negotiating :widget-id widgetId)
(reply! request {:capabilities capabilities}))
"notify_capabilities"
(do (swap! widget-state assoc :status :ready :approved (vec (:approved data)))
(reply! request {}))
"supported_api_versions"
(reply! request {:supported_versions ["0.0.1" "0.0.2" "org.matrix.msc2762"]})
"send_event"
(do (when (= "m.room.message" (:type data))
(log! :in (str (:sender data) ": " (-> data :content :body))))
(reply! request {}))
;; Anything else: say so, rather than leaving the client waiting.
(reply! request {:error {:message (str "Unsupported action: " action)}})))
(defn handle-response
"Replies from the client to requests we sent (api fromWidget)."
[{:keys [action response]}]
(when-let [error (:error response)]
(log! :error (str action " failed: " (:message error)))))
(defn on-message [event]
;; Only listen to the window that embeds us.
(when (and (not= js/window js/window.parent)
(= (.-source event) js/window.parent))
(let [{:keys [api response] :as message}
(js->clj (.-data event) :keywordize-keys true)]
(cond
(and (= "toWidget" api) (not response)) (handle-request message)
(and (= "fromWidget" api) response) (handle-response message)))))
(defn start!
"Listen for the client straight away. Clients that do not wait for the
iframe load event expect content_loaded before they start the handshake."
[]
(.addEventListener js/window "message" on-message)
(when (not= js/window js/window.parent)
(js/setTimeout #(when (= :waiting (:status @widget-state))
(request! "content_loaded" {}))
2000)))
(defonce started (start!))
;; View
(def status-text
{:waiting "Waiting for the Matrix client…"
:negotiating "Asking for permission to read and send messages…"
:ready "Connected"})
(defn widget-page [_state]
(let [{:keys [status widget-id draft log approved]} @widget-state
embedded? (not= js/window js/window.parent)]
[:div.pa3.font.lh-copy
[:h1.f3.mt0 "Matrix widget"]
(if-not embedded?
[:p "Open this page as a widget inside a Matrix room, e.g. "
[:code "/addwidget " (.-origin js/location) "/widget/"]]
[:div
[:p.b {:role "status"} (status-text status)]
(when widget-id [:p.f6.gray "Widget id: " widget-id])
(when (= :ready status)
[:div
[:p.f6.gray "Approved: " (str/join ", " approved)]
[:div.flex
[:input.input-reset.ba.b--black-20.pa2.flex-auto
{:type "text"
:placeholder "Message to the room"
:value draft
:on {:input [[:widget/set-draft :event.target/value]]}}]
[:button.ml2.pa2.ba.b--black-20.bg-blue.white.pointer
{:on {:click [[:widget/send]]}}
"Send"]]])
[:ul.list.pl0.mt3
(for [{:keys [id direction text]} (reverse log)]
[:li.pv1.bb.b--black-10 {:replicant/key id}
[:span.mr2.gray (case direction :in "←" :out "→" "!")] text])]])]))
(defmethod views/page :widget [state]
(widget-page state))

View File

@ -11,6 +11,7 @@
["/archive/{*tag}" {:name :archive}]
["/chess/" {:name :chess}]
["/about/" {:name :about}]
["/widget/" {:name :widget}]
["/posts/{*path}" {:name :posts}]])
(def router (rc/router routes))

View File

@ -52,6 +52,7 @@
["/" (page-state "/" (doc latest))]
[(href :about) (page-state (href :about) nil)]
[(href :chess) (page-state (href :chess) nil)]
[(href :widget) (page-state (href :widget) nil)]
[(archive-href "") (page-state (archive-href "") nil)]]
(for [tag tags
:let [url (archive-href tag)]]

View File

@ -25,6 +25,7 @@
:archive (if (str/blank? tag) "Past articles" (str "Articles tagged " tag))
:chess "Chess"
:about "About"
:widget "Matrix widget"
"Page not found"))]
(if (str/blank? title)
site-name
@ -58,6 +59,10 @@
#?(:clj (defmethod page :chess [_state]
[:p.lh-copy "Loading the chess board…"]))
;; Likewise the Matrix widget (pages/matrix_widget.cljs).
#?(:clj (defmethod page :widget [_state]
[:p.pa3.lh-copy "Loading the Matrix widget…"]))
(defn loading-bar [loading]
(when loading
[:div.fixed.top-0.left-0.w-100.bg-green.white.f6.tc.pa1.z-999
@ -126,6 +131,8 @@
(search-articles {:search terms})))]]]))
(defn app [state]
(layout state (if (:not-found? state)
(not-found-page)
(page state))))
(cond
(:not-found? state) (layout state (not-found-page))
;; The widget is embedded in a Matrix client, so it skips the site chrome.
(= :widget (-> state :route :data :name)) (page state)
:else (layout state (page state))))