handle sending relations to events that haven't been sent yet

This commit is contained in:
Bruno Windels 2021-06-04 10:48:59 +02:00
parent 2878208e94
commit 8bf160dfc0
3 changed files with 30 additions and 5 deletions

View file

@ -16,6 +16,7 @@ limitations under the License.
import {createEnum} from "../../../utils/enum.js"; import {createEnum} from "../../../utils/enum.js";
import {AbortError} from "../../../utils/error.js"; import {AbortError} from "../../../utils/error.js";
import {REDACTION_TYPE} from "../common.js"; import {REDACTION_TYPE} from "../common.js";
import {getRelationFromContent} from "../timeline/relations.js";
export const SendStatus = createEnum( export const SendStatus = createEnum(
"Waiting", "Waiting",
@ -49,11 +50,24 @@ export class PendingEvent {
get remoteId() { return this._data.remoteId; } get remoteId() { return this._data.remoteId; }
get content() { return this._data.content; } get content() { return this._data.content; }
get relatedTxnId() { return this._data.relatedTxnId; } get relatedTxnId() { return this._data.relatedTxnId; }
get relatedEventId() { return this._data.relatedEventId; } get relatedEventId() {
const relation = getRelationFromContent(this.content);
if (relation) {
// may be null when target is not sent yet, is indented
return relation.event_id;
} else {
return this._data.relatedEventId;
}
}
setRelatedEventId(eventId) { setRelatedEventId(eventId) {
const relation = getRelationFromContent(this.content);
if (relation) {
relation.event_id = eventId;
} else {
this._data.relatedEventId = eventId; this._data.relatedEventId = eventId;
} }
}
get data() { return this._data; } get data() { return this._data; }

View file

@ -19,6 +19,7 @@ import {ConnectionError} from "../../error.js";
import {PendingEvent, SendStatus} from "./PendingEvent.js"; import {PendingEvent, SendStatus} from "./PendingEvent.js";
import {makeTxnId, isTxnId} from "../../common.js"; import {makeTxnId, isTxnId} from "../../common.js";
import {REDACTION_TYPE} from "../common.js"; import {REDACTION_TYPE} from "../common.js";
import {getRelationFromContent} from "../timeline/relations.js";
export class SendQueue { export class SendQueue {
constructor({roomId, storage, hsApi, pendingEvents}) { constructor({roomId, storage, hsApi, pendingEvents}) {
@ -197,7 +198,13 @@ export class SendQueue {
} }
async enqueueEvent(eventType, content, attachments, log) { async enqueueEvent(eventType, content, attachments, log) {
await this._enqueueEvent(eventType, content, attachments, null, null, log); const relation = getRelationFromContent(content);
let relatedTxnId = null;
if (relation && isTxnId(relation.event_id)) {
relatedTxnId = relation.event_id;
relation.event_id = null;
}
await this._enqueueEvent(eventType, content, attachments, relatedTxnId, null, log);
} }
async _enqueueEvent(eventType, content, attachments, relatedTxnId, relatedEventId, log) { async _enqueueEvent(eventType, content, attachments, relatedTxnId, relatedEventId, log) {

View file

@ -41,7 +41,11 @@ export function getRelatedEventId(event) {
return null; return null;
} }
export function getRelation(event) { export function getRelationFromContent(content) {
return event.content?.["m.relates_to"]; return content?.["m.relates_to"];
}
export function getRelation(event) {
return getRelationFromContent(event.content);
} }