2022-04-21 16:14:10 +00: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 21:12:39 +00:00
|
|
|
import { Mocked } from "jest-mock";
|
2023-08-09 07:18:41 +00:00
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2023-02-20 18:19:50 +00:00
|
|
|
import {
|
|
|
|
M_POLL_START,
|
|
|
|
PollAnswer,
|
|
|
|
M_POLL_KIND_DISCLOSED,
|
|
|
|
M_POLL_END,
|
|
|
|
M_POLL_RESPONSE,
|
|
|
|
} from "matrix-js-sdk/src/@types/polls";
|
2023-01-13 17:02:33 +00:00
|
|
|
import { M_TEXT } from "matrix-js-sdk/src/@types/extensible_events";
|
2023-02-07 21:12:39 +00:00
|
|
|
import { uuid4 } from "@sentry/utils";
|
2022-04-21 16:14:10 +00:00
|
|
|
|
2023-02-07 21:12:39 +00:00
|
|
|
import { flushPromises } from "./utilities";
|
|
|
|
|
|
|
|
type Options = {
|
|
|
|
roomId: string;
|
|
|
|
ts: number;
|
|
|
|
id: string;
|
|
|
|
};
|
2023-02-02 21:39:23 +00:00
|
|
|
export const makePollStartEvent = (
|
|
|
|
question: string,
|
|
|
|
sender: string,
|
|
|
|
answers?: PollAnswer[],
|
2023-02-07 21:12:39 +00:00
|
|
|
{ roomId, ts, id }: Partial<Options> = {},
|
2023-02-02 21:39:23 +00:00
|
|
|
): MatrixEvent => {
|
2022-04-21 16:14:10 +00:00
|
|
|
if (!answers) {
|
|
|
|
answers = [
|
2022-12-12 11:24:14 +00:00
|
|
|
{ id: "socks", [M_TEXT.name]: "Socks" },
|
|
|
|
{ id: "shoes", [M_TEXT.name]: "Shoes" },
|
2022-04-21 16:14:10 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
return new MatrixEvent({
|
2023-02-02 21:39:23 +00:00
|
|
|
event_id: id || "$mypoll",
|
2023-02-07 21:12:39 +00:00
|
|
|
room_id: roomId || "#myroom:example.com",
|
2022-12-12 11:24:14 +00:00
|
|
|
sender: sender,
|
|
|
|
type: M_POLL_START.name,
|
|
|
|
content: {
|
|
|
|
[M_POLL_START.name]: {
|
|
|
|
question: {
|
|
|
|
[M_TEXT.name]: question,
|
2022-04-21 16:14:10 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
kind: M_POLL_KIND_DISCLOSED.name,
|
|
|
|
answers: answers,
|
2022-04-21 16:14:10 +00:00
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
[M_TEXT.name]: `${question}: answers`,
|
2022-04-21 16:14:10 +00:00
|
|
|
},
|
2023-02-02 21:39:23 +00:00
|
|
|
origin_server_ts: ts || 0,
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2022-04-21 16:14:10 +00:00
|
|
|
};
|
2023-02-07 21:12:39 +00:00
|
|
|
|
2023-02-27 20:39:55 +00:00
|
|
|
export const makePollEndEvent = (
|
|
|
|
pollStartEventId: string,
|
|
|
|
roomId: string,
|
|
|
|
sender: string,
|
|
|
|
ts = 0,
|
|
|
|
id?: string,
|
|
|
|
): MatrixEvent => {
|
2023-02-07 21:12:39 +00:00
|
|
|
return new MatrixEvent({
|
2023-02-27 20:39:55 +00:00
|
|
|
event_id: id || uuid4(),
|
2023-02-07 21:12:39 +00:00
|
|
|
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.",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-02-20 18:19:50 +00:00
|
|
|
export const makePollResponseEvent = (
|
|
|
|
pollId: string,
|
|
|
|
answerIds: string[],
|
|
|
|
sender: string,
|
|
|
|
roomId: string,
|
|
|
|
ts = 0,
|
|
|
|
): MatrixEvent =>
|
|
|
|
new MatrixEvent({
|
|
|
|
event_id: uuid4(),
|
|
|
|
room_id: roomId,
|
|
|
|
origin_server_ts: ts,
|
|
|
|
type: M_POLL_RESPONSE.name,
|
|
|
|
sender,
|
|
|
|
content: {
|
|
|
|
"m.relates_to": {
|
|
|
|
rel_type: "m.reference",
|
|
|
|
event_id: pollId,
|
|
|
|
},
|
|
|
|
[M_POLL_RESPONSE.name]: {
|
|
|
|
answers: answerIds,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-02-07 21:12:39 +00:00
|
|
|
/**
|
|
|
|
* 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 (
|
2023-02-12 20:19:45 +00:00
|
|
|
pollStartEvents: MatrixEvent[],
|
2023-02-07 21:12:39 +00:00
|
|
|
relationEvents: Array<MatrixEvent>,
|
|
|
|
endEvents: Array<MatrixEvent> = [],
|
|
|
|
mockClient: Mocked<MatrixClient>,
|
2023-02-12 20:19:45 +00:00
|
|
|
existingRoom?: Room,
|
2023-02-07 21:12:39 +00:00
|
|
|
): Promise<Room> => {
|
2023-02-12 20:19:45 +00:00
|
|
|
const room = existingRoom || new Room(pollStartEvents[0].getRoomId()!, mockClient, mockClient.getSafeUserId());
|
|
|
|
room.processPollEvents([...pollStartEvents, ...relationEvents, ...endEvents]);
|
2023-02-07 21:12:39 +00:00
|
|
|
|
|
|
|
// 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);
|
2023-02-12 20:19:45 +00:00
|
|
|
mockClient.relations.mockImplementation(async (_roomId: string, eventId: string) => {
|
|
|
|
return {
|
|
|
|
events: [...relationEvents, ...endEvents].filter((event) => event.getRelation()?.event_id === eventId),
|
|
|
|
};
|
2023-02-07 21:12:39 +00:00
|
|
|
});
|
|
|
|
return room;
|
|
|
|
};
|