diff --git a/src/base-apis.js b/src/base-apis.js index fdc194ba941..6752ad1c5e2 100644 --- a/src/base-apis.js +++ b/src/base-apis.js @@ -470,6 +470,34 @@ MatrixBaseApis.prototype.roomInitialSync = function(roomId, limit, callback) { ); }; +/** + * Set a marker to indicate the point in a room before which the user has read every + * event. This can be retrieved from room account data (the event type is `m.fully_read`) + * and displayed as a horizontal line in the timeline that is visually distinct to the + * position of the user's own read receipt. + * @param {string} roomId ID of the room that has been read + * @param {string} rmEventId ID of the event that has been read + * @param {string} rrEventId ID of the event tracked by the read receipt. This is here + * for convenience because the RR and the RM are commonly updated at the same time as + * each other. Optional. + * @return {module:client.Promise} Resolves: the empty object, {}. + */ +MatrixBaseApis.prototype.setRoomReadMarkersHttpRequest = + function(roomId, rmEventId, rrEventId) { + const path = utils.encodeUri("/rooms/$roomId/read_markers", { + $roomId: roomId, + }); + + const content = { + "m.fully_read": rmEventId, + "m.read": rrEventId, + }; + + return this._http.authedRequest( + undefined, "POST", path, undefined, content, + ); +}; + // Room Directory operations // ========================= diff --git a/src/client.js b/src/client.js index 0b74f3276e5..3aff6213e4d 100644 --- a/src/client.js +++ b/src/client.js @@ -1201,6 +1201,33 @@ MatrixClient.prototype.sendReadReceipt = function(event, callback) { return this.sendReceipt(event, "m.read", callback); }; +/** + * Set a marker to indicate the point in a room before which the user has read every + * event. This can be retrieved from room account data (the event type is `m.fully_read`) + * and displayed as a horizontal line in the timeline that is visually distinct to the + * position of the user's own read receipt. + * @param {string} roomId ID of the room that has been read + * @param {string} eventId ID of the event that has been read + * @param {string} rrEvent the event tracked by the read receipt. This is here for + * convenience because the RR and the RM are commonly updated at the same time as each + * other. The local echo of this receipt will be done if set. Optional. + * @return {module:client.Promise} Resolves: the empty object, {}. + */ +MatrixClient.prototype.setRoomReadMarkers = function(roomId, eventId, rrEvent) { + const rmEventId = eventId; + let rrEventId; + + // Add the optional RR update, do local echo like `sendReceipt` + if (rrEvent) { + rrEventId = rrEvent.getId(); + const room = this.getRoom(roomId); + if (room) { + room._addLocalEchoReceipt(this.credentials.userId, rrEvent, "m.read"); + } + } + + return this.setRoomReadMarkersHttpRequest(roomId, rmEventId, rrEventId); +}; /** * Get a preview of the given URL as of (roughly) the given point in time,