The code powering m.abunchtell.com https://m.abunchtell.com
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

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