App: fix the clients change is not notified

This commit is contained in:
thislight 2024-10-11 16:39:42 +08:00
parent fd47639bef
commit bd3ad078a2
4 changed files with 122 additions and 79 deletions

View file

@ -59,32 +59,32 @@ async function oauth2TokenViaAuthCode(app: RegisteredApp, authCode: string) {
export async function acceptAccountViaAuthCode(site: string, authCode: string) {
const $store = $accounts;
const app = $registeredApps.get()[site];
if (!app) {
throw TypeError("application not found");
}
const token = await oauth2TokenViaAuthCode(app, authCode);
if (!app) {
throw TypeError("application not found");
}
const token = await oauth2TokenViaAuthCode(app, authCode);
const acct = {
site: app.site,
accessToken: token.access_token,
tokenType: token.token_type,
scope: token.scope,
createdAt: token.created_at * 1000,
};
const acct = {
site: app.site,
accessToken: token.access_token,
tokenType: token.token_type,
scope: token.scope,
createdAt: token.created_at * 1000,
};
const all = [...$store.get(), acct];
$store.set(all);
const all = [...$store.get(), acct];
$store.set(all);
return acct;
return acct;
}
export async function updateAcctInf(idx: number) {
const o = $accounts.get();
const client = createMastoClientFor(o[idx]);
const inf = await client.v1.accounts.verifyCredentials();
o[idx].inf = inf;
$accounts.set(o);
return inf;
const client = createMastoClientFor(o[idx]);
const inf = await client.v1.accounts.verifyCredentials();
o[idx] = Object.assign({}, o[idx], { inf });
$accounts.set(Array.from(o));
return inf;
}
export function signOut(predicate: (acct: Account) => boolean) {
@ -131,57 +131,57 @@ async function getAppAccessToken(app: RegisteredApp) {
export async function getOrRegisterApp(site: string, redirectUrl: string) {
const $store = $registeredApps;
const all = $store.get();
const savedApp = all[site];
if (savedApp && savedApp.redirectUrl === redirectUrl) {
const appAccessToken = await getAppAccessToken(savedApp);
if (appAccessToken) {
const client = createRestAPIClient({
const savedApp = all[site];
if (savedApp && savedApp.redirectUrl === redirectUrl) {
const appAccessToken = await getAppAccessToken(savedApp);
if (appAccessToken) {
const client = createRestAPIClient({
url: site,
accessToken: appAccessToken,
});
try {
const verify = await client.v1.apps.verifyCredentials();
Object.assign(savedApp, {
vapidKey: verify.vapidKey,
});
const oauthClient = createOAuthAPIClient({
url: site,
accessToken: appAccessToken,
});
try {
const verify = await client.v1.apps.verifyCredentials();
Object.assign(savedApp, {
vapidKey: verify.vapidKey,
await oauthClient.revoke({
clientId: savedApp.clientId,
clientSecret: savedApp.clientSecret,
token: appAccessToken,
});
const oauthClient = createOAuthAPIClient({
url: site,
accessToken: appAccessToken,
});
try {
await oauthClient.revoke({
clientId: savedApp.clientId,
clientSecret: savedApp.clientSecret,
token: appAccessToken,
});
} catch {}
return savedApp;
} finally {
$store.set(all);
}
} catch {}
return savedApp;
} finally {
$store.set(all);
}
}
}
const client = createRestAPIClient({
url: site,
});
const app = await client.v1.apps.create({
clientName: "TuTu",
website: "https://github.com/thislight/tutu",
redirectUris: redirectUrl,
scopes: "read write push",
});
if (!app.clientId || !app.clientSecret) {
return null;
}
all[site] = {
site,
clientId: app.clientId,
clientSecret: app.clientSecret,
vapidKey: app.vapidKey ?? undefined,
redirectUrl: redirectUrl,
scope: "read write push",
};
$store.set(all);
return all[site];
const client = createRestAPIClient({
url: site,
});
const app = await client.v1.apps.create({
clientName: "TuTu",
website: "https://github.com/thislight/tutu",
redirectUris: redirectUrl,
scopes: "read write push",
});
if (!app.clientId || !app.clientSecret) {
return null;
}
all[site] = {
site,
clientId: app.clientId,
clientSecret: app.clientSecret,
vapidKey: app.vapidKey ?? undefined,
redirectUrl: redirectUrl,
scope: "read write push",
};
$store.set(all);
return all[site];
}