2023-01-11 08:28:42 +03:00
|
|
|
import store from './store';
|
|
|
|
|
2023-02-05 19:17:19 +03:00
|
|
|
export function getAccount(id) {
|
2023-01-11 08:28:42 +03:00
|
|
|
const accounts = store.local.getJSON('accounts') || [];
|
2023-02-09 20:01:06 +03:00
|
|
|
return accounts.find((a) => a.info.id === id) || accounts[0];
|
2023-02-05 19:17:19 +03:00
|
|
|
}
|
|
|
|
|
2023-09-01 10:40:00 +03:00
|
|
|
export function getAccountByAccessToken(accessToken) {
|
|
|
|
const accounts = store.local.getJSON('accounts') || [];
|
|
|
|
return accounts.find((a) => a.accessToken === accessToken);
|
|
|
|
}
|
|
|
|
|
2023-12-23 18:07:08 +03:00
|
|
|
export function getAccountByInstance(instance) {
|
|
|
|
const accounts = store.local.getJSON('accounts') || [];
|
|
|
|
return accounts.find((a) => a.instanceURL === instance);
|
|
|
|
}
|
|
|
|
|
2023-02-05 19:17:19 +03:00
|
|
|
export function getCurrentAccount() {
|
2023-10-11 14:07:36 +03:00
|
|
|
if (!window.__IGNORE_GET_ACCOUNT_ERROR__) {
|
|
|
|
// Track down getCurrentAccount() calls before account-based states are initialized
|
|
|
|
console.error('getCurrentAccount() called before states are initialized');
|
|
|
|
if (import.meta.env.DEV) console.trace();
|
|
|
|
}
|
2023-01-11 08:28:42 +03:00
|
|
|
const currentAccount = store.session.get('currentAccount');
|
2023-02-05 19:17:19 +03:00
|
|
|
const account = getAccount(currentAccount);
|
2023-01-11 08:28:42 +03:00
|
|
|
return account;
|
|
|
|
}
|
2023-01-13 10:30:09 +03:00
|
|
|
|
|
|
|
export function getCurrentAccountNS() {
|
|
|
|
const account = getCurrentAccount();
|
|
|
|
const {
|
|
|
|
instanceURL,
|
|
|
|
info: { id },
|
|
|
|
} = account;
|
|
|
|
return `${id}@${instanceURL}`;
|
|
|
|
}
|
2023-02-05 19:17:19 +03:00
|
|
|
|
|
|
|
export function saveAccount(account) {
|
|
|
|
const accounts = store.local.getJSON('accounts') || [];
|
|
|
|
const acc = accounts.find((a) => a.info.id === account.info.id);
|
|
|
|
if (acc) {
|
|
|
|
acc.info = account.info;
|
|
|
|
acc.instanceURL = account.instanceURL;
|
|
|
|
acc.accessToken = account.accessToken;
|
2023-09-01 10:40:00 +03:00
|
|
|
acc.vapidKey = account.vapidKey;
|
2023-02-05 19:17:19 +03:00
|
|
|
} else {
|
|
|
|
accounts.push(account);
|
|
|
|
}
|
|
|
|
store.local.setJSON('accounts', accounts);
|
|
|
|
store.session.set('currentAccount', account.info.id);
|
|
|
|
}
|
2023-02-12 20:21:18 +03:00
|
|
|
|
2023-06-28 12:38:01 +03:00
|
|
|
export function updateAccount(accountInfo) {
|
|
|
|
// Only update if displayName or avatar or avatar_static is different
|
|
|
|
const accounts = store.local.getJSON('accounts') || [];
|
|
|
|
const acc = accounts.find((a) => a.info.id === accountInfo.id);
|
|
|
|
if (acc) {
|
|
|
|
if (
|
|
|
|
acc.info.displayName !== accountInfo.displayName ||
|
|
|
|
acc.info.avatar !== accountInfo.avatar ||
|
|
|
|
acc.info.avatar_static !== accountInfo.avatar_static
|
|
|
|
) {
|
|
|
|
acc.info = {
|
|
|
|
...acc.info,
|
|
|
|
...accountInfo,
|
|
|
|
};
|
|
|
|
store.local.setJSON('accounts', accounts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 20:21:18 +03:00
|
|
|
let currentInstance = null;
|
|
|
|
export function getCurrentInstance() {
|
|
|
|
if (currentInstance) return currentInstance;
|
|
|
|
try {
|
|
|
|
const account = getCurrentAccount();
|
|
|
|
const instances = store.local.getJSON('instances');
|
|
|
|
const instance = account.instanceURL.toLowerCase();
|
|
|
|
return (currentInstance = instances[instance]);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2023-05-11 19:05:22 +03:00
|
|
|
alert(`Failed to load instance configuration. Please try again.\n\n${e}`);
|
2023-02-12 20:21:18 +03:00
|
|
|
// Temporary fix for corrupted data
|
|
|
|
store.local.del('instances');
|
|
|
|
location.reload();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
2023-10-24 09:30:50 +03:00
|
|
|
|
|
|
|
// Massage these instance configurations to match the Mastodon API
|
|
|
|
// - Pleroma
|
|
|
|
function getInstanceConfiguration(instance) {
|
|
|
|
const {
|
|
|
|
configuration,
|
|
|
|
maxMediaAttachments,
|
|
|
|
maxTootChars,
|
|
|
|
pleroma,
|
|
|
|
pollLimits,
|
|
|
|
} = instance;
|
|
|
|
|
|
|
|
const statuses = configuration?.statuses || {};
|
|
|
|
if (maxMediaAttachments) {
|
|
|
|
statuses.maxMediaAttachments ??= maxMediaAttachments;
|
|
|
|
}
|
|
|
|
if (maxTootChars) {
|
|
|
|
statuses.maxCharacters ??= maxTootChars;
|
|
|
|
}
|
|
|
|
|
|
|
|
const polls = configuration?.polls || {};
|
|
|
|
if (pollLimits) {
|
|
|
|
polls.maxCharactersPerOption ??= pollLimits.maxOptionChars;
|
|
|
|
polls.maxExpiration ??= pollLimits.maxExpiration;
|
|
|
|
polls.maxOptions ??= pollLimits.maxOptions;
|
|
|
|
polls.minExpiration ??= pollLimits.minExpiration;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...configuration,
|
|
|
|
statuses,
|
|
|
|
polls,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCurrentInstanceConfiguration() {
|
|
|
|
const instance = getCurrentInstance();
|
|
|
|
return getInstanceConfiguration(instance);
|
|
|
|
}
|