返回 AiToEarn
relay-callback.controller.ts
根目录 / project / aitoearn-backend / apps / aitoearn-server / src / core / channels / relay / relay-callback.controller.ts
1 import { Body, Controller, Post, Query, Render } from '@nestjs/common'
2 import { ApiTags } from '@nestjs/swagger'
3 import { Public } from '@yikart/aitoearn-auth'
4 import { AccountType, ApiDoc, AppException, ChannelAuthTaskStatus, ResponseCode } from '@yikart/common'
5 import { ServerRedisService } from '../../../common/redis'
6 import { AccountService } from '../accounts/account.service'
7 import { RelayCallbackDto } from './relay-callback.dto'
8
9 interface RelayCallbackAccount {
10 relayAccountRef: string
11 nickname: string
12 avatar?: string
13 platformUid: string
14 platform: AccountType
15 }
16
17 @ApiTags('Channels/Relay')
18 @Controller({ path: '/channels/relay', version: '2' })
19 export class RelayCallbackController {
20 constructor(
21 private readonly accountService: AccountService,
22 private readonly redisService: ServerRedisService,
23 ) {}
24
25 @Public()
26 @ApiDoc({
27 summary: 'Relay OAuth 回调',
28 description: '接收官方服务器 OAuth 完成后浏览器 form POST 过来的账号信息,凭一次性 ticket 换取本地用户并创建 relay 账号',
29 body: RelayCallbackDto.schema,
30 })
31 @Post('/callback')
32 @Render('channels/auth/callback')
33 async handleRelayCallback(
34 @Body() body: RelayCallbackDto,
35 @Query('ticket') ticket: string,
36 ) {
37 if (!ticket) {
38 throw new AppException(ResponseCode.ChannelAuthCsrfInvalid)
39 }
40
41 const ticketValue = await this.redisService.getRelayCallbackTicket<{ userId?: string, groupId?: string }>(ticket)
42 await this.redisService.deleteRelayCallbackTicket(ticket)
43 if (!ticketValue?.userId) {
44 throw new AppException(ResponseCode.ChannelAuthCsrfInvalid)
45 }
46 const { userId, groupId } = ticketValue
47
48 const relayAccounts = this.getRelayCallbackAccounts(body)
49 const accounts: Array<{ id: string }> = []
50 for (const relayAccount of relayAccounts) {
51 const account = await this.accountService.createRelayAccount(userId, {
52 type: relayAccount.platform,
53 uid: relayAccount.platformUid,
54 nickname: relayAccount.nickname,
55 avatar: relayAccount.avatar,
56 relayAccountRef: relayAccount.relayAccountRef,
57 groupId,
58 })
59 if (account) {
60 accounts.push(account)
61 }
62 }
63 const accountIds = accounts.map(account => account.id)
64
65 if (body.taskId && accountIds.length > 0) {
66 const authTaskPlatform = this.getAuthTaskPlatform(relayAccounts[0].platform)
67 await this.redisService.saveLegacyRelayAuthTask(authTaskPlatform, body.taskId, {
68 status: ChannelAuthTaskStatus.Completed,
69 accountId: accountIds[0],
70 accountIds,
71 })
72 }
73
74 return {
75 status: 1,
76 message: '授权成功',
77 accountId: accountIds[0],
78 accountIds,
79 redirectUri: body.redirectUri,
80 }
81 }
82
83 private getRelayCallbackAccounts(body: RelayCallbackDto): RelayCallbackAccount[] {
84 if (body.accounts) {
85 try {
86 const accounts = JSON.parse(body.accounts) as RelayCallbackAccount[]
87 if (Array.isArray(accounts) && accounts.length > 0) {
88 const validAccounts = accounts.filter(account => (
89 account.relayAccountRef
90 && account.platformUid
91 && account.platform
92 && account.nickname
93 ))
94 if (validAccounts.length > 0) {
95 return validAccounts
96 }
97 }
98 }
99 catch {
100 // Fall back to legacy single-account fields below.
101 }
102 }
103
104 return [{
105 relayAccountRef: body.relayAccountRef,
106 nickname: body.nickname,
107 avatar: body.avatar,
108 platformUid: body.platformUid,
109 platform: body.platform,
110 }]
111 }
112
113 private getAuthTaskPlatform(platform: string): string {
114 const META_PLATFORMS = ['facebook', 'instagram', 'threads', 'linkedin']
115 if (META_PLATFORMS.includes(platform)) {
116 return 'meta'
117 }
118 return platform
119 }
120 }
121
121 lines TYPESCRIPT