2017-03-25 16:43:55 +09:00
|
|
|
import $ from 'cafy';
|
2018-11-02 13:47:44 +09:00
|
|
|
import define from '../../../define';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { Polls, Mutings, Notes, PollVotes } from '../../../../../models';
|
|
|
|
import { Brackets, In } from 'typeorm';
|
2017-03-25 16:43:55 +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': 'Get recommended polls.'
|
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,
|
2018-11-02 12:49:08 +09:00
|
|
|
|
|
|
|
params: {
|
|
|
|
limit: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-02 12:49:08 +09:00
|
|
|
default: 10
|
|
|
|
},
|
|
|
|
|
|
|
|
offset: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.num.min(0),
|
2018-11-02 12:49:08 +09:00
|
|
|
default: 0
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 04:36:44 +09:00
|
|
|
};
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
export default define(meta, async (ps, user) => {
|
2019-04-07 21:50:36 +09:00
|
|
|
const query = Polls.createQueryBuilder('poll')
|
|
|
|
.where('poll.userHost IS NULL')
|
|
|
|
.andWhere(`poll.userId != :meId`, { meId: user.id })
|
|
|
|
.andWhere(`poll.noteVisibility = 'public'`)
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.where('poll.expiresAt IS NULL')
|
|
|
|
.orWhere('poll.expiresAt > :now', { now: new Date() });
|
|
|
|
}));
|
2017-03-25 16:43:55 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
//#region exclude arleady voted polls
|
|
|
|
const votedQuery = PollVotes.createQueryBuilder('vote')
|
|
|
|
.select('vote.noteId')
|
|
|
|
.where('vote.userId = :meId', { meId: user.id });
|
2017-03-25 16:43:55 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
query
|
|
|
|
.andWhere(`poll.noteId NOT IN (${ votedQuery.getQuery() })`);
|
2019-01-31 23:14:45 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
query.setParameters(votedQuery.getParameters());
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region mute
|
|
|
|
const mutingQuery = Mutings.createQueryBuilder('muting')
|
|
|
|
.select('muting.muteeId')
|
|
|
|
.where('muting.muterId = :muterId', { muterId: user.id });
|
|
|
|
|
|
|
|
query
|
|
|
|
.andWhere(`poll.userId NOT IN (${ mutingQuery.getQuery() })`);
|
|
|
|
|
|
|
|
query.setParameters(mutingQuery.getParameters());
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
const polls = await query.take(ps.limit).skip(ps.offset).getMany();
|
|
|
|
|
|
|
|
if (polls.length === 0) return [];
|
|
|
|
|
|
|
|
const notes = await Notes.find({
|
|
|
|
id: In(polls.map(poll => poll.noteId))
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|
2017-03-25 16:43:55 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
return await Notes.packMany(notes, user, {
|
2018-11-02 12:49:08 +09:00
|
|
|
detail: true
|
2019-04-07 21:50:36 +09:00
|
|
|
});
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|