2022-02-26 20:07:39 -06:00
|
|
|
import { readNotification } from '../../common/read-notification.js';
|
|
|
|
import define from '../../define.js';
|
|
|
|
import { makePaginationQuery } from '../../common/make-pagination-query.js';
|
|
|
|
import { generateMutedInstanceNotificationQuery } from '../../common/generate-muted-instance-query.js';
|
|
|
|
import { Notifications, Followings, Mutings, Users } from '@/models/index.js';
|
|
|
|
import { notificationTypes } from '@/types.js';
|
|
|
|
import read from '@/services/note/read.js';
|
2021-10-19 06:25:47 -05:00
|
|
|
import { Brackets } from 'typeorm';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
export const meta = {
|
2019-02-22 20:20:58 -06:00
|
|
|
tags: ['account', 'notifications'],
|
|
|
|
|
2022-01-18 07:27:10 -06:00
|
|
|
requireCredential: true,
|
2018-11-01 13:32:24 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
kind: 'read:notifications',
|
2018-11-01 13:32:24 -05:00
|
|
|
|
2019-02-24 03:13:11 -06:00
|
|
|
res: {
|
2022-01-18 07:27:10 -06:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 03:13:11 -06:00
|
|
|
items: {
|
2022-01-18 07:27:10 -06:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 08:35:26 -05:00
|
|
|
ref: 'Notification',
|
2021-12-09 08:58:30 -06:00
|
|
|
},
|
2019-02-24 03:13:11 -06:00
|
|
|
},
|
2022-01-18 07:27:10 -06:00
|
|
|
} as const;
|
2018-11-01 13:32:24 -05:00
|
|
|
|
2022-02-19 22:15:40 -06:00
|
|
|
export const paramDef = {
|
2022-02-18 23:05:32 -06:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
sinceId: { type: 'string', format: 'misskey:id' },
|
|
|
|
untilId: { type: 'string', format: 'misskey:id' },
|
|
|
|
following: { type: 'boolean', default: false },
|
|
|
|
unreadOnly: { type: 'boolean', default: false },
|
|
|
|
markAsRead: { type: 'boolean', default: true },
|
|
|
|
includeTypes: { type: 'array', items: {
|
|
|
|
type: 'string', enum: notificationTypes,
|
|
|
|
} },
|
|
|
|
excludeTypes: { type: 'array', items: {
|
|
|
|
type: 'string', enum: notificationTypes,
|
|
|
|
} },
|
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 11:12:50 -06:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-18 23:05:32 -06:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2020-08-21 20:06:17 -05:00
|
|
|
// includeTypes が空の場合はクエリしない
|
|
|
|
if (ps.includeTypes && ps.includeTypes.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
// excludeTypes に全指定されている場合はクエリしない
|
|
|
|
if (notificationTypes.every(type => ps.excludeTypes?.includes(type))) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-04-07 07:50:36 -05:00
|
|
|
const followingQuery = Followings.createQueryBuilder('following')
|
|
|
|
.select('following.followeeId')
|
|
|
|
.where('following.followerId = :followerId', { followerId: user.id });
|
2017-12-21 16:26:23 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
const mutingQuery = Mutings.createQueryBuilder('muting')
|
|
|
|
.select('muting.muteeId')
|
|
|
|
.where('muting.muterId = :muterId', { muterId: user.id });
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2020-01-01 11:47:20 -06:00
|
|
|
const suspendedQuery = Users.createQueryBuilder('users')
|
2021-01-11 05:38:34 -06:00
|
|
|
.select('users.id')
|
2020-01-01 11:47:20 -06:00
|
|
|
.where('users.isSuspended = TRUE');
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
const query = makePaginationQuery(Notifications.createQueryBuilder('notification'), ps.sinceId, ps.untilId)
|
|
|
|
.andWhere(`notification.notifieeId = :meId`, { meId: user.id })
|
2021-03-19 04:22:34 -05:00
|
|
|
.leftJoinAndSelect('notification.notifier', 'notifier')
|
|
|
|
.leftJoinAndSelect('notification.note', 'note')
|
2021-03-20 22:31:56 -05:00
|
|
|
.leftJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser');
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2021-10-19 06:25:47 -05:00
|
|
|
query.andWhere(new Brackets(qb => { qb
|
|
|
|
.where(`notification.notifierId NOT IN (${ mutingQuery.getQuery() })`)
|
|
|
|
.orWhere('notification.notifierId IS NULL');
|
|
|
|
}));
|
2019-04-07 07:50:36 -05:00
|
|
|
query.setParameters(mutingQuery.getParameters());
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2021-12-09 06:38:56 -06:00
|
|
|
generateMutedInstanceNotificationQuery(query, user);
|
|
|
|
|
2021-10-19 06:25:47 -05:00
|
|
|
query.andWhere(new Brackets(qb => { qb
|
|
|
|
.where(`notification.notifierId NOT IN (${ suspendedQuery.getQuery() })`)
|
|
|
|
.orWhere('notification.notifierId IS NULL');
|
|
|
|
}));
|
2020-01-01 11:47:20 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (ps.following) {
|
|
|
|
query.andWhere(`((notification.notifierId IN (${ followingQuery.getQuery() })) OR (notification.notifierId = :meId))`, { meId: user.id });
|
|
|
|
query.setParameters(followingQuery.getParameters());
|
2016-12-28 16:49:51 -06:00
|
|
|
}
|
|
|
|
|
2021-05-28 08:52:30 -05:00
|
|
|
if (ps.includeTypes && ps.includeTypes.length > 0) {
|
2019-04-07 07:50:36 -05:00
|
|
|
query.andWhere(`notification.type IN (:...includeTypes)`, { includeTypes: ps.includeTypes });
|
2021-05-28 08:52:30 -05:00
|
|
|
} else if (ps.excludeTypes && ps.excludeTypes.length > 0) {
|
2019-04-07 07:50:36 -05:00
|
|
|
query.andWhere(`notification.type NOT IN (:...excludeTypes)`, { excludeTypes: ps.excludeTypes });
|
2019-01-07 22:32:28 -06:00
|
|
|
}
|
|
|
|
|
2021-10-08 22:44:19 -05:00
|
|
|
if (ps.unreadOnly) {
|
|
|
|
query.andWhere(`notification.isRead = false`);
|
|
|
|
}
|
|
|
|
|
2022-02-18 23:05:32 -06:00
|
|
|
const notifications = await query.take(ps.limit).getMany();
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-07-20 00:16:02 -05:00
|
|
|
// Mark all as read
|
2018-11-01 13:32:24 -05:00
|
|
|
if (notifications.length > 0 && ps.markAsRead) {
|
2019-04-07 07:50:36 -05:00
|
|
|
readNotification(user.id, notifications.map(x => x.id));
|
2016-12-28 16:49:51 -06:00
|
|
|
}
|
2019-02-21 20:46:58 -06:00
|
|
|
|
2021-05-28 08:53:00 -05: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-19 21:05:33 -05:00
|
|
|
return await Notifications.packMany(notifications, user.id);
|
2019-02-21 20:46:58 -06:00
|
|
|
});
|