domain_blocks.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import api, { getLinks } from '../api';
  2. import { blockDomainSuccess, unblockDomainSuccess } from "./domain_blocks_typed";
  3. export * from "./domain_blocks_typed";
  4. export const DOMAIN_BLOCK_REQUEST = 'DOMAIN_BLOCK_REQUEST';
  5. export const DOMAIN_BLOCK_FAIL = 'DOMAIN_BLOCK_FAIL';
  6. export const DOMAIN_UNBLOCK_REQUEST = 'DOMAIN_UNBLOCK_REQUEST';
  7. export const DOMAIN_UNBLOCK_FAIL = 'DOMAIN_UNBLOCK_FAIL';
  8. export const DOMAIN_BLOCKS_FETCH_REQUEST = 'DOMAIN_BLOCKS_FETCH_REQUEST';
  9. export const DOMAIN_BLOCKS_FETCH_SUCCESS = 'DOMAIN_BLOCKS_FETCH_SUCCESS';
  10. export const DOMAIN_BLOCKS_FETCH_FAIL = 'DOMAIN_BLOCKS_FETCH_FAIL';
  11. export const DOMAIN_BLOCKS_EXPAND_REQUEST = 'DOMAIN_BLOCKS_EXPAND_REQUEST';
  12. export const DOMAIN_BLOCKS_EXPAND_SUCCESS = 'DOMAIN_BLOCKS_EXPAND_SUCCESS';
  13. export const DOMAIN_BLOCKS_EXPAND_FAIL = 'DOMAIN_BLOCKS_EXPAND_FAIL';
  14. export function blockDomain(domain) {
  15. return (dispatch, getState) => {
  16. dispatch(blockDomainRequest(domain));
  17. api(getState).post('/api/v1/domain_blocks', { domain }).then(() => {
  18. const at_domain = '@' + domain;
  19. const accounts = getState().get('accounts').filter(item => item.get('acct').endsWith(at_domain)).valueSeq().map(item => item.get('id'));
  20. dispatch(blockDomainSuccess({ domain, accounts }));
  21. }).catch(err => {
  22. dispatch(blockDomainFail(domain, err));
  23. });
  24. };
  25. }
  26. export function blockDomainRequest(domain) {
  27. return {
  28. type: DOMAIN_BLOCK_REQUEST,
  29. domain,
  30. };
  31. }
  32. export function blockDomainFail(domain, error) {
  33. return {
  34. type: DOMAIN_BLOCK_FAIL,
  35. domain,
  36. error,
  37. };
  38. }
  39. export function unblockDomain(domain) {
  40. return (dispatch, getState) => {
  41. dispatch(unblockDomainRequest(domain));
  42. api(getState).delete('/api/v1/domain_blocks', { params: { domain } }).then(() => {
  43. const at_domain = '@' + domain;
  44. const accounts = getState().get('accounts').filter(item => item.get('acct').endsWith(at_domain)).valueSeq().map(item => item.get('id'));
  45. dispatch(unblockDomainSuccess({ domain, accounts }));
  46. }).catch(err => {
  47. dispatch(unblockDomainFail(domain, err));
  48. });
  49. };
  50. }
  51. export function unblockDomainRequest(domain) {
  52. return {
  53. type: DOMAIN_UNBLOCK_REQUEST,
  54. domain,
  55. };
  56. }
  57. export function unblockDomainFail(domain, error) {
  58. return {
  59. type: DOMAIN_UNBLOCK_FAIL,
  60. domain,
  61. error,
  62. };
  63. }
  64. export function fetchDomainBlocks() {
  65. return (dispatch, getState) => {
  66. dispatch(fetchDomainBlocksRequest());
  67. api(getState).get('/api/v1/domain_blocks').then(response => {
  68. const next = getLinks(response).refs.find(link => link.rel === 'next');
  69. dispatch(fetchDomainBlocksSuccess(response.data, next ? next.uri : null));
  70. }).catch(err => {
  71. dispatch(fetchDomainBlocksFail(err));
  72. });
  73. };
  74. }
  75. export function fetchDomainBlocksRequest() {
  76. return {
  77. type: DOMAIN_BLOCKS_FETCH_REQUEST,
  78. };
  79. }
  80. export function fetchDomainBlocksSuccess(domains, next) {
  81. return {
  82. type: DOMAIN_BLOCKS_FETCH_SUCCESS,
  83. domains,
  84. next,
  85. };
  86. }
  87. export function fetchDomainBlocksFail(error) {
  88. return {
  89. type: DOMAIN_BLOCKS_FETCH_FAIL,
  90. error,
  91. };
  92. }
  93. export function expandDomainBlocks() {
  94. return (dispatch, getState) => {
  95. const url = getState().getIn(['domain_lists', 'blocks', 'next']);
  96. if (!url) {
  97. return;
  98. }
  99. dispatch(expandDomainBlocksRequest());
  100. api(getState).get(url).then(response => {
  101. const next = getLinks(response).refs.find(link => link.rel === 'next');
  102. dispatch(expandDomainBlocksSuccess(response.data, next ? next.uri : null));
  103. }).catch(err => {
  104. dispatch(expandDomainBlocksFail(err));
  105. });
  106. };
  107. }
  108. export function expandDomainBlocksRequest() {
  109. return {
  110. type: DOMAIN_BLOCKS_EXPAND_REQUEST,
  111. };
  112. }
  113. export function expandDomainBlocksSuccess(domains, next) {
  114. return {
  115. type: DOMAIN_BLOCKS_EXPAND_SUCCESS,
  116. domains,
  117. next,
  118. };
  119. }
  120. export function expandDomainBlocksFail(error) {
  121. return {
  122. type: DOMAIN_BLOCKS_EXPAND_FAIL,
  123. error,
  124. };
  125. }