Skip to content

Commit

Permalink
fix: get profile bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kawamataryo committed Jan 18, 2025
1 parent c27a25f commit 5503d59
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/background/messages/getMyProfile.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { PlasmoMessaging } from "@plasmohq/messaging";
import destr from "destr";
import { BskyClient } from "~lib/bskyClient";

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
const { session } = req.body;
try {
const client = await BskyClient.createAgentFromSession(session);
const result = await client.getMyProfile();
const client = await BskyClient.createAgentFromSession(destr(session));
res.send({
result: JSON.stringify(await client.getMyProfile()),
});
Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
RATE_LIMIT_ERROR_MESSAGE,
STORAGE_KEYS,
} from "~lib/constants";
import { debugLog } from "~lib/utils";
import { useErrorMessage } from "./useErrorMessage";

export const useAuth = () => {
Expand Down Expand Up @@ -122,12 +123,14 @@ export const useAuth = () => {
},
});
if (error) {
debugLog(error);
return false;
}
const parsedResult = destr<{
displayName: string;
avatar: string;
}>(result);
debugLog(parsedResult);
setDisplayName(parsedResult.displayName);
setAvatar(parsedResult.avatar);
return true;
Expand Down
26 changes: 16 additions & 10 deletions src/lib/bskyClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AtUri, AtpAgent, type AtpSessionData } from "@atproto/api";
import destr from "destr";
import { BSKY_DOMAIN } from "./constants";
import { debugLog } from "./utils";

// try and cut down the amount of session resumes by caching the clients
const clientCache = new Map<string, BskyClient>();
Expand Down Expand Up @@ -33,18 +34,23 @@ export class BskyClient {
public static async createAgentFromSession(
session: AtpSessionData,
): Promise<BskyClient> {
if (clientCache.has(session.did)) {
return clientCache.get(session.did);
debugLog("session", session);
let client = clientCache.get(session.did);

if (!client) {
debugLog("cache miss", session.did);
client = new BskyClient();
await client.agent.resumeSession(destr(session));
clientCache.set(session.did, client);
} else {
debugLog("cache hit", session.did);
}
const client = new BskyClient();
const res = await client.agent.resumeSession(destr(session));
client.me = {
did: res.data.did,
handle: res.data.handle,
email: res.data.email,
did: session.did,
handle: session.handle,
email: session.email,
};

clientCache.set(session.did, client);
debugLog("client", client);
return client;
}

Expand Down Expand Up @@ -172,7 +178,7 @@ export class BskyClient {

public getMyProfile = async () => {
const profile = await this.agent.getProfile({
actor: this.me.did,
actor: this.agent.session.did,
});
return {
pdsUrl: this.agent.pdsUrl,
Expand Down

0 comments on commit 5503d59

Please sign in to comment.