2022-04-21 18:14:10 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 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.
|
|
|
|
*/
|
|
|
|
|
2023-02-07 22:12:39 +01:00
|
|
|
import { Mocked } from "jest-mock";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { M_POLL_START, PollAnswer, M_POLL_KIND_DISCLOSED, M_POLL_END } from "matrix-js-sdk/src/@types/polls";
|
2023-01-13 18:02:33 +01:00
|
|
|
import { M_TEXT } from "matrix-js-sdk/src/@types/extensible_events";
|
2023-02-07 22:12:39 +01:00
|
|
|
import { uuid4 } from "@sentry/utils";
|
2022-04-21 18:14:10 +02:00
|
|
|
|
2023-02-07 22:12:39 +01:00
|
|
|
import { flushPromises } from "./utilities";
|
|
|
|
|
|
|
|
type Options = {
|
|
|
|
roomId: string;
|
|
|
|
ts: number;
|
|
|
|
id: string;
|
|
|
|
};
|
2023-02-02 22:39:23 +01:00
|
|
|
export const makePollStartEvent = (
|
|
|
|
question: string,
|
|
|
|
sender: string,
|
|
|
|
answers?: PollAnswer[],
|
2023-02-07 22:12:39 +01:00
|
|
|
{ roomId, ts, id }: Partial<Options> = {},
|
2023-02-02 22:39:23 +01:00
|
|
|
): MatrixEvent => {
|
2022-04-21 18:14:10 +02:00
|
|
|
if (!answers) {
|
|
|
|
answers = [
|
2022-12-12 12:24:14 +01:00
|
|
|
{ id: "socks", [M_TEXT.name]: "Socks" },
|
|
|
|
{ id: "shoes", [M_TEXT.name]: "Shoes" },
|
2022-04-21 18:14:10 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
return new MatrixEvent({
|
2023-02-02 22:39:23 +01:00
|
|
|
event_id: id || "$mypoll",
|
2023-02-07 22:12:39 +01:00
|
|
|
room_id: roomId || "#myroom:example.com",
|
2022-12-12 12:24:14 +01:00
|
|
|
sender: sender,
|
|
|
|
type: M_POLL_START.name,
|
|
|
|
content: {
|
|
|
|
[M_POLL_START.name]: {
|
|
|
|
question: {
|
|
|
|
[M_TEXT.name]: question,
|
2022-04-21 18:14:10 +02:00
|
|
|
},
|
2022-12-12 12:24:14 +01:00
|
|
|
kind: M_POLL_KIND_DISCLOSED.name,
|
|
|
|
answers: answers,
|
2022-04-21 18:14:10 +02:00
|
|
|
},
|
2022-12-12 12:24:14 +01:00
|
|
|
[M_TEXT.name]: `${question}: answers`,
|
2022-04-21 18:14:10 +02:00
|
|
|
},
|
2023-02-02 22:39:23 +01:00
|
|
|
origin_server_ts: ts || 0,
|
2022-12-12 12:24:14 +01:00
|
|
|
});
|
2022-04-21 18:14:10 +02:00
|
|
|
};
|
2023-02-07 22:12:39 +01:00
|
|
|
|
|
|
|
export const makePollEndEvent = (pollStartEventId: string, roomId: string, sender: string, ts = 0): MatrixEvent => {
|
|
|
|
return new MatrixEvent({
|
|
|
|
event_id: uuid4(),
|
|
|
|
room_id: roomId,
|
|
|
|
origin_server_ts: ts,
|
|
|
|
type: M_POLL_END.name,
|
|
|
|
sender: sender,
|
|
|
|
content: {
|
|
|
|
"m.relates_to": {
|
|
|
|
rel_type: "m.reference",
|
|
|
|
event_id: pollStartEventId,
|
|
|
|
},
|
|
|
|
[M_POLL_END.name]: {},
|
|
|
|
[M_TEXT.name]: "The poll has ended. Something.",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a room with attached poll events
|
|
|
|
* Returns room from mockClient
|
|
|
|
* mocks relations api
|
|
|
|
* @param mxEvent - poll start event
|
|
|
|
* @param relationEvents - returned by relations api
|
|
|
|
* @param endEvents - returned by relations api
|
|
|
|
* @param mockClient - client in use
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export const setupRoomWithPollEvents = async (
|
|
|
|
mxEvent: MatrixEvent,
|
|
|
|
relationEvents: Array<MatrixEvent>,
|
|
|
|
endEvents: Array<MatrixEvent> = [],
|
|
|
|
mockClient: Mocked<MatrixClient>,
|
|
|
|
): Promise<Room> => {
|
|
|
|
const room = new Room(mxEvent.getRoomId()!, mockClient, mockClient.getSafeUserId());
|
|
|
|
room.processPollEvents([mxEvent, ...relationEvents, ...endEvents]);
|
|
|
|
|
|
|
|
// set redaction allowed for current user only
|
|
|
|
// poll end events are validated against this
|
|
|
|
jest.spyOn(room.currentState, "maySendRedactionForEvent").mockImplementation((_evt: MatrixEvent, id: string) => {
|
|
|
|
return id === mockClient.getSafeUserId();
|
|
|
|
});
|
|
|
|
|
|
|
|
// wait for events to process on room
|
|
|
|
await flushPromises();
|
|
|
|
mockClient.getRoom.mockReturnValue(room);
|
|
|
|
mockClient.relations.mockResolvedValue({
|
|
|
|
events: [...relationEvents, ...endEvents],
|
|
|
|
});
|
|
|
|
return room;
|
|
|
|
};
|