diff options
Diffstat (limited to 'src/models')
| -rw-r--r-- | src/models/note.ts | 26 | ||||
| -rw-r--r-- | src/models/poll-vote.ts | 3 |
2 files changed, 24 insertions, 5 deletions
diff --git a/src/models/note.ts b/src/models/note.ts index c3413164be..369838ae74 100644 --- a/src/models/note.ts +++ b/src/models/note.ts @@ -99,7 +99,9 @@ export type INote = { }; export type IPoll = { - choices: IChoice[] + choices: IChoice[]; + multiple?: boolean; + expiresAt?: Date; }; export type IChoice = { @@ -313,15 +315,31 @@ export const pack = async ( // Poll if (meId && _note.poll) { _note.poll = (async poll => { + if (poll.multiple) { + const votes = await PollVote.find({ + userId: meId, + noteId: id + }); + + const myChoices = (poll.choices as IChoice[]).filter(x => votes.some(y => x.id == y.choice)); + for (const myChoice of myChoices) { + (myChoice as any).isVoted = true; + } + + return poll; + } else { + poll.multiple = false; + } + const vote = await PollVote .findOne({ userId: meId, noteId: id }); - if (vote != null) { - const myChoice = poll.choices - .filter((c: any) => c.id == vote.choice)[0]; + if (vote) { + const myChoice = (poll.choices as IChoice[]) + .filter(x => x.id == vote.choice)[0] as any; myChoice.isVoted = true; } diff --git a/src/models/poll-vote.ts b/src/models/poll-vote.ts index b8aceae3b0..e6178cbc26 100644 --- a/src/models/poll-vote.ts +++ b/src/models/poll-vote.ts @@ -2,9 +2,10 @@ import * as mongo from 'mongodb'; import db from '../db/mongodb'; const PollVote = db.get<IPollVote>('pollVotes'); +PollVote.dropIndex(['userId', 'noteId'], { unique: true }).catch(() => {}); PollVote.createIndex('userId'); PollVote.createIndex('noteId'); -PollVote.createIndex(['userId', 'noteId'], { unique: true }); +PollVote.createIndex(['userId', 'noteId', 'choice'], { unique: true }); export default PollVote; export interface IPollVote { |