summaryrefslogtreecommitdiff
path: root/includes/external/matrix/node_modules/matrix-js-sdk/src/models/poll.ts
blob: 1d4344a901ce28334f8b907c2b314b65071fd98b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
Copyright 2023 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { M_POLL_END, M_POLL_RESPONSE } from "../@types/polls";
import { MatrixClient } from "../client";
import { PollStartEvent } from "../extensible_events_v1/PollStartEvent";
import { MatrixEvent } from "./event";
import { Relations } from "./relations";
import { Room } from "./room";
import { TypedEventEmitter } from "./typed-event-emitter";

export enum PollEvent {
    New = "Poll.new",
    End = "Poll.end",
    Update = "Poll.update",
    Responses = "Poll.Responses",
    Destroy = "Poll.Destroy",
    UndecryptableRelations = "Poll.UndecryptableRelations",
}

export type PollEventHandlerMap = {
    [PollEvent.Update]: (event: MatrixEvent, poll: Poll) => void;
    [PollEvent.Destroy]: (pollIdentifier: string) => void;
    [PollEvent.End]: () => void;
    [PollEvent.Responses]: (responses: Relations) => void;
    [PollEvent.UndecryptableRelations]: (count: number) => void;
};

const filterResponseRelations = (
    relationEvents: MatrixEvent[],
    pollEndTimestamp: number,
): {
    responseEvents: MatrixEvent[];
} => {
    const responseEvents = relationEvents.filter((event) => {
        if (event.isDecryptionFailure()) {
            return;
        }
        return (
            M_POLL_RESPONSE.matches(event.getType()) &&
            // From MSC3381:
            // "Votes sent on or before the end event's timestamp are valid votes"
            event.getTs() <= pollEndTimestamp
        );
    });

    return { responseEvents };
};

export class Poll extends TypedEventEmitter<Exclude<PollEvent, PollEvent.New>, PollEventHandlerMap> {
    public readonly roomId: string;
    public readonly pollEvent: PollStartEvent;
    private _isFetchingResponses = false;
    private relationsNextBatch: string | undefined;
    private responses: null | Relations = null;
    private endEvent: MatrixEvent | undefined;
    /**
     * Keep track of undecryptable relations
     * As incomplete result sets affect poll results
     */
    private undecryptableRelationEventIds = new Set<string>();

    public constructor(public readonly rootEvent: MatrixEvent, private matrixClient: MatrixClient, private room: Room) {
        super();
        if (!this.rootEvent.getRoomId() || !this.rootEvent.getId()) {
            throw new Error("Invalid poll start event.");
        }
        this.roomId = this.rootEvent.getRoomId()!;
        this.pollEvent = this.rootEvent.unstableExtensibleEvent as unknown as PollStartEvent;
    }

    public get pollId(): string {
        return this.rootEvent.getId()!;
    }

    public get endEventId(): string | undefined {
        return this.endEvent?.getId();
    }

    public get isEnded(): boolean {
        return !!this.endEvent;
    }

    public get isFetchingResponses(): boolean {
        return this._isFetchingResponses;
    }

    public get undecryptableRelationsCount(): number {
        return this.undecryptableRelationEventIds.size;
    }

    public async getResponses(): Promise<Relations> {
        // if we have already fetched some responses
        // just return them
        if (this.responses) {
            return this.responses;
        }

        // if there is no fetching in progress
        // start fetching
        if (!this.isFetchingResponses) {
            await this.fetchResponses();
        }
        // return whatever responses we got from the first page
        return this.responses!;
    }

    /**
     *
     * @param event - event with a relation to the rootEvent
     * @returns void
     */
    public onNewRelation(event: MatrixEvent): void {
        if (M_POLL_END.matches(event.getType()) && this.validateEndEvent(event)) {
            this.endEvent = event;
            this.refilterResponsesOnEnd();
            this.emit(PollEvent.End);
        }

        // wait for poll responses to be initialised
        if (!this.responses) {
            return;
        }

        const pollEndTimestamp = this.endEvent?.getTs() || Number.MAX_SAFE_INTEGER;
        const { responseEvents } = filterResponseRelations([event], pollEndTimestamp);

        this.countUndecryptableEvents([event]);

        if (responseEvents.length) {
            responseEvents.forEach((event) => {
                this.responses!.addEvent(event);
            });

            this.emit(PollEvent.Responses, this.responses);
        }
    }

