Add basic viewing for rooms.
This commit is contained in:
parent
e06c4a8772
commit
7d97fc1aba
15
src/Main.elm
15
src/Main.elm
|
@ -22,6 +22,13 @@ init flags url key =
|
||||||
, loginUsername = ""
|
, loginUsername = ""
|
||||||
, loginPassword = ""
|
, loginPassword = ""
|
||||||
, apiUrl = "https://matrix.org"
|
, apiUrl = "https://matrix.org"
|
||||||
|
, sync =
|
||||||
|
{ nextBatch = ""
|
||||||
|
, rooms = Nothing
|
||||||
|
, presence = Nothing
|
||||||
|
, accountData = Nothing
|
||||||
|
}
|
||||||
|
, errors = []
|
||||||
}
|
}
|
||||||
cmd = case flags.token of
|
cmd = case flags.token of
|
||||||
Just _ -> Cmd.none
|
Just _ -> Cmd.none
|
||||||
|
@ -32,7 +39,7 @@ init flags url key =
|
||||||
view : Model -> Browser.Document Msg
|
view : Model -> Browser.Document Msg
|
||||||
view m =
|
view m =
|
||||||
{ title = "Scylla"
|
{ title = "Scylla"
|
||||||
, body = [ viewFull m ]
|
, body = viewFull m
|
||||||
}
|
}
|
||||||
|
|
||||||
update : Msg -> Model -> (Model, Cmd Msg)
|
update : Msg -> Model -> (Model, Cmd Msg)
|
||||||
|
@ -51,9 +58,9 @@ updateLoginResponse model r = case r of
|
||||||
Err e -> (model, Cmd.none)
|
Err e -> (model, Cmd.none)
|
||||||
|
|
||||||
updateSyncResponse : Model -> Result Http.Error SyncResponse -> (Model, Cmd Msg)
|
updateSyncResponse : Model -> Result Http.Error SyncResponse -> (Model, Cmd Msg)
|
||||||
updateSyncResponse model r = case r of
|
updateSyncResponse model r = let sync = model.sync in case r of
|
||||||
Ok sr -> (model, Cmd.none)
|
Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr }, Cmd.none)
|
||||||
Err e -> (model, Cmd.none)
|
_ -> (model, Cmd.none)
|
||||||
|
|
||||||
subscriptions : Model -> Sub Msg
|
subscriptions : Model -> Sub Msg
|
||||||
subscriptions m = Sub.none
|
subscriptions m = Sub.none
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
module Scylla.Model exposing (..)
|
module Scylla.Model exposing (..)
|
||||||
import Scylla.Api exposing (..)
|
import Scylla.Api exposing (..)
|
||||||
import Scylla.Sync exposing (SyncResponse)
|
import Scylla.Sync exposing (SyncResponse, JoinedRoom)
|
||||||
import Scylla.Login exposing (LoginResponse, Username, Password)
|
import Scylla.Login exposing (LoginResponse, Username, Password)
|
||||||
import Browser.Navigation as Nav
|
import Browser.Navigation as Nav
|
||||||
|
import Dict exposing (Dict)
|
||||||
import Browser
|
import Browser
|
||||||
import Http
|
import Http
|
||||||
import Url exposing (Url)
|
import Url exposing (Url)
|
||||||
|
@ -13,6 +14,8 @@ type alias Model =
|
||||||
, loginUsername : Username
|
, loginUsername : Username
|
||||||
, loginPassword : Password
|
, loginPassword : Password
|
||||||
, apiUrl : ApiUrl
|
, apiUrl : ApiUrl
|
||||||
|
, sync : SyncResponse
|
||||||
|
, errors : List String
|
||||||
}
|
}
|
||||||
|
|
||||||
type Msg =
|
type Msg =
|
||||||
|
|
|
@ -80,14 +80,14 @@ type alias StateEvent =
|
||||||
stateEventDecoder : Decoder StateEvent
|
stateEventDecoder : Decoder StateEvent
|
||||||
stateEventDecoder =
|
stateEventDecoder =
|
||||||
Decode.succeed StateEvent
|
Decode.succeed StateEvent
|
||||||
|> required "required" value
|
|> required "content" value
|
||||||
|> required "type" string
|
|> required "type" string
|
||||||
|> required "event_id" string
|
|> required "event_id" string
|
||||||
|> required "sender" string
|
|> required "sender" string
|
||||||
|> required "origin_server_ts" int
|
|> required "origin_server_ts" int
|
||||||
|> maybeDecode "unsigned" unsignedDataDecoder
|
|> maybeDecode "unsigned" unsignedDataDecoder
|
||||||
|> maybeDecode "prev_content" eventContentDecoder
|
|> maybeDecode "prev_content" eventContentDecoder
|
||||||
|> required "sate_key" string
|
|> required "state_key" string
|
||||||
|
|
||||||
-- Rooms
|
-- Rooms
|
||||||
type alias Rooms =
|
type alias Rooms =
|
||||||
|
@ -254,3 +254,7 @@ presenceDecoder : Decoder Presence
|
||||||
presenceDecoder =
|
presenceDecoder =
|
||||||
Decode.succeed Presence
|
Decode.succeed Presence
|
||||||
|> maybeDecode "events" (list eventDecoder)
|
|> maybeDecode "events" (list eventDecoder)
|
||||||
|
|
||||||
|
-- Business Logic
|
||||||
|
mergeSyncResponse : SyncResponse -> SyncResponse -> SyncResponse
|
||||||
|
mergeSyncResponse l r = r
|
||||||
|
|
|
@ -1,16 +1,30 @@
|
||||||
module Scylla.Views exposing (..)
|
module Scylla.Views exposing (..)
|
||||||
import Scylla.Model exposing (..)
|
import Scylla.Model exposing (..)
|
||||||
import Html exposing (Html, div, input, text, button)
|
import Scylla.Sync exposing (..)
|
||||||
|
import Json.Decode as Decode
|
||||||
|
import Html exposing (Html, div, input, text, button, div, span)
|
||||||
import Html.Attributes exposing (type_, value)
|
import Html.Attributes exposing (type_, value)
|
||||||
import Html.Events exposing (onInput, onClick)
|
import Html.Events exposing (onInput, onClick)
|
||||||
|
import Dict
|
||||||
|
|
||||||
viewFull : Model -> Html Msg
|
viewFull : Model -> List (Html Msg)
|
||||||
viewFull model = case model.token of
|
viewFull model =
|
||||||
Just _ -> normalView model
|
let
|
||||||
Nothing -> loginView model
|
core = case model.token of
|
||||||
|
Just _ -> normalView model
|
||||||
|
Nothing -> loginView model
|
||||||
|
errorList = errorsView model.errors
|
||||||
|
in
|
||||||
|
[ errorList ] ++ [ core ]
|
||||||
|
|
||||||
|
errorsView : List String -> Html Msg
|
||||||
|
errorsView = div [] << List.map errorView
|
||||||
|
|
||||||
|
errorView : String -> Html Msg
|
||||||
|
errorView s = div [] [ text s ]
|
||||||
|
|
||||||
normalView : Model -> Html Msg
|
normalView : Model -> Html Msg
|
||||||
normalView m = div [] [ text "You are logged in!" ]
|
normalView m = div [] []
|
||||||
|
|
||||||
loginView : Model -> Html Msg
|
loginView : Model -> Html Msg
|
||||||
loginView m = div []
|
loginView m = div []
|
||||||
|
@ -19,3 +33,40 @@ loginView m = div []
|
||||||
, input [ type_ "text", value m.apiUrl, onInput ChangeApiUrl ] []
|
, input [ type_ "text", value m.apiUrl, onInput ChangeApiUrl ] []
|
||||||
, button [ onClick AttemptLogin ] [ text "Log In" ]
|
, button [ onClick AttemptLogin ] [ text "Log In" ]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
joinedRoomView : Model -> JoinedRoom -> Html Msg
|
||||||
|
joinedRoomView m jr =
|
||||||
|
let
|
||||||
|
events = Maybe.withDefault [] <| Maybe.andThen .events jr.timeline
|
||||||
|
renderedEvents = List.filterMap (eventView m) events
|
||||||
|
eventContainer = eventContainerView m renderedEvents
|
||||||
|
in
|
||||||
|
div [] [ eventContainer ]
|
||||||
|
|
||||||
|
eventContainerView : Model -> List (Html Msg) -> Html Msg
|
||||||
|
eventContainerView m = div []
|
||||||
|
|
||||||
|
eventView : Model -> RoomEvent -> Maybe (Html Msg)
|
||||||
|
eventView m re = case re.type_ of
|
||||||
|
"m.room.message" -> messageView m re
|
||||||
|
_ -> Nothing
|
||||||
|
|
||||||
|
messageView : Model -> RoomEvent -> Maybe (Html Msg)
|
||||||
|
messageView m re =
|
||||||
|
let
|
||||||
|
msgtype = Decode.decodeValue (Decode.field "msgtype" Decode.string) re.content
|
||||||
|
in
|
||||||
|
case msgtype of
|
||||||
|
Ok "m.text" -> messageTextView m re
|
||||||
|
_ -> Nothing
|
||||||
|
|
||||||
|
messageTextView : Model -> RoomEvent -> Maybe (Html Msg)
|
||||||
|
messageTextView m re =
|
||||||
|
let
|
||||||
|
body = Decode.decodeValue (Decode.field "body" Decode.string) re.content
|
||||||
|
wrap mtext = div []
|
||||||
|
[ span [] [ text re.sender ]
|
||||||
|
, span [] [ text mtext ]
|
||||||
|
]
|
||||||
|
in
|
||||||
|
Maybe.map wrap <| Result.toMaybe body
|
||||||
|
|
Loading…
Reference in New Issue
Block a user