From d44651e9e9aea36da23fee4d23e6470bc646dc6a Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Sat, 20 Jul 2019 22:21:40 +0900 Subject: [PATCH] refs #969 Attach poll only specified, and block invalid poll options --- src/config/locales/en/translation.json | 3 ++- .../TimelineSpace/Modals/NewToot.vue | 7 +++++- src/renderer/errors/validations.js | 2 ++ .../store/TimelineSpace/Modals/NewToot.ts | 23 ++++++++++++++----- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/config/locales/en/translation.json b/src/config/locales/en/translation.json index 6d2b0ef2..2c398bd7 100644 --- a/src/config/locales/en/translation.json +++ b/src/config/locales/en/translation.json @@ -406,7 +406,8 @@ "new_toot": { "toot_length": "Toot length should be {{min}} to {{max}}", "attach_length": "You can only attach up to {{max}} images", - "attach_image": "You can only attach images or videos" + "attach_image": "You can only attach images or videos", + "poll_invalid": "Invalid poll choices" } } } diff --git a/src/renderer/components/TimelineSpace/Modals/NewToot.vue b/src/renderer/components/TimelineSpace/Modals/NewToot.vue index f730c91a..d5ff0340 100644 --- a/src/renderer/components/TimelineSpace/Modals/NewToot.vue +++ b/src/renderer/components/TimelineSpace/Modals/NewToot.vue @@ -134,7 +134,7 @@ import { clipboard } from 'electron' import Visibility from '~/src/constants/visibility' import Status from './NewToot/Status' import Poll from './NewToot/Poll' -import { NewTootTootLength, NewTootAttachLength, NewTootModalOpen, NewTootBlockSubmit } from '@/errors/validations' +import { NewTootTootLength, NewTootAttachLength, NewTootModalOpen, NewTootBlockSubmit, NewTootPollInvalid } from '@/errors/validations' export default { name: 'new-toot', @@ -261,6 +261,11 @@ export default { message: this.$t('validation.new_toot.attach_length', { max: 4 }), type: 'error' }) + } else if (err instanceof NewTootPollInvalid) { + this.$message({ + message: this.$t('validation.new_toot.poll_invalid'), + type: 'error' + }) } else if (err instanceof NewTootModalOpen || err instanceof NewTootBlockSubmit) { // Nothing } else { diff --git a/src/renderer/errors/validations.js b/src/renderer/errors/validations.js index 91f22f76..654eaa11 100644 --- a/src/renderer/errors/validations.js +++ b/src/renderer/errors/validations.js @@ -8,6 +8,8 @@ export class NewTootAttachLength extends Error {} export class NewTootMediaDescription extends Error {} +export class NewTootPollInvalid extends Error {} + export class NewTootUnknownType extends Error {} export class AuthenticationError extends Error {} diff --git a/src/renderer/store/TimelineSpace/Modals/NewToot.ts b/src/renderer/store/TimelineSpace/Modals/NewToot.ts index 17fbeca1..f5d5139b 100644 --- a/src/renderer/store/TimelineSpace/Modals/NewToot.ts +++ b/src/renderer/store/TimelineSpace/Modals/NewToot.ts @@ -11,6 +11,7 @@ import { NewTootTootLength, NewTootAttachLength, NewTootMediaDescription, + NewTootPollInvalid, NewTootUnknownType, AuthenticationError } from '@/errors/validations' @@ -205,12 +206,7 @@ const actions: ActionTree = { status: params.status, visibility: specifiedVisibility, sensitive: state.sensitive, - spoiler_text: params.spoiler, - poll: { - expires_in: params.pollExpireSeconds, - multiple: false, - options: params.polls - } + spoiler_text: params.spoiler } if (state.replyToMessage !== null) { @@ -219,6 +215,21 @@ const actions: ActionTree = { }) } + if (params.polls.length > 1) { + params.polls.map(poll => { + if (poll.length < 1) { + throw new NewTootPollInvalid() + } + }) + form = Object.assign(form, { + poll: { + expires_in: params.pollExpireSeconds, + multiple: false, + options: params.polls + } + }) + } + if (rootState.TimelineSpace.account.accessToken === undefined || rootState.TimelineSpace.account.accessToken === null) { throw new AuthenticationError() }