2023-12-20 08:55:56 +03:00
|
|
|
import { api } from './api';
|
|
|
|
import store from './store';
|
|
|
|
|
|
|
|
export async function fetchRelationships(accounts, relationshipsMap = {}) {
|
|
|
|
if (!accounts?.length) return;
|
|
|
|
const { masto } = api();
|
|
|
|
|
|
|
|
const currentAccount = store.session.get('currentAccount');
|
|
|
|
const uniqueAccountIds = accounts.reduce((acc, a) => {
|
|
|
|
// 1. Ignore duplicate accounts
|
|
|
|
// 2. Ignore accounts that are already inside relationshipsMap
|
|
|
|
// 3. Ignore currently logged in account
|
|
|
|
if (
|
|
|
|
!acc.includes(a.id) &&
|
|
|
|
!relationshipsMap[a.id] &&
|
|
|
|
a.id !== currentAccount
|
|
|
|
) {
|
|
|
|
acc.push(a.id);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, []);
|
2023-12-20 11:04:37 +03:00
|
|
|
if (!uniqueAccountIds.length) return null;
|
2023-12-20 08:55:56 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
const relationships = await masto.v1.accounts.relationships.fetch({
|
|
|
|
id: uniqueAccountIds,
|
|
|
|
});
|
|
|
|
const newRelationshipsMap = relationships.reduce((acc, r) => {
|
|
|
|
acc[r.id] = r;
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
return newRelationshipsMap;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
// It's okay to fail
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|