multi user updates for storage module

This commit is contained in:
Aidan 2020-12-03 21:59:07 -08:00
parent 23dd2ee3b3
commit 0958fa663d
No known key found for this signature in database
GPG key ID: 327711E983899316
3 changed files with 39 additions and 29 deletions

View file

@ -29,7 +29,8 @@
;; TODO: dont magic string this ;; TODO: dont magic string this
(if (= (get x "username") "Link New User") (if (= (get x "username") "Link New User")
(make-req sigd {"type" "link"}) (make-req sigd {"type" "link"})
(reset! user x))))) (do (reset! user x)
(init-user-schema (get x "username")))))))
(defn digest-group-or-contact-list (defn digest-group-or-contact-list
[list db] [list db]
@ -37,7 +38,7 @@
(doseq [contact list] (doseq [contact list]
(let [id (get-id-from-contact-or-group contact) (let [id (get-id-from-contact-or-group contact)
name (get contact "name")] name (get contact "name")]
(store-contact db id contact) (store-contact db (get @user "username") id contact)
(add-json-contact {:id id :name name})))) (add-json-contact {:id id :name name}))))
(defn -main (defn -main
@ -115,8 +116,7 @@
"username" new-user}) "username" new-user})
;; TODO: load messages or something ;; TODO: load messages or something
;; maybe wait till here to paint main UI ;; maybe wait till here to paint main UI
(add-str-flag (str "Now logged in as " new-user)) (add-str-flag (str "Now logged in as " new-user))))
))
;; close account picker ;; close account picker
(paint-user-select [] false (fn [_ _] (log/error "this cannot be called")))))))) (paint-user-select [] false (fn [_ _] (log/error "this cannot be called"))))))))

View file

@ -7,53 +7,63 @@
"opens db at filepath and makes sure schema is in place" "opens db at filepath and makes sure schema is in place"
[filepath] [filepath]
(let [db (c/open-database! filepath)] (let [db (c/open-database! filepath)]
(if (nil? (c/get-at! db [:contacts])) ;; TODO: schema preperations here
(c/assoc-at! db [:contacts] {}))
(if (nil? (c/get-at! db [:messages]))
(c/assoc-at! db [:messages] {}))
db)) db))
(defn init-user-schema
[db username]
(let [user-doc (c/get-at! db [username])
user-contact-doc (get user-doc :contacts)
user-messages-doc (get user-doc :messages)]
(if (nil? user-doc)
(c/assoc-at! db [username] {:contacts {}
:messages {}}))
(if (nil? user-contact-doc)
(c/assoc-at! db [username :contacts] {}))
(if (nil? user-messages-doc)
(c/assoc-at! db [username :messages] {}))))
(defn store-contact (defn store-contact
"adds a new contact to db" "adds a new contact to db"
[db contact-id contact-doc] [db username contact-id contact-doc]
(let [curr-doc (c/get-at! db [:contacts contact-id])] (let [curr-doc (c/get-at! db [username :contacts contact-id])]
(if (nil? curr-doc) (if (or (nil? curr-doc) (not= curr-doc contact-doc))
(c/assoc-at! db [:contacts contact-id] contact-doc) (c/assoc-at! db [username :contacts contact-id] contact-doc)))
(if (not (= curr-doc contact-doc)) (if (nil? (c/get-at! db [username :messages contact-id]))
(c/assoc-at! db [:contacts contact-id] contact-doc)))) (c/assoc-at! db [username :messages contact-id] [])))
(if (nil? (c/get-at! db [:messages contact-id]))
(c/assoc-at! db [:messages contact-id] [])))
(defn store-message (defn store-message
"adds a message to the db" "adds a message to the db"
[db contact-id message-doc] [db username contact-id message-doc]
(let [curr-thread (c/get-at! db [:messages contact-id])] (let [curr-thread (c/get-at! db [username :messages contact-id])]
;; no contact? dont bother ;; no contact? dont bother
(if (nil? curr-thread) (if (nil? curr-thread)
(log/info "Received message for undiscovered contact (wont store).") (log/info "Received message for undiscovered contact (wont store).")
(c/merge-at! db [:messages contact-id] [message-doc])))) (c/merge-at! db [username :messages contact-id] [message-doc]))))
(defn get-contacts (defn get-contacts
"retrieve all contacts" "retrieve all contacts"
[db] [db username]
(c/get-at! db [:contacts])) (c/get-at! db [username :contacts]))
(defn get-contact (defn get-contact
"retrieve a specific contact" "retrieve a specific contact"
[db contact-id] [db username contact-id]
(c/get-at! db [:contacts contact-id])) (c/get-at! db [username :contacts contact-id]))
(defn get-thread (defn get-thread
"get all messages to and from a specific contact/group" "get all messages to and from a specific contact/group"
[db contact-id] [db username contact-id]
(c/get-at! db [:messages contact-id])) (c/get-at! db [username :messages contact-id]))
(defn get-thread-and-contact-if-contact (defn get-thread-and-contact-if-contact
"get all messages to and from a contact, as well as their contact info IF they are a stored contact" "get all messages to and from a contact, as well as their contact info IF they are a stored contact"
[db contact-id] [db username contact-id]
(let [contact-doc (c/get-at! db [:contacts contact-id])] (let [contact-doc (c/get-at! db [username :contacts contact-id])]
(if (not (nil? contact-doc)) (if (not (nil? contact-doc))
;; contact exists ;; contact exists
{:contact contact-doc :thread (c/get-at! db [:messages contact-id])} {:contact contact-doc :thread (c/get-at! db [username :messages contact-id])}
;; else return nil ;; else return nil
nil))) nil)))

View file

@ -150,7 +150,7 @@
(aux-renderer (aux-renderer
{:fx/type user-select-ui {:fx/type user-select-ui
:showing showing :showing showing
;; "Make New User" is a magic token ;; "Make New User" is a magic token. see handle-users in core
;; TODO: dont ;; TODO: dont
:users (conj users {"username" "Link New User"}) :users (conj users {"username" "Link New User"})
:callback callback})) :callback callback}))