Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move all polls processing to events-sdk
Browse files Browse the repository at this point in the history
This makes polls support the full range of extensible events (both parsing and generation).
turt2live committed Jan 13, 2022
1 parent 61a0be7 commit c6ce981
Showing 12 changed files with 160 additions and 154 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -89,7 +89,7 @@
"lodash": "^4.17.20",
"maplibre-gl": "^1.15.2",
"matrix-analytics-events": "https://github.com/matrix-org/matrix-analytics-events.git#1eab4356548c97722a183912fda1ceabbe8cc7c1",
"matrix-events-sdk": "^0.0.1-beta.2",
"matrix-events-sdk": "^0.0.1-beta.3",
"matrix-js-sdk": "github:matrix-org/matrix-js-sdk#develop",
"matrix-widget-api": "^0.1.0-beta.18",
"minimist": "^1.2.5",
4 changes: 2 additions & 2 deletions src/components/views/context_menus/MessageContextMenu.tsx
Original file line number Diff line number Diff line change
@@ -19,8 +19,8 @@ import React, { ReactElement } from 'react';
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 { POLL_START_EVENT_TYPE } from "matrix-js-sdk/src/@types/polls";
import { LOCATION_EVENT_TYPE } from 'matrix-js-sdk/src/@types/location';
import { M_POLL_START } from "matrix-events-sdk";

import { MatrixClientPeg } from '../../../MatrixClientPeg';
import dis from '../../../dispatcher/dispatcher';
@@ -140,7 +140,7 @@ export default class MessageContextMenu extends React.Component<IProps, IState>

private canEndPoll(mxEvent: MatrixEvent): boolean {
return (
POLL_START_EVENT_TYPE.matches(mxEvent.getType()) &&
M_POLL_START.matches(mxEvent.getType()) &&
this.state.canRedact &&
!isPollEnded(mxEvent, MatrixClientPeg.get(), this.props.getRelationsForEvent)
);
14 changes: 3 additions & 11 deletions src/components/views/dialogs/EndPollDialog.tsx
Original file line number Diff line number Diff line change
@@ -18,8 +18,7 @@ import React from "react";
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { MatrixClient } from "matrix-js-sdk/src/client";
import { Relations } from "matrix-js-sdk/src/models/relations";
import { IPollEndContent, POLL_END_EVENT_TYPE } from "matrix-js-sdk/src/@types/polls";
import { TEXT_NODE_TYPE } from "matrix-js-sdk/src/@types/extensible_events";
import { PollEndEvent } from "matrix-events-sdk";

import { _t } from "../../../languageHandler";
import { IDialogProps } from "./IDialogProps";
@@ -57,17 +56,10 @@ export default class EndPollDialog extends React.Component<IProps> {
);

if (endPoll) {
const endContent: IPollEndContent = {
[POLL_END_EVENT_TYPE.name]: {},
"m.relates_to": {
"event_id": this.props.event.getId(),
"rel_type": "m.reference",
},
[TEXT_NODE_TYPE.name]: message,
};
const closure = PollEndEvent.from(this.props.event.getId(), message).serialize();

this.props.matrixClient.sendEvent(
this.props.event.getRoomId(), POLL_END_EVENT_TYPE.name, endContent,
this.props.event.getRoomId(), closure.type, closure.content,
).catch((e: any) => {
console.error("Failed to submit poll response event:", e);
Modal.createTrackedDialog(
17 changes: 8 additions & 9 deletions src/components/views/elements/PollCreateDialog.tsx
Original file line number Diff line number Diff line change
@@ -16,8 +16,7 @@ limitations under the License.

import React, { ChangeEvent, createRef } from "react";
import { Room } from "matrix-js-sdk/src/models/room";
import { makePollContent } from "matrix-js-sdk/src/content-helpers";
import { POLL_KIND_DISCLOSED, POLL_START_EVENT_TYPE } from "matrix-js-sdk/src/@types/polls";
import { M_POLL_KIND_DISCLOSED } from "matrix-events-sdk";

import ScrollableBaseModal, { IScrollableBaseState } from "../dialogs/ScrollableBaseModal";
import { IDialogProps } from "../dialogs/IDialogProps";
@@ -28,6 +27,7 @@ import { arrayFastClone, arraySeed } from "../../../utils/arrays";
import Field from "./Field";
import AccessibleButton from "./AccessibleButton";
import Spinner from "./Spinner";
import { PollStartEvent } from "matrix-events-sdk";

interface IProps extends IDialogProps {
room: Room;
@@ -99,13 +99,12 @@ export default class PollCreateDialog extends ScrollableBaseModal<IProps, IState

protected submit(): void {
this.setState({ busy: true, canSubmit: false });
this.matrixClient.sendEvent(
this.props.room.roomId,
POLL_START_EVENT_TYPE.name,
makePollContent(
this.state.question, this.state.options, POLL_KIND_DISCLOSED.name,
),
).then(
const pollEvent = PollStartEvent.from(
this.state.question.trim(),
this.state.options.map(a => a.trim()).filter(a => !!a),
M_POLL_KIND_DISCLOSED,
).serialize();
this.matrixClient.sendEvent(this.props.room.roomId, pollEvent.type, pollEvent.content).then(
() => this.props.onFinished(true),
).catch(e => {
console.error("Failed to post poll:", e);
Loading

0 comments on commit c6ce981

Please sign in to comment.