From 6c96bae01fe659e5ae55d3ae76c371ca07bb2354 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 15 Dec 2018 19:22:49 -0800 Subject: [PATCH] Send read receipts for messages received after opening room. --- src/Main.elm | 8 +++++++- src/Scylla/Http.elm | 18 ++++++++++++++++++ src/Scylla/Model.elm | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Main.elm b/src/Main.elm index 22fb8b2..fe812de 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -72,6 +72,7 @@ update msg model = case msg of ChangeRoomText r t -> ({ model | roomText = Dict.insert r t model.roomText}, Cmd.none) SendRoomText r -> updateSendRoomText model r SendRoomTextResponse r -> (model, Cmd.none) + ReceiveCompletedReadMarker r -> (model, Cmd.none) updateViewportAfterMessage : Model -> Result Browser.Dom.Error Viewport -> (Model, Cmd Msg) updateViewportAfterMessage m vr = @@ -135,7 +136,8 @@ updateSyncResponse model r notify = , room = s }) <| notification sr else Cmd.none - roomMessages sr = case currentRoomId model of + room = currentRoomId model + roomMessages sr = case room of Just rid -> List.filter (((==) "m.room.message") << .type_) <| Maybe.withDefault [] <| Maybe.andThen .events @@ -148,6 +150,9 @@ updateSyncResponse model r notify = <| roomMessages sr then Cmd.none else Task.attempt ViewportAfterMessage (Browser.Dom.getViewportOf "events-wrapper") + setReadReceiptCmd sr = case (room, List.head <| List.reverse <| roomMessages sr) of + (Just rid, Just re) -> setReadMarkers model.apiUrl token rid re.eventId <| Just re.eventId + _ -> Cmd.none in case r of Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr }, Cmd.batch @@ -155,6 +160,7 @@ updateSyncResponse model r notify = , newUserCmd sr , notificationCmd sr , setScrollCmd sr + , setReadReceiptCmd sr ]) _ -> (model, syncCmd) diff --git a/src/Scylla/Http.elm b/src/Scylla/Http.elm index 25a368b..f6a23aa 100644 --- a/src/Scylla/Http.elm +++ b/src/Scylla/Http.elm @@ -1,6 +1,7 @@ module Scylla.Http exposing (..) import Scylla.Model exposing (..) import Scylla.Api exposing (..) +import Scylla.Route exposing (RoomId) import Scylla.Sync exposing (syncResponseDecoder) import Scylla.Login exposing (loginResponseDecoder, Username, Password) import Scylla.UserData exposing (userDataDecoder, UserData) @@ -81,3 +82,20 @@ userData apiUrl token username = request , timeout = Nothing , tracker = Nothing } + +setReadMarkers : ApiUrl -> ApiToken -> String -> RoomId -> Maybe String -> Cmd Msg +setReadMarkers apiUrl token roomId fullyRead readReceipt = + let + readReciptFields = case readReceipt of + Just s -> [ ("m.read", string s) ] + _ -> [] + in + request + { method = "POST" + , headers = authenticatedHeaders token + , url = (fullClientUrl apiUrl) ++ "/rooms/" ++ roomId ++ "/read_markers" + , body = jsonBody <| object <| [ ("m.fully_read", string fullyRead) ] ++ readReciptFields + , expect = expectWhatever ReceiveCompletedReadMarker + , timeout = Nothing + , tracker = Nothing + } diff --git a/src/Scylla/Model.elm b/src/Scylla/Model.elm index 07c7e2f..57666a7 100644 --- a/src/Scylla/Model.elm +++ b/src/Scylla/Model.elm @@ -43,6 +43,7 @@ type Msg = | ReceiveSyncResponse (Result Http.Error SyncResponse) -- HTTP, Sync has finished | ReceiveLoginResponse (Result Http.Error LoginResponse) -- HTTP, Login has finished | ReceiveUserData Username (Result Http.Error UserData) + | ReceiveCompletedReadMarker (Result Http.Error ()) displayName : Model -> Username -> String displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData