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.4 KiB

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