2023-07-27 14:31:52 +09:00
/ *
2024-02-13 15:59:27 +00:00
* SPDX - FileCopyrightText : syuilo and misskey - project
2023-07-27 14:31:52 +09:00
* SPDX - License - Identifier : AGPL - 3.0 - only
* /
2022-09-18 03:27:08 +09:00
import { Inject , Injectable } from '@nestjs/common' ;
import { DI } from '@/di-symbols.js' ;
2024-10-03 15:06:04 +09:00
import type { SigninsRepository , UserProfilesRepository } from '@/models/_.js' ;
2022-09-18 03:27:08 +09:00
import { IdService } from '@/core/IdService.js' ;
2023-09-20 11:33:36 +09:00
import type { MiLocalUser } from '@/models/User.js' ;
2022-09-18 03:27:08 +09:00
import { GlobalEventService } from '@/core/GlobalEventService.js' ;
import { SigninEntityService } from '@/core/entities/SigninEntityService.js' ;
2022-12-04 15:03:09 +09:00
import { bindThis } from '@/decorators.js' ;
2024-10-03 15:06:04 +09:00
import { EmailService } from '@/core/EmailService.js' ;
import { NotificationService } from '@/core/NotificationService.js' ;
2022-12-14 00:01:45 +09:00
import type { FastifyRequest , FastifyReply } from 'fastify' ;
2022-09-18 03:27:08 +09:00
@Injectable ( )
export class SigninService {
constructor (
@Inject ( DI . signinsRepository )
private signinsRepository : SigninsRepository ,
2024-10-03 15:06:04 +09:00
@Inject ( DI . userProfilesRepository )
private userProfilesRepository : UserProfilesRepository ,
2022-09-18 03:27:08 +09:00
private signinEntityService : SigninEntityService ,
2024-10-03 15:06:04 +09:00
private emailService : EmailService ,
private notificationService : NotificationService ,
2022-09-18 03:27:08 +09:00
private idService : IdService ,
private globalEventService : GlobalEventService ,
) {
}
2022-12-04 15:03:09 +09:00
@bindThis
2023-08-16 17:51:28 +09:00
public signin ( request : FastifyRequest , reply : FastifyReply , user : MiLocalUser ) {
2022-12-03 19:42:05 +09:00
setImmediate ( async ( ) = > {
2024-10-03 15:06:04 +09:00
this . notificationService . createNotification ( user . id , 'login' , { } ) ;
2024-06-01 11:16:44 +09:00
const record = await this . signinsRepository . insertOne ( {
2023-10-16 10:45:22 +09:00
id : this.idService.gen ( ) ,
2022-12-03 19:42:05 +09:00
userId : user.id ,
ip : request.ip ,
2023-02-09 10:46:01 +09:00
headers : request.headers as any ,
2022-12-03 19:42:05 +09:00
success : true ,
2024-06-01 11:16:44 +09:00
} ) ;
2023-07-08 07:08:16 +09:00
2022-12-03 19:42:05 +09:00
this . globalEventService . publishMainStream ( user . id , 'signin' , await this . signinEntityService . pack ( record ) ) ;
2024-10-03 15:06:04 +09:00
const profile = await this . userProfilesRepository . findOneByOrFail ( { userId : user.id } ) ;
if ( profile . email && profile . emailVerified ) {
this . emailService . sendEmail ( profile . email , 'New login / ログインがありました' ,
'There is a new login. If you do not recognize this login, update the security status of your account, including changing your password. / 新しいログインがありました。このログインに心当たりがない場合は、パスワードを変更するなど、アカウントのセキュリティ状態を更新してください。' ,
'There is a new login. If you do not recognize this login, update the security status of your account, including changing your password. / 新しいログインがありました。このログインに心当たりがない場合は、パスワードを変更するなど、アカウントのセキュリティ状態を更新してください。' ) ;
}
2022-12-03 19:42:05 +09:00
} ) ;
2023-02-09 10:47:03 +09:00
reply . code ( 200 ) ;
return {
id : user.id ,
i : user.token ,
} ;
2022-09-18 03:27:08 +09:00
}
}