Skip to content

Render avatar for Accounts #200390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Render avatar for Accounts #200390

wants to merge 14 commits into from

Conversation

isidorn
Copy link
Contributor

@isidorn isidorn commented Dec 8, 2023

Fixes #136889

To do list:

  • Do the plumbing so the avatar is surfaced in the API and it passes from the Authentication session
  • Adopt in GitHub Authentication provider
  • Adopt in Microsoft Authentication provider
  • Figure out how to render the image in the Global Activity Bar. Will discuss with @sandy081 next week. The challenge is that right now we only render icons in the :before element, and I want to apply custom css with image.
  • Add menu actions and storage service writing for user preference on what avatar to render
  • Figure out what happens with already signed in users (ideally we can render the avatar also for them)

@TylerLeonhardt if time allows feedback is welcome. I have verified that the avatar URL nicely flows from extension to renderer. Though if you are busy I am perfectly happy if you review once I figure out the AccountsActivityActionViewItem part.
Is it ok to add an optional parameter directly in vscode.d.ts, or should I create a proposed file?

Here's how it looks (I edited CSS directly in out of source). Here's the css I plan to use:

    width: 60%;
    height: 60%;
    background-image: url(https://avatars.githubusercontent.com/u/1926584?v=4);
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    border-radius: 50%;

Screenshot 2023-12-08 at 19 03 05

And when activity bar is at top.

Screenshot 2023-12-08 at 18 50 27

@isidorn isidorn self-assigned this Dec 8, 2023
@isidorn isidorn added this to the December / January 2024 milestone Dec 8, 2023
@@ -233,7 +233,7 @@ export class GitHubServer implements IGitHubServer {
try {
const json = await result.json();
this._logger.info('Got account info!');
return { id: json.id, accountName: json.login };
return { id: json.id, accountName: json.login, avatar: json.avatar_url };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this come back when we ask for user:email scope or do we need it to have read:user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the GitHub docs: "If the authenticated user is authenticated through OAuth without the user scope, then the response lists only public profile information."
Since a users profile picture is publicly available information. I expect that we can get it just with user:email.

To be sure. I just changed from source that we do not request read:user, and indeed I got back the avatar nicely.

@TylerLeonhardt
Copy link
Member

Adopt in Microsoft Authentication provider

If you want to call this out of scope for this PR, I am ok with that... that said, it's probably not a crazy amount of work on top of this item.

@TylerLeonhardt
Copy link
Member

I would love it if this image is cached locally somehow so that if I get on a plane, the icon doesn't change.

@isidorn
Copy link
Contributor Author

isidorn commented Dec 12, 2023

Good points. I will try to tackle MS as part of this PR.
As for caching the image - I agree we should do it. Probably independent from this PR. Thank you

@TylerLeonhardt
Copy link
Member

If you don't cache the image, at the very least verify that while offline it shows something reasonable

@isidorn
Copy link
Contributor Author

isidorn commented Dec 15, 2023

Tried to wire up the image for the MS account. However Copilot says that this can only be obtained through MS Graph (and browsing the internet confirms this). I remember you told me that we can not access the MS graph due to us creating our account with a type that does not have MS Graph support. Let me know if I am missing something here?

For context, here's Copilot:
"In the Microsoft Authentication API, the user's avatar image URL is not directly included in the claims. However, you can use the Microsoft Graph API to get the user's profile photo after you've obtained an access token.

Please note that you need to have the User.Read permission in your Azure AD app registration to access the user's profile photo, and the user needs to have granted this permission during the sign-in process. Here's a basic example of how you can do this in TypeScript:"

import fetch from 'node-fetch';

async function getProfilePhoto(accessToken: string) {
  const response = await fetch('https://graph.microsoft.com/v1.0/me/photo/$value', {
    headers: {
      'Authorization': `Bearer ${accessToken}`
    }
  });

  if (!response.ok) {
    throw new Error('Failed to get profile photo');
  }

  const blob = await response.blob();
  const url = URL.createObjectURL(blob);

  return url;
}

@TylerLeonhardt
Copy link
Member

Yeah this gets a little tricky... it's doable, but maybe skip Microsoft auth for now.

@isidorn isidorn marked this pull request as ready for review December 19, 2023 15:22
@isidorn
Copy link
Contributor Author

isidorn commented Dec 19, 2023

I think I got this into a decent state -> ready for review. But I plan to merge this in January. Here's a demo. Tomorrow I will show in Zurich and share in Slack for feedback:

Screen.Recording.2023-12-19.at.16.19.31.mov

To-do list:

  • Fix rendering of the avatar in the Activity Bar. Currently I am setting width, height, margin-left and margin-bottom to decrease the size of the icon. However this breaks the badge rendering. Ideally I would just change the background size, and not affect the element. But I did not figure out how to change background-size while also making sure it renders oval (border-radius: 50%).
  • Microsoft authentication
  • Verify vscode.dev and codespaces (might not be happy how I am downloading the image and passing fspath)

@bpasero
Copy link
Member

bpasero commented Dec 19, 2023

💯 , should it be "Show Avatar" in the menu?

@isidorn
Copy link
Contributor Author

isidorn commented Dec 19, 2023

@bpasero thanks, I like your suggested wording better - updating.

@TylerLeonhardt
Copy link
Member

Some follow ups for Jan:

  • make sure vscode.dev is happy with where you put the icon
  • follow up after this PR goes in, does Codespaces' embedder already have the icon and can it be fetched already while loading the Codespace?

@rasborgdk
Copy link

rasborgdk commented Dec 20, 2023 via email

@burkeholland
Copy link

I love this so much. Is the "Show Avatar" the default?


const avatarId = this.storageService.get(AccountsActivityActionViewItem.AVATAR_STORAGE_KEY, StorageScope.PROFILE);
// account.id is sometimes a number and sometimes a string so we need to convert to string
if (account.id.toString() === avatarId || (avatarId === undefined && providerId === 'github')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not hardcode github in code. Read it from product.json

protected override updateLabel(): void {
super.updateLabel();
this.label.classList.toggle('show-avatar', !!this.accountForAvatar);
this.label.style.setProperty('background-image', this.accountForAvatar?.iconPath ? `url("${URI.from(this.accountForAvatar.iconPath).fsPath}")` : '');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this fsPath ? Is the file stored on disk? How does this work in web version then?

@@ -523,6 +555,16 @@ export class AccountsActivityActionViewItem extends AbstractGlobalActivityAction
}
}

private updateActionItem(): void {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not this method has to be called when

  • created
  • profile changed
  • avatar state changed

@isidorn isidorn modified the milestones: December / January 2024, February 2024 Jan 22, 2024
@isidorn isidorn modified the milestones: February 2024, March 2024 Feb 19, 2024
@isidorn isidorn modified the milestones: March 2024, April 2024 Mar 27, 2024
@isidorn isidorn modified the milestones: April 2024, May 2024 Apr 23, 2024
@isidorn isidorn modified the milestones: May 2024, June 2024 May 8, 2024
@isidorn isidorn modified the milestones: June 2024, Backlog Jun 21, 2024
@vivodi
Copy link

vivodi commented Dec 29, 2024

Is this still relevant?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Once user GitHub authenticated render the avatar in the activity bar
7 participants