add notifications for shared access granted/revoked/login

This commit is contained in:
Hazelnoot 2025-06-21 22:01:23 -04:00
parent 2a13e97863
commit 35a167701f
10 changed files with 260 additions and 12 deletions

View file

@ -7,7 +7,7 @@ import { Inject, Injectable } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { In, EntityNotFoundError } from 'typeorm';
import { DI } from '@/di-symbols.js';
import type { FollowRequestsRepository, NotesRepository, MiUser, UsersRepository, MiAccessToken } from '@/models/_.js';
import type { FollowRequestsRepository, NotesRepository, MiUser, UsersRepository, MiAccessToken, AccessTokensRepository } from '@/models/_.js';
import { awaitAll } from '@/misc/prelude/await-all.js';
import type { MiGroupedNotification, MiNotification } from '@/models/Notification.js';
import type { MiNote } from '@/models/Note.js';
@ -21,7 +21,7 @@ import type { OnModuleInit } from '@nestjs/common';
import type { UserEntityService } from './UserEntityService.js';
import type { NoteEntityService } from './NoteEntityService.js';
const NOTE_REQUIRED_NOTIFICATION_TYPES = new Set(['note', 'mention', 'reply', 'renote', 'renote:grouped', 'quote', 'reaction', 'reaction:grouped', 'pollEnded', 'edited', 'scheduledNotePosted'] as (typeof groupedNotificationTypes[number])[]);
const NOTE_REQUIRED_NOTIFICATION_TYPES = new Set(['note', 'mention', 'reply', 'renote', 'renote:grouped', 'quote', 'reaction', 'reaction:grouped', 'pollEnded', 'edited', 'scheduledNotePosted']);
function undefOnMissing<T>(packPromise: Promise<T>): Promise<T | undefined> {
return packPromise.catch(err => {
@ -49,6 +49,9 @@ export class NotificationEntityService implements OnModuleInit {
@Inject(DI.followRequestsRepository)
private followRequestsRepository: FollowRequestsRepository,
@Inject(DI.accessTokensRepository)
private readonly accessTokensRepository: AccessTokensRepository,
private cacheService: CacheService,
) {
}
@ -162,6 +165,9 @@ export class NotificationEntityService implements OnModuleInit {
return null;
}
const needsAccessToken = notification.type === 'sharedAccessGranted';
const accessToken = (needsAccessToken && notification.tokenId) ? await this.accessTokensRepository.findOneBy({ id: notification.tokenId }) : null;
return await awaitAll({
id: notification.id,
createdAt: new Date(notification.createdAt).toISOString(),
@ -200,6 +206,14 @@ export class NotificationEntityService implements OnModuleInit {
header: notification.customHeader,
icon: notification.customIcon,
} : {}),
...(notification.type === 'sharedAccessGranted' ? {
tokenId: notification.tokenId,
token: accessToken ? {
id: accessToken.id,
permission: accessToken.permission,
rank: accessToken.rank,
} : null,
} : {}),
});
}