statuses.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import api from '../api';
  2. import { deleteFromTimelines } from './timelines';
  3. import { importFetchedStatus, importFetchedStatuses, importFetchedAccount } from './importer';
  4. import { ensureComposeIsVisible, setComposeToStatus } from './compose';
  5. export const STATUS_FETCH_REQUEST = 'STATUS_FETCH_REQUEST';
  6. export const STATUS_FETCH_SUCCESS = 'STATUS_FETCH_SUCCESS';
  7. export const STATUS_FETCH_FAIL = 'STATUS_FETCH_FAIL';
  8. export const STATUS_DELETE_REQUEST = 'STATUS_DELETE_REQUEST';
  9. export const STATUS_DELETE_SUCCESS = 'STATUS_DELETE_SUCCESS';
  10. export const STATUS_DELETE_FAIL = 'STATUS_DELETE_FAIL';
  11. export const CONTEXT_FETCH_REQUEST = 'CONTEXT_FETCH_REQUEST';
  12. export const CONTEXT_FETCH_SUCCESS = 'CONTEXT_FETCH_SUCCESS';
  13. export const CONTEXT_FETCH_FAIL = 'CONTEXT_FETCH_FAIL';
  14. export const STATUS_MUTE_REQUEST = 'STATUS_MUTE_REQUEST';
  15. export const STATUS_MUTE_SUCCESS = 'STATUS_MUTE_SUCCESS';
  16. export const STATUS_MUTE_FAIL = 'STATUS_MUTE_FAIL';
  17. export const STATUS_UNMUTE_REQUEST = 'STATUS_UNMUTE_REQUEST';
  18. export const STATUS_UNMUTE_SUCCESS = 'STATUS_UNMUTE_SUCCESS';
  19. export const STATUS_UNMUTE_FAIL = 'STATUS_UNMUTE_FAIL';
  20. export const STATUS_REVEAL = 'STATUS_REVEAL';
  21. export const STATUS_HIDE = 'STATUS_HIDE';
  22. export const STATUS_COLLAPSE = 'STATUS_COLLAPSE';
  23. export const REDRAFT = 'REDRAFT';
  24. export const STATUS_FETCH_SOURCE_REQUEST = 'STATUS_FETCH_SOURCE_REQUEST';
  25. export const STATUS_FETCH_SOURCE_SUCCESS = 'STATUS_FETCH_SOURCE_SUCCESS';
  26. export const STATUS_FETCH_SOURCE_FAIL = 'STATUS_FETCH_SOURCE_FAIL';
  27. export const STATUS_TRANSLATE_REQUEST = 'STATUS_TRANSLATE_REQUEST';
  28. export const STATUS_TRANSLATE_SUCCESS = 'STATUS_TRANSLATE_SUCCESS';
  29. export const STATUS_TRANSLATE_FAIL = 'STATUS_TRANSLATE_FAIL';
  30. export const STATUS_TRANSLATE_UNDO = 'STATUS_TRANSLATE_UNDO';
  31. export function fetchStatusRequest(id, skipLoading) {
  32. return {
  33. type: STATUS_FETCH_REQUEST,
  34. id,
  35. skipLoading,
  36. };
  37. };
  38. export function fetchStatus(id, forceFetch = false) {
  39. return (dispatch, getState) => {
  40. const skipLoading = !forceFetch && getState().getIn(['statuses', id], null) !== null;
  41. dispatch(fetchContext(id));
  42. if (skipLoading) {
  43. return;
  44. }
  45. dispatch(fetchStatusRequest(id, skipLoading));
  46. api(getState).get(`/api/v1/statuses/${id}`).then(response => {
  47. dispatch(importFetchedStatus(response.data));
  48. dispatch(fetchStatusSuccess(skipLoading));
  49. }).catch(error => {
  50. dispatch(fetchStatusFail(id, error, skipLoading));
  51. });
  52. };
  53. };
  54. export function fetchStatusSuccess(skipLoading) {
  55. return {
  56. type: STATUS_FETCH_SUCCESS,
  57. skipLoading,
  58. };
  59. };
  60. export function fetchStatusFail(id, error, skipLoading) {
  61. return {
  62. type: STATUS_FETCH_FAIL,
  63. id,
  64. error,
  65. skipLoading,
  66. skipAlert: true,
  67. };
  68. };
  69. export function redraft(status, raw_text) {
  70. return {
  71. type: REDRAFT,
  72. status,
  73. raw_text,
  74. };
  75. };
  76. export const editStatus = (id, routerHistory) => (dispatch, getState) => {
  77. let status = getState().getIn(['statuses', id]);
  78. if (status.get('poll')) {
  79. status = status.set('poll', getState().getIn(['polls', status.get('poll')]));
  80. }
  81. dispatch(fetchStatusSourceRequest());
  82. api(getState).get(`/api/v1/statuses/${id}/source`).then(response => {
  83. dispatch(fetchStatusSourceSuccess());
  84. ensureComposeIsVisible(getState, routerHistory);
  85. dispatch(setComposeToStatus(status, response.data.text, response.data.spoiler_text));
  86. }).catch(error => {
  87. dispatch(fetchStatusSourceFail(error));
  88. });
  89. };
  90. export const fetchStatusSourceRequest = () => ({
  91. type: STATUS_FETCH_SOURCE_REQUEST,
  92. });
  93. export const fetchStatusSourceSuccess = () => ({
  94. type: STATUS_FETCH_SOURCE_SUCCESS,
  95. });
  96. export const fetchStatusSourceFail = error => ({
  97. type: STATUS_FETCH_SOURCE_FAIL,
  98. error,
  99. });
  100. export function deleteStatus(id, routerHistory, withRedraft = false) {
  101. return (dispatch, getState) => {
  102. let status = getState().getIn(['statuses', id]);
  103. if (status.get('poll')) {
  104. status = status.set('poll', getState().getIn(['polls', status.get('poll')]));
  105. }
  106. dispatch(deleteStatusRequest(id));
  107. api(getState).delete(`/api/v1/statuses/${id}`).then(response => {
  108. dispatch(deleteStatusSuccess(id));
  109. dispatch(deleteFromTimelines(id));
  110. dispatch(importFetchedAccount(response.data.account));
  111. if (withRedraft) {
  112. dispatch(redraft(status, response.data.text));
  113. ensureComposeIsVisible(getState, routerHistory);
  114. }
  115. }).catch(error => {
  116. dispatch(deleteStatusFail(id, error));
  117. });
  118. };
  119. };
  120. export function deleteStatusRequest(id) {
  121. return {
  122. type: STATUS_DELETE_REQUEST,
  123. id: id,
  124. };
  125. };
  126. export function deleteStatusSuccess(id) {
  127. return {
  128. type: STATUS_DELETE_SUCCESS,
  129. id: id,
  130. };
  131. };
  132. export function deleteStatusFail(id, error) {
  133. return {
  134. type: STATUS_DELETE_FAIL,
  135. id: id,
  136. error: error,
  137. };
  138. };
  139. export const updateStatus = status => dispatch =>
  140. dispatch(importFetchedStatus(status));
  141. export function fetchContext(id) {
  142. return (dispatch, getState) => {
  143. dispatch(fetchContextRequest(id));
  144. api(getState).get(`/api/v1/statuses/${id}/context`).then(response => {
  145. dispatch(importFetchedStatuses(response.data.ancestors.concat(response.data.descendants)));
  146. dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants));
  147. }).catch(error => {
  148. if (error.response && error.response.status === 404) {
  149. dispatch(deleteFromTimelines(id));
  150. }
  151. dispatch(fetchContextFail(id, error));
  152. });
  153. };
  154. };
  155. export function fetchContextRequest(id) {
  156. return {
  157. type: CONTEXT_FETCH_REQUEST,
  158. id,
  159. };
  160. };
  161. export function fetchContextSuccess(id, ancestors, descendants) {
  162. return {
  163. type: CONTEXT_FETCH_SUCCESS,
  164. id,
  165. ancestors,
  166. descendants,
  167. statuses: ancestors.concat(descendants),
  168. };
  169. };
  170. export function fetchContextFail(id, error) {
  171. return {
  172. type: CONTEXT_FETCH_FAIL,
  173. id,
  174. error,
  175. skipAlert: true,
  176. };
  177. };
  178. export function muteStatus(id) {
  179. return (dispatch, getState) => {
  180. dispatch(muteStatusRequest(id));
  181. api(getState).post(`/api/v1/statuses/${id}/mute`).then(() => {
  182. dispatch(muteStatusSuccess(id));
  183. }).catch(error => {
  184. dispatch(muteStatusFail(id, error));
  185. });
  186. };
  187. };
  188. export function muteStatusRequest(id) {
  189. return {
  190. type: STATUS_MUTE_REQUEST,
  191. id,
  192. };
  193. };
  194. export function muteStatusSuccess(id) {
  195. return {
  196. type: STATUS_MUTE_SUCCESS,
  197. id,
  198. };
  199. };
  200. export function muteStatusFail(id, error) {
  201. return {
  202. type: STATUS_MUTE_FAIL,
  203. id,
  204. error,
  205. };
  206. };
  207. export function unmuteStatus(id) {
  208. return (dispatch, getState) => {
  209. dispatch(unmuteStatusRequest(id));
  210. api(getState).post(`/api/v1/statuses/${id}/unmute`).then(() => {
  211. dispatch(unmuteStatusSuccess(id));
  212. }).catch(error => {
  213. dispatch(unmuteStatusFail(id, error));
  214. });
  215. };
  216. };
  217. export function unmuteStatusRequest(id) {
  218. return {
  219. type: STATUS_UNMUTE_REQUEST,
  220. id,
  221. };
  222. };
  223. export function unmuteStatusSuccess(id) {
  224. return {
  225. type: STATUS_UNMUTE_SUCCESS,
  226. id,
  227. };
  228. };
  229. export function unmuteStatusFail(id, error) {
  230. return {
  231. type: STATUS_UNMUTE_FAIL,
  232. id,
  233. error,
  234. };
  235. };
  236. export function hideStatus(ids) {
  237. if (!Array.isArray(ids)) {
  238. ids = [ids];
  239. }
  240. return {
  241. type: STATUS_HIDE,
  242. ids,
  243. };
  244. };
  245. export function revealStatus(ids) {
  246. if (!Array.isArray(ids)) {
  247. ids = [ids];
  248. }
  249. return {
  250. type: STATUS_REVEAL,
  251. ids,
  252. };
  253. };
  254. export function toggleStatusCollapse(id, isCollapsed) {
  255. return {
  256. type: STATUS_COLLAPSE,
  257. id,
  258. isCollapsed,
  259. };
  260. };
  261. export const translateStatus = id => (dispatch, getState) => {
  262. dispatch(translateStatusRequest(id));
  263. api(getState).post(`/api/v1/statuses/${id}/translate`).then(response => {
  264. dispatch(translateStatusSuccess(id, response.data));
  265. }).catch(error => {
  266. dispatch(translateStatusFail(id, error));
  267. });
  268. };
  269. export const translateStatusRequest = id => ({
  270. type: STATUS_TRANSLATE_REQUEST,
  271. id,
  272. });
  273. export const translateStatusSuccess = (id, translation) => ({
  274. type: STATUS_TRANSLATE_SUCCESS,
  275. id,
  276. translation,
  277. });
  278. export const translateStatusFail = (id, error) => ({
  279. type: STATUS_TRANSLATE_FAIL,
  280. id,
  281. error,
  282. });
  283. export const undoStatusTranslation = id => ({
  284. type: STATUS_TRANSLATE_UNDO,
  285. id,
  286. });