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
(if (= (get x "username") "Link New User")
(make-req sigd {"type" "link"})
(reset! user x)))))
(do (reset! user x)
(init-user-schema (get x "username")))))))
(defn digest-group-or-contact-list
[list db]
@ -37,7 +38,7 @@
(doseq [contact list]
(let [id (get-id-from-contact-or-group contact)
name (get contact "name")]
(store-contact db id contact)
(store-contact db (get @user "username") id contact)
(add-json-contact {:id id :name name}))))
(defn -main
@ -115,8 +116,7 @@
"username" new-user})
;; TODO: load messages or something
;; 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
(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"
[filepath]
(let [db (c/open-database! filepath)]
(if (nil? (c/get-at! db [:contacts]))
(c/assoc-at! db [:contacts] {}))
(if (nil? (c/get-at! db [:messages]))
(c/assoc-at! db [:messages] {}))
;; TODO: schema preperations here
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
"adds a new contact to db"
[db contact-id contact-doc]
(let [curr-doc (c/get-at! db [:contacts contact-id])]
(if (nil? curr-doc)
(c/assoc-at! db [:contacts contact-id] contact-doc)
(if (not (= curr-doc contact-doc))
(c/assoc-at! db [:contacts contact-id] contact-doc))))
(if (nil? (c/get-at! db [:messages contact-id]))
(c/assoc-at! db [:messages contact-id] [])))
[db username contact-id contact-doc]
(let [curr-doc (c/get-at! db [username :contacts contact-id])]
(if (or (nil? curr-doc) (not= curr-doc contact-doc))
(c/assoc-at! db [username :contacts contact-id] contact-doc)))
(if (nil? (c/get-at! db [username :messages contact-id]))
(c/assoc-at! db [username :messages contact-id] [])))
(defn store-message
"adds a message to the db"
[db contact-id message-doc]
(let [curr-thread (c/get-at! db [:messages contact-id])]
[db username contact-id message-doc]
(let [curr-thread (c/get-at! db [username :messages contact-id])]
;; no contact? dont bother
(if (nil? curr-thread)
(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
"retrieve all contacts"
[db]
(c/get-at! db [:contacts]))
[db username]
(c/get-at! db [username :contacts]))
(defn get-contact
"retrieve a specific contact"
[db contact-id]
(c/get-at! db [:contacts contact-id]))
[db username contact-id]
(c/get-at! db [username :contacts contact-id]))
(defn get-thread
"get all messages to and from a specific contact/group"
[db contact-id]
(c/get-at! db [:messages contact-id]))
[db username contact-id]
(c/get-at! db [username :messages contact-id]))
(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"
[db contact-id]
(let [contact-doc (c/get-at! db [:contacts contact-id])]
[db username contact-id]
(let [contact-doc (c/get-at! db [username :contacts contact-id])]
(if (not (nil? contact-doc))
;; 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
nil)))

View file

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