From 2136bf34b9c608d3d3d738d76d8902250ebcee6f Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Mon, 25 Feb 2019 18:08:22 -0800 Subject: [PATCH] Create an abstraction for room data. Unless you specifically need the Sync data, this will be more useful, since it stores the messages being sent and the like. --- src/Scylla/Messages.elm | 12 +++++++----- src/Scylla/Model.elm | 8 ++++---- src/Scylla/Room.elm | 25 +++++++++++++++++++++++++ src/Scylla/Views.elm | 26 +++++++++++++++----------- 4 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 src/Scylla/Room.elm diff --git a/src/Scylla/Messages.elm b/src/Scylla/Messages.elm index 767f5f9..3401bb7 100644 --- a/src/Scylla/Messages.elm +++ b/src/Scylla/Messages.elm @@ -3,18 +3,20 @@ import Scylla.Model exposing (Model) import Scylla.Sync exposing (RoomEvent) import Scylla.Login exposing (Username) +type SendingMessage = TextMessage String + type Message = - SendingTextMessage String Int - | SyncMessage RoomEvent + Sending SendingMessage + | Received RoomEvent extractMessageEvents : List RoomEvent -> List Message -extractMessageEvents es = List.map SyncMessage +extractMessageEvents es = List.map Received <| List.filter (\e -> e.type_ == "m.room.message") es messageUsername : Model -> Message -> Username messageUsername m msg = case msg of - SendingTextMessage _ _ -> m.loginUsername - SyncMessage re -> re.sender + Sending _ -> m.loginUsername + Received re -> re.sender mergeMessages : Model -> List Message -> List (Username, List Message) mergeMessages m xs = diff --git a/src/Scylla/Model.elm b/src/Scylla/Model.elm index ea59521..ca192c0 100644 --- a/src/Scylla/Model.elm +++ b/src/Scylla/Model.elm @@ -79,12 +79,12 @@ loginUrl = Url.Builder.absolute [ "login" ] [] newUsers : Model -> List Username -> List Username newUsers m lus = List.filter (\u -> not <| Dict.member u m.userData) lus +joinedRooms : Model -> Dict RoomId JoinedRoom +joinedRooms m = Maybe.withDefault Dict.empty <| Maybe.andThen .join <| m.sync.rooms + currentRoom : Model -> Maybe JoinedRoom currentRoom m = - let - roomDict = Maybe.withDefault Dict.empty <| Maybe.andThen .join <| m.sync.rooms - in - Maybe.andThen (\s -> Dict.get s roomDict) <| currentRoomId m + Maybe.andThen (\s -> Dict.get s <| joinedRooms m) <| currentRoomId m currentRoomId : Model -> Maybe RoomId currentRoomId m = case m.route of diff --git a/src/Scylla/Room.elm b/src/Scylla/Room.elm new file mode 100644 index 0000000..7d294d5 --- /dev/null +++ b/src/Scylla/Room.elm @@ -0,0 +1,25 @@ +module Scylla.Room exposing (..) +import Scylla.Model exposing (..) +import Scylla.Sync exposing (..) +import Scylla.Messages exposing (..) +import Scylla.Route exposing (..) +import Dict + +type alias RoomData = + { joinedRoom : JoinedRoom + , sendingMessages : List (SendingMessage, Int) + , inputText : Maybe String + } + +roomData : Model -> RoomId -> Maybe RoomData +roomData m rid = + case Dict.get rid (joinedRooms m) of + Just jr -> Just + { joinedRoom = jr + , sendingMessages = [] + , inputText = Nothing + } + Nothing -> Nothing + +currentRoomData : Model -> Maybe RoomData +currentRoomData m = Maybe.andThen (roomData m) <| currentRoomId m diff --git a/src/Scylla/Views.elm b/src/Scylla/Views.elm index d301b7e..8e9e98b 100644 --- a/src/Scylla/Views.elm +++ b/src/Scylla/Views.elm @@ -3,6 +3,7 @@ import Scylla.Model exposing (..) import Scylla.Sync exposing (..) import Scylla.Route exposing (..) import Scylla.Fnv as Fnv +import Scylla.Room exposing (..) import Scylla.Messages exposing (..) import Scylla.Login exposing (Username) import Scylla.Http exposing (fullMediaUrl) @@ -42,9 +43,8 @@ stringColor s = viewFull : Model -> List (Html Msg) viewFull model = let - room r = Maybe.map (\jr -> (r, jr)) - <| Maybe.andThen (Dict.get r) - <| Maybe.andThen .join model.sync.rooms + room r = Maybe.map (\rd -> (r, rd)) + <| roomData model r core = case model.route of Login -> loginView model Base -> baseView model Nothing @@ -60,7 +60,7 @@ errorsView = div [ class "errors-wrapper" ] << List.indexedMap errorView errorView : Int -> String -> Html Msg errorView i s = div [ class "error-wrapper", onClick <| DismissError i ] [ iconView "alert-triangle", text s ] -baseView : Model -> Maybe (String, JoinedRoom) -> Html Msg +baseView : Model -> Maybe (String, RoomData) -> Html Msg baseView m jr = let roomView = Maybe.map (\(id, r) -> joinedRoomView m id r) jr @@ -137,13 +137,13 @@ loginView m = div [ class "login-wrapper" ] , button [ onClick AttemptLogin ] [ text "Log In" ] ] -joinedRoomView : Model -> RoomId -> JoinedRoom -> Html Msg -joinedRoomView m roomId jr = +joinedRoomView : Model -> RoomId -> RoomData -> Html Msg +joinedRoomView m roomId rd = let - events = Maybe.withDefault [] <| Maybe.andThen .events jr.timeline + events = Maybe.withDefault [] <| Maybe.andThen .events rd.joinedRoom.timeline renderedMessages = List.map (userMessagesView m) <| mergeMessages m <| extractMessageEvents events messagesWrapper = messagesWrapperView m roomId renderedMessages - typing = List.map (displayName m) <| roomTypingUsers jr + typing = List.map (displayName m) <| roomTypingUsers rd.joinedRoom typingText = String.join ", " typing typingSuffix = case List.length typing of 0 -> "" @@ -163,7 +163,7 @@ joinedRoomView m roomId jr = ] in div [ class "room-wrapper" ] - [ h2 [] [ text <| Maybe.withDefault "" <| roomName jr ] + [ h2 [] [ text <| Maybe.withDefault "" <| roomName rd.joinedRoom ] , messagesWrapper , typingWrapper , messageInput @@ -210,8 +210,12 @@ userMessagesView m (u, ms) = messageView : Model -> Message -> Maybe (Html Msg) messageView m msg = case msg of - SendingTextMessage t _ -> Just <| div [] [ text t ] - SyncMessage re -> roomEventView m re + Sending t -> Just <| sendingMessageView m t + Received re -> roomEventView m re + +sendingMessageView : Model -> SendingMessage -> Html Msg +sendingMessageView m msg = case msg of + TextMessage t -> text t roomEventView : Model -> RoomEvent -> Maybe (Html Msg) roomEventView m re =