Skip to content

Commit

Permalink
Copy the polls constants to matrix-js-sdk from matrix-react-sdk (#2073)
Browse files Browse the repository at this point in the history
  • Loading branch information
andybalaam authored Dec 15, 2021
1 parent dd23a1a commit 75cc873
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/@types/polls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2021 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.
*/

import { UnstableValue } from "../NamespacedValue";
import { IContent } from "../models/event";

export const POLL_START_EVENT_TYPE = new UnstableValue(
"m.poll.start", "org.matrix.msc3381.poll.start");

export const POLL_RESPONSE_EVENT_TYPE = new UnstableValue(
"m.poll.response", "org.matrix.msc3381.poll.response");

export const POLL_END_EVENT_TYPE = new UnstableValue(
"m.poll.end", "org.matrix.msc3381.poll.end");

export const POLL_KIND_DISCLOSED = new UnstableValue(
"m.poll.disclosed", "org.matrix.msc3381.poll.disclosed");

export const POLL_KIND_UNDISCLOSED = new UnstableValue(
"m.poll.undisclosed", "org.matrix.msc3381.poll.undisclosed");

// TODO: [TravisR] Use extensible events library when ready
export const TEXT_NODE_TYPE = new UnstableValue("m.text", "org.matrix.msc1767.text");

export interface IPollAnswer extends IContent {
id: string;
[TEXT_NODE_TYPE.name]: string;
}

export interface IPollContent extends IContent {
[POLL_START_EVENT_TYPE.name]: {
kind: string; // disclosed or undisclosed (untypeable for now)
question: {
[TEXT_NODE_TYPE.name]: string;
};
answers: IPollAnswer[];
};
[TEXT_NODE_TYPE.name]: string;
}

export interface IPollResponseContent extends IContent {
[POLL_RESPONSE_EVENT_TYPE.name]: {
answers: string[];
};
"m.relates_to": {
"event_id": string;
"rel_type": string;
};
}

export interface IPollEndContent extends IContent {
[POLL_END_EVENT_TYPE.name]: {};
"m.relates_to": {
"event_id": string;
"rel_type": string;
};
}

export function makePollContent(
question: string,
answers: string[],
kind: string,
): IPollContent {
question = question.trim();
answers = answers.map(a => a.trim()).filter(a => !!a);
return {
[TEXT_NODE_TYPE.name]:
`${question}\n${answers.map((a, i) => `${i + 1}. ${a}`).join('\n')}`,
[POLL_START_EVENT_TYPE.name]: {
kind: kind,
question: {
[TEXT_NODE_TYPE.name]: question,
},
answers: answers.map(
(a, i) => ({ id: `${i}-${a}`, [TEXT_NODE_TYPE.name]: a }),
),
},
};
}

0 comments on commit 75cc873

Please sign in to comment.