1
0
mirror of https://github.com/MisskeyIO/misskey synced 2025-01-04 17:03:05 +09:00
MisskeyIO/src/server/api/endpoints/notes/polls/vote.ts

164 lines
3.4 KiB
TypeScript
Raw Normal View History

2019-02-05 11:48:08 +09:00
import $ from 'cafy';
import ID, { transform } from '../../../../../misc/cafy-id';
2018-03-29 20:32:18 +09:00
import Vote from '../../../../../models/poll-vote';
2018-04-08 02:30:37 +09:00
import Note from '../../../../../models/note';
import Watching from '../../../../../models/note-watching';
2018-04-08 04:02:12 +09:00
import watch from '../../../../../services/note/watch';
2019-02-05 14:14:23 +09:00
import { publishNoteStream } from '../../../../../services/stream';
import notify from '../../../../../services/create-notification';
2018-11-02 13:47:44 +09:00
import define from '../../../define';
import createNote from '../../../../../services/note/create';
import User from '../../../../../models/user';
import { ApiError } from '../../../error';
2017-02-14 13:59:26 +09:00
2018-07-17 04:36:44 +09:00
export const meta = {
desc: {
2018-08-29 06:59:43 +09:00
'ja-JP': '指定した投稿のアンケートに投票します。',
'en-US': 'Vote poll of a note.'
2018-07-17 04:36:44 +09:00
},
requireCredential: true,
2018-11-02 03:32:24 +09:00
kind: 'vote-write',
params: {
noteId: {
validator: $.type(ID),
transform: transform,
2018-11-03 22:49:36 +09:00
desc: {
'ja-JP': '対象の投稿のID',
'en-US': 'Target note ID'
}
2018-11-02 03:32:24 +09:00
},
choice: {
validator: $.num
},
},
errors: {
noSuchNote: {
message: 'No such note.',
code: 'NO_SUCH_NOTE',
id: 'ecafbd2e-c283-4d6d-aecb-1a0a33b75396'
},
noPoll: {
message: 'The note does not attach a poll.',
code: 'NO_POLL',
id: '5f979967-52d9-4314-a911-1c673727f92f'
},
invalidChoice: {
message: 'Choice ID is invalid.',
code: 'INVALID_CHOICE',
id: 'e0cc9a04-f2e8-41e4-a5f1-4127293260cc'
},
alreadyVoted: {
message: 'You have already voted.',
code: 'ALREADY_VOTED',
id: '0963fc77-efac-419b-9424-b391608dc6d8'
},
2018-11-02 03:32:24 +09:00
}
2018-07-17 04:36:44 +09:00
};
export default define(meta, async (ps, user) => {
2017-03-04 04:28:38 +09:00
// Get votee
2018-04-08 02:30:37 +09:00
const note = await Note.findOne({
2018-11-02 03:32:24 +09:00
_id: ps.noteId
2017-03-04 04:28:38 +09:00
});
2017-02-14 13:59:26 +09:00
2018-04-08 02:30:37 +09:00
if (note === null) {
throw new ApiError(meta.errors.noSuchNote);
2017-03-04 04:28:38 +09:00
}
2017-02-14 13:59:26 +09:00
2018-04-08 02:30:37 +09:00
if (note.poll == null) {
throw new ApiError(meta.errors.noPoll);
2017-03-04 04:28:38 +09:00
}
2017-02-14 13:59:26 +09:00
if (!note.poll.choices.some(x => x.id == ps.choice)) {
throw new ApiError(meta.errors.invalidChoice);
}
2017-02-14 13:59:26 +09:00
2017-03-04 04:28:38 +09:00
// if already voted
const exist = await Vote.findOne({
2018-04-08 02:30:37 +09:00
noteId: note._id,
2018-03-29 14:48:47 +09:00
userId: user._id
2017-03-04 04:28:38 +09:00
});
2017-02-14 13:59:26 +09:00
2017-03-04 04:28:38 +09:00
if (exist !== null) {
throw new ApiError(meta.errors.alreadyVoted);
2017-03-04 04:28:38 +09:00
}
2017-02-14 13:59:26 +09:00
2017-03-04 04:28:38 +09:00
// Create vote
await Vote.insert({
2018-03-29 14:48:47 +09:00
createdAt: new Date(),
2018-04-08 02:30:37 +09:00
noteId: note._id,
2018-03-29 14:48:47 +09:00
userId: user._id,
2018-11-02 03:32:24 +09:00
choice: ps.choice
2017-03-04 04:28:38 +09:00
});
2017-02-14 13:59:26 +09:00
2018-06-18 09:54:53 +09:00
const inc: any = {};
2018-11-02 03:32:24 +09:00
inc[`poll.choices.${note.poll.choices.findIndex(c => c.id == ps.choice)}.votes`] = 1;
2017-02-14 13:59:26 +09:00
2017-03-20 13:54:59 +09:00
// Increment votes count
2018-04-08 02:30:37 +09:00
await Note.update({ _id: note._id }, {
2017-03-04 04:28:38 +09:00
$inc: inc
});
2017-02-14 13:59:26 +09:00
publishNoteStream(note._id, 'pollVoted', {
2018-11-02 03:32:24 +09:00
choice: ps.choice,
userId: user._id.toHexString()
});
2017-03-20 13:54:59 +09:00
2017-03-04 04:28:38 +09:00
// Notify
2018-04-08 02:30:37 +09:00
notify(note.userId, user._id, 'poll_vote', {
noteId: note._id,
2018-11-02 03:32:24 +09:00
choice: ps.choice
2017-02-14 13:59:26 +09:00
});
2017-06-07 00:44:26 +09:00
// Fetch watchers
Watching
.find({
2018-04-08 02:30:37 +09:00
noteId: note._id,
2018-03-29 14:48:47 +09:00
userId: { $ne: user._id },
2017-06-07 00:44:26 +09:00
// 削除されたドキュメントは除く
2018-03-29 14:48:47 +09:00
deletedAt: { $exists: false }
2017-06-07 00:44:26 +09:00
}, {
fields: {
2018-03-29 14:48:47 +09:00
userId: true
2017-06-07 00:44:26 +09:00
}
})
.then(watchers => {
for (const watcher of watchers) {
2018-03-29 14:48:47 +09:00
notify(watcher.userId, user._id, 'poll_vote', {
2018-04-08 02:30:37 +09:00
noteId: note._id,
2018-11-02 03:32:24 +09:00
choice: ps.choice
2017-06-07 00:44:26 +09:00
});
}
2017-06-07 00:44:26 +09:00
});
// この投稿をWatchする
2018-04-08 03:58:11 +09:00
if (user.settings.autoWatch !== false) {
2018-04-08 02:30:37 +09:00
watch(user._id, note);
2018-03-05 08:07:09 +09:00
}
// リモート投票の場合リプライ送信
if (note._user.host != null) {
const pollOwner = await User.findOne({
_id: note.userId
});
createNote(user, {
createdAt: new Date(),
text: ps.choice.toString(),
reply: note,
visibility: 'specified',
visibleUsers: [ pollOwner ],
});
}
return;
});