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.

322 lines
7.5 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. import { ensureComposeIsVisible } from './compose';
  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. if (skipLoading) {
  69. return;
  70. }
  71. dispatch(fetchStatusRequest(id, skipLoading));
  72. openDB().then(db => {
  73. const transaction = db.transaction(['accounts', 'statuses'], 'read');
  74. const accountIndex = transaction.objectStore('accounts').index('id');
  75. const index = transaction.objectStore('statuses').index('id');
  76. return getFromDB(dispatch, getState, accountIndex, index, id).then(() => {
  77. db.close();
  78. }, error => {
  79. db.close();
  80. throw error;
  81. });
  82. }).then(() => {
  83. dispatch(fetchStatusSuccess(skipLoading));
  84. }, () => api(getState).get(`/api/v1/statuses/${id}`).then(response => {
  85. dispatch(importFetchedStatus(response.data));
  86. dispatch(fetchStatusSuccess(skipLoading));
  87. })).catch(error => {
  88. dispatch(fetchStatusFail(id, error, skipLoading));
  89. });
  90. };
  91. };
  92. export function fetchStatusSuccess(skipLoading) {
  93. return {
  94. type: STATUS_FETCH_SUCCESS,
  95. skipLoading,
  96. };
  97. };
  98. export function fetchStatusFail(id, error, skipLoading) {
  99. return {
  100. type: STATUS_FETCH_FAIL,
  101. id,
  102. error,
  103. skipLoading,
  104. skipAlert: true,
  105. };
  106. };
  107. export function redraft(status, raw_text) {
  108. return {
  109. type: REDRAFT,
  110. status,
  111. raw_text,
  112. };
  113. };
  114. export function deleteStatus(id, routerHistory, withRedraft = false) {
  115. return (dispatch, getState) => {
  116. let status = getState().getIn(['statuses', id]);
  117. if (status.get('poll')) {
  118. status = status.set('poll', getState().getIn(['polls', status.get('poll')]));
  119. }
  120. dispatch(deleteStatusRequest(id));
  121. api(getState).delete(`/api/v1/statuses/${id}`).then(response => {
  122. evictStatus(id);
  123. dispatch(deleteStatusSuccess(id));
  124. dispatch(deleteFromTimelines(id));
  125. if (withRedraft) {
  126. dispatch(redraft(status, response.data.text));
  127. ensureComposeIsVisible(getState, routerHistory);
  128. }
  129. }).catch(error => {
  130. dispatch(deleteStatusFail(id, error));
  131. });
  132. };
  133. };
  134. export function deleteStatusRequest(id) {
  135. return {
  136. type: STATUS_DELETE_REQUEST,
  137. id: id,
  138. };
  139. };
  140. export function deleteStatusSuccess(id) {
  141. return {
  142. type: STATUS_DELETE_SUCCESS,
  143. id: id,
  144. };
  145. };
  146. export function deleteStatusFail(id, error) {
  147. return {
  148. type: STATUS_DELETE_FAIL,
  149. id: id,
  150. error: error,
  151. };
  152. };
  153. export function fetchContext(id) {
  154. return (dispatch, getState) => {
  155. dispatch(fetchContextRequest(id));
  156. api(getState).get(`/api/v1/statuses/${id}/context`).then(response => {
  157. dispatch(importFetchedStatuses(response.data.ancestors.concat(response.data.descendants)));
  158. dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants));
  159. }).catch(error => {
  160. if (error.response && error.response.status === 404) {
  161. dispatch(deleteFromTimelines(id));
  162. }
  163. dispatch(fetchContextFail(id, error));
  164. });
  165. };
  166. };
  167. export function fetchContextRequest(id) {
  168. return {
  169. type: CONTEXT_FETCH_REQUEST,
  170. id,
  171. };
  172. };
  173. export function fetchContextSuccess(id, ancestors, descendants) {
  174. return {
  175. type: CONTEXT_FETCH_SUCCESS,
  176. id,
  177. ancestors,
  178. descendants,
  179. statuses: ancestors.concat(descendants),
  180. };
  181. };
  182. export function fetchContextFail(id, error) {
  183. return {
  184. type: CONTEXT_FETCH_FAIL,
  185. id,
  186. error,
  187. skipAlert: true,
  188. };
  189. };
  190. export function muteStatus(id) {
  191. return (dispatch, getState) => {
  192. dispatch(muteStatusRequest(id));
  193. api(getState).post(`/api/v1/statuses/${id}/mute`).then(() => {
  194. dispatch(muteStatusSuccess(id));
  195. }).catch(error => {
  196. dispatch(muteStatusFail(id, error));
  197. });
  198. };
  199. };
  200. export function muteStatusRequest(id) {
  201. return {
  202. type: STATUS_MUTE_REQUEST,
  203. id,
  204. };
  205. };
  206. export function muteStatusSuccess(id) {
  207. return {
  208. type: STATUS_MUTE_SUCCESS,
  209. id,
  210. };
  211. };
  212. export function muteStatusFail(id, error) {
  213. return {
  214. type: STATUS_MUTE_FAIL,
  215. id,
  216. error,
  217. };
  218. };
  219. export function unmuteStatus(id) {
  220. return (dispatch, getState) => {
  221. dispatch(unmuteStatusRequest(id));
  222. api(getState).post(`/api/v1/statuses/${id}/unmute`).then(() => {
  223. dispatch(unmuteStatusSuccess(id));
  224. }).catch(error => {
  225. dispatch(unmuteStatusFail(id, error));
  226. });
  227. };
  228. };
  229. export function unmuteStatusRequest(id) {
  230. return {
  231. type: STATUS_UNMUTE_REQUEST,
  232. id,
  233. };
  234. };
  235. export function unmuteStatusSuccess(id) {
  236. return {
  237. type: STATUS_UNMUTE_SUCCESS,
  238. id,
  239. };
  240. };
  241. export function unmuteStatusFail(id, error) {
  242. return {
  243. type: STATUS_UNMUTE_FAIL,
  244. id,
  245. error,
  246. };
  247. };
  248. export function hideStatus(ids) {
  249. if (!Array.isArray(ids)) {
  250. ids = [ids];
  251. }
  252. return {
  253. type: STATUS_HIDE,
  254. ids,
  255. };
  256. };
  257. export function revealStatus(ids) {
  258. if (!Array.isArray(ids)) {
  259. ids = [ids];
  260. }
  261. return {
  262. type: STATUS_REVEAL,
  263. ids,
  264. };
  265. };