gab-social/app/javascript/gabsocial/actions/importer/index.js

116 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-11-15 18:48:32 +00:00
import isObject from 'lodash.isobject'
import {
normalizeAccount,
normalizeStatus,
normalizePoll,
} from './normalizer'
2020-04-22 06:00:11 +01:00
import { fetchContext } from '../statuses'
2020-11-15 18:48:32 +00:00
import { importGroups } from '../groups'
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
export const ACCOUNT_IMPORT = 'ACCOUNT_IMPORT'
export const ACCOUNTS_IMPORT = 'ACCOUNTS_IMPORT'
export const STATUS_IMPORT = 'STATUS_IMPORT'
export const STATUSES_IMPORT = 'STATUSES_IMPORT'
export const POLLS_IMPORT = 'POLLS_IMPORT'
export const ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP = 'ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP'
2019-07-02 08:10:25 +01:00
2020-11-25 21:22:37 +00:00
/**
*
*/
const pushUnique = (array, object) => {
2019-07-02 08:10:25 +01:00
if (array.every(element => element.id !== object.id)) {
array.push(object);
}
}
2020-11-15 18:48:32 +00:00
export const importAccount = (account) => ({
type: ACCOUNT_IMPORT,
account,
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
export const importAccounts = (accounts) => ({
type: ACCOUNTS_IMPORT,
accounts,
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
export const importStatus = (status) => ({
type: STATUS_IMPORT,
status,
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
export const importStatuses = (statuses) => ({
type: STATUSES_IMPORT,
statuses,
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
export const importPolls = (polls) => ({
type: POLLS_IMPORT,
polls,
})
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
export const importFetchedAccount = (account) => {
2019-07-02 08:10:25 +01:00
return importFetchedAccounts([account]);
}
2020-11-15 18:48:32 +00:00
export const importFetchedAccounts = (accounts) => {
2019-07-02 08:10:25 +01:00
const normalAccounts = [];
2020-11-15 18:48:32 +00:00
const processAccount = (account) => {
2019-07-02 08:10:25 +01:00
pushUnique(normalAccounts, normalizeAccount(account));
if (account.moved) {
processAccount(account.moved);
}
}
accounts.forEach(processAccount);
return importAccounts(normalAccounts);
}
2020-11-15 18:48:32 +00:00
export const importFetchedStatus = (status) => {
2019-07-02 08:10:25 +01:00
return importFetchedStatuses([status]);
}
2020-11-15 18:48:32 +00:00
export const importFetchedStatuses = (statuses) => (dispatch, getState) => {
const accounts = []
const normalStatuses = []
const polls = []
const groups = []
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
const processStatus = (status) => {
pushUnique(normalStatuses, normalizeStatus(status, getState().getIn(['statuses', status.id])))
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
if (isObject(status.account)) pushUnique(accounts, status.account)
if (isObject(status.group)) pushUnique(groups, status.group)
if (status.reblog && status.reblog.id) {
processStatus(status.reblog)
}
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
if (status.quote && status.quote.id) {
processStatus(status.quote)
}
2019-07-31 20:45:53 +01:00
2020-11-15 18:48:32 +00:00
if (status.poll && status.poll.id) {
pushUnique(polls, normalizePoll(status.poll))
2019-07-02 08:10:25 +01:00
}
2020-11-15 18:48:32 +00:00
}
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
statuses.forEach(processStatus)
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
dispatch(importPolls(polls))
dispatch(importFetchedAccounts(accounts))
dispatch(importStatuses(normalStatuses))
dispatch(importGroups(groups))
2019-07-02 08:10:25 +01:00
}
2020-11-15 18:48:32 +00:00
export const importFetchedPoll = (poll) => (dispatch) => {
dispatch(importPolls([normalizePoll(poll)]))
2019-07-02 08:10:25 +01:00
}
2020-11-15 18:48:32 +00:00
export const importErrorWhileFetchingAccountByUsername = (username) => ({
type: ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP,
username
})