The code powering m.abunchtell.com https://m.abunchtell.com
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

298 строки
6.9 KiB

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