The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

303 rivejä
7.0 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 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. dispatch(fetchStatusCard(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 deleteStatus(id) {
  108. return (dispatch, getState) => {
  109. dispatch(deleteStatusRequest(id));
  110. api(getState).delete(`/api/v1/statuses/${id}`).then(() => {
  111. evictStatus(id);
  112. dispatch(deleteStatusSuccess(id));
  113. dispatch(deleteFromTimelines(id));
  114. }).catch(error => {
  115. dispatch(deleteStatusFail(id, error));
  116. });
  117. };
  118. };
  119. export function deleteStatusRequest(id) {
  120. return {
  121. type: STATUS_DELETE_REQUEST,
  122. id: id,
  123. };
  124. };
  125. export function deleteStatusSuccess(id) {
  126. return {
  127. type: STATUS_DELETE_SUCCESS,
  128. id: id,
  129. };
  130. };
  131. export function deleteStatusFail(id, error) {
  132. return {
  133. type: STATUS_DELETE_FAIL,
  134. id: id,
  135. error: error,
  136. };
  137. };
  138. export function fetchContext(id) {
  139. return (dispatch, getState) => {
  140. dispatch(fetchContextRequest(id));
  141. api(getState).get(`/api/v1/statuses/${id}/context`).then(response => {
  142. dispatch(importFetchedStatuses(response.data.ancestors.concat(response.data.descendants)));
  143. dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants));
  144. }).catch(error => {
  145. if (error.response && error.response.status === 404) {
  146. dispatch(deleteFromTimelines(id));
  147. }
  148. dispatch(fetchContextFail(id, error));
  149. });
  150. };
  151. };
  152. export function fetchContextRequest(id) {
  153. return {
  154. type: CONTEXT_FETCH_REQUEST,
  155. id,
  156. };
  157. };
  158. export function fetchContextSuccess(id, ancestors, descendants) {
  159. return {
  160. type: CONTEXT_FETCH_SUCCESS,
  161. id,
  162. ancestors,
  163. descendants,
  164. statuses: ancestors.concat(descendants),
  165. };
  166. };
  167. export function fetchContextFail(id, error) {
  168. return {
  169. type: CONTEXT_FETCH_FAIL,
  170. id,
  171. error,
  172. skipAlert: true,
  173. };
  174. };
  175. export function muteStatus(id) {
  176. return (dispatch, getState) => {
  177. dispatch(muteStatusRequest(id));
  178. api(getState).post(`/api/v1/statuses/${id}/mute`).then(() => {
  179. dispatch(muteStatusSuccess(id));
  180. }).catch(error => {
  181. dispatch(muteStatusFail(id, error));
  182. });
  183. };
  184. };
  185. export function muteStatusRequest(id) {
  186. return {
  187. type: STATUS_MUTE_REQUEST,
  188. id,
  189. };
  190. };
  191. export function muteStatusSuccess(id) {
  192. return {
  193. type: STATUS_MUTE_SUCCESS,
  194. id,
  195. };
  196. };
  197. export function muteStatusFail(id, error) {
  198. return {
  199. type: STATUS_MUTE_FAIL,
  200. id,
  201. error,
  202. };
  203. };
  204. export function unmuteStatus(id) {
  205. return (dispatch, getState) => {
  206. dispatch(unmuteStatusRequest(id));
  207. api(getState).post(`/api/v1/statuses/${id}/unmute`).then(() => {
  208. dispatch(unmuteStatusSuccess(id));
  209. }).catch(error => {
  210. dispatch(unmuteStatusFail(id, error));
  211. });
  212. };
  213. };
  214. export function unmuteStatusRequest(id) {
  215. return {
  216. type: STATUS_UNMUTE_REQUEST,
  217. id,
  218. };
  219. };
  220. export function unmuteStatusSuccess(id) {
  221. return {
  222. type: STATUS_UNMUTE_SUCCESS,
  223. id,
  224. };
  225. };
  226. export function unmuteStatusFail(id, error) {
  227. return {
  228. type: STATUS_UNMUTE_FAIL,
  229. id,
  230. error,
  231. };
  232. };
  233. export function hideStatus(ids) {
  234. if (!Array.isArray(ids)) {
  235. ids = [ids];
  236. }
  237. return {
  238. type: STATUS_HIDE,
  239. ids,
  240. };
  241. };
  242. export function revealStatus(ids) {
  243. if (!Array.isArray(ids)) {
  244. ids = [ids];
  245. }
  246. return {
  247. type: STATUS_REVEAL,
  248. ids,
  249. };
  250. };