diff --git a/server/express/discord.ts b/server/express/discord.ts index aebee46fa..cbae94d15 100644 --- a/server/express/discord.ts +++ b/server/express/discord.ts @@ -91,10 +91,43 @@ const getUserData = async (token: TokenSet) => { }; }; +const checkForContributions = async (req: Request, username: string): Promise => { + const TRANSLATION_STATUS = { + REJECTED: -1, + AWAITING: 0, + APPROVED: 1, + MERGED: 2, + }; + const user = await req.db.get<{ id: string; roles: string[] }>( + SQL`SELECT id, roles FROM users WHERE username=${username}`, + ); + if (!user) { + return false; + } + if (user.roles.length > 0) { + return true; + } + const translationCountByContributor = await req.db.get<{ c: number }>(SQL` + SELECT count(*) AS c FROM translations + WHERE status >= ${TRANSLATION_STATUS.APPROVED} AND author_id=${user.id} + LIMIT 1 + `); + if (translationCountByContributor !== undefined && translationCountByContributor.c > 0) { + return true; + } + const profiles = await req.db.all<{ teamName: string; footerName: string }>( + SQL`SELECT teamName, footerName FROM profiles WHERE userId=${user.id}`, + ); + if (profiles.some((profile) => profile.teamName !== '' || profile.footerName !== '')) { + return true; + } + return false; +}; + const updateMetadata = async (req: Request, res: Response, data: { uid: string; email: string }) => { - const user = await req.db.get>( + const user = await req.db.get>( SQL` - SELECT users.username, users.roles FROM social_lookup + SELECT users.username FROM social_lookup LEFT JOIN users on social_lookup.userId = users.id WHERE social_lookup.provider = 'discord' AND social_lookup.identifier = ${data.uid};`, ); @@ -111,6 +144,9 @@ const updateMetadata = async (req: Request, res: Response, data: { uid: string; }; return res.status(error.code).json(error); } + + const isTeam = await checkForContributions(req, user.username); + await fetch( `${BASE_DISCORD_URI}/users/@me/applications/${configs.discordClientId}/role-connection`, { @@ -123,7 +159,7 @@ const updateMetadata = async (req: Request, res: Response, data: { uid: string; platform_name: 'Pronouns.Page', platform_username: user.username, metadata: { - isteam: user.roles !== '', + isteam: isTeam, isuser: true, }, }),