2019-02-05 11:48:08 +09:00
|
|
|
import $ from 'cafy';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { ID } from '../../../../../misc/cafy-id';
|
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';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { createNotification } from '../../../../../services/create-notification';
|
2018-11-02 13:47:44 +09:00
|
|
|
import define from '../../../define';
|
2019-02-22 11:46:58 +09:00
|
|
|
import { ApiError } from '../../../error';
|
2019-02-22 14:53:03 +09:00
|
|
|
import { getNote } from '../../../common/getters';
|
2019-03-06 22:55:47 +09:00
|
|
|
import { deliver } from '../../../../../queue';
|
|
|
|
import { renderActivity } from '../../../../../remote/activitypub/renderer';
|
|
|
|
import renderVote from '../../../../../remote/activitypub/renderer/vote';
|
2019-03-07 21:19:32 +09:00
|
|
|
import { deliverQuestionUpdate } from '../../../../../services/note/polls/update';
|
2019-04-10 15:04:27 +09:00
|
|
|
import { PollVotes, NoteWatchings, Users, Polls, UserProfiles } from '../../../../../models';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { Not } from 'typeorm';
|
|
|
|
import { IRemoteUser } from '../../../../../models/entities/user';
|
|
|
|
import { genId } from '../../../../../misc/gen-id';
|
2019-04-13 01:43:22 +09:00
|
|
|
import { ensure } from '../../../../../prelude/ensure';
|
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
|
|
|
},
|
|
|
|
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2018-07-17 04:36:44 +09:00
|
|
|
requireCredential: true,
|
|
|
|
|
2019-04-15 12:10:40 +09:00
|
|
|
kind: 'write:votes',
|
2018-11-02 03:32:24 +09:00
|
|
|
|
|
|
|
params: {
|
|
|
|
noteId: {
|
|
|
|
validator: $.type(ID),
|
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
|
|
|
|
},
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
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'
|
|
|
|
},
|
2019-03-06 22:55:47 +09:00
|
|
|
|
|
|
|
alreadyExpired: {
|
|
|
|
message: 'The poll is already expired.',
|
|
|
|
code: 'ALREADY_EXPIRED',
|
|
|
|
id: '1022a357-b085-4054-9083-8f8de358337e'
|
|
|
|
},
|
2018-11-02 03:32:24 +09:00
|
|
|
}
|
2018-07-17 04:36:44 +09:00
|
|
|
};
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
export default define(meta, async (ps, user) => {
|
2019-03-06 22:55:47 +09:00
|
|
|
const createdAt = new Date();
|
|
|
|
|
2017-03-04 04:28:38 +09:00
|
|
|
// Get votee
|
2019-02-22 14:53:03 +09:00
|
|
|
const note = await getNote(ps.noteId).catch(e => {
|
|
|
|
if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
|
|
|
|
throw e;
|
2017-03-04 04:28:38 +09:00
|
|
|
});
|
2017-02-14 13:59:26 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (!note.hasPoll) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.noPoll);
|
2017-03-04 04:28:38 +09:00
|
|
|
}
|
2017-02-14 13:59:26 +09:00
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
const poll = await Polls.findOne({ noteId: note.id }).then(ensure);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
|
|
|
if (poll.expiresAt && poll.expiresAt < createdAt) {
|
2019-03-06 22:55:47 +09:00
|
|
|
throw new ApiError(meta.errors.alreadyExpired);
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (poll.choices[ps.choice] == null) {
|
2019-02-22 11:46:58 +09:00
|
|
|
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
|
2019-04-07 21:50:36 +09:00
|
|
|
const exist = await PollVotes.find({
|
|
|
|
noteId: note.id,
|
|
|
|
userId: user.id
|
2017-03-04 04:28:38 +09:00
|
|
|
});
|
2017-02-14 13:59:26 +09:00
|
|
|
|
2019-03-06 22:55:47 +09:00
|
|
|
if (exist.length) {
|
2019-04-07 21:50:36 +09:00
|
|
|
if (poll.multiple) {
|
2019-03-06 22:55:47 +09:00
|
|
|
if (exist.some(x => x.choice == ps.choice))
|
|
|
|
throw new ApiError(meta.errors.alreadyVoted);
|
|
|
|
} else {
|
|
|
|
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
|
2019-04-07 21:50:36 +09:00
|
|
|
const vote = await PollVotes.save({
|
|
|
|
id: genId(),
|
2019-03-06 22:55:47 +09:00
|
|
|
createdAt,
|
2019-04-07 21:50:36 +09:00
|
|
|
noteId: note.id,
|
|
|
|
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
|
|
|
|
2017-03-20 13:54:59 +09:00
|
|
|
// Increment votes count
|
2019-04-07 21:50:36 +09:00
|
|
|
const index = ps.choice + 1; // In SQL, array index is 1 based
|
2019-04-13 17:24:56 +09:00
|
|
|
await Polls.query(`UPDATE poll SET votes[${index}] = votes[${index}] + 1 WHERE "noteId" = '${poll.noteId}'`);
|
2017-02-14 13:59:26 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
publishNoteStream(note.id, 'pollVoted', {
|
2018-11-02 03:32:24 +09:00
|
|
|
choice: ps.choice,
|
2019-04-07 21:50:36 +09:00
|
|
|
userId: user.id
|
2018-10-07 11:06:17 +09:00
|
|
|
});
|
2017-03-20 13:54:59 +09:00
|
|
|
|
2017-03-04 04:28:38 +09:00
|
|
|
// Notify
|
2019-04-07 21:50:36 +09:00
|
|
|
createNotification(note.userId, user.id, 'pollVote', {
|
|
|
|
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
|
2019-04-07 21:50:36 +09:00
|
|
|
NoteWatchings.find({
|
|
|
|
noteId: note.id,
|
|
|
|
userId: Not(user.id),
|
|
|
|
}).then(watchers => {
|
|
|
|
for (const watcher of watchers) {
|
|
|
|
createNotification(watcher.userId, user.id, 'pollVote', {
|
|
|
|
noteId: note.id,
|
|
|
|
choice: ps.choice
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-06-07 00:44:26 +09:00
|
|
|
|
2019-04-17 14:32:59 +09:00
|
|
|
const profile = await UserProfiles.findOne(user.id).then(ensure);
|
2019-04-10 15:04:27 +09:00
|
|
|
|
2017-06-07 00:44:26 +09:00
|
|
|
// この投稿をWatchする
|
2019-04-10 15:04:27 +09:00
|
|
|
if (profile.autoWatch !== false) {
|
2019-04-07 21:50:36 +09:00
|
|
|
watch(user.id, note);
|
2018-03-05 08:07:09 +09:00
|
|
|
}
|
2019-01-21 13:27:19 +09:00
|
|
|
|
|
|
|
// リモート投票の場合リプライ送信
|
2019-04-07 21:50:36 +09:00
|
|
|
if (note.userHost != null) {
|
2019-04-13 01:43:22 +09:00
|
|
|
const pollOwner = await Users.findOne(note.userId).then(ensure) as IRemoteUser;
|
2019-01-21 13:27:19 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
deliver(user, renderActivity(await renderVote(user, vote, note, poll, pollOwner)), pollOwner.inbox);
|
2019-01-21 13:27:19 +09:00
|
|
|
}
|
2019-02-22 11:46:58 +09:00
|
|
|
|
2019-03-07 21:19:32 +09:00
|
|
|
// リモートフォロワーにUpdate配信
|
2019-04-07 21:50:36 +09:00
|
|
|
deliverQuestionUpdate(note.id);
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|