Clean up the @(:event-context @ui-context) BS. replace with get-event-state. Stop passing event-state and event-context around unless they are going to change.

This commit is contained in:
Robert C. Martin 2022-09-17 16:50:31 -05:00
parent 34822547e7
commit 4b5c1deab2
16 changed files with 208 additions and 209 deletions

View File

@ -42,27 +42,27 @@
) )
(it "corrects malformed tags" (it "corrects malformed tags"
(let [id (rand-int 1000000) (let [id (rand-int 1000000)
pubkey (rand-int 1000000) pubkey (rand-int 1000000)
sig (rand-int 1000000) sig (rand-int 1000000)
created_at (rand-int 10000) created_at (rand-int 10000)
content "the content" content "the content"
tags [["e:" 1 2 3] ["p:p:" 4 5 6 7]] tags [["e:" 1 2 3] ["p:p:" 4 5 6 7]]
event {"id" (hexify id) event {"id" (hexify id)
"pubkey" (hexify pubkey) "pubkey" (hexify pubkey)
"created_at" created_at "created_at" created_at
"kind" 1 "kind" 1
"tags" tags "tags" tags
"content" content "content" content
"sig" (->> sig (num->bytes 64) bytes->hex-string)}] "sig" (->> sig (num->bytes 64) bytes->hex-string)}]
(should= {:id id (should= {:id id
:pubkey pubkey :pubkey pubkey
:created-at created_at :created-at created_at
:kind 1 :kind 1
:tags [[:e- 1 2 3] [:p-p- 4 5 6 7]] :tags [[:e- 1 2 3] [:p-p- 4 5 6 7]]
:content content :content content
:sig sig} :sig sig}
(translate-event event))))) (translate-event event)))))
(declare now event state) (declare now event state)
(describe "Processing Text events (Kind 1)" (describe "Processing Text events (Kind 1)"
@ -162,9 +162,9 @@
:name "name" :name "name"
:about "about" :about "about"
:picture "picture"} :picture "picture"}
event-state {:keys keys} _ (reset! (:event-context @ui-context) {:keys keys})
now (quot (System/currentTimeMillis) 1000) now (quot (System/currentTimeMillis) 1000)
event (compose-metadata-event event-state) event (compose-metadata-event)
{:keys [pubkey created_at kind tags content id sig]} (second event) {:keys [pubkey created_at kind tags content id sig]} (second event)
] ]
(should= "EVENT" (first event)) (should= "EVENT" (first event))
@ -182,11 +182,11 @@
(it "composes an original message with no subject." (it "composes an original message with no subject."
(let [private-key (num->bytes 64 314159) (let [private-key (num->bytes 64 314159)
public-key (get-pub-key private-key) public-key (get-pub-key private-key)
event-state {:keys {:private-key (bytes->hex-string private-key) _ (reset! (:event-context @ui-context) {:keys {:private-key (bytes->hex-string private-key)
:public-key (bytes->hex-string public-key)}} :public-key (bytes->hex-string public-key)}})
text "message text" text "message text"
subject "" subject ""
event (compose-text-event event-state subject text) event (compose-text-event subject text)
{:keys [pubkey created_at kind tags content id sig]} (second event) {:keys [pubkey created_at kind tags content id sig]} (second event)
now (quot (System/currentTimeMillis) 1000)] now (quot (System/currentTimeMillis) 1000)]
(should= "EVENT" (first event)) (should= "EVENT" (first event))
@ -202,11 +202,11 @@
(it "composes an original message with a subject." (it "composes an original message with a subject."
(let [private-key (num->bytes 64 314159) (let [private-key (num->bytes 64 314159)
public-key (get-pub-key private-key) public-key (get-pub-key private-key)
event-state {:keys {:private-key (bytes->hex-string private-key) _ (reset! (:event-context @ui-context) {:keys {:private-key (bytes->hex-string private-key)
:public-key (bytes->hex-string public-key)}} :public-key (bytes->hex-string public-key)}})
text "message text" text "message text"
subject "subject" subject "subject"
event (compose-text-event event-state subject text) event (compose-text-event subject text)
{:keys [pubkey created_at kind tags content id sig]} (second event) {:keys [pubkey created_at kind tags content id sig]} (second event)
now (quot (System/currentTimeMillis) 1000)] now (quot (System/currentTimeMillis) 1000)]
(should= "EVENT" (first event)) (should= "EVENT" (first event))
@ -225,13 +225,13 @@
root-id 7734 root-id 7734
root-id-hex (hexify root-id) root-id-hex (hexify root-id)
root-author 99 root-author 99
event-state {:keys {:private-key (bytes->hex-string private-key) _ (reset! (:event-context @ui-context) {:keys {:private-key (bytes->hex-string private-key)
:public-key (bytes->hex-string public-key)} :public-key (bytes->hex-string public-key)}
:text-event-map {root-id {:pubkey root-author :text-event-map {root-id {:pubkey root-author
:tags []}} :tags []}}
:pubkey public-key} :pubkey public-key})
text "message text" text "message text"
event (compose-text-event event-state "" text root-id) event (compose-text-event "" text root-id)
{:keys [pubkey created_at kind tags content id sig]} (second event) {:keys [pubkey created_at kind tags content id sig]} (second event)
now (quot (System/currentTimeMillis) 1000)] now (quot (System/currentTimeMillis) 1000)]
(should= "EVENT" (first event)) (should= "EVENT" (first event))
@ -254,16 +254,16 @@
root-id 1952 root-id 1952
root-id-hex (hexify root-id) root-id-hex (hexify root-id)
root-author 99 root-author 99
event-state {:keys {:private-key (bytes->hex-string private-key) _ (reset! (:event-context @ui-context) {:keys {:private-key (bytes->hex-string private-key)
:public-key (bytes->hex-string public-key)} :public-key (bytes->hex-string public-key)}
:pubkey public-key :pubkey public-key
:text-event-map {root-child-id {:pubkey root-child-author :text-event-map {root-child-id {:pubkey root-child-author
:tags [[:e root-id-hex] :tags [[:e root-id-hex]
[:p (hexify root-author)]]} [:p (hexify root-author)]]}
root-id {:pubkey root-author root-id {:pubkey root-author
:tags []}}} :tags []}}})
text "message text" text "message text"
event (compose-text-event event-state "" text root-child-id) event (compose-text-event "" text root-child-id)
{:keys [pubkey created_at kind tags content id sig]} (second event) {:keys [pubkey created_at kind tags content id sig]} (second event)
now (quot (System/currentTimeMillis) 1000)] now (quot (System/currentTimeMillis) 1000)]
(should= "EVENT" (first event)) (should= "EVENT" (first event))
@ -286,12 +286,12 @@
root-id 7734 root-id 7734
root-id-hex (hexify root-id) root-id-hex (hexify root-id)
root-author 99 root-author 99
event-state {:keys {:private-key (bytes->hex-string private-key) _ (reset! (:event-context @ui-context) {:keys {:private-key (bytes->hex-string private-key)
:public-key (bytes->hex-string public-key)} :public-key (bytes->hex-string public-key)}
:text-event-map {root-id {:pubkey root-author :text-event-map {root-id {:pubkey root-author
:tags [[:p (hexify author)]]}} :tags [[:p (hexify author)]]}}
:pubkey public-key} :pubkey public-key})
event (compose-text-event event-state "" "message" root-id) event (compose-text-event "" "message" root-id)
{:keys [tags]} (second event)] {:keys [tags]} (second event)]
(should= [[:e root-id-hex "" "reply"] (should= [[:e root-id-hex "" "reply"]
@ -300,10 +300,10 @@
(it "composes a message with a slash." (it "composes a message with a slash."
(let [private-key (num->bytes 64 42) (let [private-key (num->bytes 64 42)
public-key (get-pub-key private-key) public-key (get-pub-key private-key)
event-state {:keys {:private-key (bytes->hex-string private-key) _ (reset! (:event-context @ui-context) {:keys {:private-key (bytes->hex-string private-key)
:public-key (bytes->hex-string public-key)}} :public-key (bytes->hex-string public-key)}})
text "message/text" text "message/text"
event (compose-text-event event-state "" text) event (compose-text-event "" text)
{:keys [pubkey created_at kind tags content id sig]} (second event) {:keys [pubkey created_at kind tags content id sig]} (second event)
now (quot (System/currentTimeMillis) 1000)] now (quot (System/currentTimeMillis) 1000)]
(should= "EVENT" (first event)) (should= "EVENT" (first event))
@ -321,11 +321,12 @@
(it "composes an simple contact list" (it "composes an simple contact list"
(let [private-key (num->bytes 64 42) (let [private-key (num->bytes 64 42)
public-key (get-pub-key private-key) public-key (get-pub-key private-key)
event-state {:keys {:private-key (bytes->hex-string private-key) event-context (:event-context @ui-context)
:public-key (bytes->hex-string public-key)}} _ (reset! event-context {:keys {:private-key (bytes->hex-string private-key)
:public-key (bytes->hex-string public-key)}})
contact-list [{:pubkey 1} contact-list [{:pubkey 1}
{:pubkey 2 :petname "petname"}] {:pubkey 2 :petname "petname"}]
event (compose-contact-list event-state contact-list) event (compose-contact-list contact-list)
{:keys [pubkey created_at kind tags content id sig]} (second event) {:keys [pubkey created_at kind tags content id sig]} (second event)
now (quot (System/currentTimeMillis) 1000) now (quot (System/currentTimeMillis) 1000)
] ]
@ -453,27 +454,27 @@
(emplace-references content tags)))) (emplace-references content tags))))
(it "adds an abbreviated profile name for an unamed pubkey" (it "adds an abbreviated profile name for an unamed pubkey"
(let [tags [[:e "blah"]] (let [tags [[:e "blah"]]
user-id 16r0123456789abcdef000000000000000000000000000000000000000000000000 user-id 16r0123456789abcdef000000000000000000000000000000000000000000000000
pubkey (num32->hex-string user-id) pubkey (num32->hex-string user-id)
profiles {} profiles {}
event-context (atom {:profiles profiles}) event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context}) _ (reset! ui-context {:event-context event-context})
content (str "hello @" pubkey ".")] content (str "hello @" pubkey ".")]
(should= ["hello #[1]." [[:e "blah"] [:p pubkey]]] (should= ["hello #[1]." [[:e "blah"] [:p pubkey]]]
(emplace-references content tags)) (emplace-references content tags))
(should= {user-id {:name "0123456789a-"}} (should= {user-id {:name "0123456789a-"}}
(:profiles @(:event-context @ui-context))))) (:profiles @(:event-context @ui-context)))))
(it "does not recognize pubkeys that aren't 32 bytes" (it "does not recognize pubkeys that aren't 32 bytes"
(let [tags [[:e "blah"]] (let [tags [[:e "blah"]]
profiles {} profiles {}
event-context (atom {:profiles profiles}) event-context (atom {:profiles profiles})
_ (reset! ui-context {:event-context event-context}) _ (reset! ui-context {:event-context event-context})
content "hello @01234567abc."] content "hello @01234567abc."]
(should= ["hello @01234567abc." [[:e "blah"]]] (should= ["hello @01234567abc." [[:e "blah"]]]
(emplace-references content tags)) (emplace-references content tags))
(should= {} (:profiles @(:event-context @ui-context))))))) (should= {} (:profiles @(:event-context @ui-context)))))))
(describe "fixing names" (describe "fixing names"
(it "should not fix a good name" (it "should not fix a good name"

View File

@ -81,9 +81,9 @@
event-context (atom {:user-configuration uc})] event-context (atom {:user-configuration uc})]
(reset! ui-context {:event-context event-context})) (reset! ui-context {:event-context event-context}))
(set-last-time-metadata-imported 99) (set-last-time-metadata-imported 99)
(let [event-context (:event-context @ui-context)]
(should= 99 (get-in @event-context (should= 99 (get-in (get-event-state)
[:user-configuration :import-metadata :last-time-imported]))) [:user-configuration :import-metadata :last-time-imported]))
) )
) )
) )

