diff --git a/src/Main.elm b/src/Main.elm index da6b61b..ecde96e 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -6,6 +6,7 @@ import Scylla.Model exposing (..) import Scylla.Http exposing (..) import Scylla.Views exposing (viewFull) import Scylla.Route exposing (Route(..)) +import Scylla.UserData exposing (..) import Url exposing (Url) import Url.Parser exposing (parse) import Url.Builder @@ -61,11 +62,16 @@ update msg model = case msg of ReceiveLoginResponse r -> updateLoginResponse model r ReceiveFirstSyncResponse r -> updateSyncResponse model r False ReceiveSyncResponse r -> updateSyncResponse model r True - ReceiveUserData s r -> (model, Cmd.none) + ReceiveUserData s r -> updateUserData model s r ChangeRoomText r t -> ({ model | roomText = Dict.insert r t model.roomText}, Cmd.none) SendRoomText r -> updateSendRoomText model r SendRoomTextResponse r -> (model, Cmd.none) +updateUserData : Model -> String -> Result Http.Error UserData -> (Model, Cmd Msg) +updateUserData m s r = case r of + Ok ud -> ({ m | userData = Dict.insert s ud m.userData }, Cmd.none) + Err e -> (m, userData m.apiUrl (Maybe.withDefault "" m.token) s) + updateSendRoomText : Model -> String -> (Model, Cmd Msg) updateSendRoomText m r = let @@ -96,11 +102,13 @@ updateSyncResponse model r notify = token = Maybe.withDefault "" model.token nextBatch = Result.withDefault model.sync.nextBatch <| Result.map .nextBatch r - cmd = sync nextBatch model.apiUrl token + syncCmd = sync nextBatch model.apiUrl token + newUsers sr = List.filter (\s -> not <| Dict.member s model.userData) <| roomsUsers sr + newUserCommands sr = Cmd.batch <| List.map (userData model.apiUrl <| Maybe.withDefault "" model.token) <| newUsers sr in case r of - Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr }, cmd) - _ -> (model, cmd) + Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr }, Cmd.batch [ syncCmd, newUserCommands sr ]) + _ -> (model, syncCmd) subscriptions : Model -> Sub Msg subscriptions m = Sub.none diff --git a/src/Scylla/Sync.elm b/src/Scylla/Sync.elm index 05b3ba9..2d25e5e 100644 --- a/src/Scylla/Sync.elm +++ b/src/Scylla/Sync.elm @@ -399,4 +399,4 @@ roomsUsers s = joinedUsers = usersFor .join leftUsers = usersFor .leave in - leftUsers ++ joinedUsers + uniqueBy (\u -> u) <| leftUsers ++ joinedUsers diff --git a/src/Scylla/Views.elm b/src/Scylla/Views.elm index c30a001..5ba7fef 100644 --- a/src/Scylla/Views.elm +++ b/src/Scylla/Views.elm @@ -119,16 +119,19 @@ eventView m re = "m.room.message" -> Just messageView _ -> Nothing createRow mhtml = tr [] - [ td [] [ eventSenderView re.sender ] + [ td [] [ eventSenderView m re.sender ] , td [] [ mhtml ] ] in Maybe.map createRow <| Maybe.andThen (\f -> f m re) viewFunction -eventSenderView : String -> Html Msg -eventSenderView s = - span [ style "background-color" <| stringColor s, class "sender-wrapper" ] [ text <| senderName s ] +eventSenderView : Model -> String -> Html Msg +eventSenderView m s = + let + displayName = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData + in + span [ style "background-color" <| stringColor s, class "sender-wrapper" ] [ text displayName ] messageView : Model -> RoomEvent -> Maybe (Html Msg) messageView m re =