From 23dd2ee3b328e3c5cfe75570b381dd2a017d9598 Mon Sep 17 00:00:00 2001 From: Aidan Date: Thu, 3 Dec 2020 21:19:26 -0800 Subject: [PATCH] startup user selection --- src/whisper/core.clj | 87 +++++++++++++++++++++++++--------------- src/whisper/signald.clj | 41 ++----------------- src/whisper/storage.clj~ | 3 ++ src/whisper/ui.clj | 52 ++++++++++++++++++++++-- 4 files changed, 110 insertions(+), 73 deletions(-) create mode 100644 src/whisper/storage.clj~ diff --git a/src/whisper/core.clj b/src/whisper/core.clj index 5f1d152..54c8978 100644 --- a/src/whisper/core.clj +++ b/src/whisper/core.clj @@ -17,15 +17,19 @@ (def dbpath "/tmp/temp_db") (def sockpath "/var/run/signald/signald.sock") (def user (atom {})) +(def link-status (atom false)) ;; TODO: handle multi users (with list ui) (defn handle-users - [data] - (let [users (get-in data ["data" "accounts"])] - (if (> (count users) 1) - (log/info "Alert, multi account not supported yet. picking first on list")) - (reset! user (first users))) - (disassoc-callback-type "account_list")) + [sigd data] + (paint-user-select + (get-in data ["data" "accounts"]) + true + (fn [x _] + ;; TODO: dont magic string this + (if (= (get x "username") "Link New User") + (make-req sigd {"type" "link"}) + (reset! user x))))) (defn digest-group-or-contact-list [list db] @@ -55,10 +59,12 @@ ;; register callbacks between the UI and signald (assoc-callback-type "version" (fn [x] (add-str-flag (get-version-from-version-message x)))) (assoc-callback-type "linking_uri" (fn [x] (paint-linking x))) - (assoc-callback-type "linking_successful" (fn [_] (paint-linking nil))) - (assoc-callback-type "linking_error" (fn [_] - (paint-linking nil) - (add-str-flag "Linking returned Error"))) + (assoc-callback-type "linking_successful" (fn [_] (paint-linking nil) + (reset! link-status true))) + (assoc-callback-type "linking_error" (fn [_] (paint-linking nil) + (log/error "Linking failed") + (add-str-flag "Linking failed") + (reset! link-status true))) (assoc-callback-type "unexpected_error" (fn [x] (add-str-flag (str "Unexpected error: " (get-in x ["data" "message"]))))) (assoc-callback-type "contacts_list" (fn [x] (digest-group-or-contact-list @@ -72,43 +78,60 @@ sigd-loop (thread (callback-loop sigd-output))] ;; handle login basically - (assoc-callback-type "account_list" - (fn [x] - (if (< (count (get-in x ["data" "accounts"])) 1) - (do (make-link-data-req sigd-input) - ;; recur via callback - (make-list-accounts-req sigd-input)) - (handle-users x)))) + (assoc-callback-type "account_list" (fn [x] (handle-users sigd-input x))) ;; log in every time user state changes ;; another way we could do this is to have a callback triggered on a ;; signald message type of "subscribed" ;; but I thought it would be more efficient to just watch for results ;; from the call to handle-users + ;; ALSO: close account picker (add-watch user :login-manager (fn [key atom old-state new-state] (let [old-user (get old-state "username") new-user (get new-state "username")] - (log/info key " changed state from " - old-user " to " new-user) + (if (not= old-user new-user) + (do (log/info key " changed state from " + old-user " to " new-user) - ;; unsub from old acc - (if (some? old-user) - ;; TODO: make this a do statement - ;; add a call to reset the UI - (make-unsubscribe-req sigd-input old-user)) + ;; unsub from old acc + (if (some? old-user) + ;; TODO: make this a, do statement + ;; add a call to reset the UI + (make-req sigd-input ("type" "unsubscribe" + "username" old-user))) - ;; sub to new account - (if (some? new-user) - (do - (make-subscribe-req sigd-input new-user) - (make-contact-sync-req sigd-input new-user) - (make-contact-list-req sigd-input new-user) - (make-group-list-req sigd-input new-user)))))) + ;; sub to new account + (if (some? new-user) + (do + (make-req sigd-input {"type" "subscribe" + "username" new-user}) + (make-req sigd-input {"type" "sync_contacts" + "username" new-user}) + (make-req sigd-input {"type" "list_contacts" + "username" new-user}) + (make-req sigd-input {"type" "list_groups" + "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)) + )) + + ;; close account picker + (paint-user-select [] false (fn [_ _] (log/error "this cannot be called")))))))) + + ;; whenever a link account operation finishes, retrigger handle-users + (add-watch link-status :linking-manager + (fn [key atom old-state new-state] + (if (and (not= old-state new-state) new-state) + (do + (log/info key " has detected a link account operation finished.") + (reset! link-status false) + (make-req sigd-input {"type" "list_accounts"}))))) ;; find avail accounts - (make-list-accounts-req sigd-input) + (make-req sigd-input {"type" "list_accounts"}) ;; finally, block on signald loop till return (