From 36ad2cc93c7b678def39cc249df3f4e2bff14be7 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 17 Dec 2018 20:30:11 -0800 Subject: [PATCH] Store the transaction ID locally, too. --- src/Main.elm | 31 +++++++++++++++++++++++++------ src/Scylla/Login.elm | 20 +++++++++++++------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/Main.elm b/src/Main.elm index e737db3..a7dc2cf 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -92,6 +92,7 @@ updateChangeRoomText m roomId text = typingIndicator = case (text, Dict.get roomId m.roomText) of ("", _) -> Just False (_, Just "") -> Just True + (_, Nothing) -> Just True _ -> Nothing command = case typingIndicator of Just b -> sendTypingIndicator m.apiUrl (Maybe.withDefault "" m.token) roomId m.loginUsername b typingTimeout @@ -118,7 +119,15 @@ updateStoreData m d = case (Json.Decode.decodeValue storeDataDecoder d) of updateLoginInfo : Model -> Json.Encode.Value -> (Model, Cmd Msg) updateLoginInfo m s = case Json.Decode.decodeValue (Json.Decode.map decodeLoginInfo Json.Decode.string) s of - Ok (Just (t,a,u)) -> ({ m | token = Just t, apiUrl = a, loginUsername = u}, firstSync a t) + Ok (Just { token, apiUrl, username, transactionId }) -> + ( + { m | token = Just token + , apiUrl = apiUrl + , loginUsername = username + , transactionId = transactionId + } + , firstSync apiUrl token + ) _ -> (m, Nav.pushUrl m.key <| Url.Builder.absolute [ "login" ] []) updateChangeRoute : Model -> Route -> (Model, Cmd Msg) @@ -148,16 +157,23 @@ updateUserData m s r = case r of Ok ud -> ({ m | userData = Dict.insert s ud m.userData }, Cmd.none) Err e -> (m, userData m.apiUrl (Maybe.withDefault "" m.token) s) -updateSendRoomText : Model -> String -> (Model, Cmd Msg) +updateSendRoomText : Model -> RoomId -> (Model, Cmd Msg) updateSendRoomText m r = let token = Maybe.withDefault "" m.token message = Maybe.andThen (\s -> if s == "" then Nothing else Just s) <| Dict.get r m.roomText - command = Maybe.withDefault Cmd.none - <| Maybe.map (sendTextMessage m.apiUrl token m.transactionId r) message + combinedCmd = case message of + Nothing -> Cmd.none + Just s -> Cmd.batch + [ sendTextMessage m.apiUrl token m.transactionId r s + , sendTypingIndicator m.apiUrl token r m.loginUsername False typingTimeout + , setStoreValuePort ("scylla.loginInfo", Json.Encode.string + <| encodeLoginInfo + <| LoginInfo (Maybe.withDefault "" m.token) m.apiUrl m.loginUsername (m.transactionId + 1)) + ] in - ({ m | roomText = Dict.insert r "" m.roomText, transactionId = m.transactionId + 1 }, command) + ({ m | roomText = Dict.insert r "" m.roomText, transactionId = m.transactionId + 1 }, combinedCmd) updateTryUrl : Model -> Browser.UrlRequest -> (Model, Cmd Msg) updateTryUrl m ur = case ur of @@ -228,7 +244,10 @@ updateSyncResponse model r notify = subscriptions : Model -> Sub Msg subscriptions m = let - typingTimer = case Maybe.withDefault "" <| Maybe.andThen (\rid -> Dict.get rid m.roomText) <| currentRoomId m of + currentText = Maybe.withDefault "" + <| Maybe.andThen (\rid -> Dict.get rid m.roomText) + <| currentRoomId m + typingTimer = case currentText of "" -> Sub.none _ -> every typingTimeout TypingTick in diff --git a/src/Scylla/Login.elm b/src/Scylla/Login.elm index 0fba6fd..0962a12 100644 --- a/src/Scylla/Login.elm +++ b/src/Scylla/Login.elm @@ -7,18 +7,24 @@ import Json.Encode as Encode type alias Username = String type alias Password = String -type alias LoginInfo = (ApiToken, ApiUrl, Username) +type alias LoginInfo = + { token : ApiToken + , apiUrl : ApiUrl + , username : Username + , transactionId : Int + } encodeLoginInfo : LoginInfo -> String -encodeLoginInfo (t,a,u) = t ++ "," ++ a ++ "," ++ u +encodeLoginInfo {token, apiUrl, username, transactionId} = + token ++ "," ++ apiUrl ++ "," ++ username ++ "," ++ (String.fromInt transactionId) decodeLoginInfo : String -> Maybe LoginInfo decodeLoginInfo s = case String.indexes "," s of - [ fst, snd ] -> Just - ( (String.slice 0 fst s) - , (String.slice (fst + 1) snd s) - , (String.dropLeft (snd + 1) s) - ) + [ fst, snd, thd ] -> Just <| LoginInfo + (String.slice 0 fst s) + (String.slice (fst + 1) snd s) + (String.slice (snd + 1) thd s) + (Maybe.withDefault 0 <| String.toInt <| String.dropLeft (thd + 1) s) _ -> Nothing type alias LoginResponse =