this repo has no description
1import store from './store';
2
3export function getAccount(id) {
4 const accounts = store.local.getJSON('accounts') || [];
5 return accounts.find((a) => a.info.id === id) || accounts[0];
6}
7
8export function getAccountByAccessToken(accessToken) {
9 const accounts = store.local.getJSON('accounts') || [];
10 return accounts.find((a) => a.accessToken === accessToken);
11}
12
13export function getAccountByInstance(instance) {
14 const accounts = store.local.getJSON('accounts') || [];
15 return accounts.find((a) => a.instanceURL === instance);
16}
17
18export function getCurrentAccount() {
19 if (!window.__IGNORE_GET_ACCOUNT_ERROR__) {
20 // Track down getCurrentAccount() calls before account-based states are initialized
21 console.error('getCurrentAccount() called before states are initialized');
22 if (import.meta.env.DEV) console.trace();
23 }
24 const currentAccount = store.session.get('currentAccount');
25 const account = getAccount(currentAccount);
26 return account;
27}
28
29export function getCurrentAccountNS() {
30 const account = getCurrentAccount();
31 const {
32 instanceURL,
33 info: { id },
34 } = account;
35 return `${id}@${instanceURL}`;
36}
37
38export function saveAccount(account) {
39 const accounts = store.local.getJSON('accounts') || [];
40 const acc = accounts.find((a) => a.info.id === account.info.id);
41 if (acc) {
42 acc.info = account.info;
43 acc.instanceURL = account.instanceURL;
44 acc.accessToken = account.accessToken;
45 acc.vapidKey = account.vapidKey;
46 } else {
47 accounts.push(account);
48 }
49 store.local.setJSON('accounts', accounts);
50 store.session.set('currentAccount', account.info.id);
51}
52
53export function updateAccount(accountInfo) {
54 // Only update if displayName or avatar or avatar_static is different
55 const accounts = store.local.getJSON('accounts') || [];
56 const acc = accounts.find((a) => a.info.id === accountInfo.id);
57 if (acc) {
58 if (
59 acc.info.displayName !== accountInfo.displayName ||
60 acc.info.avatar !== accountInfo.avatar ||
61 acc.info.avatar_static !== accountInfo.avatar_static
62 ) {
63 acc.info = {
64 ...acc.info,
65 ...accountInfo,
66 };
67 store.local.setJSON('accounts', accounts);
68 }
69 }
70}
71
72let currentInstance = null;
73export function getCurrentInstance() {
74 if (currentInstance) return currentInstance;
75 try {
76 const account = getCurrentAccount();
77 const instances = store.local.getJSON('instances');
78 const instance = account.instanceURL.toLowerCase();
79 return (currentInstance = instances[instance]);
80 } catch (e) {
81 console.error(e);
82 alert(`Failed to load instance configuration. Please try again.\n\n${e}`);
83 // Temporary fix for corrupted data
84 store.local.del('instances');
85 location.reload();
86 return {};
87 }
88}
89
90// Massage these instance configurations to match the Mastodon API
91// - Pleroma
92function getInstanceConfiguration(instance) {
93 const {
94 configuration,
95 maxMediaAttachments,
96 maxTootChars,
97 pleroma,
98 pollLimits,
99 } = instance;
100
101 const statuses = configuration?.statuses || {};
102 if (maxMediaAttachments) {
103 statuses.maxMediaAttachments ??= maxMediaAttachments;
104 }
105 if (maxTootChars) {
106 statuses.maxCharacters ??= maxTootChars;
107 }
108
109 const polls = configuration?.polls || {};
110 if (pollLimits) {
111 polls.maxCharactersPerOption ??= pollLimits.maxOptionChars;
112 polls.maxExpiration ??= pollLimits.maxExpiration;
113 polls.maxOptions ??= pollLimits.maxOptions;
114 polls.minExpiration ??= pollLimits.minExpiration;
115 }
116
117 return {
118 ...configuration,
119 statuses,
120 polls,
121 };
122}
123
124export function getCurrentInstanceConfiguration() {
125 const instance = getCurrentInstance();
126 return getInstanceConfiguration(instance);
127}