From 5bd6124df2fd87cd579e3fcb70b6626842b7ab84 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 15 May 2019 20:40:21 -0700 Subject: [PATCH] Move the room name code into a separate function. --- src/Scylla/Model.elm | 21 ++++++++++++++++++++- src/Scylla/Views.elm | 11 ++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Scylla/Model.elm b/src/Scylla/Model.elm index d1df57c..5d612a1 100644 --- a/src/Scylla/Model.elm +++ b/src/Scylla/Model.elm @@ -1,6 +1,6 @@ module Scylla.Model 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.UserData exposing (UserData) import Scylla.Route exposing (Route(..), RoomId) @@ -72,6 +72,25 @@ type Msg = displayName : Model -> Username -> String 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 "" + [ customName + , singleUserDisplayName + , singleUserName + ] + roomUrl : String -> String roomUrl s = Url.Builder.absolute [ "room", s ] [] diff --git a/src/Scylla/Views.elm b/src/Scylla/Views.elm index 9d4be09..bc9a77e 100644 --- a/src/Scylla/Views.elm +++ b/src/Scylla/Views.elm @@ -107,14 +107,7 @@ homeserverView m hs rs = roomListElementView : Model -> String -> JoinedRoom -> Html Msg roomListElementView m s jr = let - roomUsers = List.filter ((/=) m.loginUsername) <| roomJoinedUsers 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 "" maybeRoomName + name = roomDisplayName m jr in div [ class "room-link-wrapper" ] [ a [ href <| roomUrl s ] [ text name ] @@ -169,7 +162,7 @@ joinedRoomView m roomId rd = ] in div [ class "room-wrapper" ] - [ h2 [] [ text <| Maybe.withDefault "" <| roomName rd.joinedRoom ] + [ h2 [] [ text <| roomDisplayName m rd.joinedRoom ] , messagesWrapper , typingWrapper , messageInput