import { queryClient } from 'pl-hooks/contexts/query-client'; import { type DeduplicatedNotification, getNotificationStatus, type NormalizedNotification, normalizeNotification } from './normalizers/notification'; import { type NormalizedStatus, normalizeStatus } from './normalizers/status'; import type { Account as BaseAccount, Group as BaseGroup, Poll as BasePoll, Relationship as BaseRelationship, Status as BaseStatus, } from 'pl-api'; const importAccount = (account: BaseAccount) => queryClient.setQueryData( ['accounts', 'entities', account.id], account, ); const importGroup = (group: BaseGroup) => queryClient.setQueryData( ['groups', 'entities', group.id], group, ); const importNotification = (notification: DeduplicatedNotification) => queryClient.setQueryData( ['notifications', 'entities', notification.id], existingNotification => existingNotification?.duplicate ? existingNotification : normalizeNotification(notification), ); const importPoll = (poll: BasePoll) => queryClient.setQueryData( ['polls', 'entities', poll.id], poll, ); const importRelationship = (relationship: BaseRelationship) => queryClient.setQueryData( ['relationships', 'entities', relationship.id], relationship, ); const importStatus = (status: BaseStatus) => queryClient.setQueryData( ['statuses', 'entities', status.id], normalizeStatus(status), ); const isEmpty = (object: Record) => !Object.values(object).some(value => value); type OptionalArray = Array; const importEntities = (entities: { accounts?: OptionalArray; groups?: OptionalArray; notifications?: OptionalArray; polls?: OptionalArray; statuses?: OptionalArray; relationships?: OptionalArray; }, options = { withParents: true, }) => { const accounts: Record = {}; const groups: Record = {}; const notifications: Record = {}; const polls: Record = {}; const relationships: Record = {}; const statuses: Record = {}; const processAccount = (account: BaseAccount, withSelf = true) => { if (withSelf) accounts[account.id] = account; queryClient.setQueryData(['accounts', 'byAcct', account.acct.toLocaleLowerCase()], account.id); if (account.moved) processAccount(account.moved); if (account.relationship) relationships[account.relationship.id] = account.relationship; }; const processNotification = (notification: DeduplicatedNotification, withSelf = true) => { if (withSelf) notifications[notification.id] = notification; processAccount(notification.account); if (notification.type === 'move') processAccount(notification.target); const status = getNotificationStatus(notification); if (status) { processStatus(status); } }; const processStatus = (status: BaseStatus, withSelf = true) => { if (withSelf) statuses[status.id] = status; if (status.account) { processAccount(status.account); } if (status.quote) processStatus(status.quote); if (status.reblog) processStatus(status.reblog); if (status.poll) polls[status.poll.id] = status.poll; if (status.group) groups[status.group.id] = status.group; }; if (options.withParents) { entities.groups?.forEach(group => group && (groups[group.id] = group)); entities.polls?.forEach(poll => poll && (polls[poll.id] = poll)); entities.relationships?.forEach(relationship => relationship && (relationships[relationship.id] = relationship)); } entities.accounts?.forEach((account) => account && processAccount(account, options.withParents)); entities.notifications?.forEach((notification) => notification && processNotification(notification, options.withParents)); entities.statuses?.forEach((status) => status && processStatus(status, options.withParents)); if (!isEmpty(accounts)) Object.values(accounts).forEach(importAccount); if (!isEmpty(groups)) Object.values(groups).forEach(importGroup); if (!isEmpty(notifications)) Object.values(notifications).forEach(importNotification); if (!isEmpty(polls)) Object.values(polls).forEach(importPoll); if (!isEmpty(relationships)) Object.values(relationships).forEach(importRelationship); if (!isEmpty(statuses)) Object.values(statuses).forEach(importStatus); }; export { importEntities };