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 {AbortError} from "../../../utils/error.js";
import {REDACTION_TYPE} from "../common.js";
import {getRelationFromContent} from "../timeline/relations.js";
export const SendStatus = createEnum(
"Waiting",
@ -49,10 +50,23 @@ export class PendingEvent {
get remoteId() { return this._data.remoteId; }
get content() { return this._data.content; }
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) {
this._data.relatedEventId = eventId;
const relation = getRelationFromContent(this.content);
if (relation) {
relation.event_id = eventId;
} else {
this._data.relatedEventId = eventId;
}
}
get data() { return this._data; }

View File

@ -19,6 +19,7 @@ import {ConnectionError} from "../../error.js";
import {PendingEvent, SendStatus} from "./PendingEvent.js";
import {makeTxnId, isTxnId} from "../../common.js";
import {REDACTION_TYPE} from "../common.js";
import {getRelationFromContent} from "../timeline/relations.js";
export class SendQueue {
constructor({roomId, storage, hsApi, pendingEvents}) {
@ -197,7 +198,13 @@ export class SendQueue {
}
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) {

View File

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