1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-12-05 17:02:07 +03:00

Add getRelation helper

This adds a `getRelation` helper to ensure we always read relation info from the
wire content as required in E2E rooms.
This commit is contained in:
J. Ryan Stinnett
2019-05-15 13:42:06 +01:00
parent ce0b014a5a
commit f17ecba519
4 changed files with 26 additions and 19 deletions

View File

@@ -719,9 +719,8 @@ EventTimelineSet.prototype._aggregateRelations = function(event) {
return; return;
} }
const content = event.getContent(); const relation = event.getRelation();
const relation = content && content["m.relates_to"]; if (!relation) {
if (!relation || !relation.rel_type || !relation.event_id) {
return; return;
} }

View File

@@ -735,20 +735,34 @@ utils.extend(module.exports.MatrixEvent.prototype, {
}, },
/** /**
* Get whether the event is a relation event, and of a given type if `relType` is passed in. * Get whether the event is a relation event, and of a given type if
* `relType` is passed in.
* *
* @param {string?} relType if given, checks that the relation is of the given type * @param {string?} relType if given, checks that the relation is of the
* given type
* @return {boolean} * @return {boolean}
*/ */
isRelation(relType = undefined) { isRelation(relType = undefined) {
// must use event.content as m.relates_to is not encrypted // Relation info is lifted out of the encrypted content when sent to
// and _clearEvent doesn't have it. // encrypted rooms, so we have to check `getWireContent` for this.
const content = this.event.content; const content = this.getWireContent();
const relation = content && content["m.relates_to"]; const relation = content && content["m.relates_to"];
return relation && relation.rel_type && relation.event_id && return relation && relation.rel_type && relation.event_id &&
((relType && relation.rel_type === relType) || !relType); ((relType && relation.rel_type === relType) || !relType);
}, },
/**
* Get relation info for the event, if any.
*
* @return {Object}
*/
getRelation() {
if (!this.isRelation()) {
return null;
}
return this.getWireContent()["m.relates_to"];
},
/** /**
* Set an event that replaces the content of this event, through an m.replace relation. * Set an event that replaces the content of this event, through an m.replace relation.
* *
@@ -768,11 +782,7 @@ utils.extend(module.exports.MatrixEvent.prototype, {
} }
const oldContent = this.getContent(); const oldContent = this.getContent();
const newContent = newEvent.getContent()["m.new_content"]; const newContent = newEvent.getContent()["m.new_content"];
// need to always replace m.relates_to with the old one, Object.assign(oldContent, newContent);
// even if there is none, as the m.replace relation should
// not be exposed on the target event m.relates_to (that's what the server does).
Object.assign(oldContent, newContent,
{"m.relates_to": oldContent["m.relates_to"]});
this._replacingEvent = newEvent; this._replacingEvent = newEvent;
}, },

View File

@@ -52,9 +52,8 @@ export default class Relations extends EventEmitter {
* The new relation event to be aggregated. * The new relation event to be aggregated.
*/ */
addEvent(event) { addEvent(event) {
const content = event.getContent(); const relation = event.getRelation();
const relation = content && content["m.relates_to"]; if (!relation) {
if (!relation || !relation.rel_type || !relation.event_id) {
console.error("Event must have relation info"); console.error("Event must have relation info");
return; return;
} }
@@ -139,8 +138,7 @@ export default class Relations extends EventEmitter {
if (this.relationType === "m.annotation") { if (this.relationType === "m.annotation") {
// Remove the redacted annotation from aggregation by key // Remove the redacted annotation from aggregation by key
const content = redactedEvent.getContent(); const relation = redactedEvent.getRelation();
const relation = content && content["m.relates_to"];
if (!relation) { if (!relation) {
return; return;
} }

View File

@@ -1034,7 +1034,7 @@ Room.prototype._addLiveEvent = function(event, duplicateStrategy) {
if (this._opts.unstableClientRelationReplacements && event.isRelation("m.replace")) { if (this._opts.unstableClientRelationReplacements && event.isRelation("m.replace")) {
const relatesTo = event.getContent()["m.relates_to"]; const relatesTo = event.getRelation();
const replacedId = relatesTo && relatesTo.event_id; const replacedId = relatesTo && relatesTo.event_id;
const replacedEvent = this.getUnfilteredTimelineSet().findEventById(replacedId); const replacedEvent = this.getUnfilteredTimelineSet().findEventById(replacedId);
if (replacedEvent) { if (replacedEvent) {