2023-04-04 07:06:57 +02:00
|
|
|
import { Brackets, In } from 'typeorm';
|
2023-04-14 06:50:05 +02:00
|
|
|
import * as Redis from 'ioredis';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-04-04 07:06:57 +02:00
|
|
|
import type { UsersRepository, FollowingsRepository, MutingsRepository, UserProfilesRepository, NotesRepository } from '@/models/index.js';
|
2023-02-23 12:46:14 +01:00
|
|
|
import { obsoleteNotificationTypes, notificationTypes } from '@/types.js';
|
2022-09-17 20:27:08 +02: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';
|
2023-04-04 07:06:57 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
|
|
|
import { Notification } from '@/models/entities/Notification.js';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['account', 'notifications'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2018-11-01 19:32:24 +01:00
|
|
|
|
2022-06-14 15:55:58 +02:00
|
|
|
limit: {
|
2023-02-09 09:32:42 +01:00
|
|
|
duration: 30000,
|
2023-02-09 10:11:11 +01:00
|
|
|
max: 30,
|
2022-06-14 15:55:58 +02:00
|
|
|
},
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
kind: 'read:notifications',
|
2018-11-01 19:32:24 +01:00
|
|
|
|
2019-02-24 10:13:11 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 10:13:11 +01:00
|
|
|
items: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'Notification',
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-02-24 10:13:11 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-11-01 19:32:24 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01: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' },
|
|
|
|
markAsRead: { type: 'boolean', default: true },
|
2023-02-23 12:46:14 +01:00
|
|
|
// 後方互換のため、廃止された通知タイプも受け付ける
|
2022-02-19 06:05:32 +01:00
|
|
|
includeTypes: { type: 'array', items: {
|
2023-02-23 12:46:14 +01:00
|
|
|
type: 'string', enum: [...notificationTypes, ...obsoleteNotificationTypes],
|
2022-02-19 06:05:32 +01:00
|
|
|
} },
|
|
|
|
excludeTypes: { type: 'array', items: {
|
2023-02-23 12:46:14 +01:00
|
|
|
type: 'string', enum: [...notificationTypes, ...obsoleteNotificationTypes],
|
2022-02-19 06:05:32 +01:00
|
|
|
} },
|
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
2023-04-04 07:06:57 +02:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.mutingsRepository)
|
|
|
|
private mutingsRepository: MutingsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
2023-04-04 07:06:57 +02:00
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-04-04 07:06:57 +02:00
|
|
|
private idService: IdService,
|
2022-09-17 20:27:08 +02:00
|
|
|
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 [];
|
|
|
|
}
|
2023-02-23 12:46:14 +01:00
|
|
|
|
|
|
|
const includeTypes = ps.includeTypes && ps.includeTypes.filter(type => !(obsoleteNotificationTypes).includes(type as any)) as typeof notificationTypes[number][];
|
|
|
|
const excludeTypes = ps.excludeTypes && ps.excludeTypes.filter(type => !(obsoleteNotificationTypes).includes(type as any)) as typeof notificationTypes[number][];
|
|
|
|
|
2023-04-12 04:40:08 +02:00
|
|
|
const limit = ps.limit + (ps.untilId ? 1 : 0); // untilIdに指定したものも含まれるため+1
|
2023-04-04 07:06:57 +02:00
|
|
|
const notificationsRes = await this.redisClient.xrevrange(
|
|
|
|
`notificationTimeline:${me.id}`,
|
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : '+',
|
|
|
|
'-',
|
2023-04-12 04:40:08 +02:00
|
|
|
'COUNT', limit);
|
2023-04-04 07:06:57 +02:00
|
|
|
|
|
|
|
if (notificationsRes.length === 0) {
|
|
|
|
return [];
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-04 07:06:57 +02:00
|
|
|
let notifications = notificationsRes.map(x => JSON.parse(x[1][1])).filter(x => x.id !== ps.untilId) as Notification[];
|
|
|
|
|
2023-02-23 12:46:14 +01:00
|
|
|
if (includeTypes && includeTypes.length > 0) {
|
2023-04-04 07:06:57 +02:00
|
|
|
notifications = notifications.filter(notification => includeTypes.includes(notification.type));
|
2023-02-23 12:46:14 +01:00
|
|
|
} else if (excludeTypes && excludeTypes.length > 0) {
|
2023-04-04 07:06:57 +02:00
|
|
|
notifications = notifications.filter(notification => !excludeTypes.includes(notification.type));
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-04 07:06:57 +02:00
|
|
|
if (notifications.length === 0) {
|
|
|
|
return [];
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mark all as read
|
2023-04-04 07:06:57 +02:00
|
|
|
if (ps.markAsRead) {
|
|
|
|
this.notificationService.readAllNotification(me.id);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2023-04-04 07:06:57 +02:00
|
|
|
const noteIds = notifications
|
|
|
|
.filter(notification => ['mention', 'reply', 'quote'].includes(notification.type))
|
|
|
|
.map(notification => notification.noteId!);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-04-04 07:06:57 +02:00
|
|
|
if (noteIds.length > 0) {
|
|
|
|
const notes = await this.notesRepository.findBy({ id: In(noteIds) });
|
2022-09-17 20:27:08 +02:00
|
|
|
this.noteReadService.read(me.id, notes);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.notificationEntityService.packMany(notifications, me.id);
|
|
|
|
});
|
2020-08-22 03:06:17 +02:00
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|