This commit is contained in:
tamaina 2024-02-21 18:08:27 +00:00
parent ed33ac7141
commit e2443d8dc5
3 changed files with 39 additions and 71 deletions

View file

@ -16,6 +16,8 @@ import { bindThis } from '@/decorators.js';
import { isNotNull } from '@/misc/is-not-null.js'; import { isNotNull } from '@/misc/is-not-null.js';
import { FilterUnionByProperty, groupedNotificationTypes } from '@/types.js'; import { FilterUnionByProperty, groupedNotificationTypes } from '@/types.js';
import { CacheService } from '@/core/CacheService.js'; import { CacheService } from '@/core/CacheService.js';
import { NoteReadService } from '@/core/NoteReadService.js';
import { trackPromise } from '@/misc/promise-tracker.js';
import { RoleEntityService } from './RoleEntityService.js'; import { RoleEntityService } from './RoleEntityService.js';
import type { OnModuleInit } from '@nestjs/common'; import type { OnModuleInit } from '@nestjs/common';
import type { UserEntityService } from './UserEntityService.js'; import type { UserEntityService } from './UserEntityService.js';
@ -42,6 +44,7 @@ export class NotificationEntityService implements OnModuleInit {
private followRequestsRepository: FollowRequestsRepository, private followRequestsRepository: FollowRequestsRepository,
private cacheService: CacheService, private cacheService: CacheService,
private noteReadService: NoteReadService,
//private userEntityService: UserEntityService, //private userEntityService: UserEntityService,
//private noteEntityService: NoteEntityService, //private noteEntityService: NoteEntityService,
@ -82,9 +85,7 @@ export class NotificationEntityService implements OnModuleInit {
}) })
) : undefined; ) : undefined;
// if the note has been deleted, don't show this notification // if the note has been deleted, don't show this notification
if (needsNote && !noteIfNeed) { if (needsNote && !noteIfNeed) return null;
return null;
}
const needsUser = 'notifierId' in notification; const needsUser = 'notifierId' in notification;
const userIfNeed = needsUser ? ( const userIfNeed = needsUser ? (
@ -93,9 +94,7 @@ export class NotificationEntityService implements OnModuleInit {
: this.userEntityService.pack(notification.notifierId, { id: meId }) : this.userEntityService.pack(notification.notifierId, { id: meId })
) : undefined; ) : undefined;
// if the user has been deleted, don't show this notification // if the user has been deleted, don't show this notification
if (needsUser && !userIfNeed) { if (needsUser && !userIfNeed) return null;
return null;
}
// #region Grouped notifications // #region Grouped notifications
if (notification.type === 'reaction:grouped') { if (notification.type === 'reaction:grouped') {
@ -178,6 +177,7 @@ export class NotificationEntityService implements OnModuleInit {
async #packManyInternal <T extends MiNotification | MiGroupedNotification> ( async #packManyInternal <T extends MiNotification | MiGroupedNotification> (
notifications: T[], notifications: T[],
meId: MiUser['id'], meId: MiUser['id'],
markNotesAsRead = false,
): Promise<T[]> { ): Promise<T[]> {
if (notifications.length === 0) return []; if (notifications.length === 0) return [];
@ -218,39 +218,29 @@ export class NotificationEntityService implements OnModuleInit {
validNotifications = validNotifications.filter(x => (x.type !== 'receiveFollowRequest') || reqs.some(r => r.followerId === x.notifierId)); validNotifications = validNotifications.filter(x => (x.type !== 'receiveFollowRequest') || reqs.some(r => r.followerId === x.notifierId));
} }
return (await Promise.all(validNotifications.map(x => this.packGrouped(x, meId, { checkValidNotifier: false }, { const packPromises = validNotifications.map(x => {
packedNotes, return this.pack(
packedUsers, x,
})))).filter(isNotNull); meId,
{ checkValidNotifier: false },
{ packedNotes, packedUsers },
);
});
if (markNotesAsRead) {
try {
trackPromise(this.noteReadService.read(meId, notes));
} catch (e) {
// console.error('error thrown by NoteReadService.read', e);
}
}
return (await Promise.all(packPromises)).filter(isNotNull);
} }
@bindThis @bindThis
public async pack( public async pack(
src: MiNotification, src: MiNotification | MiGroupedNotification,
meId: MiUser['id'],
// eslint-disable-next-line @typescript-eslint/ban-types
options: {
checkValidNotifier?: boolean;
},
hint?: {
packedNotes: Map<MiNote['id'], Packed<'Note'>>;
packedUsers: Map<MiUser['id'], Packed<'UserLite'>>;
},
): Promise<Packed<'Notification'> | null> {
return this.#packInternal(src, meId, options, hint);
}
@bindThis
public async packMany(
notifications: MiNotification[],
meId: MiUser['id'],
): Promise<MiNotification[]> {
return await this.#packManyInternal(notifications, meId);
}
@bindThis
public async packGrouped(
src: MiGroupedNotification,
meId: MiUser['id'], meId: MiUser['id'],
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types
options: { options: {
@ -264,12 +254,22 @@ export class NotificationEntityService implements OnModuleInit {
return await this.#packInternal(src, meId, options, hint); return await this.#packInternal(src, meId, options, hint);
} }
@bindThis
public async packMany(
notifications: MiNotification[],
meId: MiUser['id'],
markNotesAsRead = false,
): Promise<MiNotification[]> {
return await this.#packManyInternal(notifications, meId, markNotesAsRead);
}
@bindThis @bindThis
public async packGroupedMany( public async packGroupedMany(
notifications: MiGroupedNotification[], notifications: MiGroupedNotification[],
meId: MiUser['id'], meId: MiUser['id'],
markNotesAsRead = false,
) : Promise<MiGroupedNotification[]> { ) : Promise<MiGroupedNotification[]> {
return await this.#packManyInternal(notifications, meId); return await this.#packManyInternal(notifications, meId, markNotesAsRead);
} }
/** /**
@ -301,21 +301,7 @@ export class NotificationEntityService implements OnModuleInit {
notification: T, notification: T,
meId: MiUser['id'], meId: MiUser['id'],
) : Promise<boolean> { ) : Promise<boolean> {
const [ return (await this.#filterValidNotifier([notification], meId)).length === 1;
userIdsWhoMeMuting,
userMutedInstances,
] = await Promise.all([
this.cacheService.userMutingsCache.fetch(meId),
this.cacheService.userProfileCache.fetch(meId).then(p => new Set(p.mutedInstances)),
]);
const notifiers = (await Promise.all([notification]
.map(x => 'notifierId' in x ? x.notifierId : null)
.filter(isNotNull)
.map(async id => await this.usersRepository.findOneBy({ id }))))
.filter(isNotNull);
return this.#validateNotifier(notification, userIdsWhoMeMuting, userMutedInstances, notifiers);
} }
/** /**

View file

@ -163,16 +163,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
groupedNotifications = groupedNotifications.slice(0, ps.limit); groupedNotifications = groupedNotifications.slice(0, ps.limit);
const noteIds = groupedNotifications return await this.notificationEntityService.packGroupedMany(groupedNotifications, me.id, true);
.filter((notification): notification is FilterUnionByProperty<MiNotification, 'type', 'mention' | 'reply' | 'quote'> => ['mention', 'reply', 'quote'].includes(notification.type))
.map(notification => notification.noteId!);
if (noteIds.length > 0) {
const notes = await this.notesRepository.findBy({ id: In(noteIds) });
this.noteReadService.read(me.id, notes);
}
return await this.notificationEntityService.packGroupedMany(groupedNotifications, me.id);
}); });
} }
} }

View file

@ -112,16 +112,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
this.notificationService.readAllNotification(me.id); this.notificationService.readAllNotification(me.id);
} }
const noteIds = notifications return await this.notificationEntityService.packMany(notifications, me.id, true);
.filter((notification): notification is FilterUnionByProperty<MiNotification, 'type', 'mention' | 'reply' | 'quote'> => ['mention', 'reply', 'quote'].includes(notification.type))
.map(notification => notification.noteId);
if (noteIds.length > 0) {
const notes = await this.notesRepository.findBy({ id: In(noteIds) });
this.noteReadService.read(me.id, notes);
}
return await this.notificationEntityService.packMany(notifications, me.id);
}); });
} }
} }