diff --git a/spec/more_speech/db/in_memory_spec.clj b/spec/more_speech/db/in_memory_spec.clj new file mode 100644 index 0000000..c7c7eca --- /dev/null +++ b/spec/more_speech/db/in_memory_spec.clj @@ -0,0 +1,15 @@ +(ns more-speech.db.in-memory-spec + (:require [speclj.core :refer :all] + [more-speech.db.gateway :as gateway] + [more-speech.db.in-memory :as in-memory])) + +(declare db) +(describe "in-memory database" + (with db (in-memory/get-db)) + (before (in-memory/clear-db @db)) + + (it "checks for existing event" + (gateway/add-event @db 1 {:content "event"}) + (should (gateway/event-exists? @db 1)) + (should-not (gateway/event-exists? @db 2))) + ) diff --git a/spec/more_speech/user_configuration_spec.clj b/spec/more_speech/user_configuration_spec.clj index 3e33fa6..31a05c4 100644 --- a/spec/more_speech/user_configuration_spec.clj +++ b/spec/more_speech/user_configuration_spec.clj @@ -5,6 +5,17 @@ (describe "user configuration" (with-stubs) + (context "basic functions" + (before (reset! ui-context {:event-context (atom {})})) + (it "sets and gets a config" + (set-config :test "test") + (should= "test" (get-config :test))) + + (it "sets and gets a nested config" + (set-config [:test :nest] "nest") + (should= "nest" (get-config [:test :nest]))) + ) + (context "export user profile" (it "creates if empty" (let [uc (validate {})] diff --git a/src/more_speech/nostr/event_handlers.clj b/src/more_speech/nostr/event_handlers.clj index c5f10dc..95cc669 100644 --- a/src/more_speech/nostr/event_handlers.clj +++ b/src/more_speech/nostr/event_handlers.clj @@ -60,14 +60,12 @@ (defn add-event [db event urls] (let [id (:id event) - time (:created-at event) - event-atom (:data db)] + time (:created-at event)] (when-not (gateway/event-exists? db id) (gateway/add-event db id event) (add-cross-reference db event) - (swap! event-atom update :days-changed conj (quot time 86400))) - (gateway/add-relays-to-event db id urls) - )) + (set-mem :days-changed (conj (get-mem :days-changed) (quot time 86400)))) + (gateway/add-relays-to-event db id urls))) (defn process-text-event [db event url] (add-event db event [url]) diff --git a/src/more_speech/user_configuration.clj b/src/more_speech/user_configuration.clj index af9e0ef..7edca3f 100644 --- a/src/more_speech/user_configuration.clj +++ b/src/more_speech/user_configuration.clj @@ -2,6 +2,21 @@ (:require [more-speech.ui.swing.ui-context :refer :all] [more-speech.config :as config])) +(defn get-config + ([] + (get-mem :user-configuration)) + ([tag] + (get-config tag nil)) + ([tag default] + (if (coll? tag) + (get-in (get-config) tag default) + (get (get-mem :user-configuration) tag default)))) + +(defn set-config [tag value] + (if (coll? tag) + (swap! (get-mem) assoc-in (concat [:user-configuration] tag) value) + (swap! (get-mem) assoc-in [:user-configuration tag] value))) + (defn validate-export-user-profile [user-configuration] (let [xup (get user-configuration :export-user-profile {}) xad (get xup :export-after-days 7) @@ -22,48 +37,37 @@ (defn validate [user-configuration] (-> user-configuration validate-export-user-profile - validate-import-metadata) - ) + validate-import-metadata)) (defn keys-last-modified [] (let [keys-file (clojure.java.io/file @config/keys-filename)] (quot (.lastModified keys-file) 1000))) (defn should-export-profile? [now-in-seconds] - (let [user-configuration (get-event-state :user-configuration) - xad (get-in user-configuration [:export-user-profile :export-after-days]) - lte (get-in user-configuration [:export-user-profile :last-time-exported])] + (let [xad (get-config [:export-user-profile :export-after-days]) + lte (get-config [:export-user-profile :last-time-exported])] (and (number? xad) (or (>= now-in-seconds (+ lte (* 86400 xad))) (>= (keys-last-modified) lte)))) ) (defn set-last-time-profile-exported [export-time] - (let [event-context (:event-context @ui-context)] - (swap! event-context - assoc-in - [:user-configuration :export-user-profile :last-time-exported] - export-time))) + (set-config [:export-user-profile :last-time-exported] export-time)) (defn should-import-metadata? [now-in-seconds] - (let [user-configuration (get-event-state :user-configuration) - iad (get-in user-configuration [:import-metadata :import-after-days]) - lti (get-in user-configuration [:import-metadata :last-time-imported])] + (let [iad (get-config [:import-metadata :import-after-days]) + lti (get-config [:import-metadata :last-time-imported])] (and (number? iad) (>= now-in-seconds (+ lti (* 86400 iad)))))) (defn set-last-time-metadata-imported [import-time] - (let [event-context (:event-context @ui-context)] - (swap! event-context - assoc-in - [:user-configuration :import-metadata :last-time-imported] - import-time))) + (set-config [:import-metadata :last-time-imported] import-time)) -(defn get-default-font[] - (get (get-event-state :user-configuration) :default-font config/default-font)) +(defn get-default-font [] + (get-config :default-font config/default-font)) (defn get-bold-font [] - (get (get-event-state :user-configuration) :bold-font config/bold-font)) + (get-config :bold-font config/bold-font)) (defn get-small-font [] - (get (get-event-state :user-configuration) :small-font config/small-font)) \ No newline at end of file + (get-config :small-font config/small-font)) \ No newline at end of file