2023-01-11 06:28:42 +01:00
|
|
|
import store from './store';
|
|
|
|
|
2023-02-05 17:17:19 +01:00
|
|
|
export function getAccount(id) {
|
2023-01-11 06:28:42 +01:00
|
|
|
const accounts = store.local.getJSON('accounts') || [];
|
2024-03-27 07:58:32 +01:00
|
|
|
if (!id) return accounts[0];
|
2023-02-09 18:01:06 +01:00
|
|
|
return accounts.find((a) => a.info.id === id) || accounts[0];
|
2023-02-05 17:17:19 +01:00
|
|
|
}
|
|
|
|
|
2023-09-01 09:40:00 +02:00
|
|
|
export function getAccountByAccessToken(accessToken) {
|
|
|
|
const accounts = store.local.getJSON('accounts') || [];
|
|
|
|
return accounts.find((a) => a.accessToken === accessToken);
|
|
|
|
}
|
|
|
|
|
2023-12-23 16:07:08 +01:00
|
|
|
export function getAccountByInstance(instance) {
|
|
|
|
const accounts = store.local.getJSON('accounts') || [];
|
|
|
|
return accounts.find((a) => a.instanceURL === instance);
|
|
|
|
}
|
|
|
|
|
2024-04-12 18:06:34 +02:00
|
|
|
const standaloneMQ = window.matchMedia('(display-mode: standalone)');
|
|
|
|
|
|
|
|
export function getCurrentAccountID() {
|
|
|
|
try {
|
|
|
|
const id = store.session.get('currentAccount');
|
|
|
|
if (id) return id;
|
|
|
|
} catch (e) {}
|
|
|
|
if (standaloneMQ.matches) {
|
|
|
|
try {
|
|
|
|
const id = store.local.get('currentAccount');
|
|
|
|
if (id) return id;
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setCurrentAccountID(id) {
|
|
|
|
try {
|
|
|
|
store.session.set('currentAccount', id);
|
|
|
|
} catch (e) {}
|
|
|
|
if (standaloneMQ.matches) {
|
|
|
|
try {
|
|
|
|
store.local.set('currentAccount', id);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 17:17:19 +01:00
|
|
|
export function getCurrentAccount() {
|
2023-10-11 13:07:36 +02: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();
|
|
|
|
}
|
2024-04-12 18:06:34 +02:00
|
|
|
const currentAccount = getCurrentAccountID();
|
2023-02-05 17:17:19 +01:00
|
|
|
const account = getAccount(currentAccount);
|
2023-01-11 06:28:42 +01:00
|
|
|
return account;
|
|
|
|
}
|
2023-01-13 08:30:09 +01:00
|
|
|
|
|
|
|
export function getCurrentAccountNS() {
|
|
|
|
const account = getCurrentAccount();
|
|
|
|
const {
|
|
|
|
instanceURL,
|
|
|
|
info: { id },
|
|
|
|
} = account;
|
|
|
|
return `${id}@${instanceURL}`;
|
|
|
|
}
|
2023-02-05 17:17:19 +01: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 09:40:00 +02:00
|
|
|
acc.vapidKey = account.vapidKey;
|
2023-02-05 17:17:19 +01:00
|
|
|
} else {
|
|
|
|
accounts.push(account);
|
|
|
|
}
|
|
|
|
store.local.setJSON('accounts', accounts);
|
2024-04-12 18:06:34 +02:00
|
|
|
setCurrentAccountID(account.info.id);
|
2023-02-05 17:17:19 +01:00
|
|
|
}
|
2023-02-12 18:21:18 +01:00
|
|
|
|
2023-06-28 11:38:01 +02: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 18:21:18 +01: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);
|
2024-04-18 17:10:26 +02:00
|
|
|
// alert(`Failed to load instance configuration. Please try again.\n\n${e}`);
|
2023-02-12 18:21:18 +01:00
|
|
|
// Temporary fix for corrupted data
|
2024-04-18 17:10:26 +02:00
|
|
|
// store.local.del('instances');
|
|
|
|
// location.reload();
|
2023-02-12 18:21:18 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
2023-10-24 08:30:50 +02: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);
|
|
|
|
}
|
2024-04-11 11:18:17 +02:00
|
|
|
|
|
|
|
export function isMediaFirstInstance() {
|
|
|
|
const instance = getCurrentInstance();
|
|
|
|
return /pixelfed/i.test(instance?.version);
|
|
|
|
}
|