diff --git a/src/whisper/core.clj b/src/whisper/core.clj index 1ae48f0..0b3942a 100644 --- a/src/whisper/core.clj +++ b/src/whisper/core.clj @@ -12,23 +12,21 @@ [whisper.signald])) (def sockpath "/var/run/signald/signald.sock") -(def user (atom {"username" "" "filename" ""})) +(def user (atom {})) ;; 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")) - (swap! user (first users)))) + (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")) (defn -main "packs and shows main window with signald version string" [& args] - ;; TODO: removeme - (add-str-flag (json/read-str "\"loading....\"")) - ;; paint main chat window (paint-main) @@ -39,11 +37,17 @@ (assoc-callback-type "linking_error" (fn [_] (paint-linking nil) (add-str-flag "Linking returned Error"))) + (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] (let [contacts (get x "data")] + ;; Should we iter in a background thread? + (doseq [contact contacts] + ;; TODO: sync with storage + (add-json-contact contact))))) - ;; update ui loop + ;; prep to read signald (let [sigd (get-signald-sock sockpath)] (if (nil? sigd) - ;; TODO: change this to call add-json-flag (add-str-flag "No signald connection.") ;; else, we have connection to signald @@ -56,10 +60,15 @@ (fn [x] (if (< (count (get-in x ["data" "accounts"])) 1) (do (make-link-data-req sigd-input) - (make-list-accounts-req sigd-input) - (handle-users x))))) + ;; recur via callback + (make-list-accounts-req sigd-input)) + (handle-users x)))) - ;; log in every time username changes + ;; 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 (add-watch user :login-manager (fn [key atom old-state new-state] (let [old-user (get old-state "username") @@ -76,8 +85,10 @@ ;; sub to new account (if (some? new-user) - (make-subscribe-req sigd-input new-user) - (make-contact-sync-req sigd-input 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)))))) ;; find avail accounts (make-list-accounts-req sigd-input) diff --git a/src/whisper/signald.clj b/src/whisper/signald.clj index dccb59f..c62614e 100644 --- a/src/whisper/signald.clj +++ b/src/whisper/signald.clj @@ -21,6 +21,10 @@ [callback-token callback] (swap! callback-table assoc callback-token callback)) +(defn disassoc-callback-type + [callback-token] + (swap! callback-table dissoc callback-token)) + ;; TODO wrap attempt to call callback func in try catch (defn callback-loop [BufferedReader] @@ -40,18 +44,20 @@ (get data "version") " " (get data "branch")))) +;; sends data to signal socket +;; adds a newline (defn -make-req [output-stream data] - (try (.write output-stream data) (.flush output-stream) + (try (.write output-stream (str data "\n")) (.flush output-stream) (catch Exception e (log/error e "Error sending to signald")))) (defn make-link-data-req [output-stream] - (-make-req output-stream "{\"type\": \"link\"}\n")) + (-make-req output-stream "{\"type\": \"link\"}")) (defn make-list-accounts-req [output-stream] - (-make-req output-stream "{\"type\": \"list_accounts\"}\n")) + (-make-req output-stream "{\"type\": \"list_accounts\"}")) (defn make-subscribe-req [output-stream username] @@ -70,3 +76,8 @@ (-make-req output-stream (json/write-str {"type" "sync_contacts" "username" username}))) +(defn make-contact-list-req + [output-stream username] + (-make-req output-stream + (json/write-str {"type" "list_contacts" + "username" username})))