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.

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