Update new users in history responses.

This commit is contained in:
Danila Fedorin 2018-12-20 16:39:10 -08:00
parent 437039bcc4
commit 2cdfc45a93
3 changed files with 35 additions and 18 deletions

View File

@ -82,8 +82,17 @@ update msg model = case msg of
ReceiveHistoryResponse r hr -> updateHistoryResponse model r hr ReceiveHistoryResponse r hr -> updateHistoryResponse model r hr
updateHistoryResponse : Model -> RoomId -> Result Http.Error HistoryResponse -> (Model, Cmd Msg) updateHistoryResponse : Model -> RoomId -> Result Http.Error HistoryResponse -> (Model, Cmd Msg)
updateHistoryResponse m r hr = case hr of updateHistoryResponse m r hr =
Ok h -> ({ m | sync = appendHistoryResponse m.sync r h }, Cmd.none) let
newUsersCmd h = Cmd.batch
<| List.map (userData m.apiUrl (Maybe.withDefault "" m.token))
<| newUsers m
<| uniqueBy (\s -> s)
<| List.map .sender
<| h.chunk
in
case hr of
Ok h -> ({ m | sync = appendHistoryResponse m.sync r h }, newUsersCmd h)
Err _ -> (m, Cmd.none) Err _ -> (m, Cmd.none)
updateHistory : Model -> RoomId -> (Model, Cmd Msg) updateHistory : Model -> RoomId -> (Model, Cmd Msg)
@ -212,11 +221,11 @@ updateSyncResponse model r notify =
nextBatch = Result.withDefault model.sync.nextBatch nextBatch = Result.withDefault model.sync.nextBatch
<| Result.map .nextBatch r <| Result.map .nextBatch r
syncCmd = sync model.apiUrl token nextBatch syncCmd = sync model.apiUrl token nextBatch
newUsers sr = List.filter (\s -> not <| Dict.member s model.userData) <| roomsUsers sr
newUserCmd sr = Cmd.batch newUserCmd sr = Cmd.batch
<| List.map (userData model.apiUrl <| List.map (userData model.apiUrl
<| Maybe.withDefault "" model.token) <| Maybe.withDefault "" model.token)
<| newUsers sr <| newUsers model
<| allUsers sr
notification sr = findFirstBy notification sr = findFirstBy
(\(s, e) -> e.originServerTs) (\(s, e) -> e.originServerTs)
(\(s, e) -> e.sender /= model.loginUsername) (\(s, e) -> e.sender /= model.loginUsername)

View File

@ -62,6 +62,9 @@ roomUrl s = Url.Builder.absolute [ "room", s ] []
loginUrl : String loginUrl : String
loginUrl = Url.Builder.absolute [ "login" ] [] loginUrl = Url.Builder.absolute [ "login" ] []
newUsers : Model -> List Username -> List Username
newUsers m lus = List.filter (\u -> not <| Dict.member u m.userData) lus
currentRoom : Model -> Maybe JoinedRoom currentRoom : Model -> Maybe JoinedRoom
currentRoom m = currentRoom m =
let let

View File

@ -448,9 +448,9 @@ roomStateEvents jr =
, prevContent = Nothing , prevContent = Nothing
, stateKey = "" , stateKey = ""
} }
allEvents = uniqueBy .eventId (stateEvents ++ (List.map roomToStateEvent timelineEvents)) allStateEvents = uniqueBy .eventId (stateEvents ++ (List.map roomToStateEvent timelineEvents))
in in
allEvents allStateEvents
roomAccountData : JoinedRoom -> String -> Maybe Decode.Value roomAccountData : JoinedRoom -> String -> Maybe Decode.Value
roomAccountData jr et = roomAccountData jr et =
@ -495,16 +495,21 @@ roomTypingUsers jr = Maybe.withDefault []
<| Maybe.andThen (findLast (((==) "m.typing") << .type_)) <| Maybe.andThen (findLast (((==) "m.typing") << .type_))
<| Maybe.andThen .events jr.ephemeral <| Maybe.andThen .events jr.ephemeral
roomsUsers : SyncResponse -> List Username allRoomDictEvents : Dict String { a | timeline : Maybe Timeline } -> List RoomEvent
roomsUsers s = allRoomDictEvents dict = List.concatMap (Maybe.withDefault [] << .events)
let <| List.filterMap .timeline
users dict =
List.map .sender
<| (List.concatMap <| Maybe.withDefault [] << .events)
<| (List.filterMap .timeline)
<| Dict.values dict <| Dict.values dict
usersFor f = Maybe.withDefault [] <| Maybe.map users <| Maybe.andThen f s.rooms
joinedUsers = usersFor .join allEvents : SyncResponse -> List RoomEvent
leftUsers = usersFor .leave allEvents s =
let
eventsFor f = Maybe.withDefault []
<| Maybe.map allRoomDictEvents
<| Maybe.andThen f s.rooms
joinedEvents = eventsFor .join
leftEvents = eventsFor .leave
in in
uniqueBy (\u -> u) <| leftUsers ++ joinedUsers uniqueBy .eventId <| leftEvents ++ joinedEvents
allUsers : SyncResponse -> List Username
allUsers s = uniqueBy (\u -> u) <| List.map .sender <| allEvents s