Scylla/src/Main.elm

121 lines
4.1 KiB
Elm
Raw Normal View History

2018-12-08 20:02:29 -08:00
import Browser exposing (application, UrlRequest(..))
import Browser.Navigation as Nav
2018-12-08 13:49:30 -08:00
import Scylla.Sync exposing (..)
2018-12-08 15:06:14 -08:00
import Scylla.Login exposing (..)
2018-12-08 13:49:30 -08:00
import Scylla.Model exposing (..)
import Scylla.Http exposing (..)
2018-12-08 15:06:14 -08:00
import Scylla.Views exposing (viewFull)
2018-12-08 19:09:20 -08:00
import Scylla.Route exposing (Route(..))
import Url exposing (Url)
2018-12-08 19:09:20 -08:00
import Url.Parser exposing (parse)
import Url.Builder
2018-12-08 13:49:30 -08:00
import Html exposing (div, text)
2018-12-08 15:06:14 -08:00
import Http
2018-12-09 23:38:43 -08:00
import Dict
type alias Flags =
{ token : Maybe String
}
init : Flags -> Url -> Nav.Key -> (Model, Cmd Msg)
init flags url key =
let
model =
{ key = key
2018-12-08 19:09:20 -08:00
, route = Maybe.withDefault Unknown <| parse Scylla.Route.route url
, token = flags.token
2018-12-08 15:06:14 -08:00
, loginUsername = ""
, loginPassword = ""
2018-12-08 13:49:30 -08:00
, apiUrl = "https://matrix.org"
2018-12-08 17:15:35 -08:00
, sync =
{ nextBatch = ""
, rooms = Nothing
, presence = Nothing
, accountData = Nothing
}
, errors = []
2018-12-09 23:38:43 -08:00
, roomText = Dict.empty
, transactionId = 0
}
cmd = case flags.token of
Just _ -> Cmd.none
2018-12-08 19:09:20 -08:00
Nothing -> Nav.pushUrl key <| Url.Builder.absolute [ "login" ] []
in
(model, cmd)
view : Model -> Browser.Document Msg
view m =
{ title = "Scylla"
2018-12-08 17:15:35 -08:00
, body = viewFull m
}
update : Msg -> Model -> (Model, Cmd Msg)
2018-12-08 15:06:14 -08:00
update msg model = case msg of
ChangeApiUrl u -> ({ model | apiUrl = u }, Cmd.none)
ChangeLoginUsername u -> ({ model | loginUsername = u }, Cmd.none)
ChangeLoginPassword p -> ({ model | loginPassword = p }, Cmd.none)
AttemptLogin -> (model, Scylla.Http.login model.apiUrl model.loginUsername model.loginPassword) -- TODO
2018-12-08 20:02:29 -08:00
TryUrl urlRequest -> updateTryUrl model urlRequest
2018-12-08 19:09:20 -08:00
ChangeRoute r -> ({ model | route = r }, Cmd.none)
2018-12-08 15:06:14 -08:00
ReceiveLoginResponse r -> updateLoginResponse model r
ReceiveFirstSyncResponse r -> updateSyncResponse model r False
ReceiveSyncResponse r -> updateSyncResponse model r True
2018-12-13 02:15:14 -08:00
ReceiveUserData s r -> (model, Cmd.none)
2018-12-09 23:38:43 -08:00
ChangeRoomText r t -> ({ model | roomText = Dict.insert r t model.roomText}, Cmd.none)
SendRoomText r -> updateSendRoomText model r
SendRoomTextResponse r -> (model, Cmd.none)
updateSendRoomText : Model -> String -> (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
in
({ m | roomText = Dict.insert r "" m.roomText, transactionId = m.transactionId + 1 }, command)
2018-12-08 20:02:29 -08:00
updateTryUrl : Model -> Browser.UrlRequest -> (Model, Cmd Msg)
updateTryUrl m ur = case ur of
Internal u -> (m, Nav.pushUrl m.key (Url.toString u))
_ -> (m, Cmd.none)
2018-12-08 15:06:14 -08:00
updateLoginResponse : Model -> Result Http.Error LoginResponse -> (Model, Cmd Msg)
updateLoginResponse model r = case r of
2018-12-08 19:09:20 -08:00
Ok lr -> ( { model | token = Just lr.accessToken } , Cmd.batch
[ firstSync model.apiUrl lr.accessToken
, Nav.pushUrl model.key <| Url.Builder.absolute [] []
] )
2018-12-08 15:06:14 -08:00
Err e -> (model, Cmd.none)
updateSyncResponse : Model -> Result Http.Error SyncResponse -> Bool -> (Model, Cmd Msg)
updateSyncResponse model r notify =
2018-12-09 00:35:07 -08:00
let
token = Maybe.withDefault "" model.token
nextBatch = Result.withDefault model.sync.nextBatch
<| Result.map .nextBatch r
cmd = sync nextBatch model.apiUrl token
2018-12-09 00:35:07 -08:00
in
case r of
Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr }, cmd)
_ -> (model, cmd)
subscriptions : Model -> Sub Msg
subscriptions m = Sub.none
onUrlRequest : Browser.UrlRequest -> Msg
onUrlRequest = TryUrl
onUrlChange : Url -> Msg
2018-12-08 19:09:20 -08:00
onUrlChange = ChangeRoute << Maybe.withDefault Unknown << parse Scylla.Route.route
main = application
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
, onUrlRequest = onUrlRequest
, onUrlChange = onUrlChange
}