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';
|
|
|
|
import { readNotification } from '../../common/read-notification';
|
2018-11-02 13:47:44 +09:00
|
|
|
import define from '../../define';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { makePaginationQuery } from '../../common/make-pagination-query';
|
|
|
|
import { Notifications, Followings, Mutings } from '../../../../models';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '通知一覧を取得します。',
|
|
|
|
'en-US': 'Get notifications.'
|
|
|
|
},
|
|
|
|
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['account', 'notifications'],
|
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
requireCredential: true,
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
kind: 'read:notifications',
|
2018-11-02 03:32:24 +09:00
|
|
|
|
|
|
|
params: {
|
|
|
|
limit: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-02 03:32:24 +09:00
|
|
|
default: 10
|
|
|
|
},
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
sinceId: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
untilId: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
following: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.bool,
|
2018-11-02 03:32:24 +09:00
|
|
|
default: false
|
|
|
|
},
|
2017-03-03 08:56:07 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
markAsRead: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.bool,
|
2018-11-02 03:32:24 +09:00
|
|
|
default: true
|
2019-01-08 13:32:28 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
includeTypes: {
|
2019-04-07 21:50:36 +09:00
|
|
|
validator: $.optional.arr($.str.or(['follow', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote', 'receiveFollowRequest'])),
|
2019-01-08 13:32:28 +09:00
|
|
|
default: [] as string[]
|
|
|
|
},
|
|
|
|
|
|
|
|
excludeTypes: {
|
2019-04-07 21:50:36 +09:00
|
|
|
validator: $.optional.arr($.str.or(['follow', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote', 'receiveFollowRequest'])),
|
2019-01-08 13:32:28 +09:00
|
|
|
default: [] as string[]
|
2018-11-02 03:32:24 +09:00
|
|
|
}
|
2019-02-24 18:13:11 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'array',
|
|
|
|
items: {
|
|
|
|
type: 'Notification',
|
|
|
|
},
|
|
|
|
},
|
2018-11-02 03:32:24 +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 followingQuery = Followings.createQueryBuilder('following')
|
|
|
|
.select('following.followeeId')
|
|
|
|
.where('following.followerId = :followerId', { followerId: user.id });
|
2017-12-22 07:26:23 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const mutingQuery = Mutings.createQueryBuilder('muting')
|
|
|
|
.select('muting.muteeId')
|
|
|
|
.where('muting.muterId = :muterId', { muterId: user.id });
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const query = makePaginationQuery(Notifications.createQueryBuilder('notification'), ps.sinceId, ps.untilId)
|
|
|
|
.andWhere(`notification.notifieeId = :meId`, { meId: user.id })
|
|
|
|
.leftJoinAndSelect('notification.notifier', 'notifier');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
query.andWhere(`notification.notifierId NOT IN (${ mutingQuery.getQuery() })`);
|
|
|
|
query.setParameters(mutingQuery.getParameters());
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (ps.following) {
|
|
|
|
query.andWhere(`((notification.notifierId IN (${ followingQuery.getQuery() })) OR (notification.notifierId = :meId))`, { meId: user.id });
|
|
|
|
query.setParameters(followingQuery.getParameters());
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
if (ps.includeTypes!.length > 0) {
|
2019-04-07 21:50:36 +09:00
|
|
|
query.andWhere(`notification.type IN (:...includeTypes)`, { includeTypes: ps.includeTypes });
|
2019-04-13 01:43:22 +09:00
|
|
|
} else if (ps.excludeTypes!.length > 0) {
|
2019-04-07 21:50:36 +09:00
|
|
|
query.andWhere(`notification.type NOT IN (:...excludeTypes)`, { excludeTypes: ps.excludeTypes });
|
2019-01-08 13:32:28 +09:00
|
|
|
}
|
|
|
|
|
2019-04-13 01:43:22 +09:00
|
|
|
const notifications = await query.take(ps.limit!).getMany();
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-07-20 14:16:02 +09:00
|
|
|
// Mark all as read
|
2018-11-02 03:32:24 +09:00
|
|
|
if (notifications.length > 0 && ps.markAsRead) {
|
2019-04-07 21:50:36 +09:00
|
|
|
readNotification(user.id, notifications.map(x => x.id));
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
2019-02-22 11:46:58 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
return await Notifications.packMany(notifications);
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|