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

View File

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

View File

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