2024-09-28 13:10:31 -07:00
|
|
|
import { importAccounts, importGroups, importPolls, importStatuses } from 'pl-fe/actions/importer';
|
|
|
|
import { importEntities as importEntityStoreEntities } from 'pl-fe/entity-store/actions';
|
|
|
|
import { Entities } from 'pl-fe/entity-store/entities';
|
|
|
|
import { queryClient } from 'pl-fe/queries/client';
|
2024-09-29 03:53:06 -07:00
|
|
|
|
|
|
|
let dispatch: AppDispatch;
|
|
|
|
|
|
|
|
import('pl-fe/store').then(value => dispatch = value.store.dispatch).catch(() => {});
|
2024-09-28 13:10:31 -07:00
|
|
|
|
2024-09-28 15:03:28 -07:00
|
|
|
import { MinifiedNotification, minifyNotification } from './minifiers/minifyNotification';
|
2024-10-02 03:17:49 -07:00
|
|
|
import { MinifiedStatus, minifyStatus } from './minifiers/minifyStatus';
|
2024-09-28 15:03:28 -07:00
|
|
|
import { DeduplicatedNotification } from './normalizers/deduplicateNotifications';
|
2024-10-02 03:17:49 -07:00
|
|
|
import { normalizeStatus } from './normalizers/normalizeStatus';
|
2024-09-28 13:10:31 -07:00
|
|
|
|
2024-09-27 08:02:02 -07:00
|
|
|
import type {
|
|
|
|
Account as BaseAccount,
|
|
|
|
Group as BaseGroup,
|
|
|
|
Poll as BasePoll,
|
2024-09-28 13:10:31 -07:00
|
|
|
Relationship as BaseRelationship,
|
2024-09-29 04:26:09 -07:00
|
|
|
Status as BaseStatus,
|
2024-09-27 08:02:02 -07:00
|
|
|
} from 'pl-api';
|
2024-09-29 03:53:06 -07:00
|
|
|
import type { AppDispatch } from 'pl-fe/store';
|
2024-09-27 08:02:02 -07:00
|
|
|
|
2024-09-28 13:10:31 -07:00
|
|
|
const importNotification = (notification: DeduplicatedNotification) => {
|
|
|
|
queryClient.setQueryData<MinifiedNotification>(
|
|
|
|
['notifications', 'entities', notification.id],
|
|
|
|
existingNotification => existingNotification?.duplicate ? existingNotification : minifyNotification(notification),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-10-02 03:17:49 -07:00
|
|
|
const importStatus = (status: BaseStatus) => {
|
|
|
|
queryClient.setQueryData<MinifiedStatus>(
|
|
|
|
['statuses', 'entities', status.id],
|
|
|
|
_ => minifyStatus(normalizeStatus(status)),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-09-28 13:51:54 -07:00
|
|
|
const isEmpty = (object: Record<string, any>) => {
|
|
|
|
for (const i in object) return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2024-09-27 08:02:02 -07:00
|
|
|
const importEntities = (entities: {
|
|
|
|
accounts?: Array<BaseAccount>;
|
2024-09-28 15:03:28 -07:00
|
|
|
groups?: Array<BaseGroup>;
|
2024-09-28 13:10:31 -07:00
|
|
|
notifications?: Array<DeduplicatedNotification>;
|
2024-09-28 13:51:54 -07:00
|
|
|
polls?: Array<BasePoll>;
|
2024-09-27 08:02:02 -07:00
|
|
|
statuses?: Array<BaseStatus>;
|
2024-09-28 13:10:31 -07:00
|
|
|
relationships?: Array<BaseRelationship>;
|
2024-09-27 08:02:02 -07:00
|
|
|
}) => {
|
|
|
|
const accounts: Record<string, BaseAccount> = {};
|
|
|
|
const groups: Record<string, BaseGroup> = {};
|
2024-09-28 13:10:31 -07:00
|
|
|
const notifications: Record<string, DeduplicatedNotification> = {};
|
2024-09-27 08:02:02 -07:00
|
|
|
const polls: Record<string, BasePoll> = {};
|
2024-09-28 13:10:31 -07:00
|
|
|
const relationships: Record<string, BaseRelationship> = {};
|
2024-09-27 08:02:02 -07:00
|
|
|
const statuses: Record<string, BaseStatus> = {};
|
|
|
|
|
|
|
|
const processAccount = (account: BaseAccount) => {
|
|
|
|
accounts[account.id] = account;
|
|
|
|
if (account.moved) processAccount(account.moved);
|
2024-09-28 13:10:31 -07:00
|
|
|
if (account.relationship) relationships[account.relationship.id] = account.relationship;
|
2024-09-27 08:02:02 -07:00
|
|
|
};
|
|
|
|
|
2024-09-28 13:10:31 -07:00
|
|
|
const processNotification = (notification: DeduplicatedNotification) => {
|
2024-09-27 08:02:02 -07:00
|
|
|
notifications[notification.id] = notification;
|
|
|
|
|
|
|
|
processAccount(notification.account);
|
|
|
|
if (notification.type === 'move') processAccount(notification.target);
|
|
|
|
|
|
|
|
if (['mention', 'status', 'reblog', 'favourite', 'poll', 'update', 'emoji_reaction', 'event_reminder', 'participation_accepted', 'participation_request'].includes(notification.type))
|
|
|
|
// @ts-ignore
|
|
|
|
processStatus(notification.status);
|
|
|
|
};
|
|
|
|
|
|
|
|
const processStatus = (status: BaseStatus) => {
|
2024-09-29 04:26:09 -07:00
|
|
|
if (status.account) {
|
|
|
|
statuses[status.id] = status;
|
|
|
|
processAccount(status.account);
|
|
|
|
}
|
2024-09-27 08:02:02 -07:00
|
|
|
|
|
|
|
if (status.quote) processStatus(status.quote);
|
|
|
|
if (status.reblog) processStatus(status.reblog);
|
2024-09-28 13:10:31 -07:00
|
|
|
if (status.poll) polls[status.poll.id] = status.poll;
|
|
|
|
if (status.group) groups[status.group.id] = status.group;
|
2024-09-27 08:02:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
entities.accounts?.forEach(processAccount);
|
2024-09-28 15:03:28 -07:00
|
|
|
entities.groups?.forEach(group => groups[group.id] = group);
|
2024-09-27 08:02:02 -07:00
|
|
|
entities.notifications?.forEach(processNotification);
|
2024-09-28 13:51:54 -07:00
|
|
|
entities.polls?.forEach(poll => polls[poll.id] = poll);
|
|
|
|
entities.relationships?.forEach(relationship => relationships[relationship.id] = relationship);
|
2024-09-27 08:02:02 -07:00
|
|
|
entities.statuses?.forEach(processStatus);
|
2024-09-28 13:10:31 -07:00
|
|
|
|
2024-09-28 13:51:54 -07:00
|
|
|
if (!isEmpty(accounts)) dispatch(importAccounts(Object.values(accounts)));
|
|
|
|
if (!isEmpty(groups)) dispatch(importGroups(Object.values(groups)));
|
|
|
|
if (!isEmpty(notifications)) Object.values(notifications).forEach(importNotification);
|
|
|
|
if (!isEmpty(polls)) dispatch(importPolls(Object.values(polls)));
|
|
|
|
if (!isEmpty(relationships)) dispatch(importEntityStoreEntities(Object.values(relationships), Entities.RELATIONSHIPS));
|
|
|
|
if (!isEmpty(statuses)) dispatch(importStatuses(Object.values(statuses)));
|
2024-10-02 03:17:49 -07:00
|
|
|
if (!isEmpty(statuses)) Object.values(statuses).forEach(importStatus);
|
2024-09-27 08:02:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
export { importEntities };
|