From 9455d14ad5a4257c065d502b8b855cda86f445a2 Mon Sep 17 00:00:00 2001 From: Zeeshan Bhati <53969232+zeeshanbhati@users.noreply.github.com> Date: Thu, 15 Feb 2024 17:21:24 +0530 Subject: [PATCH] feat: Add Google Profile Photo when connecting Google Calendar (#13627) Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> --- packages/app-store/googlecalendar/api/add.ts | 1 + .../app-store/googlecalendar/api/callback.ts | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/app-store/googlecalendar/api/add.ts b/packages/app-store/googlecalendar/api/add.ts index 6d2c65385a..a898fed283 100644 --- a/packages/app-store/googlecalendar/api/add.ts +++ b/packages/app-store/googlecalendar/api/add.ts @@ -11,6 +11,7 @@ import { encodeOAuthState } from "../../_utils/oauth/encodeOAuthState"; export const scopes = [ "https://www.googleapis.com/auth/calendar.readonly", "https://www.googleapis.com/auth/calendar.events", + "https://www.googleapis.com/auth/userinfo.profile", ]; let client_id = ""; diff --git a/packages/app-store/googlecalendar/api/callback.ts b/packages/app-store/googlecalendar/api/callback.ts index 87fbb2ee2b..1aec90fbe0 100644 --- a/packages/app-store/googlecalendar/api/callback.ts +++ b/packages/app-store/googlecalendar/api/callback.ts @@ -1,9 +1,11 @@ +import type { Auth } from "googleapis"; import { google } from "googleapis"; import type { NextApiRequest, NextApiResponse } from "next"; import { WEBAPP_URL_FOR_OAUTH, WEBAPP_URL } from "@calcom/lib/constants"; import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl"; import { HttpError } from "@calcom/lib/http-error"; +import logger from "@calcom/lib/logger"; import { defaultHandler, defaultResponder } from "@calcom/lib/server"; import prisma from "@calcom/prisma"; import { Prisma } from "@calcom/prisma/client"; @@ -91,6 +93,10 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { throw new HttpError({ message: "Internal Error", statusCode: 500 }); } + // Update the user's profile photo with google profile photo if it's null + // Since we don't want to block the user from using the app if this fails, we don't await this + updateProfilePhoto(oAuth2Client, req.session.user.id); + const credential = await prisma.credential.create({ data: { type: "google_calendar", @@ -156,6 +162,23 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) { ); } +async function updateProfilePhoto(oAuth2Client: Auth.OAuth2Client, userId: number) { + try { + const oauth2 = google.oauth2({ version: "v2", auth: oAuth2Client }); + const userDetails = await oauth2.userinfo.get(); + if (userDetails.data?.picture) { + await prisma.user.update({ + where: { id: userId, avatarUrl: null, avatar: null }, + data: { + avatarUrl: userDetails.data.picture, + }, + }); + } + } catch (error) { + logger.error("Error updating avatarUrl from google calendar connect", error); + } +} + export default defaultHandler({ GET: Promise.resolve({ default: defaultResponder(getHandler) }), });