You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-25 05:23:13 +03:00
Dispatch thread events
This commit is contained in:
@@ -30,6 +30,7 @@ import { deepSortedObjectEntries } from "../utils";
|
|||||||
import { RoomMember } from "./room-member";
|
import { RoomMember } from "./room-member";
|
||||||
import { Thread } from "./thread";
|
import { Thread } from "./thread";
|
||||||
import { IActionsObject } from '../pushprocessor';
|
import { IActionsObject } from '../pushprocessor';
|
||||||
|
import { ReEmitter } from '../ReEmitter';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enum for event statuses.
|
* Enum for event statuses.
|
||||||
@@ -217,6 +218,8 @@ export class MatrixEvent extends EventEmitter {
|
|||||||
*/
|
*/
|
||||||
public verificationRequest = null;
|
public verificationRequest = null;
|
||||||
|
|
||||||
|
private readonly reEmitter: ReEmitter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a Matrix Event object
|
* Construct a Matrix Event object
|
||||||
* @constructor
|
* @constructor
|
||||||
@@ -266,6 +269,7 @@ export class MatrixEvent extends EventEmitter {
|
|||||||
|
|
||||||
this.txnId = event.txn_id || null;
|
this.txnId = event.txn_id || null;
|
||||||
this.localTimestamp = Date.now() - this.getAge();
|
this.localTimestamp = Date.now() - this.getAge();
|
||||||
|
this.reEmitter = new ReEmitter(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1287,6 +1291,7 @@ export class MatrixEvent extends EventEmitter {
|
|||||||
|
|
||||||
public setThread(thread: Thread): void {
|
public setThread(thread: Thread): void {
|
||||||
this.thread = thread;
|
this.thread = thread;
|
||||||
|
this.reEmitter.reEmit(thread, ["Thread.ready", "Thread.update"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getThread(): Thread {
|
public getThread(): Thread {
|
||||||
|
|||||||
@@ -866,7 +866,20 @@ export class Room extends EventEmitter {
|
|||||||
* @return {?module:models/event.MatrixEvent} the given event, or undefined if unknown
|
* @return {?module:models/event.MatrixEvent} the given event, or undefined if unknown
|
||||||
*/
|
*/
|
||||||
public findEventById(eventId: string): MatrixEvent | undefined {
|
public findEventById(eventId: string): MatrixEvent | undefined {
|
||||||
return this.getUnfilteredTimelineSet().findEventById(eventId);
|
let event = this.getUnfilteredTimelineSet().findEventById(eventId);
|
||||||
|
|
||||||
|
if (event) {
|
||||||
|
return event;
|
||||||
|
} else {
|
||||||
|
const threads = this.getThreads();
|
||||||
|
for (let i = 0; i < threads.length; i++) {
|
||||||
|
const thread = threads[i];
|
||||||
|
event = thread.findEventById(eventId);
|
||||||
|
if (event) {
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1261,6 +1274,24 @@ export class Room extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public addThreadedEvent(event: MatrixEvent): void {
|
||||||
|
if (event.getUnsigned().transaction_id) {
|
||||||
|
const existingEvent = this.txnToEvent[event.getUnsigned().transaction_id];
|
||||||
|
if (existingEvent) {
|
||||||
|
// remote echo of an event we sent earlier
|
||||||
|
this.handleRemoteEcho(event, existingEvent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let thread = this.findEventById(event.replyEventId)?.getThread();
|
||||||
|
if (thread) {
|
||||||
|
thread.addEvent(event);
|
||||||
|
} else {
|
||||||
|
thread = new Thread([event], this, this.client);
|
||||||
|
this.addThread(thread);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an event to the end of this room's live timelines. Will fire
|
* Add an event to the end of this room's live timelines. Will fire
|
||||||
* "Room.timeline".
|
* "Room.timeline".
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ export class Thread extends EventEmitter {
|
|||||||
* the tail/root references if needed
|
* the tail/root references if needed
|
||||||
* @param event The event to add
|
* @param event The event to add
|
||||||
*/
|
*/
|
||||||
public addEvent(event: MatrixEvent): void {
|
public async addEvent(event: MatrixEvent): Promise<void> {
|
||||||
if (this.events.has(event.getId()) || event.status !== null) {
|
if (this.events.has(event.getId()) || event.status !== null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -62,25 +62,26 @@ export class Thread extends EventEmitter {
|
|||||||
|
|
||||||
event.setThread(this);
|
event.setThread(this);
|
||||||
this.events.set(event.getId(), event);
|
this.events.set(event.getId(), event);
|
||||||
this._timelineSet.addEventToTimeline(
|
this._timelineSet.addLiveEvent(event);
|
||||||
event,
|
|
||||||
this._timelineSet.getLiveTimeline(),
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (this.ready) {
|
if (this.ready) {
|
||||||
this.client.decryptEventIfNeeded(event, {});
|
this.client.decryptEventIfNeeded(event, {});
|
||||||
this.emit("Thread.update", this);
|
this.emit("Thread.update", this);
|
||||||
|
} else {
|
||||||
|
this.emit("Thread.update", this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchReplyChain(): Promise<void> {
|
public async fetchReplyChain(): Promise<void> {
|
||||||
if (!this.ready) {
|
if (!this.ready) {
|
||||||
const mxEvent = await this.fetchEventById(
|
let mxEvent = this.room.findEventById(this.rootEvent.replyEventId);
|
||||||
|
if (!mxEvent) {
|
||||||
|
mxEvent = await this.fetchEventById(
|
||||||
this.rootEvent.getRoomId(),
|
this.rootEvent.getRoomId(),
|
||||||
this.rootEvent.replyEventId,
|
this.rootEvent.replyEventId,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this.addEvent(mxEvent);
|
this.addEvent(mxEvent);
|
||||||
if (mxEvent.replyEventId) {
|
if (mxEvent.replyEventId) {
|
||||||
await this.fetchReplyChain();
|
await this.fetchReplyChain();
|
||||||
@@ -101,7 +102,7 @@ export class Thread extends EventEmitter {
|
|||||||
this.decrypted = true;
|
this.decrypted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchEventById(roomId: string, eventId: string): Promise<MatrixEvent> {
|
private async fetchEventById(roomId: string, eventId: string): Promise<MatrixEvent> {
|
||||||
const response = await this.client.http.authedRequest(
|
const response = await this.client.http.authedRequest(
|
||||||
undefined,
|
undefined,
|
||||||
"GET",
|
"GET",
|
||||||
@@ -110,8 +111,12 @@ export class Thread extends EventEmitter {
|
|||||||
return new MatrixEvent(response);
|
return new MatrixEvent(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public findEventById(eventId: string) {
|
||||||
|
return this.events.get(eventId);
|
||||||
|
}
|
||||||
|
|
||||||
public get ready(): boolean {
|
public get ready(): boolean {
|
||||||
return this.rootEvent.replyEventId === undefined && this.decrypted;
|
return this.rootEvent.replyEventId === undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
14
src/sync.ts
14
src/sync.ts
@@ -301,7 +301,7 @@ export class SyncApi {
|
|||||||
client.store.storeRoom(room);
|
client.store.storeRoom(room);
|
||||||
client.emit("Room", room);
|
client.emit("Room", room);
|
||||||
|
|
||||||
this.processEventsForNotifs(room, timelineEvents);
|
this.processEventsForNotifs(room, events);
|
||||||
});
|
});
|
||||||
return rooms;
|
return rooms;
|
||||||
});
|
});
|
||||||
@@ -1314,7 +1314,7 @@ export class SyncApi {
|
|||||||
client.emit("Room", room);
|
client.emit("Room", room);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.processEventsForNotifs(room, timelineEvents);
|
this.processEventsForNotifs(room, events);
|
||||||
|
|
||||||
const processRoomEvent = async (e) => {
|
const processRoomEvent = async (e) => {
|
||||||
client.emit("event", e);
|
client.emit("event", e);
|
||||||
@@ -1370,7 +1370,7 @@ export class SyncApi {
|
|||||||
client.emit("Room", room);
|
client.emit("Room", room);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.processEventsForNotifs(room, timelineEvents);
|
this.processEventsForNotifs(room, events);
|
||||||
|
|
||||||
stateEvents.forEach(function(e) {
|
stateEvents.forEach(function(e) {
|
||||||
client.emit("event", e);
|
client.emit("event", e);
|
||||||
@@ -1702,13 +1702,7 @@ export class SyncApi {
|
|||||||
|
|
||||||
private processThreadEvents(room: Room, threadedEvents: MatrixEvent[]): void {
|
private processThreadEvents(room: Room, threadedEvents: MatrixEvent[]): void {
|
||||||
threadedEvents.forEach(event => {
|
threadedEvents.forEach(event => {
|
||||||
let thread = room.findEventById(event.replyEventId).getThread();
|
room.addThreadedEvent(event);
|
||||||
if (thread) {
|
|
||||||
thread.addEvent(event);
|
|
||||||
} else {
|
|
||||||
thread = new Thread([event], room, this.client);
|
|
||||||
room.addThread(thread);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user