diff --git a/app/soapbox/actions/export_data.js b/app/soapbox/actions/export_data.js new file mode 100644 index 000000000..479b44b0a --- /dev/null +++ b/app/soapbox/actions/export_data.js @@ -0,0 +1,102 @@ +import { defineMessages } from 'react-intl'; +import snackbar from 'soapbox/actions/snackbar'; +import api, { getLinks } from '../api'; + +export const EXPORT_FOLLOWS_REQUEST = 'EXPORT_FOLLOWS_REQUEST'; +export const EXPORT_FOLLOWS_SUCCESS = 'EXPORT_FOLLOWS_SUCCESS'; +export const EXPORT_FOLLOWS_FAIL = 'EXPORT_FOLLOWS_FAIL'; + +export const EXPORT_BLOCKS_REQUEST = 'EXPORT_BLOCKS_REQUEST'; +export const EXPORT_BLOCKS_SUCCESS = 'EXPORT_BLOCKS_SUCCESS'; +export const EXPORT_BLOCKS_FAIL = 'EXPORT_BLOCKS_FAIL'; + +export const EXPORT_MUTES_REQUEST = 'EXPORT_MUTES_REQUEST'; +export const EXPORT_MUTES_SUCCESS = 'EXPORT_MUTES_SUCCESS'; +export const EXPORT_MUTES_FAIL = 'EXPORT_MUTES_FAIL'; + +const messages = defineMessages({ + blocksSuccess: { id: 'export_data.success.blocks', defaultMessage: 'Blocks exported successfully' }, + followersSuccess: { id: 'export_data.success.followers', defaultMessage: 'Followers exported successfully' }, + mutesSuccess: { id: 'export_data.success.mutes', defaultMessage: 'Mutes exported successfully' }, +}); + +function fileExport(content, fileName) { + const fileToDownload = document.createElement('a'); + + fileToDownload.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(content)); + fileToDownload.setAttribute('download', fileName); + fileToDownload.style.display = 'none'; + document.body.appendChild(fileToDownload); + fileToDownload.click(); + document.body.removeChild(fileToDownload); +} + +function listAccounts(state) { + return async apiResponse => { + const followings = apiResponse.data; + let accounts = []; + let next = getLinks(apiResponse).refs.find(link => link.rel === 'next'); + while (next) { + apiResponse = await api(state).get(next.uri); + next = getLinks(apiResponse).refs.find(link => link.rel === 'next'); + Array.prototype.push.apply(followings, apiResponse.data); + } + + accounts = followings.map(account => account.fqn); + return [... new Set(accounts)]; + }; +} + +export function exportFollows(intl) { + return (dispatch, getState) => { + dispatch({ type: EXPORT_FOLLOWS_REQUEST }); + const me = getState().get('me'); + return api(getState) + .get(`/api/v1/accounts/${me}/following?limit=40`) + .then(listAccounts(getState)) + .then((followings) => { + followings = followings.map(fqn => fqn + ',true'); + followings.unshift('Account address,Show boosts'); + fileExport(followings.join('\n'), 'export_followings.csv'); + + dispatch(snackbar.success(intl.formatMessage(messages.followersSuccess))); + dispatch({ type: EXPORT_FOLLOWS_SUCCESS }); + }).catch(error => { + dispatch({ type: EXPORT_FOLLOWS_FAIL, error }); + }); + }; +} + +export function exportBlocks(intl) { + return (dispatch, getState) => { + dispatch({ type: EXPORT_BLOCKS_REQUEST }); + return api(getState) + .get('/api/v1/blocks?limit=40') + .then(listAccounts(getState)) + .then((blocks) => { + fileExport(blocks.join('\n'), 'export_block.csv'); + + dispatch(snackbar.success(intl.formatMessage(messages.blocksSuccess))); + dispatch({ type: EXPORT_BLOCKS_SUCCESS }); + }).catch(error => { + dispatch({ type: EXPORT_BLOCKS_FAIL, error }); + }); + }; +} + +export function exportMutes(intl) { + return (dispatch, getState) => { + dispatch({ type: EXPORT_MUTES_REQUEST }); + return api(getState) + .get('/api/v1/mutes?limit=40') + .then(listAccounts(getState)) + .then((mutes) => { + fileExport(mutes.join('\n'), 'export_mutes.csv'); + + dispatch(snackbar.success(intl.formatMessage(messages.mutesSuccess))); + dispatch({ type: EXPORT_MUTES_SUCCESS }); + }).catch(error => { + dispatch({ type: EXPORT_MUTES_FAIL, error }); + }); + }; +} diff --git a/app/soapbox/features/export_data/components/csv_exporter.js b/app/soapbox/features/export_data/components/csv_exporter.js new file mode 100644 index 000000000..41973fcfd --- /dev/null +++ b/app/soapbox/features/export_data/components/csv_exporter.js @@ -0,0 +1,50 @@ +import React from 'react'; +import { connect } from 'react-redux'; +import { injectIntl } from 'react-intl'; +import ImmutablePureComponent from 'react-immutable-pure-component'; +import PropTypes from 'prop-types'; +import { SimpleForm } from 'soapbox/features/forms'; + +export default @connect() +@injectIntl +class CSVExporter extends ImmutablePureComponent { + + static propTypes = { + action: PropTypes.func.isRequired, + messages: PropTypes.object.isRequired, + dispatch: PropTypes.func.isRequired, + intl: PropTypes.object.isRequired, + }; + + state = { + isLoading: false, + } + + handleClick = (event) => { + const { dispatch, action, intl } = this.props; + + this.setState({ isLoading: true }); + dispatch(action(intl)).then(() => { + this.setState({ isLoading: false }); + }).catch((error) => { + this.setState({ isLoading: false }); + }); + } + + render() { + const { intl, messages } = this.props; + + return ( + +

{intl.formatMessage(messages.input_label)}

+
+

{intl.formatMessage(messages.input_hint)}

+ +
+
+ ); + } + +} diff --git a/app/soapbox/features/export_data/index.js b/app/soapbox/features/export_data/index.js new file mode 100644 index 000000000..f5aea5867 --- /dev/null +++ b/app/soapbox/features/export_data/index.js @@ -0,0 +1,63 @@ +import React from 'react'; +import { connect } from 'react-redux'; +import { defineMessages, injectIntl } from 'react-intl'; +import ImmutablePureComponent from 'react-immutable-pure-component'; +import PropTypes from 'prop-types'; +import Column from '../ui/components/column'; +import { + exportFollows, + exportBlocks, + exportMutes, +} from 'soapbox/actions/export_data'; +import CSVExporter from './components/csv_exporter'; +import { getFeatures } from 'soapbox/utils/features'; + +const messages = defineMessages({ + heading: { id: 'column.export_data', defaultMessage: 'Export data' }, + submit: { id: 'export_data.actions.export', defaultMessage: 'Export' }, +}); + +const followMessages = defineMessages({ + input_label: { id: 'export_data.follows_label', defaultMessage: 'Follows' }, + input_hint: { id: 'export_data.hints.follows', defaultMessage: 'Get a CSV file containing a list of followed accounts' }, + submit: { id: 'export_data.actions.export_follows', defaultMessage: 'Export follows' }, +}); + +const blockMessages = defineMessages({ + input_label: { id: 'export_data.blocks_label', defaultMessage: 'Blocks' }, + input_hint: { id: 'export_data.hints.blocks', defaultMessage: 'Get a CSV file containing a list of blocked accounts' }, + submit: { id: 'export_data.actions.export_blocks', defaultMessage: 'Export blocks' }, +}); + +const muteMessages = defineMessages({ + input_label: { id: 'export_data.mutes_label', defaultMessage: 'Mutes' }, + input_hint: { id: 'export_data.hints.mutes', defaultMessage: 'Get a CSV file containing a list of muted accounts' }, + submit: { id: 'export_data.actions.export_mutes', defaultMessage: 'Export mutes' }, +}); + +const mapStateToProps = state => ({ + features: getFeatures(state.get('instance')), +}); + +export default @connect(mapStateToProps) +@injectIntl +class ExportData extends ImmutablePureComponent { + + static propTypes = { + intl: PropTypes.object.isRequired, + features: PropTypes.object, + }; + + render() { + const { intl } = this.props; + + return ( + + + + + + ); + } + +} diff --git a/app/soapbox/features/ui/components/link_footer.js b/app/soapbox/features/ui/components/link_footer.js index 1f427aa9c..6ea917cf2 100644 --- a/app/soapbox/features/ui/components/link_footer.js +++ b/app/soapbox/features/ui/components/link_footer.js @@ -43,6 +43,7 @@ const LinkFooter = ({ onOpenHotkeys, account, federating, showAliases, onClickLo
  • {isAdmin(account) &&
  • } {isAdmin(account) &&
  • } +
  • {(federating && showAliases) &&
  • }
  • diff --git a/app/soapbox/features/ui/index.js b/app/soapbox/features/ui/index.js index 010242b0d..fc00c7702 100644 --- a/app/soapbox/features/ui/index.js +++ b/app/soapbox/features/ui/index.js @@ -84,6 +84,7 @@ import { Preferences, EditProfile, SoapboxConfig, + ExportData, ImportData, Backups, PasswordReset, @@ -268,6 +269,7 @@ class SwitchingColumnsArea extends React.PureComponent { + diff --git a/app/soapbox/features/ui/util/async-components.js b/app/soapbox/features/ui/util/async-components.js index 1fe50212c..0a4f44405 100644 --- a/app/soapbox/features/ui/util/async-components.js +++ b/app/soapbox/features/ui/util/async-components.js @@ -186,6 +186,10 @@ export function SoapboxConfig() { return import(/* webpackChunkName: "features/soapbox_config" */'../../soapbox_config'); } +export function ExportData() { + return import(/* webpackChunkName: "features/export_data" */ '../../export_data'); +} + export function ImportData() { return import(/* webpackChunkName: "features/import_data" */'../../import_data'); }