Update discord avatar (#3068)

* Update avatar

* Remove linking notification

* fixed spacing

Co-authored-by: Miodec <bartnikjack@gmail.com>
This commit is contained in:
Bruce Berrios 2022-06-03 13:42:00 -04:00 committed by GitHub
parent 398d4eb63c
commit 4dc6f1e791
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 67 additions and 94 deletions

View file

@ -122,26 +122,25 @@ export async function linkDiscord(
req: MonkeyTypes.Request
): Promise<MonkeyResponse> {
const { uid } = req.ctx.decodedToken;
const {
data: { tokenType, accessToken },
} = req.body;
const { tokenType, accessToken } = req.body;
const userInfo = await UserDAL.getUser(uid, "link discord");
if (userInfo.discordId) {
throw new MonkeyError(
409,
"This account is already linked to a Discord account"
);
}
if (userInfo.banned) {
throw new MonkeyError(403, "Banned accounts cannot link with Discord");
}
const { id: discordId, avatar } = await getDiscordUser(
const { id: discordId, avatar: discordAvatar } = await getDiscordUser(
tokenType,
accessToken
);
if (userInfo.discordId) {
await UserDAL.linkDiscord(uid, userInfo.discordId, discordAvatar);
return new MonkeyResponse("Discord avatar updated", {
discordAvatar,
});
}
if (!discordId) {
throw new MonkeyError(
500,
@ -158,12 +157,14 @@ export async function linkDiscord(
);
}
await UserDAL.linkDiscord(uid, discordId, avatar);
await UserDAL.linkDiscord(uid, discordId, discordAvatar);
George.linkDiscord(discordId, uid);
Logger.logToDb("user_discord_link", `linked to ${discordId}`, uid);
return new MonkeyResponse("Discord account linked", discordId);
return new MonkeyResponse("Discord account linked", {
discordId,
});
}
export async function unlinkDiscord(
@ -183,23 +184,6 @@ export async function unlinkDiscord(
return new MonkeyResponse("Discord account unlinked");
}
export async function updateDiscordAvatar(
req: MonkeyTypes.Request
): Promise<MonkeyResponse> {
const { uid } = req.ctx.decodedToken;
const { tokenType, accessToken } = req.body;
const userInfo = await UserDAL.getUser(uid, "get discord avatar");
if (!userInfo.discordId) {
throw new MonkeyError(404, "User does not have a linked Discord account");
}
const { id, avatar } = await getDiscordUser(tokenType, accessToken);
await UserDAL.updateDiscordAvatar(uid, id, avatar);
return new MonkeyResponse("Discord avatar updated", avatar);
}
export async function addTag(
req: MonkeyTypes.Request
): Promise<MonkeyResponse> {

View file

@ -274,11 +274,8 @@ router.post(
authenticateRequest(),
validateRequest({
body: {
data: joi.object({
tokenType: joi.string().required(),
accessToken: joi.string().required(),
uid: joi.string(),
}),
tokenType: joi.string().required(),
accessToken: joi.string().required(),
},
}),
asyncHandler(UserController.linkDiscord)
@ -291,19 +288,6 @@ router.post(
asyncHandler(UserController.unlinkDiscord)
);
router.put(
"/discord/avatar",
RateLimit.userDiscordLink,
authenticateRequest(),
validateRequest({
body: {
tokenType: joi.string().required(),
accessToken: joi.string().required(),
},
}),
asyncHandler(UserController.updateDiscordAvatar)
);
router.get(
"/personalBests",
RateLimit.userGet,

View file

@ -391,12 +391,16 @@ export async function linkDiscord(
uid: string,
discordId: string,
discordAvatar?: string
): Promise<UpdateResult> {
await getUser(uid, "link discord");
): Promise<void> {
const updates = _.pickBy({ discordId, discordAvatar }, _.identity);
const result = await getUsersCollection().updateOne(
{ uid },
{ $set: updates }
);
return await getUsersCollection().updateOne({ uid }, { $set: updates });
if (result.matchedCount === 0) {
throw new MonkeyError(404, "User not found");
}
}
export async function unlinkDiscord(uid: string): Promise<UpdateResult> {
@ -408,21 +412,6 @@ export async function unlinkDiscord(uid: string): Promise<UpdateResult> {
);
}
export async function updateDiscordAvatar(
uid: string,
discordId: string,
discordAvatar?: string
): Promise<void> {
const updateResult = await getUsersCollection().updateOne(
{ uid, discordId },
{ $set: { discordAvatar } }
);
if (updateResult.matchedCount === 0) {
throw new MonkeyError(404, "User not found");
}
}
export async function incrementBananas(
uid: string,
wpm

View file

@ -193,8 +193,16 @@
color: var(--main-color);
}
#unlinkDiscordButton {
margin: 0.25rem auto 0 auto;
#discordButtonGroup {
display: grid;
grid-auto-flow: column;
justify-content: center;
gap: 0.5rem;
}
#unlinkDiscordButton,
#updateDiscordAvatarButton {
margin: 0.5rem auto 0 auto;
font-size: 0.75rem;
line-height: 0.7rem;
}

View file

@ -129,13 +129,9 @@ export default class Users {
return await this.httpClient.post(`${BASE_PATH}/customThemes`, { payload });
}
async linkDiscord(data: {
tokenType: string;
accessToken: string;
uid?: string;
}): Ape.EndpointData {
async linkDiscord(tokenType: string, accessToken: string): Ape.EndpointData {
return await this.httpClient.post(`${BASE_PATH}/discord/link`, {
payload: { data },
payload: { tokenType, accessToken },
});
}
@ -143,15 +139,6 @@ export default class Users {
return await this.httpClient.post(`${BASE_PATH}/discord/unlink`);
}
async updateDiscordAvatar(
tokenType: string,
accessToken: string
): Ape.EndpointData {
return await this.httpClient.put(`${BASE_PATH}/discord/avatar`, {
payload: { tokenType, accessToken },
});
}
async addQuoteToFavorites(
language: string,
quoteId: string

View file

@ -255,7 +255,7 @@ export async function loadUser(user: UserType): Promise<void> {
$(".pageAccount .group.createdDate").text(text);
if (VerificationController.data !== null) {
VerificationController.verify(user.uid);
VerificationController.verify();
}
if (TestLogic.notSignedInLastResult !== null) {

View file

@ -7,7 +7,6 @@ import * as Loader from "../elements/loader";
interface Data {
accessToken: string;
tokenType: string;
uid?: string;
}
export let data: Data | null = null;
@ -16,24 +15,30 @@ export function set(val: Data): void {
data = val;
}
export async function verify(uid: string): Promise<void> {
export async function verify(): Promise<void> {
if (data === null) return;
Notifications.add("Linking Discord account", 0, 3);
Loader.show();
data.uid = uid;
const response = await Ape.users.linkDiscord(data);
const { accessToken, tokenType } = data;
const response = await Ape.users.linkDiscord(tokenType, accessToken);
Loader.hide();
if (response.status !== 200) {
return Notifications.add("Failed to link Discord: " + response.message, -1);
}
Notifications.add("Accounts linked", 1);
Notifications.add(response.message, 1);
const snapshot = DB.getSnapshot();
snapshot.discordId = response.data.did;
const { discordId, discordAvatar } = response.data;
if (discordId) {
snapshot.discordId = discordId;
} else {
snapshot.discordAvatar = discordAvatar;
}
DB.setSnapshot(snapshot);
Settings.updateDiscordSection();

View file

@ -43,6 +43,12 @@ if (window.location.hostname === "localhost") {
"href",
"https://discord.com/api/oauth2/authorize?client_id=798272335035498557&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fverify&response_type=token&scope=identify"
);
$(
".pageSettings .discordIntegration .info #discordButtonGroup #updateDiscordAvatarButton"
).attr(
"href",
"https://discord.com/api/oauth2/authorize?client_id=798272335035498557&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fverify&response_type=token&scope=identify"
);
}
//stop space scrolling

View file

@ -57,9 +57,19 @@
<i class="fas fa-check"></i>
Your accounts are linked!
</div>
<div id="unlinkDiscordButton" class="text-button">
<i class="fas fa-unlink" aria-hidden="true"></i>
Unlink
<div id="discordButtonGroup">
<a
id="updateDiscordAvatarButton"
href="https://discord.com/api/oauth2/authorize?client_id=798272335035498557&redirect_uri=https%3A%2F%2Fmonkeytype.com%2Fverify&response_type=token&scope=identify"
class="text-button"
>
<i class="fas fa-sync-alt" aria-hidden="true"></i>
Update avatar
</a>
<div id="unlinkDiscordButton" class="text-button">
<i class="fas fa-unlink" aria-hidden="true"></i>
Unlink
</div>
</div>
</div>
</div>