From badb2c4b2780591c6914602614d32b3a10c49c04 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 13 Jan 2023 10:02:33 -0700 Subject: [PATCH] Use js-sdk imports for poll event types instead of events-sdk (#9904) * Use js-sdk imports for poll event types instead of events-sdk * Attempt to appease some tsc --strict errors * Manually create poll response in cypress test --- cypress/e2e/polls/polls.spec.ts | 14 +++++++---- src/TextForEvent.tsx | 3 ++- .../context_menus/MessageContextMenu.tsx | 2 +- .../views/dialogs/EndPollDialog.tsx | 2 +- .../views/elements/PollCreateDialog.tsx | 12 +++++----- src/components/views/messages/MPollBody.tsx | 14 ++++------- .../views/messages/MessageEvent.tsx | 2 +- .../views/rooms/MessageComposerButtons.tsx | 2 +- .../views/rooms/PinnedEventTile.tsx | 2 +- src/events/EventTileFactory.tsx | 3 ++- src/events/forward/getForwardableEvent.ts | 2 +- src/stores/room-list/MessagePreviewStore.ts | 2 +- .../previews/PollStartEventPreview.ts | 6 +++-- src/utils/EventRenderingUtils.ts | 2 +- src/utils/EventUtils.ts | 2 +- src/utils/PinningUtils.ts | 2 +- .../context_menus/MessageContextMenu-test.tsx | 3 ++- .../views/dialogs/ForwardDialog-test.tsx | 8 +++---- .../views/elements/PollCreateDialog-test.tsx | 10 +++----- .../views/messages/MPollBody-test.tsx | 24 +++++++++---------- .../right_panel/PinnedMessagesCard-test.tsx | 12 ++++------ test/test-utils/poll.ts | 5 ++-- test/utils/location/isSelfLocation-test.ts | 6 ++--- 23 files changed, 69 insertions(+), 71 deletions(-) diff --git a/cypress/e2e/polls/polls.spec.ts b/cypress/e2e/polls/polls.spec.ts index f73524965c..51d169d61b 100644 --- a/cypress/e2e/polls/polls.spec.ts +++ b/cypress/e2e/polls/polls.spec.ts @@ -16,8 +16,6 @@ limitations under the License. /// -import { PollResponseEvent } from "matrix-events-sdk"; - import { HomeserverInstance } from "../../plugins/utils/homeserver"; import { MatrixClient } from "../../global"; import Chainable = Cypress.Chainable; @@ -70,8 +68,16 @@ describe("Polls", () => { cy.get('input[type="radio"]') .invoke("attr", "value") .then((optionId) => { - const pollVote = PollResponseEvent.from([optionId], pollId).serialize(); - bot.sendEvent(roomId, pollVote.type, pollVote.content); + // We can't use the js-sdk types for this stuff directly, so manually construct the event. + bot.sendEvent(roomId, "org.matrix.msc3381.poll.response", { + "m.relates_to": { + rel_type: "m.reference", + event_id: pollId, + }, + "org.matrix.msc3381.poll.response": { + answers: [optionId], + }, + }); }); }); }; diff --git a/src/TextForEvent.tsx b/src/TextForEvent.tsx index ef7d518e74..7f874f8a89 100644 --- a/src/TextForEvent.tsx +++ b/src/TextForEvent.tsx @@ -20,7 +20,8 @@ import { logger } from "matrix-js-sdk/src/logger"; import { removeDirectionOverrideChars } from "matrix-js-sdk/src/utils"; import { GuestAccess, HistoryVisibility, JoinRule } from "matrix-js-sdk/src/@types/partials"; import { EventType, MsgType } from "matrix-js-sdk/src/@types/event"; -import { M_POLL_START, M_POLL_END, PollStartEvent } from "matrix-events-sdk"; +import { M_POLL_START, M_POLL_END } from "matrix-js-sdk/src/@types/polls"; +import { PollStartEvent } from "matrix-js-sdk/src/extensible_events_v1/PollStartEvent"; import { _t } from "./languageHandler"; import * as Roles from "./Roles"; diff --git a/src/components/views/context_menus/MessageContextMenu.tsx b/src/components/views/context_menus/MessageContextMenu.tsx index 74d6232c2f..3d38fc5a70 100644 --- a/src/components/views/context_menus/MessageContextMenu.tsx +++ b/src/components/views/context_menus/MessageContextMenu.tsx @@ -21,7 +21,7 @@ import { EventStatus, MatrixEvent } from "matrix-js-sdk/src/models/event"; import { EventType, RelationType } from "matrix-js-sdk/src/@types/event"; import { Relations } from "matrix-js-sdk/src/models/relations"; import { RoomMemberEvent } from "matrix-js-sdk/src/models/room-member"; -import { M_POLL_START } from "matrix-events-sdk"; +import { M_POLL_START } from "matrix-js-sdk/src/@types/polls"; import { Thread } from "matrix-js-sdk/src/models/thread"; import { MatrixClientPeg } from "../../../MatrixClientPeg"; diff --git a/src/components/views/dialogs/EndPollDialog.tsx b/src/components/views/dialogs/EndPollDialog.tsx index dfba9cb075..946f209d31 100644 --- a/src/components/views/dialogs/EndPollDialog.tsx +++ b/src/components/views/dialogs/EndPollDialog.tsx @@ -17,7 +17,7 @@ limitations under the License. import React from "react"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { MatrixClient } from "matrix-js-sdk/src/client"; -import { PollEndEvent } from "matrix-events-sdk"; +import { PollEndEvent } from "matrix-js-sdk/src/extensible_events_v1/PollEndEvent"; import { _t } from "../../../languageHandler"; import { IDialogProps } from "./IDialogProps"; diff --git a/src/components/views/elements/PollCreateDialog.tsx b/src/components/views/elements/PollCreateDialog.tsx index 594feabbda..4cc4231332 100644 --- a/src/components/views/elements/PollCreateDialog.tsx +++ b/src/components/views/elements/PollCreateDialog.tsx @@ -17,14 +17,14 @@ limitations under the License. import React, { ChangeEvent, createRef } from "react"; import { Room } from "matrix-js-sdk/src/models/room"; import { - IPartialEvent, - KNOWN_POLL_KIND, + KnownPollKind, M_POLL_KIND_DISCLOSED, M_POLL_KIND_UNDISCLOSED, M_POLL_START, - PollStartEvent, -} from "matrix-events-sdk"; +} from "matrix-js-sdk/src/@types/polls"; +import { PollStartEvent } from "matrix-js-sdk/src/extensible_events_v1/PollStartEvent"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; +import { IPartialEvent } from "matrix-js-sdk/src/@types/extensible_events"; import ScrollableBaseModal, { IScrollableBaseState } from "../dialogs/ScrollableBaseModal"; import { IDialogProps } from "../dialogs/IDialogProps"; @@ -51,7 +51,7 @@ interface IState extends IScrollableBaseState { question: string; options: string[]; busy: boolean; - kind: KNOWN_POLL_KIND; + kind: KnownPollKind; autoFocusTarget: FocusTarget; } @@ -263,7 +263,7 @@ export default class PollCreateDialog extends ScrollableBaseModal { const expectedStrippedContent = { ...modernLocationEvent.getContent(), body: text, - [TEXT_NODE_TYPE.name]: text, + [M_TEXT.name]: text, [M_TIMESTAMP.name]: now, [M_ASSET.name]: { type: LocationAssetType.Pin }, [M_LOCATION.name]: { @@ -276,7 +276,7 @@ describe("ForwardDialog", () => { const expectedStrippedContent = { ...modernLocationEvent.getContent(), body: text, - [TEXT_NODE_TYPE.name]: text, + [M_TEXT.name]: text, [M_ASSET.name]: { type: LocationAssetType.Pin }, [M_LOCATION.name]: { uri: geoUri, @@ -297,7 +297,7 @@ describe("ForwardDialog", () => { const expectedContent = { msgtype: "m.location", body: text, - [TEXT_NODE_TYPE.name]: text, + [M_TEXT.name]: text, [M_ASSET.name]: { type: LocationAssetType.Pin }, [M_LOCATION.name]: { uri: geoUri, diff --git a/test/components/views/elements/PollCreateDialog-test.tsx b/test/components/views/elements/PollCreateDialog-test.tsx index fdfb4b50e7..2a0087b355 100644 --- a/test/components/views/elements/PollCreateDialog-test.tsx +++ b/test/components/views/elements/PollCreateDialog-test.tsx @@ -18,14 +18,10 @@ import React from "react"; // eslint-disable-next-line deprecate/import import { mount, ReactWrapper } from "enzyme"; import { Room } from "matrix-js-sdk/src/models/room"; -import { - M_POLL_KIND_DISCLOSED, - M_POLL_KIND_UNDISCLOSED, - M_POLL_START, - M_TEXT, - PollStartEvent, -} from "matrix-events-sdk"; +import { M_POLL_KIND_DISCLOSED, M_POLL_KIND_UNDISCLOSED, M_POLL_START } from "matrix-js-sdk/src/@types/polls"; +import { PollStartEvent } from "matrix-js-sdk/src/extensible_events_v1/PollStartEvent"; import { MatrixEvent } from "matrix-js-sdk/src/models/event"; +import { M_TEXT } from "matrix-js-sdk/src/@types/extensible_events"; import { findById, getMockClientWithEventEmitter } from "../../../test-utils"; import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; diff --git a/test/components/views/messages/MPollBody-test.tsx b/test/components/views/messages/MPollBody-test.tsx index c3907a61b9..1be993f88e 100644 --- a/test/components/views/messages/MPollBody-test.tsx +++ b/test/components/views/messages/MPollBody-test.tsx @@ -26,10 +26,10 @@ import { M_POLL_KIND_UNDISCLOSED, M_POLL_RESPONSE, M_POLL_START, - M_POLL_START_EVENT_CONTENT, - M_TEXT, - POLL_ANSWER, -} from "matrix-events-sdk"; + PollStartEventContent, + PollAnswer, +} from "matrix-js-sdk/src/@types/polls"; +import { M_TEXT } from "matrix-js-sdk/src/@types/extensible_events"; import { MockedObject } from "jest-mock"; import { @@ -440,7 +440,7 @@ describe("MPollBody", () => { responseEvent("@catrd:example.com", "poutine"), responseEvent("@dune2:example.com", "wings"), ]; - const body = newMPollBody(votes, [], null, false); + const body = newMPollBody(votes, [], undefined, false); expect(votesCount(body, "pizza")).toBe(""); expect(votesCount(body, "poutine")).toBe(""); expect(votesCount(body, "italian")).toBe(""); @@ -456,7 +456,7 @@ describe("MPollBody", () => { responseEvent("@catrd:example.com", "poutine"), responseEvent("@dune2:example.com", "wings"), ]; - const body = newMPollBody(votes, [], null, false); + const body = newMPollBody(votes, [], undefined, false); // My vote is marked expect(body.find('input[value="pizza"]').prop("checked")).toBeTruthy(); @@ -474,7 +474,7 @@ describe("MPollBody", () => { responseEvent("@dune2:example.com", "wings"), ]; const ends = [endEvent("@me:example.com", 12)]; - const body = newMPollBody(votes, ends, null, false); + const body = newMPollBody(votes, ends, undefined, false); expect(endedVotesCount(body, "pizza")).toBe("3 votes"); expect(endedVotesCount(body, "poutine")).toBe("1 vote"); expect(endedVotesCount(body, "italian")).toBe("0 votes"); @@ -913,7 +913,7 @@ describe("MPollBody", () => { responseEvent("@yh:example.com", "poutine", 14), ]; const ends = []; - const body = newMPollBody(votes, ends, null, false); + const body = newMPollBody(votes, ends, undefined, false); expect(body.html()).toMatchSnapshot(); }); @@ -927,7 +927,7 @@ describe("MPollBody", () => { responseEvent("@yh:example.com", "poutine", 14), ]; const ends = [endEvent("@me:example.com", 25)]; - const body = newMPollBody(votes, ends, null, false); + const body = newMPollBody(votes, ends, undefined, false); expect(body.html()).toMatchSnapshot(); }); }); @@ -951,7 +951,7 @@ function newRelations(relationEvents: Array, eventType: string): Re function newMPollBody( relationEvents: Array, endEvents: Array = [], - answers?: POLL_ANSWER[], + answers?: PollAnswer[], disclosed = true, ): ReactWrapper { const mxEvent = new MatrixEvent({ @@ -1033,7 +1033,7 @@ function endedVotesCount(wrapper: ReactWrapper, value: string): string { return wrapper.find(`div[data-value="${value}"] .mx_MPollBody_optionVoteCount`).text(); } -function newPollStart(answers?: POLL_ANSWER[], question?: string, disclosed = true): M_POLL_START_EVENT_CONTENT { +function newPollStart(answers?: PollAnswer[], question?: string, disclosed = true): PollStartEventContent { if (!answers) { answers = [ { id: "pizza", [M_TEXT.name]: "Pizza" }, @@ -1047,7 +1047,7 @@ function newPollStart(answers?: POLL_ANSWER[], question?: string, disclosed = tr question = "What should we order for the party?"; } - const answersFallback = answers.map((a, i) => `${i + 1}. ${a[M_TEXT.name]}`).join("\n"); + const answersFallback = answers.map((a, i) => `${i + 1}. ${M_TEXT.findIn(a)}`).join("\n"); const fallback = `${question}\n${answersFallback}`; diff --git a/test/components/views/right_panel/PinnedMessagesCard-test.tsx b/test/components/views/right_panel/PinnedMessagesCard-test.tsx index e4be6191c3..1ce533837d 100644 --- a/test/components/views/right_panel/PinnedMessagesCard-test.tsx +++ b/test/components/views/right_panel/PinnedMessagesCard-test.tsx @@ -23,14 +23,10 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { EventType, RelationType, MsgType } from "matrix-js-sdk/src/@types/event"; import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state"; import { IEvent, Room, EventTimelineSet, IMinimalEvent } from "matrix-js-sdk/src/matrix"; -import { - M_POLL_RESPONSE, - M_POLL_END, - M_POLL_KIND_DISCLOSED, - PollStartEvent, - PollResponseEvent, - PollEndEvent, -} from "matrix-events-sdk"; +import { M_POLL_RESPONSE, M_POLL_END, M_POLL_KIND_DISCLOSED } from "matrix-js-sdk/src/@types/polls"; +import { PollStartEvent } from "matrix-js-sdk/src/extensible_events_v1/PollStartEvent"; +import { PollResponseEvent } from "matrix-js-sdk/src/extensible_events_v1/PollResponseEvent"; +import { PollEndEvent } from "matrix-js-sdk/src/extensible_events_v1/PollEndEvent"; import { stubClient, mkStubRoom, mkEvent, mkMessage } from "../../../test-utils"; import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; diff --git a/test/test-utils/poll.ts b/test/test-utils/poll.ts index 88b7c3035a..5096f8c51a 100644 --- a/test/test-utils/poll.ts +++ b/test/test-utils/poll.ts @@ -15,9 +15,10 @@ limitations under the License. */ import { MatrixEvent } from "matrix-js-sdk/src/matrix"; -import { M_TEXT, M_POLL_START, POLL_ANSWER, M_POLL_KIND_DISCLOSED } from "matrix-events-sdk"; +import { M_POLL_START, PollAnswer, M_POLL_KIND_DISCLOSED } from "matrix-js-sdk/src/@types/polls"; +import { M_TEXT } from "matrix-js-sdk/src/@types/extensible_events"; -export const makePollStartEvent = (question: string, sender: string, answers?: POLL_ANSWER[]): MatrixEvent => { +export const makePollStartEvent = (question: string, sender: string, answers?: PollAnswer[]): MatrixEvent => { if (!answers) { answers = [ { id: "socks", [M_TEXT.name]: "Socks" }, diff --git a/test/utils/location/isSelfLocation-test.ts b/test/utils/location/isSelfLocation-test.ts index fd89ae104b..d04149b681 100644 --- a/test/utils/location/isSelfLocation-test.ts +++ b/test/utils/location/isSelfLocation-test.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { TEXT_NODE_TYPE } from "matrix-js-sdk/src/@types/extensible_events"; +import { M_TEXT } from "matrix-js-sdk/src/@types/extensible_events"; import { ILocationContent, LocationAssetType, @@ -38,7 +38,7 @@ describe("isSelfLocation", () => { msgtype: "m.location", geo_uri: "", [M_LOCATION.name]: { uri: "" }, - [TEXT_NODE_TYPE.name]: "", + [M_TEXT.name]: "", [M_TIMESTAMP.name]: 0, // Note: no m.asset! }; @@ -51,7 +51,7 @@ describe("isSelfLocation", () => { msgtype: "m.location", geo_uri: "", [M_LOCATION.name]: { uri: "" }, - [TEXT_NODE_TYPE.name]: "", + [M_TEXT.name]: "", [M_TIMESTAMP.name]: 0, [M_ASSET.name]: { // Note: no type!