Skip to content

Commit

Permalink
refactor: GithubInfo saves avatar to filesystem
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Jul 4, 2024
1 parent 8dac930 commit 10ee440
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
4 changes: 2 additions & 2 deletions plugin/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def _on_result_check_status(self, payload: CopilotPayloadSignInConfirm | Copilot
user = ""

CopilotPlugin.set_account_status(user=user)
GithubInfo.fetch_avatar(user)
GithubInfo.update_avatar(user)

if payload["status"] == "OK":
CopilotPlugin.set_account_status(signed_in=True, authorized=True)
Expand Down Expand Up @@ -733,4 +733,4 @@ def _on_result_sign_out(self, payload: CopilotPayloadSignOut) -> None:
CopilotPlugin.set_account_status(signed_in=False, authorized=False, user=None)
message_dialog("Sign out OK. Bye!")

GithubInfo.fetch_avatar("")
GithubInfo.clear_avatar()
50 changes: 42 additions & 8 deletions plugin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import sublime
from wcmatch import glob

from .constants import COPILOT_WINDOW_SETTINGS_PREFIX
from .constants import COPILOT_WINDOW_SETTINGS_PREFIX, PACKAGE_NAME
from .log import log_error
from .utils import (
all_views,
all_windows,
Expand Down Expand Up @@ -54,18 +55,51 @@ def _run(self) -> None:


class GithubInfo:
# @todo if we want to cache avatar to filesystem, we probably only delete it when sign out
AVATAR_PATH = Path(sublime.cache_path()) / f"{PACKAGE_NAME}/avatar.png"

avatar_bytes = b""
avatar_data_url = ""

@classmethod
def fetch_avatar(cls, username: str, *, size: int = 64) -> None:
if username:
cls.avatar_bytes = simple_urlopen(f"https://github.com/{username}.png?size={size}")
def load_avatar_cache(cls) -> bool:
"""Loads the avatar from the cache directory. Returns successful or not."""
try:
cls.avatar_bytes = cls.AVATAR_PATH.read_bytes()
cls.avatar_data_url = bytes_to_data_url(cls.avatar_bytes, mime_type="image/png")
else:
cls.avatar_bytes = b""
cls.avatar_data_url = ""
except FileNotFoundError:
cls.clear_avatar()
return False
return True

@classmethod
def fetch_avatar(cls, username: str, *, size: int = 64) -> None:
"""If there is no cached avatar, fetches the avatar from GitHub and saves it to the cache."""
if not username:
log_error("No username provided for fetching avatar.")
return

if not cls.avatar_bytes and cls.load_avatar_cache():
return

cls.update_avatar(username, size=size)

@classmethod
def update_avatar(cls, username: str, *, size: int = 64) -> None:
"""Updates the avatar from GitHub and saves it to the cache directory."""
if not username:
cls.clear_avatar()
return

cls.AVATAR_PATH.parent.mkdir(parents=True, exist_ok=True)
cls.AVATAR_PATH.write_bytes(simple_urlopen(f"https://github.com/{username}.png?size={size}"))
cls.load_avatar_cache()

@classmethod
def clear_avatar(cls) -> None:
cls.avatar_bytes = b""
cls.avatar_data_url = ""

cls.AVATAR_PATH.unlink(missing_ok=True)


class CopilotIgnore:
Expand Down

0 comments on commit 10ee440

Please sign in to comment.