use db in contact-list-spec

This commit is contained in:
Robert C. Martin 2023-01-25 10:49:49 -06:00
parent 3d417324c9
commit 1a204e75ca
5 changed files with 126 additions and 125 deletions

View File

@ -8,110 +8,107 @@
(defn hexify [n] (util/num32->hex-string n))
(describe "Processing Contact List events (Kind 3)"
(it "processes and saves a full event"
(let [db {:data (atom {}) ::gateway/type ::in-memory/type}
pubkey 99
contact1 1
contact2 2
event {:pubkey pubkey
:tags [[:p (hexify contact1) "relay1" "contact1"]
[:p (hexify contact2) "relay2" "contact2"]]}
_ (process-contact-list db event)
state @(:data db)]
(should= {:contact-lists
{99 [{:pubkey 1, :relay "relay1", :petname "contact1"}
{:pubkey 2, :relay "relay2", :petname "contact2"}]}}
state)))
(declare db)
(it "unpacks a full event"
(let [pubkey 99
contact1 1
contact2 2
event {:pubkey pubkey
:tags [[:p (hexify contact1) "relay1" "contact1"]
[:p (hexify contact2) "relay2" "contact2"]]}]
(should= [pubkey
[{:pubkey contact1
:relay "relay1"
:petname "contact1"}
{:pubkey contact2
:relay "relay2"
:petname "contact2"}]]
(unpack-contact-list-event event))))
(describe "contact-lists"
(with db (in-memory/get-db))
(before (clear-mem)
(in-memory/clear-db @db))
(it "unpacks a partial event"
(let [pubkey 99
contact1 1
contact2 2
event {:pubkey pubkey
:tags [[:p (hexify contact1)]
[:p (hexify contact2)]]}]
(should= [pubkey
[{:pubkey contact1
:relay nil
:petname nil}
{:pubkey contact2
:relay nil
:petname nil}]]
(unpack-contact-list-event event))))
(describe "Processing Contact List events (Kind 3)"
(it "processes and saves a full event"
(let [pubkey 99
contact1 1
contact2 2
event {:pubkey pubkey
:tags [[:p (hexify contact1) "relay1" "contact1"]
[:p (hexify contact2) "relay2" "contact2"]]}
_ (process-contact-list @db event)]
(should= [{:pubkey 1, :relay "relay1", :petname "contact1"}
{:pubkey 2, :relay "relay2", :petname "contact2"}]
(gateway/get-contacts @db pubkey))))
(it "properly skips a malformed tag"
(let [pubkey 99
contact2 2
event {:pubkey pubkey
:tags [[:p "garbage"]
[:p (hexify contact2)]]}]
(should= [pubkey
[{:pubkey contact2
:relay nil
:petname nil}]]
(unpack-contact-list-event event))))
(it "unpacks a full event"
(let [pubkey 99
contact1 1
contact2 2
event {:pubkey pubkey
:tags [[:p (hexify contact1) "relay1" "contact1"]
[:p (hexify contact2) "relay2" "contact2"]]}]
(should= [pubkey
[{:pubkey contact1
:relay "relay1"
:petname "contact1"}
{:pubkey contact2
:relay "relay2"
:petname "contact2"}]]
(unpack-contact-list-event event))))
(it "unpacks a partial event"
(let [pubkey 99
contact1 1
contact2 2
event {:pubkey pubkey
:tags [[:p (hexify contact1)]
[:p (hexify contact2)]]}]
(should= [pubkey
[{:pubkey contact1
:relay nil
:petname nil}
{:pubkey contact2
:relay nil
:petname nil}]]
(unpack-contact-list-event event))))
(it "properly skips a malformed tag"
(let [pubkey 99
contact2 2
event {:pubkey pubkey
:tags [[:p "garbage"]
[:p (hexify contact2)]]}]
(should= [pubkey
[{:pubkey contact2
:relay nil
:petname nil}]]
(unpack-contact-list-event event))))
)
(describe "Determining trust"
(it "determines if a pubkey is trusted"
(set-mem :pubkey 1)
(gateway/add-contacts @db 1 [{:pubkey 2}])
(should (is-trusted? 1))
(should (is-trusted? 2))
(should-not (is-trusted? 3)))
(it "determines second degree trust"
(let [my-pubkey 99
trusted-user 1
trusted-by-trusted-user 2]
(set-mem :pubkey my-pubkey)
(gateway/add-profile @db trusted-user {:name "trusted"})
(gateway/add-profile @db trusted-by-trusted-user {:name "second-degree"})
(gateway/add-contacts @db my-pubkey [{:pubkey trusted-user}])
(gateway/add-contacts @db trusted-user [{:pubkey trusted-by-trusted-user}])
(should= trusted-user (trusted-by-contact trusted-by-trusted-user))
))
(it "gets my petname for a trusted user"
(set-mem :pubkey 1)
(gateway/add-contacts @db 1 [{:pubkey 2 :petname "two"}
{:pubkey 3}])
(should= "two" (get-petname 2))
(should= nil (get-petname 3))
(should= nil (get-petname 4)))
(it "gets the pubkey for a trusted pet name"
(set-mem :pubkey 1)
(gateway/add-contacts @db 1 [{:pubkey 2 :petname "two"}
{:pubkey 3}])
(should= 2 (get-pubkey-from-petname "two"))
(should= nil (get-pubkey-from-petname "none"))
(should= nil (get-petname 3))
(should= nil (get-petname 4)))
)
)
(describe "Determining trust"
(it "determines if a pubkey is trusted"
(let [my-pubkey 1
contact-lists {1 [{:pubkey 2}]}
event-state {:pubkey my-pubkey :contact-lists contact-lists}]
(reset! ui-context {:event-context (atom event-state)}))
(should (is-trusted? 1))
(should (is-trusted? 2))
(should-not (is-trusted? 3)))
(it "determines second degree trust"
(let [my-pubkey 99
trusted-user 1
trusted-by-trusted-user 2
profiles {trusted-user {:name "trusted"}
trusted-by-trusted-user {:name "second-degree"}}
contact-lists {my-pubkey [{:pubkey trusted-user}]
trusted-user [{:pubkey trusted-by-trusted-user}]}
event-state {:pubkey my-pubkey
:profiles profiles
:contact-lists contact-lists}]
(reset! ui-context {:event-context (atom event-state)})
(should= trusted-user (trusted-by-contact trusted-by-trusted-user))
))
(it "gets my petname for a trusted user"
(let [my-pubkey 1
contact-lists {1 [{:pubkey 2 :petname "two"}
{:pubkey 3}]}
event-state {:pubkey my-pubkey :contact-lists contact-lists}]
(reset! ui-context {:event-context (atom event-state)}))
(should= "two" (get-petname 2))
(should= nil (get-petname 3))
(should= nil (get-petname 4)))
(it "gets the pubkey for a trusted pet name"
(let [my-pubkey 1
contact-lists {1 [{:pubkey 2 :petname "two"}
{:pubkey 3}]}
event-state {:pubkey my-pubkey :contact-lists contact-lists}]
(reset! ui-context {:event-context (atom event-state)}))
(should= 2 (get-pubkey-from-petname "two"))
(should= nil (get-pubkey-from-petname "none"))
(should= nil (get-petname 3))
(should= nil (get-petname 4)))
)

View File

@ -8,4 +8,6 @@
(defmulti add-relays-to-event (fn [db _id _relays] (::type db)))
(defmulti add-reference-to-event (fn [db _id _reference] (::type db)))
(defmulti add-contacts (fn [db _user-id _contacts] (::type db)))
(defmulti get-contacts (fn [db _user-id] (::type db)))
(defmulti get-id-from-petname (fn [db _user-id _petname] (::type db)))

View File

@ -27,6 +27,17 @@
(defmethod gateway/add-contacts ::type [db user-id contacts]
(swap! (:data db) assoc-in [:contact-lists user-id] contacts))
(defmethod gateway/get-contacts ::type [db user-id]
(get-in @(:data db) [:contact-lists user-id]))
(defmethod gateway/get-id-from-petname ::type [db user-id petname]
(loop [contacts (gateway/get-contacts db user-id)]
(if (seq contacts)
(if (= petname (:petname (first contacts)))
(:pubkey (first contacts))
(recur (rest contacts)))
nil)))
;----------methods for tests
(defn get-db [] {:data (get-mem) ::gateway/type ::type})

View File

@ -1,7 +1,8 @@
(ns more-speech.nostr.contact-list
(:require [more-speech.nostr.util :as util]
[more-speech.ui.swing.ui-context :refer :all]
[more-speech.db.gateway :as gateway]))
[more-speech.db.gateway :as gateway]
[more-speech.config :refer [get-db]]))
(defn make-contact-from-tag [[_p pubkey relay petname]]
@ -36,10 +37,10 @@
(defn trusted-by-contact [candidate-pubkey]
(let [event-state @(:event-context @ui-context)
my-pubkey (:pubkey event-state)
contact-lists (:contact-lists event-state)
my-contacts (get contact-lists my-pubkey)
my-contact-ids (map :pubkey my-contacts)]
my-pubkey (:pubkey event-state)
contact-lists (:contact-lists event-state)
my-contacts (get contact-lists my-pubkey)
my-contact-ids (map :pubkey my-contacts)]
(loop [my-contact-ids my-contact-ids]
(if (empty? my-contact-ids)
nil
@ -59,17 +60,7 @@
(:petname his-entry)))
(defn get-pubkey-from-petname [petname]
(let [event-state @(:event-context @ui-context)
my-pubkey (:pubkey event-state)
contact-lists (:contact-lists event-state)
my-contacts (get contact-lists my-pubkey)]
(loop [contacts my-contacts]
(if (seq contacts)
(if (= petname (:petname (first contacts)))
(:pubkey (first contacts))
(recur (rest contacts)))
nil))
) )
(gateway/get-id-from-petname (get-db) (get-mem :pubkey) petname))

View File

@ -5,9 +5,10 @@
[more-speech.nostr.elliptic-signature :as ecc]
[more-speech.nostr.util :as util]
[more-speech.nostr.contact-list :as contact-list]
[more-speech.config :as config]
[more-speech.config :as config :refer [get-db]]
[clojure.string :as string]
[clojure.core.async :as async])
[clojure.core.async :as async]
[more-speech.db.gateway :as gateway])
(:import (ecdhJava SECP256K1)))
(defn body->event
@ -68,9 +69,9 @@
(defn make-people-reference-tags [pubkey reply-to-or-nil]
(if (nil? reply-to-or-nil)
[]
(let [event-map (get-event-state :text-event-map)
(let [
parent-event-id reply-to-or-nil
parent-event (get event-map parent-event-id)
parent-event (gateway/get-event (get-db) parent-event-id)
parent-tags (:tags parent-event)
people-ids (map second (filter #(= :p (first %)) parent-tags))
parent-author (:pubkey parent-event)
@ -87,8 +88,7 @@
(if (nil? reply-to-or-nil)
nil
(let [reply-id reply-to-or-nil
event-map (get-event-state :text-event-map)
replied-to-event (get event-map reply-id)
replied-to-event (gateway/get-event (get-db) reply-id)
[root _mentions _referent] (events/get-references replied-to-event)]
root)))