You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-18 05:42:00 +03:00
Merge pull request #41 from matrix-org/implicit_read_receipts
Implicit read receipts
This commit is contained in:
@@ -954,6 +954,9 @@ function _sendEvent(client, room, event, callback) {
|
|||||||
// the fake event we made above. If we don't find it, we're still
|
// the fake event we made above. If we don't find it, we're still
|
||||||
// waiting on the real event and so should assign the fake event
|
// waiting on the real event and so should assign the fake event
|
||||||
// with the real event_id for matching later.
|
// with the real event_id for matching later.
|
||||||
|
|
||||||
|
// FIXME: This manipulation of the room should probably be done
|
||||||
|
// inside the room class, not by the client.
|
||||||
var matchingEvent = utils.findElement(room.timeline, function(ev) {
|
var matchingEvent = utils.findElement(room.timeline, function(ev) {
|
||||||
return ev.getId() === eventId;
|
return ev.getId() === eventId;
|
||||||
}, true);
|
}, true);
|
||||||
@@ -966,13 +969,13 @@ function _sendEvent(client, room, event, callback) {
|
|||||||
matchingEvent.event.content = event.event.content;
|
matchingEvent.event.content = event.event.content;
|
||||||
matchingEvent.event.type = event.event.type;
|
matchingEvent.event.type = event.event.type;
|
||||||
}
|
}
|
||||||
utils.removeElement(room.timeline, function(ev) {
|
room.removeEvents([event.getId()]);
|
||||||
return ev.getId() === event.getId();
|
|
||||||
}, true);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
room.removeEvents([event.getId()]);
|
||||||
event.event.event_id = res.event_id;
|
event.event.event_id = res.event_id;
|
||||||
event.status = null;
|
event.status = null;
|
||||||
|
room.addEventsToTimeline([event]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -194,7 +194,26 @@ Room.prototype.addEventsToTimeline = function(events, toStartOfTimeline) {
|
|||||||
else {
|
else {
|
||||||
this.timeline.push(events[i]);
|
this.timeline.push(events[i]);
|
||||||
}
|
}
|
||||||
this.emit("Room.timeline", events[i], this, Boolean(toStartOfTimeline));
|
|
||||||
|
// synthesize and inject implicit read receipts
|
||||||
|
// Done after adding the event because otherwise the app would get a read receipt
|
||||||
|
// pointing to an event that wasn't yet in the timeline
|
||||||
|
|
||||||
|
// This is really ugly because JS has no way to express an object literal
|
||||||
|
// where the name of a key comes from an expression
|
||||||
|
if (events[i].sender) {
|
||||||
|
var fakeReceipt = {content: {}};
|
||||||
|
fakeReceipt.content[events[i].getId()] = {
|
||||||
|
'm.read': {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
fakeReceipt.content[events[i].getId()]['m.read'][events[i].sender.userId] = {
|
||||||
|
ts: events[i].getTs()
|
||||||
|
};
|
||||||
|
this.addReceipt(new MatrixEvent(fakeReceipt));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.emit("Room.timeline", events[i], this, Boolean(toStartOfTimeline), false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -260,6 +279,29 @@ Room.prototype.addEvents = function(events, duplicateStrategy) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes events from this room.
|
||||||
|
* @param {String} event_ids A list of event_ids to remove.
|
||||||
|
*/
|
||||||
|
Room.prototype.removeEvents = function(event_ids) {
|
||||||
|
// avoids defining a function in the loop, which is a lint error
|
||||||
|
function removeEventWithId(timeline, id) {
|
||||||
|
// NB. we supply reverse to search from the end,
|
||||||
|
// on the assumption that recents events are much
|
||||||
|
// more likley to be removed than older ones.
|
||||||
|
return utils.removeElement(timeline, function(e) {
|
||||||
|
return e.getId() == id;
|
||||||
|
}, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < event_ids.length; ++i) {
|
||||||
|
var removed = removeEventWithId(this.timeline, event_ids[i]);
|
||||||
|
if (removed) {
|
||||||
|
this.emit("Room.timeline", removed, this, undefined, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recalculate various aspects of the room, including the room name and
|
* Recalculate various aspects of the room, including the room name and
|
||||||
* room summary. Call this any time the room's current state is modified.
|
* room summary. Call this any time the room's current state is modified.
|
||||||
@@ -513,6 +555,7 @@ module.exports = Room;
|
|||||||
* @param {MatrixEvent} event The matrix event which caused this event to fire.
|
* @param {MatrixEvent} event The matrix event which caused this event to fire.
|
||||||
* @param {Room} room The room whose Room.timeline was updated.
|
* @param {Room} room The room whose Room.timeline was updated.
|
||||||
* @param {boolean} toStartOfTimeline True if this event was added to the start
|
* @param {boolean} toStartOfTimeline True if this event was added to the start
|
||||||
|
* @param {boolean} removed True if this event has just been removed from the timeline
|
||||||
* (beginning; oldest) of the timeline e.g. due to pagination.
|
* (beginning; oldest) of the timeline e.g. due to pagination.
|
||||||
* @example
|
* @example
|
||||||
* matrixClient.on("Room.timeline", function(event, room, toStartOfTimeline){
|
* matrixClient.on("Room.timeline", function(event, room, toStartOfTimeline){
|
||||||
|
@@ -152,19 +152,22 @@ module.exports.findElement = function(array, fn, reverse) {
|
|||||||
*/
|
*/
|
||||||
module.exports.removeElement = function(array, fn, reverse) {
|
module.exports.removeElement = function(array, fn, reverse) {
|
||||||
var i;
|
var i;
|
||||||
|
var removed;
|
||||||
if (reverse) {
|
if (reverse) {
|
||||||
for (i = array.length - 1; i >= 0; i--) {
|
for (i = array.length - 1; i >= 0; i--) {
|
||||||
if (fn(array[i], i, array)) {
|
if (fn(array[i], i, array)) {
|
||||||
|
removed = array[i];
|
||||||
array.splice(i, 1);
|
array.splice(i, 1);
|
||||||
return true;
|
return removed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (i = 0; i < array.length; i++) {
|
for (i = 0; i < array.length; i++) {
|
||||||
if (fn(array[i], i, array)) {
|
if (fn(array[i], i, array)) {
|
||||||
|
removed = array[i];
|
||||||
array.splice(i, 1);
|
array.splice(i, 1);
|
||||||
return true;
|
return removed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user