2022-06-14 09:01:23 +00:00
|
|
|
import { Brackets } from 'typeorm';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { UsersRepository, FollowingsRepository, MutingsRepository, UserProfilesRepository, NotificationsRepository } from '@/models/index.js';
|
2022-06-14 09:01:23 +00:00
|
|
|
import { notificationTypes } from '@/types.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { QueryService } from '@/core/QueryService.js';
|
|
|
|
import { NoteReadService } from '@/core/NoteReadService.js';
|
|
|
|
import { NotificationEntityService } from '@/core/entities/NotificationEntityService.js';
|
|
|
|
import { NotificationService } from '@/core/NotificationService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['account', 'notifications'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
2018-11-01 18:32:24 +00:00
|
|
|
|
2022-06-14 13:55:58 +00:00
|
|
|
limit: {
|
|
|
|
duration: 60000,
|
2022-07-24 08:56:48 +00:00
|
|
|
max: 15,
|
2022-06-14 13:55:58 +00:00
|
|
|
},
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
kind: 'read:notifications',
|
2018-11-01 18:32:24 +00:00
|
|
|
|
2019-02-24 09:13:11 +00:00
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 09:13:11 +00:00
|
|
|
items: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 13:35:26 +00:00
|
|
|
ref: 'Notification',
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2019-02-24 09:13:11 +00:00
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-11-01 18:32:24 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00: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 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
private followingsRepository: FollowingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.mutingsRepository)
|
|
|
|
private mutingsRepository: MutingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notificationsRepository)
|
|
|
|
private notificationsRepository: NotificationsRepository,
|
|
|
|
|
|
|
|
private notificationEntityService: NotificationEntityService,
|
|
|
|
private notificationService: NotificationService,
|
|
|
|
private queryService: QueryService,
|
|
|
|
private noteReadService: NoteReadService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
// includeTypes が空の場合はクエリしない
|
|
|
|
if (ps.includeTypes && ps.includeTypes.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
// excludeTypes に全指定されている場合はクエリしない
|
|
|
|
if (notificationTypes.every(type => ps.excludeTypes?.includes(type))) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const followingQuery = this.followingsRepository.createQueryBuilder('following')
|
|
|
|
.select('following.followeeId')
|
|
|
|
.where('following.followerId = :followerId', { followerId: me.id });
|
|
|
|
|
|
|
|
const mutingQuery = this.mutingsRepository.createQueryBuilder('muting')
|
|
|
|
.select('muting.muteeId')
|
|
|
|
.where('muting.muterId = :muterId', { muterId: me.id });
|
|
|
|
|
|
|
|
const mutingInstanceQuery = this.userProfilesRepository.createQueryBuilder('user_profile')
|
|
|
|
.select('user_profile.mutedInstances')
|
|
|
|
.where('user_profile.userId = :muterId', { muterId: me.id });
|
|
|
|
|
|
|
|
const suspendedQuery = this.usersRepository.createQueryBuilder('users')
|
|
|
|
.select('users.id')
|
|
|
|
.where('users.isSuspended = TRUE');
|
|
|
|
|
|
|
|
const query = this.queryService.makePaginationQuery(this.notificationsRepository.createQueryBuilder('notification'), ps.sinceId, ps.untilId)
|
|
|
|
.andWhere('notification.notifieeId = :meId', { meId: me.id })
|
|
|
|
.leftJoinAndSelect('notification.notifier', 'notifier')
|
|
|
|
.leftJoinAndSelect('notification.note', 'note')
|
|
|
|
.leftJoinAndSelect('notifier.avatar', 'notifierAvatar')
|
|
|
|
.leftJoinAndSelect('notifier.banner', 'notifierBanner')
|
|
|
|
.leftJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('user.avatar', 'avatar')
|
|
|
|
.leftJoinAndSelect('user.banner', 'banner')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
|
|
|
.leftJoinAndSelect('replyUser.avatar', 'replyUserAvatar')
|
|
|
|
.leftJoinAndSelect('replyUser.banner', 'replyUserBanner')
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser')
|
|
|
|
.leftJoinAndSelect('renoteUser.avatar', 'renoteUserAvatar')
|
|
|
|
.leftJoinAndSelect('renoteUser.banner', 'renoteUserBanner');
|
|
|
|
|
|
|
|
// muted users
|
|
|
|
query.andWhere(new Brackets(qb => { qb
|
|
|
|
.where(`notification.notifierId NOT IN (${ mutingQuery.getQuery() })`)
|
|
|
|
.orWhere('notification.notifierId IS NULL');
|
|
|
|
}));
|
|
|
|
query.setParameters(mutingQuery.getParameters());
|
|
|
|
|
|
|
|
// muted instances
|
|
|
|
query.andWhere(new Brackets(qb => { qb
|
|
|
|
.andWhere('notifier.host IS NULL')
|
|
|
|
.orWhere(`NOT (( ${mutingInstanceQuery.getQuery()} )::jsonb ? notifier.host)`);
|
|
|
|
}));
|
|
|
|
query.setParameters(mutingInstanceQuery.getParameters());
|
|
|
|
|
|
|
|
// suspended users
|
|
|
|
query.andWhere(new Brackets(qb => { qb
|
|
|
|
.where(`notification.notifierId NOT IN (${ suspendedQuery.getQuery() })`)
|
|
|
|
.orWhere('notification.notifierId IS NULL');
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (ps.following) {
|
|
|
|
query.andWhere(`((notification.notifierId IN (${ followingQuery.getQuery() })) OR (notification.notifierId = :meId))`, { meId: me.id });
|
|
|
|
query.setParameters(followingQuery.getParameters());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ps.includeTypes && ps.includeTypes.length > 0) {
|
|
|
|
query.andWhere('notification.type IN (:...includeTypes)', { includeTypes: ps.includeTypes });
|
|
|
|
} else if (ps.excludeTypes && ps.excludeTypes.length > 0) {
|
|
|
|
query.andWhere('notification.type NOT IN (:...excludeTypes)', { excludeTypes: ps.excludeTypes });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ps.unreadOnly) {
|
|
|
|
query.andWhere('notification.isRead = false');
|
|
|
|
}
|
|
|
|
|
|
|
|
const notifications = await query.take(ps.limit).getMany();
|
|
|
|
|
|
|
|
// Mark all as read
|
|
|
|
if (notifications.length > 0 && ps.markAsRead) {
|
|
|
|
this.notificationService.readNotification(me.id, notifications.map(x => x.id));
|
|
|
|
}
|
|
|
|
|
|
|
|
const notes = notifications.filter(notification => ['mention', 'reply', 'quote'].includes(notification.type)).map(notification => notification.note!);
|
|
|
|
|
|
|
|
if (notes.length > 0) {
|
|
|
|
this.noteReadService.read(me.id, notes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.notificationEntityService.packMany(notifications, me.id);
|
|
|
|
});
|
2020-08-22 01:06:17 +00:00
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|