2019-02-05 04:48:08 +02:00
|
|
|
import $ from 'cafy';
|
2021-03-23 10:43:07 +02:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { readNotification } from '../../common/read-notification';
|
2018-11-02 06:47:44 +02:00
|
|
|
import define from '../../define';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { makePaginationQuery } from '../../common/make-pagination-query';
|
2020-01-01 19:47:20 +02:00
|
|
|
import { Notifications, Followings, Mutings, Users } from '../../../../models';
|
2020-05-26 08:33:55 +03:00
|
|
|
import { notificationTypes } from '../../../../types';
|
2021-05-28 16:53:00 +03:00
|
|
|
import read from '@/services/note/read';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
export const meta = {
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['account', 'notifications'],
|
|
|
|
|
2020-02-15 14:33:32 +02:00
|
|
|
requireCredential: true as const,
|
2018-11-01 20:32:24 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
kind: 'read:notifications',
|
2018-11-01 20:32:24 +02:00
|
|
|
|
|
|
|
params: {
|
|
|
|
limit: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2018-11-01 20:32:24 +02:00
|
|
|
default: 10
|
|
|
|
},
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
sinceId: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
untilId: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
following: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 20:32:24 +02:00
|
|
|
default: false
|
|
|
|
},
|
2017-03-03 01:56:07 +02:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
markAsRead: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 20:32:24 +02:00
|
|
|
default: true
|
2019-01-08 06:32:28 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
includeTypes: {
|
2020-05-26 08:33:55 +03:00
|
|
|
validator: $.optional.arr($.str.or(notificationTypes as unknown as string[])),
|
2019-01-08 06:32:28 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
excludeTypes: {
|
2020-05-26 08:33:55 +03:00
|
|
|
validator: $.optional.arr($.str.or(notificationTypes as unknown as string[])),
|
2018-11-01 20:32:24 +02:00
|
|
|
}
|
2019-02-24 11:13:11 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-02-24 11:13:11 +02:00
|
|
|
items: {
|
2019-06-27 12:04:09 +03:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 16:35:26 +03:00
|
|
|
ref: 'Notification',
|
|
|
|
}
|
2019-02-24 11:13:11 +02:00
|
|
|
},
|
2018-11-01 20:32:24 +02:00
|
|
|
};
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
export default define(meta, async (ps, user) => {
|
2020-08-22 04:06:17 +03:00
|
|
|
// includeTypes が空の場合はクエリしない
|
|
|
|
if (ps.includeTypes && ps.includeTypes.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
// excludeTypes に全指定されている場合はクエリしない
|
|
|
|
if (notificationTypes.every(type => ps.excludeTypes?.includes(type))) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-04-07 15:50:36 +03:00
|
|
|
const followingQuery = Followings.createQueryBuilder('following')
|
|
|
|
.select('following.followeeId')
|
|
|
|
.where('following.followerId = :followerId', { followerId: user.id });
|
2017-12-22 00:26:23 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
const mutingQuery = Mutings.createQueryBuilder('muting')
|
|
|
|
.select('muting.muteeId')
|
|
|
|
.where('muting.muterId = :muterId', { muterId: user.id });
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2020-01-01 19:47:20 +02:00
|
|
|
const suspendedQuery = Users.createQueryBuilder('users')
|
2021-01-11 13:38:34 +02:00
|
|
|
.select('users.id')
|
2020-01-01 19:47:20 +02:00
|
|
|
.where('users.isSuspended = TRUE');
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
const query = makePaginationQuery(Notifications.createQueryBuilder('notification'), ps.sinceId, ps.untilId)
|
|
|
|
.andWhere(`notification.notifieeId = :meId`, { meId: user.id })
|
2021-03-19 11:22:34 +02:00
|
|
|
.leftJoinAndSelect('notification.notifier', 'notifier')
|
|
|
|
.leftJoinAndSelect('notification.note', 'note')
|
2021-03-21 05:31:56 +02:00
|
|
|
.leftJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser');
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
query.andWhere(`notification.notifierId NOT IN (${ mutingQuery.getQuery() })`);
|
|
|
|
query.setParameters(mutingQuery.getParameters());
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2020-01-01 19:47:20 +02:00
|
|
|
query.andWhere(`notification.notifierId NOT IN (${ suspendedQuery.getQuery() })`);
|
|
|
|
|
2019-04-07 15:50:36 +03: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 00:49:51 +02:00
|
|
|
}
|
|
|
|
|
2021-05-28 16:52:30 +03:00
|
|
|
if (ps.includeTypes && ps.includeTypes.length > 0) {
|
2019-04-07 15:50:36 +03:00
|
|
|
query.andWhere(`notification.type IN (:...includeTypes)`, { includeTypes: ps.includeTypes });
|
2021-05-28 16:52:30 +03:00
|
|
|
} else if (ps.excludeTypes && ps.excludeTypes.length > 0) {
|
2019-04-07 15:50:36 +03:00
|
|
|
query.andWhere(`notification.type NOT IN (:...excludeTypes)`, { excludeTypes: ps.excludeTypes });
|
2019-01-08 06:32:28 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 19:43:22 +03:00
|
|
|
const notifications = await query.take(ps.limit!).getMany();
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-07-20 08:16:02 +03:00
|
|
|
// Mark all as read
|
2018-11-01 20:32:24 +02:00
|
|
|
if (notifications.length > 0 && ps.markAsRead) {
|
2019-04-07 15:50:36 +03:00
|
|
|
readNotification(user.id, notifications.map(x => x.id));
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
2019-02-22 04:46:58 +02:00
|
|
|
|
2021-05-28 16:53:00 +03:00
|
|
|
const notes = notifications.filter(notification => ['mention', 'reply', 'quote'].includes(notification.type)).map(notification => notification.note!);
|
|
|
|
|
|
|
|
if (notes.length > 0) {
|
|
|
|
read(user.id, notes);
|
|
|
|
}
|
|
|
|
|
2021-03-20 04:05:33 +02:00
|
|
|
return await Notifications.packMany(notifications, user.id);
|
2019-02-22 04:46:58 +02:00
|
|
|
});
|