From d666eb45e6d6ece7e06f4e9a9e4b1ddb034e9a5e Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 27 Nov 2026 12:04:53 +0000 Subject: [PATCH] matrix todo experiment. --- .../do_blog/pages/matrix_widget.cljs | 8 ++++- .../do_blog/pages/todo/model.cljc | 36 ++++++++++++++----- .../do_blog/pages/todo_widget.cljs | 32 +++++++++++++++-- .../com/oly/static_sites/do_blog/views.cljc | 9 +++++ .../static_sites/matrix_widget/browser.cljs | 9 +++++ .../static_sites/matrix_widget/interface.cljc | 6 ++++ 6 files changed, 87 insertions(+), 13 deletions(-) diff --git a/bases/do-blog/src/com/oly/static_sites/do_blog/pages/matrix_widget.cljs b/bases/do-blog/src/com/oly/static_sites/do_blog/pages/matrix_widget.cljs index 8c962a4..e9b8fe4 100644 --- a/bases/do-blog/src/com/oly/static_sites/do_blog/pages/matrix_widget.cljs +++ b/bases/do-blog/src/com/oly/static_sites/do_blog/pages/matrix_widget.cljs @@ -62,9 +62,15 @@ {:keys [status widget-id approved]} (some-> c deref :state)] [:div.pa3.font.lh-copy [:h1.f3.mt0 "Matrix widget"] - (if-not (mw/embedded?) + (cond + (not (mw/embedded?)) [:p "Open this page as a widget inside a Matrix room, e.g. " [:code "/addwidget " (.-origin js/location) "/widget/"]] + + (and (mw/nested?) (not= :ready status)) + (views/wrapped-widget-help (str (.-origin js/location) "/widget/")) + + :else [:div [:p.b {:role "status"} (status-text status (status-text :waiting))] (when widget-id [:p.f6.gray "Widget id: " widget-id]) diff --git a/bases/do-blog/src/com/oly/static_sites/do_blog/pages/todo/model.cljc b/bases/do-blog/src/com/oly/static_sites/do_blog/pages/todo/model.cljc index 76bb8bf..e517ba5 100644 --- a/bases/do-blog/src/com/oly/static_sites/do_blog/pages/todo/model.cljc +++ b/bases/do-blog/src/com/oly/static_sites/do_blog/pages/todo/model.cljc @@ -3,11 +3,14 @@ Every change is a room event of event-type whose content is the whole item: {:id \"uuid\" :text \"Buy milk\" :done false} - Toggling an item sends its content again with :done flipped. The list is the - newest event per :id, ordered by the server timestamp (origin_server_ts), so - it comes out the same whatever order events arrive in. + Toggling an item sends its content again with :done flipped, and removing it + sends it with :deleted true. The list is the newest event per :id, ordered by + the server timestamp (origin_server_ts), so it comes out the same whatever + order events arrive in. Deleted items are kept (so an older event arriving + late cannot bring them back) but not shown. - A chat message \"!todo Buy milk\" adds an item too; its id is the event id." + A chat message \"!todo Buy milk\" adds an item too; its id is the event id, + and it is removed the same way." (:require [clojure.string :as str])) (def event-type "com.digitaloctave.todo") @@ -26,10 +29,11 @@ (let [ts (or origin_server_ts 0)] (cond (= event-type type) - (let [{:keys [id text done]} content] + (let [{:keys [id text done deleted]} content] (when (and (string? id) (string? text) (not (str/blank? text))) - {:id id :text text :done (true? done) - :sender sender :created ts :updated ts})) + (cond-> {:id id :text text :done (true? done) + :sender sender :created ts :updated ts} + (true? deleted) (assoc :deleted true)))) (= "m.room.message" type) (when-let [text (chat-todo-text (:body content))] @@ -72,12 +76,26 @@ [{:keys [id text done]}] {:id id :text text :done (not done)}) +(defn deleted-content + "Content for an event removing a todo. The text is kept so the event is + still a valid todo event for older versions of the widget." + [{:keys [id text done]}] + {:id id :text text :done (true? done) :deleted true}) + (defn pending "Show content locally straight away, until the server echoes the event." [content now] (assoc content :pending true :created now :updated now)) (defn sorted-todos - "Open items first, then done ones; oldest first within each group." + "Items to show: open ones first, then done ones; oldest first within each + group. Deleted items are left out." [todos] - (sort-by (juxt :done :created :id) (vals todos))) + (->> (vals todos) + (remove :deleted) + (sort-by (juxt :done :created :id)))) + +(defn done-todos + "Shown items that are done, e.g. for clearing them all at once." + [todos] + (filter :done (sorted-todos todos))) diff --git a/bases/do-blog/src/com/oly/static_sites/do_blog/pages/todo_widget.cljs b/bases/do-blog/src/com/oly/static_sites/do_blog/pages/todo_widget.cljs index 71c7eeb..b2f842e 100644 --- a/bases/do-blog/src/com/oly/static_sites/do_blog/pages/todo_widget.cljs +++ b/bases/do-blog/src/com/oly/static_sites/do_blog/pages/todo_widget.cljs @@ -61,13 +61,25 @@ (when-let [todo (get-in @todo-state [:todos id])] (publish! (model/toggled-content todo)))) +(defn delete! [id] + (when-let [todo (get-in @todo-state [:todos id])] + (publish! (model/deleted-content todo)))) + +(defn clear-done! + "Remove every done item: one event per item." + [] + (run! #(publish! (model/deleted-content %)) + (model/done-todos (:todos @todo-state)))) + (defn handle-action "Actions dispatched from the view; see core/handle-action." [action args] (case action :todo/set-draft (swap! todo-state assoc :draft (first args)) :todo/add (add!) - :todo/toggle (toggle! (first args)))) + :todo/toggle (toggle! (first args)) + :todo/delete (delete! (first args)) + :todo/clear-done (clear-done!))) ;; View @@ -85,7 +97,14 @@ [:label.flex-auto {:for (str "todo-" id) :class (when done ["strike" "gray"])} text] - (when sender [:span.f7.gray.ml2 sender])]) + (when sender [:span.f7.gray.ml2 sender]) + [:button.ml2.bn.bg-transparent.gray.hover-dark-red.pointer.f5 + {:type "button" + :title "Remove" + :aria-label (str "Remove " text) + :disabled pending + :on {:click [[:todo/delete id]]}} + "×"]]) (defn todo-form [draft] [:form.flex.mb3 {:on {:submit [[:dom/prevent-default] [:todo/add]]}} @@ -109,6 +128,9 @@ [:p "Add this page to a Matrix room as a widget: " [:code "/addwidget " (.-origin js/location) "/todo/"]] + (and (mw/nested?) (not= :ready status)) + (views/wrapped-widget-help (str (.-origin js/location) "/todo/")) + (not= :ready status) [:p {:role "status"} (status-text status (status-text :waiting))] @@ -122,7 +144,11 @@ (if (seq items) [:ul.list.pl0.mt0 (map todo-item items)] [:p.gray "Nothing to do. Add an item above, or send " - [:code "!todo something"] " in the chat."])])])) + [:code "!todo something"] " in the chat."]) + (when (some :done items) + [:button.mt2.pa2.ba.b--black-20.bg-white.pointer + {:type "button" :on {:click [[:todo/clear-done]]}} + "Clear done"])])])) (defmethod views/page :todo [state] (todo-page state)) diff --git a/bases/do-blog/src/com/oly/static_sites/do_blog/views.cljc b/bases/do-blog/src/com/oly/static_sites/do_blog/views.cljc index 2369fb9..bf73a5f 100644 --- a/bases/do-blog/src/com/oly/static_sites/do_blog/views.cljc +++ b/bases/do-blog/src/com/oly/static_sites/do_blog/views.cljc @@ -67,6 +67,15 @@ #?(:clj (defmethod page :todo [_state] [:p.pa3.lh-copy "Loading the todo list…"])) +(defn wrapped-widget-help + "Shown when a widget page is inside an integration manager's wrapper, + where the Matrix client's messages cannot reach it." + [widget-url] + [:div.lh-copy {:role "alert"} + [:p.b "This widget was added through an integration manager wrapper, which does not pass the widget API on."] + [:p "Remove it from the room (Room info → Extensions), then add it again by sending this command in the room:"] + [:p [:code "/addwidget " widget-url]]]) + (defn loading-bar [loading] (when loading [:div.fixed.top-0.left-0.w-100.bg-green.white.f6.tc.pa1.z-999 diff --git a/components/matrix-widget/src/com/oly/static_sites/matrix_widget/browser.cljs b/components/matrix-widget/src/com/oly/static_sites/matrix_widget/browser.cljs index 55b6805..6e17930 100644 --- a/components/matrix-widget/src/com/oly/static_sites/matrix_widget/browser.cljs +++ b/components/matrix-widget/src/com/oly/static_sites/matrix_widget/browser.cljs @@ -15,6 +15,15 @@ [] (not (identical? js/window js/window.parent))) +(defn nested? + "Is the page inside an iframe that is itself inside an iframe? That is what + happens when a widget is added through an integration manager's generic + wrapper (e.g. scalar.vector.im/api/widgets/generic.html?url=...): the client + talks to the wrapper, which does not pass the widget API on to us." + [] + (and (embedded?) + (not (identical? js/window.parent js/window.top)))) + (defn- post! "Send a message to the parent. Until the client has spoken we do not know its origin, so only content_loaded (which carries no data) goes to \"*\"." diff --git a/components/matrix-widget/src/com/oly/static_sites/matrix_widget/interface.cljc b/components/matrix-widget/src/com/oly/static_sites/matrix_widget/interface.cljc index 823c002..338f3c7 100644 --- a/components/matrix-widget/src/com/oly/static_sites/matrix_widget/interface.cljc +++ b/components/matrix-widget/src/com/oly/static_sites/matrix_widget/interface.cljc @@ -33,6 +33,12 @@ [] (browser/embedded?)) + (defn nested? + "Inside an iframe inside an iframe, e.g. an integration manager's + generic widget wrapper, where the widget API cannot reach us." + [] + (browser/nested?)) + (defn connect! "Start listening for the embedding Matrix client. Options: :capabilities, :on-ready (fn [client]), :on-event (fn [event]).