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.

319 lines
7.3 KiB

  1. import api from '../api';
  2. import openDB from '../storage/db';
  3. import { evictStatus } from '../storage/modifier';
  4. import { deleteFromTimelines } from './timelines';
  5. import { importFetchedStatus, importFetchedStatuses, importAccount, importStatus } from './importer';
  6. export const STATUS_FETCH_REQUEST = 'STATUS_FETCH_REQUEST';
  7. export const STATUS_FETCH_SUCCESS = 'STATUS_FETCH_SUCCESS';
  8. export const STATUS_FETCH_FAIL = 'STATUS_FETCH_FAIL';
  9. export const STATUS_DELETE_REQUEST = 'STATUS_DELETE_REQUEST';
  10. export const STATUS_DELETE_SUCCESS = 'STATUS_DELETE_SUCCESS';
  11. export const STATUS_DELETE_FAIL = 'STATUS_DELETE_FAIL';
  12. export const CONTEXT_FETCH_REQUEST = 'CONTEXT_FETCH_REQUEST';
  13. export const CONTEXT_FETCH_SUCCESS = 'CONTEXT_FETCH_SUCCESS';
  14. export const CONTEXT_FETCH_FAIL = 'CONTEXT_FETCH_FAIL';
  15. export const STATUS_MUTE_REQUEST = 'STATUS_MUTE_REQUEST';
  16. export const STATUS_MUTE_SUCCESS = 'STATUS_MUTE_SUCCESS';
  17. export const STATUS_MUTE_FAIL = 'STATUS_MUTE_FAIL';
  18. export const STATUS_UNMUTE_REQUEST = 'STATUS_UNMUTE_REQUEST';
  19. export const STATUS_UNMUTE_SUCCESS = 'STATUS_UNMUTE_SUCCESS';
  20. export const STATUS_UNMUTE_FAIL = 'STATUS_UNMUTE_FAIL';
  21. export const STATUS_REVEAL = 'STATUS_REVEAL';
  22. export const STATUS_HIDE = 'STATUS_HIDE';
  23. export const REDRAFT = 'REDRAFT';
  24. export function fetchStatusRequest(id, skipLoading) {
  25. return {
  26. type: STATUS_FETCH_REQUEST,
  27. id,
  28. skipLoading,
  29. };
  30. };
  31. function getFromDB(dispatch, getState, accountIndex, index, id) {
  32. return new Promise((resolve, reject) => {
  33. const request = index.get(id);
  34. request.onerror = reject;
  35. request.onsuccess = () => {
  36. const promises = [];
  37. if (!request.result) {
  38. reject();
  39. return;
  40. }
  41. dispatch(importStatus(request.result));
  42. if (getState().getIn(['accounts', request.result.account], null) === null) {
  43. promises.push(new Promise((accountResolve, accountReject) => {
  44. const accountRequest = accountIndex.get(request.result.account);
  45. accountRequest.onerror = accountReject;
  46. accountRequest.onsuccess = () => {
  47. if (!request.result) {
  48. accountReject();
  49. return;
  50. }
  51. dispatch(importAccount(accountRequest.result));
  52. accountResolve();
  53. };
  54. }));
  55. }
  56. if (request.result.reblog && getState().getIn(['statuses', request.result.reblog], null) === null) {
  57. promises.push(getFromDB(dispatch, getState, accountIndex, index, request.result.reblog));
  58. }
  59. resolve(Promise.all(promises));
  60. };
  61. });
  62. }
  63. export function fetchStatus(id) {
  64. return (dispatch, getState) => {
  65. const skipLoading = getState().getIn(['statuses', id], null) !== null;
  66. dispatch(fetchContext(id));
  67. if (skipLoading) {
  68. return;
  69. }
  70. dispatch(fetchStatusRequest(id, skipLoading));
  71. openDB().then(db => {
  72. const transaction = db.transaction(['accounts', 'statuses'], 'read');
  73. const accountIndex = transaction.objectStore('accounts').index('id');
  74. const index = transaction.objectStore('statuses').index('id');
  75. return getFromDB(dispatch, getState, accountIndex, index, id).then(() => {
  76. db.close();
  77. }, error => {
  78. db.close();
  79. throw error;
  80. });
  81. }).then(() => {
  82. dispatch(fetchStatusSuccess(skipLoading));
  83. }, () => api(getState).get(`/api/v1/statuses/${id}`).then(response => {
  84. dispatch(importFetchedStatus(response.data));
  85. dispatch(fetchStatusSuccess(skipLoading));
  86. })).catch(error => {
  87. dispatch(fetchStatusFail(id, error, skipLoading));
  88. });
  89. };
  90. };
  91. export function fetchStatusSuccess(skipLoading) {
  92. return {
  93. type: STATUS_FETCH_SUCCESS,
  94. skipLoading,
  95. };
  96. };
  97. export function fetchStatusFail(id, error, skipLoading) {
  98. return {
  99. type: STATUS_FETCH_FAIL,
  100. id,
  101. error,
  102. skipLoading,
  103. skipAlert: true,
  104. };
  105. };
  106. export function redraft(status) {
  107. return {
  108. type: REDRAFT,
  109. status,
  110. };
  111. };
  112. export function deleteStatus(id, router, withRedraft = false) {
  113. return (dispatch, getState) => {
  114. const status = getState().getIn(['statuses', id]);
  115. dispatch(deleteStatusRequest(id));
  116. api(getState).delete(`/api/v1/statuses/${id}`).then(() => {
  117. evictStatus(id);
  118. dispatch(deleteStatusSuccess(id));
  119. dispatch(deleteFromTimelines(id));
  120. if (withRedraft) {
  121. dispatch(redraft(status));
  122. if (!getState().getIn(['compose', 'mounted'])) {
  123. router.push('/statuses/new');
  124. }
  125. }
  126. }).catch(error => {
  127. dispatch(deleteStatusFail(id, error));
  128. });
  129. };
  130. };
  131. export function deleteStatusRequest(id) {
  132. return {
  133. type: STATUS_DELETE_REQUEST,
  134. id: id,
  135. };
  136. };
  137. export function deleteStatusSuccess(id) {
  138. return {
  139. type: STATUS_DELETE_SUCCESS,
  140. id: id,
  141. };
  142. };
  143. export function deleteStatusFail(id, error) {
  144. return {
  145. type: STATUS_DELETE_FAIL,
  146. id: id,
  147. error: error,
  148. };
  149. };
  150. export function fetchContext(id) {
  151. return (dispatch, getState) => {
  152. dispatch(fetchContextRequest(id));
  153. api(getState).get(`/api/v1/statuses/${id}/context`).then(response => {
  154. dispatch(importFetchedStatuses(response.data.ancestors.concat(response.data.descendants)));
  155. dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants));
  156. }).catch(error => {
  157. if (error.response && error.response.status === 404) {
  158. dispatch(deleteFromTimelines(id));
  159. }
  160. dispatch(fetchContextFail(id, error));
  161. });
  162. };
  163. };
  164. export function fetchContextRequest(id) {
  165. return {
  166. type: CONTEXT_FETCH_REQUEST,
  167. id,
  168. };
  169. };
  170. export function fetchContextSuccess(id, ancestors, descendants) {
  171. return {
  172. type: CONTEXT_FETCH_SUCCESS,
  173. id,
  174. ancestors,
  175. descendants,
  176. statuses: ancestors.concat(descendants),
  177. };
  178. };
  179. export function fetchContextFail(id, error) {
  180. return {
  181. type: CONTEXT_FETCH_FAIL,
  182. id,
  183. error,
  184. skipAlert: true,
  185. };
  186. };
  187. export function muteStatus(id) {
  188. return (dispatch, getState) => {
  189. dispatch(muteStatusRequest(id));
  190. api(getState).post(`/api/v1/statuses/${id}/mute`).then(() => {
  191. dispatch(muteStatusSuccess(id));
  192. }).catch(error => {
  193. dispatch(muteStatusFail(id, error));
  194. });
  195. };
  196. };
  197. export function muteStatusRequest(id) {
  198. return {
  199. type: STATUS_MUTE_REQUEST,
  200. id,
  201. };
  202. };
  203. export function muteStatusSuccess(id) {
  204. return {
  205. type: STATUS_MUTE_SUCCESS,
  206. id,
  207. };
  208. };
  209. export function muteStatusFail(id, error) {
  210. return {
  211. type: STATUS_MUTE_FAIL,
  212. id,
  213. error,
  214. };
  215. };
  216. export function unmuteStatus(id) {
  217. return (dispatch, getState) => {
  218. dispatch(unmuteStatusRequest(id));
  219. api(getState).post(`/api/v1/statuses/${id}/unmute`).then(() => {
  220. dispatch(unmuteStatusSuccess(id));
  221. }).catch(error => {
  222. dispatch(unmuteStatusFail(id, error));
  223. });
  224. };
  225. };
  226. export function unmuteStatusRequest(id) {
  227. return {
  228. type: STATUS_UNMUTE_REQUEST,
  229. id,
  230. };
  231. };
  232. export function unmuteStatusSuccess(id) {
  233. return {
  234. type: STATUS_UNMUTE_SUCCESS,
  235. id,
  236. };
  237. };
  238. export function unmuteStatusFail(id, error) {
  239. return {
  240. type: STATUS_UNMUTE_FAIL,
  241. id,
  242. error,
  243. };
  244. };
  245. export function hideStatus(ids) {
  246. if (!Array.isArray(ids)) {
  247. ids = [ids];
  248. }
  249. return {
  250. type: STATUS_HIDE,
  251. ids,
  252. };
  253. };
  254. export function revealStatus(ids) {
  255. if (!Array.isArray(ids)) {
  256. ids = [ids];
  257. }
  258. return {
  259. type: STATUS_REVEAL,
  260. ids,
  261. };
  262. };