Compare commits

...

1 commit

Author SHA1 Message Date
syuilo
647faa3c03
wip 2018-12-06 10:59:58 +09:00
4 changed files with 27 additions and 9 deletions

View file

@ -1,5 +1,6 @@
<template>
<div class="mk-poll-editor">
<ui-input type="date"></ui-input>
<p class="caution" v-if="choices.length < 2">
<fa icon="exclamation-triangle"/>{{ $t('no-only-one-choice') }}
</p>
@ -72,6 +73,8 @@ export default Vue.extend({
<style lang="stylus" scoped>
.mk-poll-editor
padding 8px
max-height 300px
overflow auto
> .caution
margin 0 0 8px 0

View file

@ -39,6 +39,7 @@ export type INote = {
replyId: mongo.ObjectID;
renoteId: mongo.ObjectID;
poll: {
period: Date;
choices: Array<{
id: number;
}>

View file

@ -133,10 +133,10 @@ export const meta = {
poll: {
validator: $.obj({
choices: $.arr($.str)
period: $.num.optional.nullable,
choices: $.arr($.str.range(0, 50))
.unique()
.range(2, 10)
.each(c => c.length > 0 && c.length < 50)
}).optional.strict(),
desc: {
'ja-JP': 'アンケート'
@ -210,16 +210,26 @@ export default define(meta, (ps, user, app) => new Promise(async (res, rej) => {
}
}
let poll: INote['poll'];
if (ps.poll) {
(ps.poll as any).choices = (ps.poll as any).choices.map((choice: string, i: number) => ({
const now = new Date();
if (ps.poll.period && (ps.poll.period <= now.getTime())) {
return rej('invalid poll period range');
}
poll = {
period: ps.poll.period ? new Date(ps.poll.period) : null,
choices: ps.poll.choices.map((choice: string, i: number) => ({
id: i, // IDを付与
text: choice.trim(),
votes: 0
}));
}))
};
}
// テキストが無いかつ添付ファイルが無いかつRenoteも無いかつ投票も無かったらエラー
if (!(ps.text || files.length || renote || ps.poll)) {
if (!(ps.text || files.length || renote || poll)) {
return rej('text, fileIds, renoteId or poll is required');
}
@ -227,7 +237,7 @@ export default define(meta, (ps, user, app) => new Promise(async (res, rej) => {
create(user, {
createdAt: new Date(),
files: files,
poll: ps.poll,
poll: poll,
text: ps.text,
reply,
renote,

View file

@ -59,6 +59,10 @@ export default define(meta, (ps, user) => new Promise(async (res, rej) => {
return rej('already voted');
}
if (note.poll.period && (note.poll.period.getTime() - Date.now() < 0)) {
return rej('poll period expired');
}
// Create vote
await Vote.insert({
createdAt: new Date(),