Skip to content

Commit

Permalink
Include pending events in thread summary and count again
Browse files Browse the repository at this point in the history
  • Loading branch information
justjanne committed Nov 30, 2022
1 parent 1606274 commit 87171f8
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/models/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ limitations under the License.

import { Optional } from "matrix-events-sdk";

import { MatrixClient } from "../client";
import { MatrixClient, PendingEventOrdering } from "../client";
import { TypedReEmitter } from "../ReEmitter";
import { RelationType } from "../@types/event";
import { IThreadBundledRelationship, MatrixEvent, MatrixEventEvent } from "./event";
import { EventStatus, IThreadBundledRelationship, MatrixEvent, MatrixEventEvent } from "./event";
import { EventTimeline } from "./event-timeline";
import { EventTimelineSet, EventTimelineSetHandlerMap } from './event-timeline-set';
import { NotificationCountType, Room, RoomEvent } from './room';
Expand Down Expand Up @@ -51,6 +51,7 @@ export type EventHandlerMap = {
interface IThreadOpts {
room: Room;
client: MatrixClient;
pendingEventOrdering?: PendingEventOrdering;
}

export enum FeatureSupport {
Expand Down Expand Up @@ -88,9 +89,12 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {

private lastEvent: MatrixEvent | undefined;
private replyCount = 0;
private lastPendingEvent: MatrixEvent | undefined;
private pendingReplyCount = 0;

public readonly room: Room;
public readonly client: MatrixClient;
private readonly pendingEventOrdering: PendingEventOrdering;

public initialEventsFetched = !Thread.hasServerSideSupport;

Expand All @@ -109,6 +113,7 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {

this.room = opts.room;
this.client = opts.client;
this.pendingEventOrdering = opts.pendingEventOrdering ?? PendingEventOrdering.Chronological;
this.timelineSet = new EventTimelineSet(this.room, {
timelineSupport: true,
pendingEvents: true,
Expand Down Expand Up @@ -300,6 +305,18 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
bundledRelationship = this.getRootEventBundledRelationship();
}

let pendingEvents: MatrixEvent[] = [];
if (this.pendingEventOrdering === PendingEventOrdering.Detached) {
pendingEvents = this.room.getPendingEvents().filter((ev) => {
const isNotSent = ev.status === EventStatus.NOT_SENT;
const belongsToTheThread = this.id === ev.threadRootId;
return isNotSent && belongsToTheThread;
});
pendingEvents.map((ev) => this.processEvent(ev));
}
this.lastPendingEvent = pendingEvents.length ? pendingEvents[pendingEvents.length - 1] : undefined;
this.pendingReplyCount = pendingEvents.length;

if (Thread.hasServerSideSupport && bundledRelationship) {
this.replyCount = bundledRelationship.count;
this._currentUserParticipated = !!bundledRelationship.current_user_participated;
Expand Down Expand Up @@ -393,14 +410,14 @@ export class Thread extends ReadReceipt<EmittedEvents, EventHandlerMap> {
* exclude annotations from that number
*/
public get length(): number {
return this.replyCount;
return this.replyCount + this.pendingReplyCount;
}

/**
* A getter for the last event added to the thread, if known.
*/
public get replyToEvent(): Optional<MatrixEvent> {
return this.lastEvent ?? this.lastReply();
return this.lastPendingEvent ?? this.lastEvent ?? this.lastReply();
}

public get events(): MatrixEvent[] {
Expand Down

0 comments on commit 87171f8

Please sign in to comment.