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.
This commit is contained in:
Danila Fedorin 2019-02-25 18:08:22 -08:00
parent 6c67e85ca5
commit 2136bf34b9
4 changed files with 51 additions and 20 deletions

View File

@ -3,18 +3,20 @@ import Scylla.Model exposing (Model)
import Scylla.Sync exposing (RoomEvent) import Scylla.Sync exposing (RoomEvent)
import Scylla.Login exposing (Username) import Scylla.Login exposing (Username)
type SendingMessage = TextMessage String
type Message = type Message =
SendingTextMessage String Int Sending SendingMessage
| SyncMessage RoomEvent | Received RoomEvent
extractMessageEvents : List RoomEvent -> List Message extractMessageEvents : List RoomEvent -> List Message
extractMessageEvents es = List.map SyncMessage extractMessageEvents es = List.map Received
<| List.filter (\e -> e.type_ == "m.room.message") es <| List.filter (\e -> e.type_ == "m.room.message") es
messageUsername : Model -> Message -> Username messageUsername : Model -> Message -> Username
messageUsername m msg = case msg of messageUsername m msg = case msg of
SendingTextMessage _ _ -> m.loginUsername Sending _ -> m.loginUsername
SyncMessage re -> re.sender Received re -> re.sender
mergeMessages : Model -> List Message -> List (Username, List Message) mergeMessages : Model -> List Message -> List (Username, List Message)
mergeMessages m xs = mergeMessages m xs =

View File

@ -79,12 +79,12 @@ loginUrl = Url.Builder.absolute [ "login" ] []
newUsers : Model -> List Username -> List Username newUsers : Model -> List Username -> List Username
newUsers m lus = List.filter (\u -> not <| Dict.member u m.userData) lus 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 : Model -> Maybe JoinedRoom
currentRoom m = currentRoom m =
let Maybe.andThen (\s -> Dict.get s <| joinedRooms m) <| currentRoomId m
roomDict = Maybe.withDefault Dict.empty <| Maybe.andThen .join <| m.sync.rooms
in
Maybe.andThen (\s -> Dict.get s roomDict) <| currentRoomId m
currentRoomId : Model -> Maybe RoomId currentRoomId : Model -> Maybe RoomId
currentRoomId m = case m.route of currentRoomId m = case m.route of

25
src/Scylla/Room.elm Normal file
View File

@ -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

View File

@ -3,6 +3,7 @@ import Scylla.Model exposing (..)
import Scylla.Sync exposing (..) import Scylla.Sync exposing (..)
import Scylla.Route exposing (..) import Scylla.Route exposing (..)
import Scylla.Fnv as Fnv import Scylla.Fnv as Fnv
import Scylla.Room exposing (..)
import Scylla.Messages exposing (..) import Scylla.Messages exposing (..)
import Scylla.Login exposing (Username) import Scylla.Login exposing (Username)
import Scylla.Http exposing (fullMediaUrl) import Scylla.Http exposing (fullMediaUrl)
@ -42,9 +43,8 @@ stringColor s =
viewFull : Model -> List (Html Msg) viewFull : Model -> List (Html Msg)
viewFull model = viewFull model =
let let
room r = Maybe.map (\jr -> (r, jr)) room r = Maybe.map (\rd -> (r, rd))
<| Maybe.andThen (Dict.get r) <| roomData model r
<| Maybe.andThen .join model.sync.rooms
core = case model.route of core = case model.route of
Login -> loginView model Login -> loginView model
Base -> baseView model Nothing Base -> baseView model Nothing
@ -60,7 +60,7 @@ errorsView = div [ class "errors-wrapper" ] << List.indexedMap errorView
errorView : Int -> String -> Html Msg errorView : Int -> String -> Html Msg
errorView i s = div [ class "error-wrapper", onClick <| DismissError i ] [ iconView "alert-triangle", text s ] 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 = baseView m jr =
let let
roomView = Maybe.map (\(id, r) -> joinedRoomView m id r) jr 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" ] , button [ onClick AttemptLogin ] [ text "Log In" ]
] ]
joinedRoomView : Model -> RoomId -> JoinedRoom -> Html Msg joinedRoomView : Model -> RoomId -> RoomData -> Html Msg
joinedRoomView m roomId jr = joinedRoomView m roomId rd =
let 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 renderedMessages = List.map (userMessagesView m) <| mergeMessages m <| extractMessageEvents events
messagesWrapper = messagesWrapperView m roomId renderedMessages messagesWrapper = messagesWrapperView m roomId renderedMessages
typing = List.map (displayName m) <| roomTypingUsers jr typing = List.map (displayName m) <| roomTypingUsers rd.joinedRoom
typingText = String.join ", " typing typingText = String.join ", " typing
typingSuffix = case List.length typing of typingSuffix = case List.length typing of
0 -> "" 0 -> ""
@ -163,7 +163,7 @@ joinedRoomView m roomId jr =
] ]
in in
div [ class "room-wrapper" ] div [ class "room-wrapper" ]
[ h2 [] [ text <| Maybe.withDefault "<No Name>" <| roomName jr ] [ h2 [] [ text <| Maybe.withDefault "<No Name>" <| roomName rd.joinedRoom ]
, messagesWrapper , messagesWrapper
, typingWrapper , typingWrapper
, messageInput , messageInput
@ -210,8 +210,12 @@ userMessagesView m (u, ms) =
messageView : Model -> Message -> Maybe (Html Msg) messageView : Model -> Message -> Maybe (Html Msg)
messageView m msg = case msg of messageView m msg = case msg of
SendingTextMessage t _ -> Just <| div [] [ text t ] Sending t -> Just <| sendingMessageView m t
SyncMessage re -> roomEventView m re 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 : Model -> RoomEvent -> Maybe (Html Msg)
roomEventView m re = roomEventView m re =