    private async fetchResponses(): Promise<void> {
        this._isFetchingResponses = true;

        // we want:
        // - stable and unstable M_POLL_RESPONSE
        // - stable and unstable M_POLL_END
        // so make one api call and filter by event type client side
        const allRelations = await this.matrixClient.relations(
            this.roomId,
            this.rootEvent.getId()!,
            "m.reference",
            undefined,
            {
                from: this.relationsNextBatch || undefined,
            },
        );

        await Promise.all(allRelations.events.map((event) => this.matrixClient.decryptEventIfNeeded(event)));

        const responses =
            this.responses ||
            new Relations("m.reference", M_POLL_RESPONSE.name, this.matrixClient, [M_POLL_RESPONSE.altName!]);

        const pollEndEvent = allRelations.events.find((event) => M_POLL_END.matches(event.getType()));

        if (this.validateEndEvent(pollEndEvent)) {
            this.endEvent = pollEndEvent;
            this.refilterResponsesOnEnd();
            this.emit(PollEvent.End);
        }

        const pollCloseTimestamp = this.endEvent?.getTs() || Number.MAX_SAFE_INTEGER;

        const { responseEvents } = filterResponseRelations(allRelations.events, pollCloseTimestamp);

        responseEvents.forEach((event) => {
            responses.addEvent(event);
        });

        this.relationsNextBatch = allRelations.nextBatch ?? undefined;
        this.responses = responses;
        this.countUndecryptableEvents(allRelations.events);

        // while there are more pages of relations
        // fetch them
        if (this.relationsNextBatch) {
            // don't await
            // we want to return the first page as soon as possible
            this.fetchResponses();
        } else {
            // no more pages
            this._isFetchingResponses = false;
        }

        // emit after updating _isFetchingResponses state
        this.emit(PollEvent.Responses, this.responses);
    }

    /**
     * Only responses made before the poll ended are valid
     * Refilter after an end event is recieved
     * To ensure responses are valid
     */
    private refilterResponsesOnEnd(): void {
        if (!this.responses) {
            return;
        }

        const pollEndTimestamp = this.endEvent?.getTs() || Number.MAX_SAFE_INTEGER;
        this.responses.getRelations().forEach((event) => {
            if (event.getTs() > pollEndTimestamp) {
                this.responses?.removeEvent(event);
            }
        });

        this.emit(PollEvent.Responses, this.responses);
    }

    private countUndecryptableEvents = (events: MatrixEvent[]): void => {
        const undecryptableEventIds = events
            .filter((event) => event.isDecryptionFailure())
            .map((event) => event.getId()!);

        const previousCount = this.undecryptableRelationsCount;
        this.undecryptableRelationEventIds = new Set([...this.undecryptableRelationEventIds, ...undecryptableEventIds]);

        if (this.undecryptableRelationsCount !== previousCount) {
            this.emit(PollEvent.UndecryptableRelations, this.undecryptableRelationsCount);
        }
    };

    private validateEndEvent(endEvent?: MatrixEvent): boolean {
        if (!endEvent) {
            return false;
        }
        /**
         * Repeated end events are ignored -
         * only the first (valid) closure event by origin_server_ts is counted.
         */
        if (this.endEvent && this.endEvent.getTs() < endEvent.getTs()) {
            return false;
        }

        /**
         * MSC3381
         * If a m.poll.end event is received from someone other than the poll creator or user with permission to redact
         * others' messages in the room, the event must be ignored by clients due to being invalid.
         */
        const roomCurrentState = this.room.currentState;
        const endEventSender = endEvent.getSender();
        return (
            !!endEventSender &&
            (endEventSender === this.rootEvent.getSender() ||
                roomCurrentState.maySendRedactionForEvent(this.rootEvent, endEventSender))
        );
    }
}