fix fqn could not show

This commit is contained in:
sevichecc 2023-04-17 14:46:03 +08:00
parent 0f1f8a33d1
commit cd9a3e5d71
Signed by untrusted user who does not match committer: SevicheCC
GPG key ID: C577000000000000
4 changed files with 50 additions and 10 deletions

View file

@ -3,7 +3,7 @@ import { Action, ActionPanel, List, Toast, showToast, Cache } from "@raycast/api
import { Status, AkkomaError } from "./utils/types"; import { Status, AkkomaError } from "./utils/types";
import apiServer from "./utils/api"; import apiServer from "./utils/api";
import { authorize } from "./utils/oauth"; import { getAccessToken } from "./utils/oauth";
import { statusParser } from "./utils/util"; import { statusParser } from "./utils/util";
const cache = new Cache(); const cache = new Cache();
@ -16,7 +16,7 @@ export default function BookmarkCommand() {
useEffect(() => { useEffect(() => {
const getBookmark = async () => { const getBookmark = async () => {
try { try {
await authorize(); await getAccessToken();
showToast(Toast.Style.Animated, "Loading bookmarks..☆ミ(o*・ω・)ノ."); showToast(Toast.Style.Animated, "Loading bookmarks..☆ミ(o*・ω・)ノ.");
const newBookmarks = await apiServer.fetchBookmarks(); const newBookmarks = await apiServer.fetchBookmarks();
setBookmarks(newBookmarks); setBookmarks(newBookmarks);

View file

@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
import { Action, ActionPanel, List, Toast, showToast, Cache } from "@raycast/api"; import { Action, ActionPanel, List, Toast, showToast, Cache } from "@raycast/api";
import { Status, AkkomaError } from "./utils/types"; import { Status, AkkomaError } from "./utils/types";
import { authorize } from "./utils/oauth"; import { getAccessToken } from "./utils/oauth";
import apiServer from "./utils/api"; import apiServer from "./utils/api";
import { statusParser } from "./utils/util"; import { statusParser } from "./utils/util";
@ -16,7 +16,7 @@ export default function ViewStatusCommand() {
useEffect(() => { useEffect(() => {
const getBookmark = async () => { const getBookmark = async () => {
try { try {
await authorize(); await getAccessToken();
showToast(Toast.Style.Animated, "Loading Status...ε=ε=┌( >_<)┘"); showToast(Toast.Style.Animated, "Loading Status...ε=ε=┌( >_<)┘");
const status = await apiServer.fetchUserStatus(); const status = await apiServer.fetchUserStatus();
setStatus(status); setStatus(status);

View file

@ -14,7 +14,7 @@ import {
} from "@raycast/api"; } from "@raycast/api";
import apiServer from "./utils/api"; import apiServer from "./utils/api";
import { AkkomaError, StatusResponse, Preference, StatusRequest } from "./utils/types"; import { AkkomaError, StatusResponse, Preference, StatusRequest } from "./utils/types";
import { authorize } from "./utils/oauth"; import { getAccessToken } from "./utils/oauth";
import { dateTimeFormatter } from "./utils/util"; import { dateTimeFormatter } from "./utils/util";
import VisibilityDropdown from "./components/VisibilityDropdown"; import VisibilityDropdown from "./components/VisibilityDropdown";
@ -51,11 +51,11 @@ export default function SimpleCommand(props: CommandProps) {
useEffect(() => { useEffect(() => {
const init = async () => { const init = async () => {
try { try {
await authorize(); await getAccessToken();
const newFqn = (await LocalStorage.getItem<string>("account-fqn")) ?? ""; const fqn = await LocalStorage.getItem<string>("account-fqn") || "";
setState((prevState) => ({ setState((prevState) => ({
...prevState, ...prevState,
fqn: newFqn, fqn: fqn,
})); }));
} catch (error) { } catch (error) {
console.error("Error during authorization or fetching account-fqn:", error); console.error("Error during authorization or fetching account-fqn:", error);

View file

@ -44,7 +44,7 @@ const refreshToken = async (
return tokenResponse; return tokenResponse;
}; };
export const authorize = async (): Promise<void> => { export const authorize = async (): Promise<string> => {
const { instance } = getPreferenceValues<Preference>(); const { instance } = getPreferenceValues<Preference>();
const tokenSet = await client.getTokens(); const tokenSet = await client.getTokens();
@ -53,7 +53,8 @@ export const authorize = async (): Promise<void> => {
const { client_id, client_secret } = await apiServer.createApp(); const { client_id, client_secret } = await apiServer.createApp();
await client.setTokens(await refreshToken(client_id, client_secret, tokenSet.refreshToken)); await client.setTokens(await refreshToken(client_id, client_secret, tokenSet.refreshToken));
} }
return; const { fqn } = await apiServer.fetchAccountInfo();
return fqn;
} }
const { client_id, client_secret } = await apiServer.createApp(); const { client_id, client_secret } = await apiServer.createApp();
@ -68,4 +69,43 @@ export const authorize = async (): Promise<void> => {
const { fqn } = await apiServer.fetchAccountInfo(); const { fqn } = await apiServer.fetchAccountInfo();
await LocalStorage.setItem("account-fqn", fqn); await LocalStorage.setItem("account-fqn", fqn);
return fqn;
}; };
async function getValidTokens(): Promise<OAuth.TokenSet> {
const tokenSet = await client.getTokens();
if (!tokenSet || !tokenSet.accessToken) {
const fqn = await authorize();
const updatedTokenSet = await client.getTokens();
if (updatedTokenSet && updatedTokenSet.accessToken) {
await LocalStorage.setItem("account-fqn", fqn);
return updatedTokenSet;
} else {
throw new Error("Failed to get valid access token");
}
}
if (tokenSet.refreshToken && tokenSet.isExpired()) {
const { client_id, client_secret } = await apiServer.createApp();
const refreshedTokens = await refreshToken(client_id, client_secret, tokenSet.refreshToken);
await client.setTokens(refreshedTokens);
const updatedTokenSet = await client.getTokens();
if (updatedTokenSet && updatedTokenSet.accessToken) {
return updatedTokenSet;
} else {
throw new Error("Failed to refresh access token");
}
}
return tokenSet;
}
export async function getAccessToken(): Promise<string> {
const validTokenSet = await getValidTokens();
if (validTokenSet && validTokenSet.accessToken) {
return validTokenSet.accessToken;
} else {
throw new Error("Failed to get valid access token");
}
}