Update Discord Linked Roles to take into account contributors

This commit is contained in:
Benjamin 2025-01-16 19:53:32 -05:00
parent 8cc8ab4b95
commit 563a0c0420
No known key found for this signature in database
GPG Key ID: E9469FAA8B44BB16

View File

@ -91,10 +91,43 @@ const getUserData = async (token: TokenSet) => {
};
};
const checkForContributions = async (req: Request, username: string): Promise<boolean> => {
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<Pick<UserRow, 'username' | 'roles'>>(
const user = await req.db.get<Pick<UserRow, 'username'>>(
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,
},
}),