Skip to content

Commit

Permalink
process incoming poll relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerry Archibald committed Dec 30, 2022
1 parent 5ca533c commit 984e5eb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
41 changes: 35 additions & 6 deletions src/models/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { TypedEventEmitter } from "./typed-event-emitter";

export enum PollEvent {
New = "Poll.new",
End = "Poll.end",
Update = "Poll.update",
Responses = "Poll.Responses",
Destroy = "Poll.Destroy",
Expand All @@ -32,7 +33,7 @@ export enum PollEvent {
export type PollEventHandlerMap = {
[PollEvent.Update]: (event: MatrixEvent, poll: Poll) => void;
[PollEvent.Destroy]: (pollIdentifier: string) => void;
[PollEvent.Destroy]: (pollIdentifier: string) => void;
[PollEvent.End]: () => void;
[PollEvent.Responses]: (responses: Relations) => void;
};

Expand All @@ -51,7 +52,7 @@ export class Poll extends TypedEventEmitter<Exclude<PollEvent, PollEvent.New>, P
private pollEvent: PollStartEvent | undefined;
private fetchingResponsesPromise: null | Promise<void> = null;
private responses: null | Relations = null;
private closeEvent: MatrixEvent | undefined;
private endEvent: MatrixEvent | undefined;

public constructor(private rootEvent: MatrixEvent, private matrixClient: MatrixClient) {
super();
Expand All @@ -65,7 +66,7 @@ export class Poll extends TypedEventEmitter<Exclude<PollEvent, PollEvent.New>, P

public get isEnded(): boolean {
// @TODO(kerrya) should be false while responses are loading?
return !!this.closeEvent;
return !!this.endEvent;
}

public setPollInstance(event: MatrixEvent): void {
Expand Down Expand Up @@ -96,6 +97,33 @@ export class Poll extends TypedEventEmitter<Exclude<PollEvent, PollEvent.New>, P
return this.responses!;
}

public onNewRelation(event: MatrixEvent): void {
if (M_POLL_END.matches(event.getType())) {
this.endEvent = event;
this.emit(PollEvent.End);
}

// wait for poll to be initialised
// @TODO(kerrya) races here?
if (!this.responses) {
return;
}

if (event.isDecryptionFailure()) {
// undecryptableRelationsCount++
return;
}
const pollCloseTimestamp = this.endEvent?.getTs() || Number.MAX_SAFE_INTEGER;
if (
M_POLL_RESPONSE.matches(event.getType()) &&
// response made before poll closed
event.getTs() <= pollCloseTimestamp
) {
this.responses.addEvent(event);
}

}

private async fetchResponses(): Promise<void> {
this.fetchingResponsesPromise = new Promise<void>(() => {});

Expand All @@ -116,8 +144,8 @@ export class Poll extends TypedEventEmitter<Exclude<PollEvent, PollEvent.New>, P
const responses = new Relations('m.reference', M_POLL_RESPONSE.name, this.matrixClient);
let undecryptableRelationsCount = 0;

const pollCloseEvent = allRelations.events.find(event => M_POLL_END.matches(event.getType()));
const pollCloseTimestamp = pollCloseEvent?.getTs() || Number.MAX_SAFE_INTEGER;
const pollEndEvent = allRelations.events.find(event => M_POLL_END.matches(event.getType()));
const pollCloseTimestamp = pollEndEvent?.getTs() || Number.MAX_SAFE_INTEGER;

allRelations.events.forEach(event => {
if (event.isDecryptionFailure()) {
Expand All @@ -135,8 +163,9 @@ export class Poll extends TypedEventEmitter<Exclude<PollEvent, PollEvent.New>, P

console.log('hhh', 'relations!!', responses);


this.responses = responses;
this.closeEvent = pollCloseEvent;
this.endEvent = pollEndEvent;
this.emit(PollEvent.Responses, this.responses);
}
}
19 changes: 16 additions & 3 deletions src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { M_POLL_START, Optional } from "matrix-events-sdk";
import { M_POLL_RESPONSE, M_POLL_START, Optional } from "matrix-events-sdk";

import {
EventTimelineSet,
Expand Down Expand Up @@ -1878,16 +1878,29 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
this.emit(PollEvent.New, poll);
}

const processPollRelationEvent = (event: MatrixEvent) => {
const relationEventId = event.getRelation()?.event_id;
if (relationEventId && this.polls.has(relationEventId)) {
const poll = this.polls.get(relationEventId);
poll?.onNewRelation(event);
}
}

const processPollEvent = (event: MatrixEvent) => {
processPollStartEvent(event);
processPollRelationEvent(event);
}

events.forEach((event: MatrixEvent) => {
matrixClient.decryptEventIfNeeded(event);

if (event.isBeingDecrypted() || event.isDecryptionFailure()) {
// add an event listener for once the event is decrypted.
event.once(MatrixEventEvent.Decrypted, async () => {
processPollStartEvent(event);
processPollEvent(event);
});
} else {
processPollStartEvent(event);
processPollEvent(event);
}
});
}
Expand Down

0 comments on commit 984e5eb

Please sign in to comment.