You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
4.7 KiB

  1. import api from '../api';
  2. import { deleteFromTimelines } from './timelines';
  3. import { fetchStatusCard } from './cards';
  4. export const STATUS_FETCH_REQUEST = 'STATUS_FETCH_REQUEST';
  5. export const STATUS_FETCH_SUCCESS = 'STATUS_FETCH_SUCCESS';
  6. export const STATUS_FETCH_FAIL = 'STATUS_FETCH_FAIL';
  7. export const STATUS_DELETE_REQUEST = 'STATUS_DELETE_REQUEST';
  8. export const STATUS_DELETE_SUCCESS = 'STATUS_DELETE_SUCCESS';
  9. export const STATUS_DELETE_FAIL = 'STATUS_DELETE_FAIL';
  10. export const CONTEXT_FETCH_REQUEST = 'CONTEXT_FETCH_REQUEST';
  11. export const CONTEXT_FETCH_SUCCESS = 'CONTEXT_FETCH_SUCCESS';
  12. export const CONTEXT_FETCH_FAIL = 'CONTEXT_FETCH_FAIL';
  13. export const STATUS_MUTE_REQUEST = 'STATUS_MUTE_REQUEST';
  14. export const STATUS_MUTE_SUCCESS = 'STATUS_MUTE_SUCCESS';
  15. export const STATUS_MUTE_FAIL = 'STATUS_MUTE_FAIL';
  16. export const STATUS_UNMUTE_REQUEST = 'STATUS_UNMUTE_REQUEST';
  17. export const STATUS_UNMUTE_SUCCESS = 'STATUS_UNMUTE_SUCCESS';
  18. export const STATUS_UNMUTE_FAIL = 'STATUS_UNMUTE_FAIL';
  19. export function fetchStatusRequest(id, skipLoading) {
  20. return {
  21. type: STATUS_FETCH_REQUEST,
  22. id,
  23. skipLoading,
  24. };
  25. };
  26. export function fetchStatus(id) {
  27. return (dispatch, getState) => {
  28. const skipLoading = getState().getIn(['statuses', id], null) !== null;
  29. dispatch(fetchContext(id));
  30. dispatch(fetchStatusCard(id));
  31. if (skipLoading) {
  32. return;
  33. }
  34. dispatch(fetchStatusRequest(id, skipLoading));
  35. api(getState).get(`/api/v1/statuses/${id}`).then(response => {
  36. dispatch(fetchStatusSuccess(response.data, skipLoading));
  37. }).catch(error => {
  38. dispatch(fetchStatusFail(id, error, skipLoading));
  39. });
  40. };
  41. };
  42. export function fetchStatusSuccess(status, skipLoading) {
  43. return {
  44. type: STATUS_FETCH_SUCCESS,
  45. status,
  46. skipLoading,
  47. };
  48. };
  49. export function fetchStatusFail(id, error, skipLoading) {
  50. return {
  51. type: STATUS_FETCH_FAIL,
  52. id,
  53. error,
  54. skipLoading,
  55. skipAlert: true,
  56. };
  57. };
  58. export function deleteStatus(id) {
  59. return (dispatch, getState) => {
  60. dispatch(deleteStatusRequest(id));
  61. api(getState).delete(`/api/v1/statuses/${id}`).then(() => {
  62. dispatch(deleteStatusSuccess(id));
  63. dispatch(deleteFromTimelines(id));
  64. }).catch(error => {
  65. dispatch(deleteStatusFail(id, error));
  66. });
  67. };
  68. };
  69. export function deleteStatusRequest(id) {
  70. return {
  71. type: STATUS_DELETE_REQUEST,
  72. id: id,
  73. };
  74. };
  75. export function deleteStatusSuccess(id) {
  76. return {
  77. type: STATUS_DELETE_SUCCESS,
  78. id: id,
  79. };
  80. };
  81. export function deleteStatusFail(id, error) {
  82. return {
  83. type: STATUS_DELETE_FAIL,
  84. id: id,
  85. error: error,
  86. };
  87. };
  88. export function fetchContext(id) {
  89. return (dispatch, getState) => {
  90. dispatch(fetchContextRequest(id));
  91. api(getState).get(`/api/v1/statuses/${id}/context`).then(response => {
  92. dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants));
  93. }).catch(error => {
  94. if (error.response.status === 404) {
  95. dispatch(deleteFromTimelines(id));
  96. }
  97. dispatch(fetchContextFail(id, error));
  98. });
  99. };
  100. };
  101. export function fetchContextRequest(id) {
  102. return {
  103. type: CONTEXT_FETCH_REQUEST,
  104. id,
  105. };
  106. };
  107. export function fetchContextSuccess(id, ancestors, descendants) {
  108. return {
  109. type: CONTEXT_FETCH_SUCCESS,
  110. id,
  111. ancestors,
  112. descendants,
  113. statuses: ancestors.concat(descendants),
  114. };
  115. };
  116. export function fetchContextFail(id, error) {
  117. return {
  118. type: CONTEXT_FETCH_FAIL,
  119. id,
  120. error,
  121. skipAlert: true,
  122. };
  123. };
  124. export function muteStatus(id) {
  125. return (dispatch, getState) => {
  126. dispatch(muteStatusRequest(id));
  127. api(getState).post(`/api/v1/statuses/${id}/mute`).then(() => {
  128. dispatch(muteStatusSuccess(id));
  129. }).catch(error => {
  130. dispatch(muteStatusFail(id, error));
  131. });
  132. };
  133. };
  134. export function muteStatusRequest(id) {
  135. return {
  136. type: STATUS_MUTE_REQUEST,
  137. id,
  138. };
  139. };
  140. export function muteStatusSuccess(id) {
  141. return {
  142. type: STATUS_MUTE_SUCCESS,
  143. id,
  144. };
  145. };
  146. export function muteStatusFail(id, error) {
  147. return {
  148. type: STATUS_MUTE_FAIL,
  149. id,
  150. error,
  151. };
  152. };
  153. export function unmuteStatus(id) {
  154. return (dispatch, getState) => {
  155. dispatch(unmuteStatusRequest(id));
  156. api(getState).post(`/api/v1/statuses/${id}/unmute`).then(() => {
  157. dispatch(unmuteStatusSuccess(id));
  158. }).catch(error => {
  159. dispatch(unmuteStatusFail(id, error));
  160. });
  161. };
  162. };
  163. export function unmuteStatusRequest(id) {
  164. return {
  165. type: STATUS_UNMUTE_REQUEST,
  166. id,
  167. };
  168. };
  169. export function unmuteStatusSuccess(id) {
  170. return {
  171. type: STATUS_UNMUTE_SUCCESS,
  172. id,
  173. };
  174. };
  175. export function unmuteStatusFail(id, error) {
  176. return {
  177. type: STATUS_UNMUTE_FAIL,
  178. id,
  179. error,
  180. };
  181. };