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.

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