Removing ui-context.

This commit is contained in:
Robert C. Martin 2023-01-28 15:09:07 -06:00
parent 741b39c81d
commit be937b6a97
5 changed files with 33 additions and 68 deletions

View File

@ -11,12 +11,14 @@
(should= "short" (abbreviate "short" 10)) (should= "short" (abbreviate "short" 10))
(should= "some lo..." (abbreviate "some long string." 10)))) (should= "some lo..." (abbreviate "some long string." 10))))
(declare db)
(describe "format header" (describe "format header"
(with db (in-memory/get-db))
(before (in-memory/clear-db @db)
(set-mem :pubkey 99))
(it "formats an empty message" (it "formats an empty message"
(let [profiles {} (let [event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1 :created-at 1
:content "" :content ""
:tags []} :tags []}
@ -25,10 +27,7 @@
(should= (str " (1111111...) " timestamp " \n") header))) (should= (str " (1111111...) " timestamp " \n") header)))
(it "formats a simple message" (it "formats a simple message"
(let [profiles {} (let [event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1 :created-at 1
:content "the message" :content "the message"
:tags []} :tags []}
@ -37,10 +36,8 @@
(should= (str " (1111111...) " timestamp " the message\n") header))) (should= (str " (1111111...) " timestamp " the message\n") header)))
(it "formats a simple message with a user profile" (it "formats a simple message with a user profile"
(let [profiles {1 {:name "user-1"}} (gateway/add-profile @db 1 {:name "user-1"})
event-context (atom {:profiles profiles}) (let [event {:pubkey 1
_ (reset! ui-context {:event-context event-context})
event {:pubkey 1
:created-at 1 :created-at 1
:content "the message" :content "the message"
:tags []} :tags []}
@ -49,10 +46,7 @@
(should= (str " (user-1) " timestamp " the message\n") header))) (should= (str " (user-1) " timestamp " the message\n") header)))
(it "formats a long message with line ends." (it "formats a long message with line ends."
(let [profiles {} (let [event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1 :created-at 1
:content (str "Four score and seven years ago\n" :content (str "Four score and seven years ago\n"
"our fathers brought forth upon this continent\n" "our fathers brought forth upon this continent\n"
@ -68,10 +62,7 @@
(should (.endsWith header "...\n")))) (should (.endsWith header "...\n"))))
(it "formats a message with a subject" (it "formats a message with a subject"
(let [profiles {} (let [event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
:created-at 1 :created-at 1
:content "the message" :content "the message"
:tags [[:subject "the subject"]]} :tags [[:subject "the subject"]]}
@ -99,80 +90,62 @@
)) ))
(describe "Replacing References" (describe "Replacing References"
(with db (in-memory/get-db))
(before (in-memory/clear-db @db))
(context "using #[n] and p tags" (context "using #[n] and p tags"
(before (gateway/add-profile @db 0 {:name "x"}))
(it "replaces nothing if nothing to replace" (it "replaces nothing if nothing to replace"
(let [profiles {0 {:name "x"}} (let [content "content"
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
content "content"
event {:content content}] event {:content content}]
(should= "content" (replace-references event)))) (should= "content" (replace-references event))))
(it "replaces a single p reference" (it "replaces a single p reference"
(let [content "the #[0] reference" (let [content "the #[0] reference"
profiles {0 {:name "x"}}
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p (hexify 0)]]}] event {:content content :tags [[:p (hexify 0)]]}]
(should= "the @x reference" (replace-references event)))) (should= "the @x reference" (replace-references event))))
(it "replaces a single p reference at the start" (it "replaces a single p reference at the start"
(let [content "#[0] reference" (let [content "#[0] reference"
profiles {0 {:name "x"}}
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p (hexify 0)]]}] event {:content content :tags [[:p (hexify 0)]]}]
(should= "@x reference" (replace-references event)))) (should= "@x reference" (replace-references event))))
(it "replaces a single p reference at the end" (it "replaces a single p reference at the end"
(let [content "the #[0]" (let [content "the #[0]"
profiles {0 {:name "x"}}
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p (hexify 0)]]}] event {:content content :tags [[:p (hexify 0)]]}]
(should= "the @x" (replace-references event)))) (should= "the @x" (replace-references event))))
(it "replaces a single p reference alone" (it "replaces a single p reference alone"
(let [content "#[0]" (let [content "#[0]"
profiles {0 {:name "x"}}
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p (hexify 0)]]}] event {:content content :tags [[:p (hexify 0)]]}]
(should= "@x" (replace-references event)))) (should= "@x" (replace-references event))))
(it "replaces a two p references" (it "replaces a two p references"
(gateway/add-profile @db 1 {:name "y"})
(let [content "the #[0] and #[1] reference" (let [content "the #[0] and #[1] reference"
profiles {0 {:name "x"}
1 {:name "y"}}
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p (hexify 0)] event {:content content :tags [[:p (hexify 0)]
[:p (hexify 1)]]}] [:p (hexify 1)]]}]
(should= "the @x and @y reference" (replace-references event)))) (should= "the @x and @y reference" (replace-references event))))
(it "Replaces a p reference with an abbreviated id if not a profile name" (it "Replaces a p reference with an abbreviated id if not a profile name"
(let [content "#[0]" (let [content "#[0]"
profiles {0 {:name "x"}}
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p "deadbeef"]]}] event {:content content :tags [[:p "deadbeef"]]}]
(should= "@00000000000000000000000000000000000000000000000000000000deadbeef" (should= "@00000000000000000000000000000000000000000000000000000000deadbeef"
(replace-references event)))) (replace-references event))))
(it "does not replace reference if there is no p tag" (it "does not replace reference if there is no p tag"
(let [content "#[1]" (let [content "#[1]"
profiles {0 {:name "x"}}
event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context})
event {:content content :tags [[:p "deadbeef"]]}] event {:content content :tags [[:p "deadbeef"]]}]
(should= "#[1]" (replace-references event)))))) (should= "#[1]" (replace-references event))))))
(describe "format-reply" (describe "format-reply"
(with db (in-memory/get-db))
(before (in-memory/clear-db @db)
(gateway/add-profile @db 1 {:name "user-1"})
(gateway/add-profile @db 2 {:name "user-2"}))
(it "formats a reply to an event" (it "formats a reply to an event"
(let [profiles {1 {:name "user-1"} (let [created-at (make-date "07/05/2022")
2 {:name "user-2"}}
_ (reset! ui-context {:event-context (atom {:profiles profiles})})
created-at (make-date "07/05/2022")
relays ["relay-1"] relays ["relay-1"]
tags [["p" (hexify 1)]] tags [["p" (hexify 1)]]
event {:pubkey 1 :created-at created-at event {:pubkey 1 :created-at created-at
@ -182,19 +155,14 @@
(format-reply event)))) (format-reply event))))
(it "formats a reply to a DM" (it "formats a reply to a DM"
(let [profiles {1 {:name "user-1"} (let [created-at (make-date "07/05/2022")
2 {:name "user-2"}}
_ (reset! ui-context {:event-context (atom {:profiles profiles})})
created-at (make-date "07/05/2022")
relays ["relay-1"] relays ["relay-1"]
tags [["p" (hexify 2)]] tags [["p" (hexify 2)]]
event {:pubkey 1 :created-at created-at :dm true event {:pubkey 1 :created-at created-at :dm true
:relays relays :tags tags :content "Hello #[0]."}] :relays relays :tags tags :content "Hello #[0]."}]
(should= (should=
"D @user-1\n>From: (user-1) at 07/05/22 00:00:00 on relay-1\n>---------------\n>Hello @user-2." "D @user-1\n>From: (user-1) at 07/05/22 00:00:00 on relay-1\n>---------------\n>Hello @user-2."
(format-reply event))) (format-reply event))))
)
) )
@ -248,7 +216,7 @@
(should= "two&nbsp spaces" (non-breaking-spaces "two spaces")) (should= "two&nbsp spaces" (non-breaking-spaces "two spaces"))
(should= "three&nbsp&nbsp spaces" (non-breaking-spaces "three spaces")) (should= "three&nbsp&nbsp spaces" (non-breaking-spaces "three spaces"))
(should= "1 2&nbsp 3&nbsp&nbsp 4&nbsp&nbsp&nbsp ." (should= "1 2&nbsp 3&nbsp&nbsp 4&nbsp&nbsp&nbsp ."
(non-breaking-spaces "1 2 3 4 .")) (non-breaking-spaces "1 2 3 4 ."))
)) ))
(describe "Segment article content" (describe "Segment article content"

View File

@ -20,10 +20,10 @@
(prn 'main 'loading-configuration) (prn 'main 'loading-configuration)
(data-storage/load-configuration) (data-storage/load-configuration)
(prn 'main 'setting-up-gui) (prn 'main 'setting-up-gui)
(let [event-context (:event-context @ui-context) (let [handler (swing/setup-main-window)]
handler (swing/setup-main-window)]
(prn 'main 'main-window-setup-complete) (prn 'main 'main-window-setup-complete)
(swap! event-context assoc :send-chan send-chan :event-handler handler) (set-mem :send-chan send-chan)
(set-mem :event-handler handler)
(prn 'main 'reading-in-last-n-days) (prn 'main 'reading-in-last-n-days)
(let [latest-old-message-time (let [latest-old-message-time
(if (not config/test-run?) (if (not config/test-run?)

View File

@ -151,7 +151,7 @@
(set-mem :event-history (conj (get-mem :event-history) item))) (set-mem :event-history (conj (get-mem :event-history) item)))
(defn select-event [tab-index id] (defn select-event [tab-index id]
(swap! ui-context assoc :selected-tab tab-index) (set-mem :selected-tab tab-index)
(if-not (get-mem :backing-up) (if-not (get-mem :backing-up)
(do (do
(gateway/update-event-as-read (get-db) id) (gateway/update-event-as-read (get-db) id)

View File

@ -55,7 +55,7 @@
(defn id-click [id] (defn id-click [id]
(let [frame (:frame @ui-context) (let [frame (:frame @ui-context)
tab-index (:selected-tab @ui-context) tab-index (get-mem :selected-tab)
tab-selector (keyword (str "#" tab-index)) tab-selector (keyword (str "#" tab-index))
tree (select frame [tab-selector]) tree (select frame [tab-selector])
model (config tree :model) model (config tree :model)
@ -69,9 +69,7 @@
node (find-header-node root-node id)] node (find-header-node root-node id)]
(when (some? node) (when (some? node)
(util/select-tab "all") (util/select-tab "all")
(select-tree-node tree node)) (select-tree-node tree node))))))
)
)))
(defn adjust-back-count [event-data n] (defn adjust-back-count [event-data n]
(let [event-history (:event-history event-data) (let [event-history (:event-history event-data)

View File

@ -4,8 +4,7 @@
(def ui-context (atom {:frame nil (def ui-context (atom {:frame nil
:event-context (atom nil) :event-context (atom nil)
:node-map {} :node-map {}
:orphaned-references {} :orphaned-references {}}))
:selected-tab nil}))
(s/def ::id number?) (s/def ::id number?)
(s/def ::orphaned-references (s/map-of ::id (s/coll-of ::id :kind set?))) (s/def ::orphaned-references (s/map-of ::id (s/coll-of ::id :kind set?)))