Move the room name code into a separate function.

This commit is contained in:
Danila Fedorin 2019-05-15 20:40:21 -07:00
parent 63fcb22998
commit 5bd6124df2
2 changed files with 22 additions and 10 deletions

View File

@ -1,6 +1,6 @@
module Scylla.Model exposing (..) module Scylla.Model exposing (..)
import Scylla.Api exposing (..) import Scylla.Api exposing (..)
import Scylla.Sync exposing (SyncResponse, HistoryResponse, JoinedRoom, senderName) import Scylla.Sync exposing (SyncResponse, HistoryResponse, JoinedRoom, senderName, roomName, roomJoinedUsers)
import Scylla.Login exposing (LoginResponse, Username, Password) import Scylla.Login exposing (LoginResponse, Username, Password)
import Scylla.UserData exposing (UserData) import Scylla.UserData exposing (UserData)
import Scylla.Route exposing (Route(..), RoomId) import Scylla.Route exposing (Route(..), RoomId)
@ -72,6 +72,25 @@ type Msg =
displayName : Model -> Username -> String displayName : Model -> Username -> String
displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData
roomDisplayName : Model -> JoinedRoom -> String
roomDisplayName m jr =
let
customName = roomName jr
roomUsers = List.filter ((/=) m.loginUsername) <| roomJoinedUsers jr
singleUserName = if List.length roomUsers == 1 then List.head roomUsers else Nothing
singleUserDisplayName = Maybe.andThen
(\u -> Maybe.andThen .displayName <| Dict.get u m.userData) singleUserName
firstOption d os = case os of
[] -> d
((Just v)::_) -> v
(Nothing::xs) -> firstOption d xs
in
firstOption "<No Name>"
[ customName
, singleUserDisplayName
, singleUserName
]
roomUrl : String -> String roomUrl : String -> String
roomUrl s = Url.Builder.absolute [ "room", s ] [] roomUrl s = Url.Builder.absolute [ "room", s ] []

View File

@ -107,14 +107,7 @@ homeserverView m hs rs =
roomListElementView : Model -> String -> JoinedRoom -> Html Msg roomListElementView : Model -> String -> JoinedRoom -> Html Msg
roomListElementView m s jr = roomListElementView m s jr =
let let
roomUsers = List.filter ((/=) m.loginUsername) <| roomJoinedUsers jr name = roomDisplayName m jr
privateChatName = case (List.length roomUsers) of
1 -> Maybe.andThen (\u -> Maybe.andThen .displayName <| Dict.get u m.userData) <| List.head roomUsers
_ -> Nothing
maybeRoomName = case roomName jr of
Just rn -> Just rn
Nothing -> privateChatName
name = Maybe.withDefault "<No Name>" maybeRoomName
in in
div [ class "room-link-wrapper" ] div [ class "room-link-wrapper" ]
[ a [ href <| roomUrl s ] [ text name ] [ a [ href <| roomUrl s ] [ text name ]
@ -169,7 +162,7 @@ joinedRoomView m roomId rd =
] ]
in in
div [ class "room-wrapper" ] div [ class "room-wrapper" ]
[ h2 [] [ text <| Maybe.withDefault "<No Name>" <| roomName rd.joinedRoom ] [ h2 [] [ text <| roomDisplayName m rd.joinedRoom ]
, messagesWrapper , messagesWrapper
, typingWrapper , typingWrapper
, messageInput , messageInput