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.

234 lines
5.0 KiB

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