Retrieve user display names on initial log in.

This commit is contained in:
Danila Fedorin 2018-12-13 12:45:30 -08:00
parent 996da079e2
commit b25e5d77af
3 changed files with 20 additions and 9 deletions

View File

@ -6,6 +6,7 @@ import Scylla.Model exposing (..)
import Scylla.Http exposing (..) import Scylla.Http exposing (..)
import Scylla.Views exposing (viewFull) import Scylla.Views exposing (viewFull)
import Scylla.Route exposing (Route(..)) import Scylla.Route exposing (Route(..))
import Scylla.UserData exposing (..)
import Url exposing (Url) import Url exposing (Url)
import Url.Parser exposing (parse) import Url.Parser exposing (parse)
import Url.Builder import Url.Builder
@ -61,11 +62,16 @@ update msg model = case msg of
ReceiveLoginResponse r -> updateLoginResponse model r ReceiveLoginResponse r -> updateLoginResponse model r
ReceiveFirstSyncResponse r -> updateSyncResponse model r False ReceiveFirstSyncResponse r -> updateSyncResponse model r False
ReceiveSyncResponse r -> updateSyncResponse model r True 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) ChangeRoomText r t -> ({ model | roomText = Dict.insert r t model.roomText}, Cmd.none)
SendRoomText r -> updateSendRoomText model r SendRoomText r -> updateSendRoomText model r
SendRoomTextResponse r -> (model, Cmd.none) 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 : Model -> String -> (Model, Cmd Msg)
updateSendRoomText m r = updateSendRoomText m r =
let let
@ -96,11 +102,13 @@ updateSyncResponse model r notify =
token = Maybe.withDefault "" model.token token = Maybe.withDefault "" model.token
nextBatch = Result.withDefault model.sync.nextBatch nextBatch = Result.withDefault model.sync.nextBatch
<| Result.map .nextBatch r <| 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 in
case r of case r of
Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr }, cmd) Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr }, Cmd.batch [ syncCmd, newUserCommands sr ])
_ -> (model, cmd) _ -> (model, syncCmd)
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
subscriptions m = Sub.none subscriptions m = Sub.none

View File

@ -399,4 +399,4 @@ roomsUsers s =
joinedUsers = usersFor .join joinedUsers = usersFor .join
leftUsers = usersFor .leave leftUsers = usersFor .leave
in in
leftUsers ++ joinedUsers uniqueBy (\u -> u) <| leftUsers ++ joinedUsers

View File

@ -119,16 +119,19 @@ eventView m re =
"m.room.message" -> Just messageView "m.room.message" -> Just messageView
_ -> Nothing _ -> Nothing
createRow mhtml = tr [] createRow mhtml = tr []
[ td [] [ eventSenderView re.sender ] [ td [] [ eventSenderView m re.sender ]
, td [] [ mhtml ] , td [] [ mhtml ]
] ]
in in
Maybe.map createRow Maybe.map createRow
<| Maybe.andThen (\f -> f m re) viewFunction <| Maybe.andThen (\f -> f m re) viewFunction
eventSenderView : String -> Html Msg eventSenderView : Model -> String -> Html Msg
eventSenderView s = eventSenderView m s =
span [ style "background-color" <| stringColor s, class "sender-wrapper" ] [ text <| senderName 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 : Model -> RoomEvent -> Maybe (Html Msg)
messageView m re = messageView m re =