-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.ts
183 lines (157 loc) · 5.44 KB
/
worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
export interface Env {
CROWDIN_OAUTH_HOST: string;
CROWDIN_API_HOST: string;
REDIRECT_URI: string;
REDIRECT_URI_HANDOVER: string;
HANDOVER_URI: string;
CLIENT_ID: string;
CLIENT_SECRET: string;
PYCORD_SUPPORT_API_KEY: string;
}
export interface CrowdinUser {
username: string;
isAdmin: boolean;
id: number;
createdAt: string;
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
return await handleRequest(request, env)
}
} satisfies ExportedHandler<Env>;
async function handleRequest(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url)
if (url.pathname === '/crowdin/handover-login') {
return handleLogin(env.REDIRECT_URI_HANDOVER, env)
} else if (url.pathname === '/crowdin/login') {
return handleLogin(env.REDIRECT_URI, env)
} else if (url.pathname === '/crowdin/callback') {
return handleCallback(url, env)
} else if (url.pathname === '/crowdin/handover') {
return handleHandoverCallback(url, env)
} else {
return new Response('Not Found', { status: 404 })
}
}
function handleLogin(uri: string, env: Env): Response {
const authorizationUrl = `${env.CROWDIN_OAUTH_HOST}/authorize?client_id=${env.CLIENT_ID}&redirect_uri=${encodeURIComponent(uri)}&response_type=code&scope=*`
return Response.redirect(authorizationUrl, 302)
}
async function handleHandoverCallback(url: URL, env: Env): Promise<Response> {
const code = url.searchParams.get('code');
if (!code) {
return new Response('Authorization code not found.', { status: 400 });
}
const tokenResponse = await fetch(`${env.CROWDIN_OAUTH_HOST}/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
client_id: env.CLIENT_ID,
client_secret: env.CLIENT_SECRET,
redirect_uri: env.REDIRECT_URI_HANDOVER,
code: code,
grant_type: 'authorization_code'
})
});
if (!tokenResponse.ok) {
return new Response('Failed to fetch access token.', { status: 500 });
}
const tokenData: any = await tokenResponse.json();
const accessToken: string = tokenData.access_token;
const userInfo = await fetchUserInfo(accessToken, env)
const userTranslations = await fetchUserTotalTranslations(accessToken, userInfo.id, env)
const handoverResponse = await fetch(env.HANDOVER_URI, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `AITSYS ${env.PYCORD_SUPPORT_API_KEY}`
},
body: JSON.stringify({ "crowdin_user": userInfo, "crowdin_translation_count": userTranslations })
});
if (!handoverResponse.ok) {
return new Response('Failed to hand over data.', { status: 500 });
}
return new Response('Data successfully handed over.', { status: 200 });
}
async function handleCallback(url: URL, env: Env): Promise<Response> {
const code = url.searchParams.get('code')
if (!code) {
return new Response('Authorization code not found.', { status: 400 })
}
const tokenResponse = await fetch(`${env.CROWDIN_OAUTH_HOST}/token`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
client_id: env.CLIENT_ID,
client_secret: env.CLIENT_SECRET,
redirect_uri: env.REDIRECT_URI,
code: code,
grant_type: 'authorization_code'
})
})
if (!tokenResponse.ok) {
return new Response('Failed to fetch access token.', { status: 500 })
}
const tokenData: any = await tokenResponse.json()
const accessToken: string = tokenData.access_token
const userInfo = await fetchUserInfo(accessToken, env)
const userTranslations = await fetchUserTotalTranslations(accessToken, userInfo.id, env)
return new Response(`Hi there ${userInfo.username}! You have ${userTranslations} translations. Please make sure you execute '/crowdin-sync' on the server with the Pycord Support application`, { status: 200 })
}
async function fetchUserInfo(accessToken: string, env: Env): Promise<CrowdinUser> {
const query = `
query GetUserInfo {
viewer {
username
isAdmin
id
createdAt
}
}
`
const response: any = await fetchCrowdinApi(query, accessToken, env)
return response.viewer as CrowdinUser
}
async function fetchUserTotalTranslations(accessToken: string, userId: number, env: Env): Promise<number> {
const query = `
query GetUserTotalTranslations {
viewer {
projects(first: 1) {
edges {
node {
id
identifier
description
nodeId
name
translations(userId: ${userId}, first: 10) {
totalCount
}
}
}
}
}
}
`
const response = await fetchCrowdinApi(query, accessToken, env)
return response.viewer.projects.edges[0].node.translations.totalCount as number
}
async function fetchCrowdinApi(query: string, accessToken: string, env: Env): Promise<any> {
const response = await fetch(env.CROWDIN_API_HOST, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify({ query })
})
const responseData: any = await response.json()
if (responseData.errors) {
throw new Error(responseData.errors[0].message)
}
return responseData.data
}