View File

@ -31,13 +31,13 @@
(swap! event-context assoc :send-chan send-chan :event-handler handler) (swap! event-context assoc :send-chan send-chan :event-handler handler)
(let [latest-old-message-time (let [latest-old-message-time
(if (not config/test-run?) (if (not config/test-run?)
(data-storage/read-in-last-n-days config/days-to-read event-context handler) (data-storage/read-in-last-n-days config/days-to-read handler)
(-> (System/currentTimeMillis) (quot 1000) (- 86400))) (-> (System/currentTimeMillis) (quot 1000) (- 86400)))
exit-condition (protocol/get-events event-context latest-old-message-time)] exit-condition (protocol/get-events latest-old-message-time)]
(when (not config/test-run?) (when (not config/test-run?)
(data-storage/write-configuration) (data-storage/write-configuration)
(data-storage/write-changed-days event-context)) (data-storage/write-changed-days))
(if (= exit-condition :relaunch) (if (= exit-condition :relaunch)
(do (do
(invoke-now (.dispose (:frame @ui-context))) (invoke-now (.dispose (:frame @ui-context)))

View File

@ -12,26 +12,26 @@
(defn write-configuration [] (defn write-configuration []
(let [event-context (:event-context @ui-context)]
(spit @config/profiles-filename (spit @config/profiles-filename
(with-out-str (with-out-str
(clojure.pprint/pprint (:profiles @event-context)))) (clojure.pprint/pprint (get-event-state :profiles))))
(spit @config/read-event-ids-filename (spit @config/read-event-ids-filename
(with-out-str (with-out-str
(clojure.pprint/pprint (:read-event-ids @event-context)))) (clojure.pprint/pprint (get-event-state :read-event-ids))))
(spit @config/relays-filename (spit @config/relays-filename
(with-out-str (with-out-str
(clojure.pprint/pprint (relays/relays-for-writing)))) (clojure.pprint/pprint (relays/relays-for-writing))))
(spit @config/tabs-list-filename (spit @config/tabs-list-filename
(with-out-str (with-out-str
(clojure.pprint/pprint (:tabs-list @event-context)))) (clojure.pprint/pprint (get-event-state :tabs-list))))
(spit @config/user-configuration-filename (spit @config/user-configuration-filename
(with-out-str (with-out-str
(clojure.pprint/pprint (:user-configuration @event-context)))) (clojure.pprint/pprint (get-event-state :user-configuration))))
(spit @config/contact-lists-filename (spit @config/contact-lists-filename
(with-out-str (with-out-str
(clojure.pprint/pprint (:contact-lists @event-context)))) (clojure.pprint/pprint (get-event-state :contact-lists))))
)) )
(defn write-messages [] (defn write-messages []
(let [event-context (:event-context @ui-context)] (let [event-context (:event-context @ui-context)]
@ -61,16 +61,16 @@
(swap! ui-context assoc :event-context event-context) (swap! ui-context assoc :event-context event-context)
(relays/load-relays-from-file @config/relays-filename))) (relays/load-relays-from-file @config/relays-filename)))
(defn load-events [old-events event-context handler] (defn load-events [old-events handler]
(doseq [event old-events] (doseq [event old-events]
(let [url (first (:relays event))] (let [url (first (:relays event))]
(swap! event-context events/add-event event url) (swap! (:event-context @ui-context) events/add-event event url)
(events/handle-text-event handler event)))) (events/handle-text-event handler event))))
(defn read-old-events [event-context handler] (defn read-old-events [handler]
(let [old-events (vals (read-string (slurp @config/messages-filename))) (let [old-events (vals (read-string (slurp @config/messages-filename)))
creation-times (map :created-at old-events)] creation-times (map :created-at old-events)]
(load-events old-events event-context handler) (load-events old-events handler)
(if (empty? creation-times) (if (empty? creation-times)
(-> (System/currentTimeMillis) (quot 1000) (- 86400)) (-> (System/currentTimeMillis) (quot 1000) (- 86400))
(apply max creation-times)))) (apply max creation-times))))
@ -106,11 +106,11 @@
(clojure.pprint/pprint (clojure.pprint/pprint
(second day-partition))))))))) (second day-partition)))))))))
(defn write-changed-days [event-context] (defn write-changed-days []
(let [days-changed (:days-changed @event-context) (let [days-changed (get-event-state :days-changed)
first-day-loaded (quot (:earliest-loaded-time @event-context) 86400) first-day-loaded (quot (get-event-state :earliest-loaded-time) 86400)
days-to-write (set (filter #(>= % first-day-loaded) days-changed)) days-to-write (set (filter #(>= % first-day-loaded) days-changed))
daily-partitions (partition-messages-by-day (:text-event-map @event-context)) daily-partitions (partition-messages-by-day (get-event-state :text-event-map))
changed-partitions (filter #(contains? days-to-write (first %)) daily-partitions)] changed-partitions (filter #(contains? days-to-write (first %)) daily-partitions)]
(write-messages-by-day changed-partitions))) (write-messages-by-day changed-partitions)))
@ -133,7 +133,7 @@
(defn is-message-file? [file-name] (defn is-message-file? [file-name]
(re-matches #"\d+\-\d+\w+\d+" file-name)) (re-matches #"\d+\-\d+\w+\d+" file-name))
(defn read-in-last-n-days [n event-context handler] (defn read-in-last-n-days [n handler]
(let [message-directory (clojure.java.io/file @config/messages-directory) (let [message-directory (clojure.java.io/file @config/messages-directory)
files (.listFiles message-directory) files (.listFiles message-directory)
file-names (for [file files] (.getName file)) file-names (for [file files] (.getName file))
@ -150,7 +150,7 @@
(doseq [file-name (reverse file-names)] (doseq [file-name (reverse file-names)]
(prn 'reading file-name) (prn 'reading file-name)
(let [old-events (read-string (slurp (str @config/messages-directory "/" file-name)))] (let [old-events (read-string (slurp (str @config/messages-directory "/" file-name)))]
(load-events old-events event-context handler))) (load-events old-events handler)))
(swap! event-context assoc :days-changed #{(quot last-time 86400)} :earliest-loaded-time first-time)) (swap! (:event-context @ui-context) assoc :days-changed #{(quot last-time 86400)} :earliest-loaded-time first-time))
last-time)) last-time))

View File

@ -105,7 +105,7 @@
fixed-name)))) fixed-name))))
(defn add-suffix-for-duplicate [pubkey name] (defn add-suffix-for-duplicate [pubkey name]
(let [profiles (:profiles @(:event-context @ui-context)) (let [profiles (get-event-state :profiles)
others-profiles (dissoc profiles pubkey) others-profiles (dissoc profiles pubkey)
profile-vals (vals others-profiles) profile-vals (vals others-profiles)
dups (filter #(= name (:name %)) profile-vals)] dups (filter #(= name (:name %)) profile-vals)]
@ -176,15 +176,15 @@
(prn 'get-references 'bad-tags-in-event (.getMessage e) event) (prn 'get-references 'bad-tags-in-event (.getMessage e) event)
[nil nil nil]))) [nil nil nil])))
(defn process-references [state event] (defn process-references [event-state event]
(let [[_ _ referent] (get-references event)] (let [[_ _ referent] (get-references event)]
(if (nil? referent) (if (nil? referent)
state event-state
(let [referent-path [:text-event-map referent]] (let [referent-path [:text-event-map referent]]
(if (nil? (get-in state referent-path)) (if (nil? (get-in event-state referent-path))
state event-state
(update-in (update-in
state event-state
(concat referent-path [:references]) (concat referent-path [:references])
conj (:id event))))))) conj (:id event)))))))
@ -256,8 +256,7 @@
event-state))))) event-state)))))
(defn get-root-of-thread [id] (defn get-root-of-thread [id]
(let [event-context (:event-context @ui-context) (let [messages (get-event-state :text-event-map)
messages (:text-event-map @event-context)
event (get messages id) event (get messages id)
[root _ _] (get-references event)] [root _ _] (get-references event)]
(if (some? root) root id))) (if (some? root) root id)))
@ -282,8 +281,8 @@
"Adds pubkey, created-at, id, and sig to the partially composed body, "Adds pubkey, created-at, id, and sig to the partially composed body,
which must include kind, tags, and content. The body is put into an which must include kind, tags, and content. The body is put into an
EVENT wrapper that is ready to send." EVENT wrapper that is ready to send."
[event-state body] [body]
(let [keys (:keys event-state) (let [keys (get-event-state :keys)
private-key (util/hex-string->bytes (:private-key keys)) private-key (util/hex-string->bytes (:private-key keys))
pubkey (util/hex-string->bytes (:public-key keys)) pubkey (util/hex-string->bytes (:public-key keys))
now (quot (System/currentTimeMillis) 1000) now (quot (System/currentTimeMillis) 1000)
@ -298,15 +297,15 @@
["EVENT" event]) ["EVENT" event])
) )
(defn compose-metadata-event [event-state] (defn compose-metadata-event []
(let [keys (:keys event-state) (let [keys (get-event-state :keys)
content (to-json {:name (:name keys) content (to-json {:name (:name keys)
:about (:about keys) :about (:about keys)
:picture (:picture keys)}) :picture (:picture keys)})
body {:kind 0 body {:kind 0
:tags [] :tags []
:content content}] :content content}]
(body->event event-state body)) (body->event body))
) )
(defn make-contact-list-tag [contact-entry] (defn make-contact-list-tag [contact-entry]
@ -317,13 +316,13 @@
(defn make-contact-list-tags [contact-list] (defn make-contact-list-tags [contact-list]
(map make-contact-list-tag contact-list)) (map make-contact-list-tag contact-list))
(defn compose-contact-list [event-state contact-list] (defn compose-contact-list [contact-list]
(let [tags (make-contact-list-tags contact-list) (let [tags (make-contact-list-tags contact-list)
body {:kind 3 body {:kind 3
:tags tags :tags tags
:content "more-speech contact list"} :content "more-speech contact list"}
] ]
(body->event event-state body)) (body->event body))
) )
(defn make-event-reference-tags (defn make-event-reference-tags
@ -337,10 +336,10 @@
[] []
[[:e (hexify reply-to) "" "reply"]]))) [[:e (hexify reply-to) "" "reply"]])))
(defn make-people-reference-tags [event-state pubkey reply-to-or-nil] (defn make-people-reference-tags [pubkey reply-to-or-nil]
(if (nil? reply-to-or-nil) (if (nil? reply-to-or-nil)
[] []
(let [event-map (:text-event-map event-state) (let [event-map (get-event-state :text-event-map)
parent-event-id reply-to-or-nil parent-event-id reply-to-or-nil
parent-event (get event-map parent-event-id) parent-event (get event-map parent-event-id)
parent-tags (:tags parent-event) parent-tags (:tags parent-event)
@ -356,11 +355,11 @@
[[:subject subject]])) [[:subject subject]]))
(defn get-reply-root [event-state reply-to-or-nil] (defn get-reply-root [reply-to-or-nil]
(if (nil? reply-to-or-nil) (if (nil? reply-to-or-nil)
nil nil
(let [reply-id reply-to-or-nil (let [reply-id reply-to-or-nil
event-map (:text-event-map event-state) event-map (get-event-state :text-event-map)
replied-to-event (get event-map reply-id) replied-to-event (get event-map reply-id)
[root _mentions _referent] (get-references replied-to-event)] [root _mentions _referent] (get-references replied-to-event)]
root))) root)))
@ -369,7 +368,7 @@
(let [pet-pubkey (contact-list/get-pubkey-from-petname user-name)] (let [pet-pubkey (contact-list/get-pubkey-from-petname user-name)]
(if (some? pet-pubkey) (if (some? pet-pubkey)
pet-pubkey pet-pubkey
(let [profiles (:profiles @(:event-context @ui-context))] (let [profiles (get-event-state :profiles)]
(loop [pairs (vec profiles)] (loop [pairs (vec profiles)]
(if (empty? pairs) (if (empty? pairs)
nil nil
@ -423,36 +422,36 @@
)) ))
(defn compose-text-event (defn compose-text-event
([event-state subject text] ([subject text]
(compose-text-event event-state subject text nil)) (compose-text-event subject text nil))
([event-state subject text reply-to-or-nil] ([subject text reply-to-or-nil]
(let [pubkey (:pubkey event-state) (let [pubkey (get-event-state :pubkey)
root (get-reply-root event-state reply-to-or-nil) root (get-reply-root reply-to-or-nil)
tags (concat (make-event-reference-tags reply-to-or-nil root) tags (concat (make-event-reference-tags reply-to-or-nil root)
(make-people-reference-tags event-state pubkey reply-to-or-nil) (make-people-reference-tags pubkey reply-to-or-nil)
(make-subject-tag subject) (make-subject-tag subject)
[[:client (str "more-speech - " config/version)]]) [[:client (str "more-speech - " config/version)]])
[content tags] (emplace-references text tags) [content tags] (emplace-references text tags)
body {:kind 1 body {:kind 1
:tags tags :tags tags
:content content}] :content content}]
(body->event event-state body)))) (body->event body))))
(defn send-event [event-state event] (defn send-event [event]
(let [send-chan (:send-chan event-state)] (let [send-chan (get-event-state :send-chan)]
(async/>!! send-chan [:event event]))) (async/>!! send-chan [:event event])))
(defn compose-and-send-text-event [event-state source-event-or-nil subject message] (defn compose-and-send-text-event [source-event-or-nil subject message]
(let [reply-to-or-nil (:id source-event-or-nil) (let [reply-to-or-nil (:id source-event-or-nil)
event (compose-text-event event-state subject message reply-to-or-nil)] event (compose-text-event subject message reply-to-or-nil)]
(send-event event-state event))) (send-event event)))
(defn compose-and-send-metadata-event [event-state] (defn compose-and-send-metadata-event []
(send-event event-state (compose-metadata-event event-state))) (send-event (compose-metadata-event)))
(defn compose-and-send-contact-list [event-state contact-list] (defn compose-and-send-contact-list [contact-list]
(send-event event-state (compose-contact-list event-state contact-list))) (send-event (compose-contact-list contact-list)))

View File

@ -1,6 +1,7 @@
(ns more-speech.nostr.protocol (ns more-speech.nostr.protocol
(:require [clojure.data.json :as json] (:require [clojure.data.json :as json]
[clojure.core.async :as async] [clojure.core.async :as async]
[more-speech.ui.swing.ui-context :refer :all]
[more-speech.nostr.events :as events] [more-speech.nostr.events :as events]
[more-speech.nostr.relays :refer [relays]] [more-speech.nostr.relays :refer [relays]]
[more-speech.nostr.util :as util] [more-speech.nostr.util :as util]
@ -58,17 +59,17 @@
:tags (get event "tags") :tags (get event "tags")
:content (get event "content")}))) :content (get event "content")})))
(defn record-and-display-event [_agent event-context envelope url] (defn record-and-display-event [_agent envelope url]
(try (try
(let [[_name _subscription-id inner-event :as _decoded-msg] envelope (let [[_name _subscription-id inner-event :as _decoded-msg] envelope
event (events/translate-event inner-event) event (events/translate-event inner-event)
id (:id event) id (:id event)
computed-id (compute-id inner-event) computed-id (compute-id inner-event)
ui-handler (:event-handler @event-context) ui-handler (get-event-state :event-handler)
dup? (contains? (:text-event-map @event-context) id)] dup? (contains? (get-event-state :text-event-map) id)]
(if (= id computed-id) (if (= id computed-id)
(do (do
(swap! event-context events/process-event event url) (swap! (:event-context @ui-context) events/process-event event url)
(when (and (not dup?) (= (:kind event) 1)) (when (and (not dup?) (= (:kind event) 1))
(events/handle-text-event ui-handler event) (events/handle-text-event ui-handler event)
)) ))
@ -81,18 +82,18 @@
(def event-agent (agent nil)) (def event-agent (agent nil))
(defn handle-text [{:keys [buffer event-context url]} data last] (defn handle-text [{:keys [buffer url]} data last]
(.append buffer (.toString data)) (.append buffer (.toString data))
(when last (when last
(try (try
(let [envelope (json/read-str (.toString buffer))] (let [envelope (json/read-str (.toString buffer))]
(send event-agent record-and-display-event event-context envelope url)) (send event-agent record-and-display-event envelope url))
(catch Exception e (catch Exception e
(prn 'onText url (.getMessage e)) (prn 'onText url (.getMessage e))
(prn (.toString buffer)))) (prn (.toString buffer))))
(.delete buffer 0 (.length buffer)))) (.delete buffer 0 (.length buffer))))
(defrecord listener [buffer event-context url] (defrecord listener [buffer url]
WebSocket$Listener WebSocket$Listener
(onOpen [_this _webSocket] (onOpen [_this _webSocket]
(prn 'open url)) (prn 'open url))
@ -112,11 +113,11 @@
(prn 'websocket-listener-error url error)) (prn 'websocket-listener-error url error))
) )
(defn connect-to-relay ^WebSocket [url event-context] (defn connect-to-relay ^WebSocket [url]
(try (try
(let [client (HttpClient/newHttpClient) (let [client (HttpClient/newHttpClient)
cl (.newWebSocketBuilder client) cl (.newWebSocketBuilder client)
cws (.buildAsync cl (URI/create url) (->listener (StringBuffer.) event-context url)) cws (.buildAsync cl (URI/create url) (->listener (StringBuffer.) url))
wsf (future (.get cws)) wsf (future (.get cws))
ws (deref wsf 1000 :time-out)] ws (deref wsf 1000 :time-out)]
(if (= ws :time-out) (if (= ws :time-out)
@ -141,12 +142,12 @@
(when-let [conn (get-in @relays [url :connection])] (when-let [conn (get-in @relays [url :connection])]
(.sendPing conn (ByteBuffer/allocate 4))))) (.sendPing conn (ByteBuffer/allocate 4)))))
(defn connect-to-relays [event-context] (defn connect-to-relays []
(doseq [url (keys @relays)] (doseq [url (keys @relays)]
(let [relay (get @relays url) (let [relay (get @relays url)
should-connect? (or (:read relay) (:write relay)) should-connect? (or (:read relay) (:write relay))
connection (if should-connect? connection (if should-connect?
(connect-to-relay url event-context) (connect-to-relay url)
nil)] nil)]
(when (some? connection) (when (some? connection)
(swap! relays assoc-in [url :connection] connection)))) (swap! relays assoc-in [url :connection] connection))))
@ -182,8 +183,8 @@
(subscribe conn id date) (subscribe conn id date)
(swap! relays assoc-in [url :subscribed] true)))))) (swap! relays assoc-in [url :subscribed] true))))))
(defn process-send-channel [event-context] (defn process-send-channel []
(let [send-chan (:send-chan @event-context) (let [send-chan (get-event-state :send-chan)
urls (keys @relays) urls (keys @relays)
send-urls (filter #(:write (get @relays %)) urls) send-urls (filter #(:write (get @relays %)) urls)
send-connections (map #(get-in @relays [% :connection]) send-urls) send-connections (map #(get-in @relays [% :connection]) send-urls)
@ -204,13 +205,13 @@
(prn 'closing url) (prn 'closing url)
(close-connection conn id))))) (close-connection conn id)))))
(defn get-events [event-context subscription-time] (defn get-events [subscription-time]
(let [subscription-id "more-speech" (let [subscription-id "more-speech"
metadata-request-id "more-speech-metadata" metadata-request-id "more-speech-metadata"
contact-lists-request-id "more-speech-contact-lists" contact-lists-request-id "more-speech-contact-lists"
event-handler (:event-handler @event-context) event-handler (get-event-state :event-handler)
now-in-seconds (quot (System/currentTimeMillis) 1000)] now-in-seconds (quot (System/currentTimeMillis) 1000)]
(connect-to-relays event-context) (connect-to-relays)
(request-contact-lists-from-relays contact-lists-request-id) (request-contact-lists-from-relays contact-lists-request-id)
(when (user-configuration/should-import-metadata? now-in-seconds) (when (user-configuration/should-import-metadata? now-in-seconds)
(request-metadata-from-relays metadata-request-id) (request-metadata-from-relays metadata-request-id)
@ -220,9 +221,9 @@
(if (user-configuration/should-export-profile? now-in-seconds) (if (user-configuration/should-export-profile? now-in-seconds)
(do (do
(user-configuration/set-last-time-profile-exported now-in-seconds) (user-configuration/set-last-time-profile-exported now-in-seconds)
(future (events/compose-and-send-metadata-event @event-context))) (future (events/compose-and-send-metadata-event)))
(println "Not time to export profile yet.")) (println "Not time to export profile yet."))
(let [exit-condition (process-send-channel event-context)] (let [exit-condition (process-send-channel)]
(unsubscribe-from-relays subscription-id) (unsubscribe-from-relays subscription-id)
(Thread/sleep 100) (Thread/sleep 100)
(prn 'done) (prn 'done)

View File

@ -13,8 +13,7 @@
(format-user-id user-id 20)) (format-user-id user-id 20))
([user-id length] ([user-id length]
(let [event-context (:event-context @ui-context) (let [profiles (get-event-state :profiles)]
profiles (:profiles @event-context)]
(if (nil? user-id) (if (nil? user-id)
"" ""
(let [trusted? (contact-list/is-trusted? user-id) (let [trusted? (contact-list/is-trusted? user-id)
@ -40,7 +39,7 @@
(str "(" (abbreviate profile-name (- length 2)) ")"))))))) (str "(" (abbreviate profile-name (- length 2)) ")")))))))
(defn lookup-reference [event reference] (defn lookup-reference [event reference]
(let [profiles (:profiles @(:event-context @ui-context)) (let [profiles (get-event-state :profiles)
ref-string (re-find #"\d+" reference) ref-string (re-find #"\d+" reference)
index (Integer/parseInt ref-string) index (Integer/parseInt ref-string)
tags (:tags event)] tags (:tags event)]

View File

@ -24,7 +24,7 @@
(defn id-click [e] (defn id-click [e]
(if (.isPopupTrigger e) (if (.isPopupTrigger e)
(copy-click e) (copy-click e)
(article-tree-util/id-click ui-context (config e :user-data)))) (article-tree-util/id-click (config e :user-data))))
(defn make-article-info-panel [] (defn make-article-info-panel []
(let [author-name-label (label :id :author-name-label) (let [author-name-label (label :id :author-name-label)
@ -97,9 +97,8 @@
:center (flow-panel :items [reply-button create-button])))) :center (flow-panel :items [reply-button create-button]))))
(defn load-article-info [selected-id] (defn load-article-info [selected-id]
(let [event-state @(:event-context @ui-context) (let [main-frame (:frame @ui-context)
main-frame (:frame @ui-context) text-map (get-event-state :text-event-map)
text-map (:text-event-map event-state)
event (get text-map selected-id) event (get text-map selected-id)
[root-id _ referent] (events/get-references event) [root-id _ referent] (events/get-references event)
reply-to (select main-frame [:#reply-to-label]) reply-to (select main-frame [:#reply-to-label])

View File

@ -52,8 +52,7 @@
(defn trust-this-author [event _e] (defn trust-this-author [event _e]
(let [his-pubkey (:pubkey event) (let [his-pubkey (:pubkey event)
event-context (:event-context @ui-context) profiles (get-event-state :profiles)
profiles (:profiles @event-context)
profile (get profiles his-pubkey) profile (get profiles his-pubkey)
petname (input "Name this author" petname (input "Name this author"
:value (:name profile) :value (:name profile)
@ -67,11 +66,10 @@
path (.getPathForLocation tree (.getX e) (.getY e)) path (.getPathForLocation tree (.getX e) (.getY e))
node (.getLastPathComponent path) node (.getLastPathComponent path)
event-id (.getUserObject ^DefaultMutableTreeNode node) event-id (.getUserObject ^DefaultMutableTreeNode node)
event-context (:event-context @ui-context) event-map (get-event-state :text-event-map)
event-map (:text-event-map @event-context)
event (get event-map event-id) event (get event-map event-id)
public-key (:pubkey event) public-key (:pubkey event)
tab-names (vec (remove #(= "all" %) (map :name (:tabs-list @event-context)))) tab-names (vec (remove #(= "all" %) (map :name (get-event-state :tabs-list))))
tab-names (conj tab-names "<new-tab>") tab-names (conj tab-names "<new-tab>")
add-author-actions (map #(action :name % :handler (partial add-author-to-tab public-key %)) tab-names) add-author-actions (map #(action :name % :handler (partial add-author-to-tab public-key %)) tab-names)
block-author-actions (map #(action :name % :handler (partial block-author-from-tab public-key %)) tab-names) block-author-actions (map #(action :name % :handler (partial block-author-from-tab public-key %)) tab-names)
@ -106,13 +104,11 @@
(defn render-event [widget info] (defn render-event [widget info]
(if (seqable? (:value info)) (if (seqable? (:value info))
(text! widget "Articles") (text! widget "Articles")
(let [event-context (:event-context @ui-context) (let [event-map (get-event-state :text-event-map)
event-state @event-context
event-map (:text-event-map event-state)
node (:value info) node (:value info)
event-id (.getUserObject node) event-id (.getUserObject node)
event (get event-map event-id) event (get event-map event-id)
read? (contains? (:read-event-ids @event-context) event-id) read? (contains? (get-event-state :read-event-ids) event-id)
font (if read? config/default-font config/bold-font)] font (if read? config/default-font config/bold-font)]
(config! widget :font font) (config! widget :font font)
(text! widget (formatters/format-header event))))) (text! widget (formatters/format-header event)))))
@ -222,10 +218,9 @@
(defn add-event [event] (defn add-event [event]
(let [frame (:frame @ui-context) (let [frame (:frame @ui-context)
event-state @(:event-context @ui-context) event-map (get-event-state :text-event-map)
event-map (:text-event-map event-state)
event-id (:id event)] event-id (:id event)]
(loop [tabs (:tabs-list event-state) (loop [tabs (get-event-state :tabs-list)
index 0] index 0]
(if (empty? tabs) (if (empty? tabs)
nil nil

View File

@ -51,7 +51,7 @@
(.setSelectionPath tree tree-path) (.setSelectionPath tree tree-path)
(.scrollPathToVisible tree tree-path))) (.scrollPathToVisible tree tree-path)))
(defn id-click [ui-context id] (defn id-click [id]
(let [frame (:frame @ui-context) (let [frame (:frame @ui-context)
tab-index (:selected-tab @ui-context) tab-index (:selected-tab @ui-context)
tab-selector (keyword (str "#" tab-index)) tab-selector (keyword (str "#" tab-index))
@ -89,10 +89,10 @@
(defn go-back-by [n] (defn go-back-by [n]
(let [event-context (:event-context @ui-context) (let [event-context (:event-context @ui-context)
event-history (:event-history @event-context)] event-history (get-event-state :event-history)]
(when-not (empty? event-history) (when-not (empty? event-history)
(swap! event-context adjust-back-count n) (swap! event-context adjust-back-count n)
(let [back-count (:back-count @event-context) (let [back-count (get-event-state :back-count)
event-position (- (count event-history) back-count 1) event-position (- (count event-history) back-count 1)
[tab-index event-id] (nth event-history event-position)] [tab-index event-id] (nth event-history event-position)]
(display-event tab-index event-id))))) (display-event tab-index event-id)))))

View File

@ -7,8 +7,6 @@
(defn make-edit-window [kind] (defn make-edit-window [kind]
(let [reply? (= kind :reply) (let [reply? (= kind :reply)
event-context (:event-context @ui-context)
event-state @event-context
subject-label (label "Subject:") subject-label (label "Subject:")
subject-text (text :id :subject :text "") subject-text (text :id :subject :text "")
subject-panel (left-right-split subject-label subject-text subject-panel (left-right-split subject-label subject-text
@ -19,8 +17,8 @@
edit-area (styled-text :font config/default-font edit-area (styled-text :font config/default-font
:wrap-lines? true) :wrap-lines? true)
send-button (button :text "Send") send-button (button :text "Send")
event-map (:text-event-map event-state) event-map (get-event-state :text-event-map)
selected-id (if reply? (:selected-event @event-context) nil) selected-id (if reply? (get-event-state :selected-event) nil)
event (if reply? (get event-map selected-id) nil)] event (if reply? (get event-map selected-id) nil)]
(when (or (not reply?) (when (or (not reply?)
(and reply? (some? event))) (and reply? (some? event)))
@ -32,7 +30,7 @@
(fn [_] (fn [_]
(let [message (text edit-area) (let [message (text edit-area)
subject (text subject-text)] subject (text subject-text)]
(events/compose-and-send-text-event event-state event subject message)) (events/compose-and-send-text-event event subject message))
(dispose! edit-frame))) (dispose! edit-frame)))
(text! edit-area (text! edit-area
(if reply? (if reply?

View File

@ -17,7 +17,7 @@
(handle-text-event [_handler event] (handle-text-event [_handler event]
(invoke-now (article-tree/add-event event))) (invoke-now (article-tree/add-event event)))
(update-relay-panel [_handler] (update-relay-panel [_handler]
(invoke-later (relay-panel/update-relay-panel ui-context)))) (invoke-later (relay-panel/update-relay-panel))))
(defn open-link [e] (defn open-link [e]
(when (= HyperlinkEvent$EventType/ACTIVATED (.getEventType e)) (when (= HyperlinkEvent$EventType/ACTIVATED (.getEventType e))
@ -33,8 +33,7 @@
) )
(defn make-main-window [] (defn make-main-window []
(let [event-context @(:event-context @ui-context) (let [title (str "More-Speech:" (:name (get-event-state :keys)) " - " config/version)
title (str "More-Speech:" (:name (:keys event-context)) " - " config/version)
main-frame (frame :title title :size [1500 :by 1000]) main-frame (frame :title title :size [1500 :by 1000])
_ (swap! ui-context assoc :frame main-frame) _ (swap! ui-context assoc :frame main-frame)
article-area (article-panel/make-article-area) article-area (article-panel/make-article-area)
@ -56,8 +55,7 @@
(listen main-frame :window-closing (listen main-frame :window-closing
(fn [_] (fn [_]
(.stop timer) (.stop timer)
(let [event-context (:event-context @ui-context) (let [send-chan (get-event-state :send-chan)]
send-chan (:send-chan @event-context)]
(future (async/>!! send-chan [:closed]))) (future (async/>!! send-chan [:closed])))
(.dispose main-frame))) (.dispose main-frame)))
(show! main-frame) (show! main-frame)

View File

@ -1,6 +1,7 @@
(ns more-speech.ui.swing.relay-panel (ns more-speech.ui.swing.relay-panel
(:use [seesaw core]) (:use [seesaw core])
(:require [more-speech.nostr.relays :refer [relays]])) (:require [more-speech.nostr.relays :refer [relays]]
[more-speech.ui.swing.ui-context :refer :all]))
(defn make-relay-control-panel [relay-url] (defn make-relay-control-panel [relay-url]
(let [relay (get @relays relay-url) (let [relay (get @relays relay-url)
@ -22,7 +23,7 @@
relay-panel (vertical-panel :items relay-control-panels :id :relay-panel :user-data panel-map)] relay-panel (vertical-panel :items relay-control-panels :id :relay-panel :user-data panel-map)]
relay-panel)) relay-panel))
(defn update-relay-panel [ui-context] (defn update-relay-panel []
(let [frame (:frame @ui-context) (let [frame (:frame @ui-context)
relay-panel (select frame [:#relay-panel]) relay-panel (select frame [:#relay-panel])
panel-map (config relay-panel :user-data)] panel-map (config relay-panel :user-data)]

View File

@ -9,3 +9,11 @@
(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?)))
(defn get-event-state
([]
@(:event-context @ui-context))
([field]
(get @(:event-context @ui-context) field))
)

View File

@ -10,7 +10,7 @@
(defn get-tab-index [name] (defn get-tab-index [name]
(loop [tab-list (:tabs-list @(:event-context @ui-context)) (loop [tab-list (get-event-state :tabs-list)
index 0] index 0]
(cond (cond
(empty? tab-list) nil (empty? tab-list) nil
@ -28,7 +28,7 @@
(selection! tabbed-panel tab-index))) (selection! tabbed-panel tab-index)))
(defn change-tabs-list-name [old-name new-name] (defn change-tabs-list-name [old-name new-name]
(loop [tabs-list (:tabs-list @(:event-context @ui-context)) (loop [tabs-list (get-event-state :tabs-list)
new-tabs-list []] new-tabs-list []]
(cond (cond
(empty? tabs-list) (empty? tabs-list)
@ -43,7 +43,7 @@
(conj new-tabs-list (first tabs-list)))))) (conj new-tabs-list (first tabs-list))))))
(defn delete-tab-from-tabs-list [tab-name] (defn delete-tab-from-tabs-list [tab-name]
(loop [tabs-list (:tabs-list @(:event-context @ui-context)) (loop [tabs-list (get-event-state :tabs-list)
new-tabs-list []] new-tabs-list []]
(cond (cond
(empty? tabs-list) (empty? tabs-list)
@ -57,13 +57,13 @@
(conj new-tabs-list (first tabs-list)))))) (conj new-tabs-list (first tabs-list))))))
(defn add-tab-to-tabs-list [tab-name] (defn add-tab-to-tabs-list [tab-name]
(let [tabs-list (:tabs-list @(:event-context @ui-context)) (let [tabs-list (get-event-state :tabs-list)
new-tabs-list (conj tabs-list {:name tab-name :selected [] :blocked []})] new-tabs-list (conj tabs-list {:name tab-name :selected [] :blocked []})]
(swap! (:event-context @ui-context) assoc :tabs-list new-tabs-list))) (swap! (:event-context @ui-context) assoc :tabs-list new-tabs-list)))
(defn add-id-to-tab [tab-name key id] (defn add-id-to-tab [tab-name key id]
(loop [tabs-list (:tabs-list @(:event-context @ui-context)) (loop [tabs-list (get-event-state :tabs-list)
new-tabs-list []] new-tabs-list []]
(cond (cond
(empty? tabs-list) (empty? tabs-list)
@ -85,7 +85,7 @@
tab-name)) tab-name))
(defn relaunch [] (defn relaunch []
(let [send-chan (:send-chan @(:event-context @ui-context))] (let [send-chan (get-event-state :send-chan)]
(future (async/>!! send-chan [:relaunch])))) (future (async/>!! send-chan [:relaunch]))))
(defn- get-clipboard [] (defn- get-clipboard []

View File

@ -30,7 +30,7 @@
(quot (.lastModified keys-file) 1000))) (quot (.lastModified keys-file) 1000)))
(defn should-export-profile? [now-in-seconds] (defn should-export-profile? [now-in-seconds]
(let [user-configuration (:user-configuration @(:event-context @ui-context)) (let [user-configuration (get-event-state :user-configuration)
xad (get-in user-configuration [:export-user-profile :export-after-days]) xad (get-in user-configuration [:export-user-profile :export-after-days])
lte (get-in user-configuration [:export-user-profile :last-time-exported])] lte (get-in user-configuration [:export-user-profile :last-time-exported])]
(and (number? xad) (and (number? xad)
@ -46,7 +46,7 @@
export-time))) export-time)))
(defn should-import-metadata? [now-in-seconds] (defn should-import-metadata? [now-in-seconds]
(let [user-configuration (:user-configuration @(:event-context @ui-context)) (let [user-configuration (get-event-state :user-configuration)
iad (get-in user-configuration [:import-metadata :import-after-days]) iad (get-in user-configuration [:import-metadata :import-after-days])
lti (get-in user-configuration [:import-metadata :last-time-imported])] lti (get-in user-configuration [:import-metadata :last-time-imported])]
(and (number? iad) (and (number? iad)