1
0
Fork 0
mirror of https://github.com/cheeaun/phanpy.git synced 2025-05-01 21:00:51 +03:00

Yes, push notifications (beta).

Heck this feature is tough.
This commit is contained in:
Lim Chee Aun 2023-09-01 15:40:00 +08:00
parent 0b04e01d60
commit 0e745663f0
12 changed files with 854 additions and 15 deletions
src/utils

View file

@ -1,7 +1,12 @@
import { createClient } from 'masto';
import store from './store';
import { getAccount, getCurrentAccount, saveAccount } from './store-utils';
import {
getAccount,
getAccountByAccessToken,
getCurrentAccount,
saveAccount,
} from './store-utils';
// Default *fallback* instance
const DEFAULT_INSTANCE = 'mastodon.social';
@ -18,6 +23,7 @@ const apis = {};
// Just in case if I need this one day.
// E.g. accountApis['mastodon.social']['ACCESS_TOKEN']
const accountApis = {};
window.__ACCOUNT_APIS__ = accountApis;
// Current account masto instance
let currentAccountApi;
@ -92,7 +98,7 @@ export async function initInstance(client, instance) {
}
// Get the account information and store it
export async function initAccount(client, instance, accessToken) {
export async function initAccount(client, instance, accessToken, vapidKey) {
const masto = client;
const mastoAccount = await masto.v1.accounts.verifyCredentials();
@ -102,6 +108,7 @@ export async function initAccount(client, instance, accessToken) {
info: mastoAccount,
instanceURL: instance.toLowerCase(),
accessToken,
vapidKey,
});
}
@ -136,6 +143,35 @@ export function api({ instance, accessToken, accountID, account } = {}) {
};
}
if (accessToken) {
// If only accessToken is provided, get the masto instance for that accessToken
console.log('X 1', accountApis);
for (const instance in accountApis) {
if (accountApis[instance][accessToken]) {
console.log('X 2', accountApis, instance, accessToken);
return {
masto: accountApis[instance][accessToken],
authenticated: true,
instance,
};
} else {
console.log('X 3', accountApis, instance, accessToken);
const account = getAccountByAccessToken(accessToken);
if (account) {
const accessToken = account.accessToken;
const instance = account.instanceURL.toLowerCase().trim();
return {
masto: initClient({ instance, accessToken }),
authenticated: true,
instance,
};
} else {
throw new Error(`Access token ${accessToken} not found`);
}
}
}
}
// If account is provided, get the masto instance for that account
if (account || accountID) {
account = account || getAccount(accountID);