logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

statuses.js (8208B)


  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. // PLEROMA TEMP FIX
  143. // If we already have the status, we can do some preprocessing on descendants & ancestors.
  144. const status = getState().getIn(['statuses', id], null);
  145. if (status) {
  146. console.log("we have the status already - we can do preproc on ancestors");
  147. // Filter posts so that they're in our same branch
  148. let nextID = status.get("in_reply_to_id");
  149. console.log(status, response.data.ancestors);
  150. response.data.ancestors = response.data.ancestors.reverse().filter(s => {
  151. if (s.in_reply_to_id == null || s.id == nextID) {
  152. nextID = s.in_reply_to_id;
  153. return true;
  154. }
  155. return false;
  156. }).reverse();
  157. }
  158. const allowedIDs = {};
  159. allowedIDs[id] = true;
  160. response.data.descendants = response.data.descendants.filter(s => {
  161. if (s.in_reply_to_id in allowedIDs) {
  162. allowedIDs[s.id] = true;
  163. return true;
  164. }
  165. return false;
  166. });
  167. dispatch(importFetchedStatuses(response.data.ancestors.concat(response.data.descendants)));
  168. dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants));
  169. }).catch(error => {
  170. if (error.response && error.response.status === 404) {
  171. dispatch(deleteFromTimelines(id));
  172. }
  173. dispatch(fetchContextFail(id, error));
  174. });
  175. };
  176. };
  177. export function fetchContextRequest(id) {
  178. return {
  179. type: CONTEXT_FETCH_REQUEST,
  180. id,
  181. };
  182. };
  183. export function fetchContextSuccess(id, ancestors, descendants) {
  184. return {
  185. type: CONTEXT_FETCH_SUCCESS,
  186. id,
  187. ancestors,
  188. descendants,
  189. statuses: ancestors.concat(descendants),
  190. };
  191. };
  192. export function fetchContextFail(id, error) {
  193. return {
  194. type: CONTEXT_FETCH_FAIL,
  195. id,
  196. error,
  197. skipAlert: true,
  198. };
  199. };
  200. export function muteStatus(id) {
  201. return (dispatch, getState) => {
  202. dispatch(muteStatusRequest(id));
  203. api(getState).post(`/api/v1/statuses/${id}/mute`).then(() => {
  204. dispatch(muteStatusSuccess(id));
  205. }).catch(error => {
  206. dispatch(muteStatusFail(id, error));
  207. });
  208. };
  209. };
  210. export function muteStatusRequest(id) {
  211. return {
  212. type: STATUS_MUTE_REQUEST,
  213. id,
  214. };
  215. };
  216. export function muteStatusSuccess(id) {
  217. return {
  218. type: STATUS_MUTE_SUCCESS,
  219. id,
  220. };
  221. };
  222. export function muteStatusFail(id, error) {
  223. return {
  224. type: STATUS_MUTE_FAIL,
  225. id,
  226. error,
  227. };
  228. };
  229. export function unmuteStatus(id) {
  230. return (dispatch, getState) => {
  231. dispatch(unmuteStatusRequest(id));
  232. api(getState).post(`/api/v1/statuses/${id}/unmute`).then(() => {
  233. dispatch(unmuteStatusSuccess(id));
  234. }).catch(error => {
  235. dispatch(unmuteStatusFail(id, error));
  236. });
  237. };
  238. };
  239. export function unmuteStatusRequest(id) {
  240. return {
  241. type: STATUS_UNMUTE_REQUEST,
  242. id,
  243. };
  244. };
  245. export function unmuteStatusSuccess(id) {
  246. return {
  247. type: STATUS_UNMUTE_SUCCESS,
  248. id,
  249. };
  250. };
  251. export function unmuteStatusFail(id, error) {
  252. return {
  253. type: STATUS_UNMUTE_FAIL,
  254. id,
  255. error,
  256. };
  257. };
  258. export function hideStatus(ids) {
  259. if (!Array.isArray(ids)) {
  260. ids = [ids];
  261. }
  262. return {
  263. type: STATUS_HIDE,
  264. ids,
  265. };
  266. };
  267. export function revealStatus(ids) {
  268. if (!Array.isArray(ids)) {
  269. ids = [ids];
  270. }
  271. return {
  272. type: STATUS_REVEAL,
  273. ids,
  274. };
  275. };