Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poll model - page /relations results #3073

Merged
merged 42 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
5ca533c
first cut poll model
Dec 30, 2022
984e5eb
process incoming poll relations
Dec 30, 2022
e578d84
allow alt event types in relations model
Jan 4, 2023
1123f1d
allow alt event types in relations model
Jan 4, 2023
ea71efe
remove unneccesary checks on remove relation
Jan 4, 2023
dfe4038
comment
Jan 4, 2023
515db7a
Revert "allow alt event types in relations model"
Jan 4, 2023
ae6ffb7
Revert "Revert "allow alt event types in relations model""
Jan 4, 2023
f189901
Merge branch 'psg-1014/relations-alt-event-types' into psg-1014/poll-…
Jan 4, 2023
cace5d4
basic handling for new poll relations
Jan 4, 2023
6b4f630
Merge branch 'develop' into psg-1014/poll-model
Jan 5, 2023
0f7829a
tests
Jan 9, 2023
32abfed
test room.processPollEvents
Jan 9, 2023
797123c
join processBeaconEvents and poll events in client
Jan 9, 2023
51896e1
Merge branch 'develop' into psg-1014/poll-model
Jan 9, 2023
ad49a00
Merge branch 'develop' into psg-1014/poll-model
Jan 10, 2023
b158dcc
tidy and set 23 copyrights
Jan 11, 2023
924a6c0
use rooms instance of matrixClient
Jan 11, 2023
d09700f
tidy
Jan 11, 2023
9e0d95e
more copyright
Jan 11, 2023
b173130
simplify processPollEvent code
Jan 12, 2023
15dd1c4
Merge branch 'develop' into psg-1014/poll-model
Jan 12, 2023
4a0494c
throw when poll start event has no roomId
Jan 12, 2023
07d154a
Merge branch 'develop' into psg-1014/poll-model
Jan 15, 2023
68ffea1
updates for events-sdk move
Jan 16, 2023
7d8f51c
more type changes for events-sdk changes
Jan 16, 2023
a4832d1
page poll relation results
Jan 17, 2023
10117e7
validate poll end event senders
Jan 17, 2023
0a618a2
reformatted copyright
Jan 17, 2023
68291e8
undo more comment reformatting
Jan 17, 2023
9c239b2
Merge branch 'develop' into psg-1014/poll-model-only-valid-end-events
Jan 17, 2023
48ea040
Merge branch 'psg-1014/poll-model-only-valid-end-events' into psg-101…
Jan 17, 2023
8e90212
test paging
Jan 17, 2023
7588f07
Merge branch 'develop' into psg-1014/poll-model-only-valid-end-events
Jan 26, 2023
0d965e3
Merge branch 'psg-1014/poll-model-only-valid-end-events' into psg-101…
Jan 26, 2023
9996113
use correct pollstartevent type
Jan 26, 2023
ed93046
emit after updating _isFetchingResponses state
Jan 27, 2023
a2fc0b9
Merge branch 'develop' into psg-1014/poll-model-only-valid-end-events
Jan 29, 2023
35c5b32
Merge branch 'psg-1014/poll-model-only-valid-end-events' into psg-101…
Jan 29, 2023
48db239
make rootEvent public readonly
Jan 30, 2023
4b80253
fix poll end validation logic to allow poll creator to end poll regar…
Jan 31, 2023
e2fc68a
Merge branch 'develop' into psg-1014/poll-relations-paging
Feb 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "Revert "allow alt event types in relations model""
This reverts commit 515db7a.
  • Loading branch information
Kerry Archibald committed Jan 4, 2023
commit ae6ffb70437d6d391c9f5f1f10c5ec05dbff0100
94 changes: 94 additions & 0 deletions spec/unit/relations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

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

import { EventTimelineSet } from "../../src/models/event-timeline-set";
import { MatrixEvent, MatrixEventEvent } from "../../src/models/event";
import { Room } from "../../src/models/room";
import { Relations } from "../../src/models/relations";
import { TestClient } from "../TestClient";
import { RelationType } from "../../src";
import { logger } from "../../src/logger";

