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.

232 lines
5.0 KiB

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