describe("Relations", function () {
afterEach(() => {
jest.spyOn(logger, "error").mockRestore();
});

it("should deduplicate annotations", function () {
const room = new Room("room123", null!, null!);
const relations = new Relations("m.annotation", "m.reaction", room);
Expand Down Expand Up @@ -75,6 +83,92 @@ describe("Relations", function () {
}
});

describe("addEvent()", () => {
const relationType = RelationType.Reference;
const eventType = M_POLL_START.stable!;
const altEventTypes = [M_POLL_START.unstable!];
const room = new Room("room123", null!, null!);

it("should not add events without a relation", async () => {
// dont pollute console
const logSpy = jest.spyOn(logger, "error").mockImplementation(() => {});
const relations = new Relations(relationType, eventType, room);
const emitSpy = jest.spyOn(relations, "emit");
const event = new MatrixEvent({ type: eventType });

await relations.addEvent(event);
expect(logSpy).toHaveBeenCalledWith("Event must have relation info");
// event not added
expect(relations.getRelations().length).toBe(0);
expect(emitSpy).not.toHaveBeenCalled();
});

it("should not add events of incorrect event type", async () => {
// dont pollute console
const logSpy = jest.spyOn(logger, "error").mockImplementation(() => {});
const relations = new Relations(relationType, eventType, room);
const emitSpy = jest.spyOn(relations, "emit");
const event = new MatrixEvent({
type: "different-event-type",
content: {
"m.relates_to": {
event_id: "$2s4yYpEkVQrPglSCSqB_m6E8vDhWsg0yFNyOJdVIb_o",
rel_type: relationType,
},
},
});

await relations.addEvent(event);

expect(logSpy).toHaveBeenCalledWith(`Event relation info doesn't match this container`);
// event not added
expect(relations.getRelations().length).toBe(0);
expect(emitSpy).not.toHaveBeenCalled();
});

it("adds events that match alt event types", async () => {
const relations = new Relations(relationType, eventType, room, altEventTypes);
const emitSpy = jest.spyOn(relations, "emit");
const event = new MatrixEvent({
type: M_POLL_START.unstable!,
content: {
"m.relates_to": {
event_id: "$2s4yYpEkVQrPglSCSqB_m6E8vDhWsg0yFNyOJdVIb_o",
rel_type: relationType,
},
},
});

await relations.addEvent(event);

// event added
expect(relations.getRelations()).toEqual([event]);
expect(emitSpy).toHaveBeenCalled();
});

it("should not add events of incorrect relation type", async () => {
const logSpy = jest.spyOn(logger, "error").mockImplementation(() => {});
const relations = new Relations(relationType, eventType, room);
const event = new MatrixEvent({
type: eventType,
content: {
"m.relates_to": {
event_id: "$2s4yYpEkVQrPglSCSqB_m6E8vDhWsg0yFNyOJdVIb_o",
rel_type: "m.annotation",
},
},
});

await relations.addEvent(event);
const emitSpy = jest.spyOn(relations, "emit");

expect(logSpy).toHaveBeenCalledWith(`Event relation info doesn't match this container`);
// event not added
expect(relations.getRelations().length).toBe(0);
expect(emitSpy).not.toHaveBeenCalled();
});
});

it("should emit created regardless of ordering", async function () {
const targetEvent = new MatrixEvent({
sender: "@bob:example.com",
Expand Down
8 changes: 6 additions & 2 deletions src/models/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export type EventHandlerMap = {
[RelationsEvent.Redaction]: (event: MatrixEvent) => void;
};

const matchesEventType = (eventType: string, targetEventType: string, altTargetEventTypes: string[] = []): boolean =>
[targetEventType, ...altTargetEventTypes].includes(eventType);

/**
* A container for relation events that supports easy access to common ways of
* aggregating such events. Each instance holds events that of a single relation
Expand Down Expand Up @@ -60,6 +63,7 @@ export class Relations extends TypedEventEmitter<RelationsEvent, EventHandlerMap
public readonly relationType: RelationType | string,
public readonly eventType: string,
client: MatrixClient | Room,
public readonly altEventTypes?: string[],
) {
super();
this.client = client instanceof Room ? client.client : client;
Expand All @@ -84,7 +88,7 @@ export class Relations extends TypedEventEmitter<RelationsEvent, EventHandlerMap
const relationType = relation.rel_type;
const eventType = event.getType();

if (this.relationType !== relationType || this.eventType !== eventType) {
if (this.relationType !== relationType || !matchesEventType(eventType, this.eventType, this.altEventTypes)) {
logger.error("Event relation info doesn't match this container");
return;
}
Expand Down Expand Up @@ -131,7 +135,7 @@ export class Relations extends TypedEventEmitter<RelationsEvent, EventHandlerMap
const relationType = relation.rel_type;
const eventType = event.getType();

if (this.relationType !== relationType || this.eventType !== eventType) {
if (this.relationType !== relationType || !matchesEventType(eventType, this.eventType, this.altEventTypes)) {
logger.error("Event relation info doesn't match this container");
return;
}
Expand Down