logo

mastofe

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

accounts.js (17766B)


  1. import api, { getLinks } from '../api';
  2. import openDB from '../storage/db';
  3. import { importAccount, importFetchedAccount, importFetchedAccounts } from './importer';
  4. export const ACCOUNT_FETCH_REQUEST = 'ACCOUNT_FETCH_REQUEST';
  5. export const ACCOUNT_FETCH_SUCCESS = 'ACCOUNT_FETCH_SUCCESS';
  6. export const ACCOUNT_FETCH_FAIL = 'ACCOUNT_FETCH_FAIL';
  7. export const ACCOUNT_FOLLOW_REQUEST = 'ACCOUNT_FOLLOW_REQUEST';
  8. export const ACCOUNT_FOLLOW_SUCCESS = 'ACCOUNT_FOLLOW_SUCCESS';
  9. export const ACCOUNT_FOLLOW_FAIL = 'ACCOUNT_FOLLOW_FAIL';
  10. export const ACCOUNT_UNFOLLOW_REQUEST = 'ACCOUNT_UNFOLLOW_REQUEST';
  11. export const ACCOUNT_UNFOLLOW_SUCCESS = 'ACCOUNT_UNFOLLOW_SUCCESS';
  12. export const ACCOUNT_UNFOLLOW_FAIL = 'ACCOUNT_UNFOLLOW_FAIL';
  13. export const ACCOUNT_BLOCK_REQUEST = 'ACCOUNT_BLOCK_REQUEST';
  14. export const ACCOUNT_BLOCK_SUCCESS = 'ACCOUNT_BLOCK_SUCCESS';
  15. export const ACCOUNT_BLOCK_FAIL = 'ACCOUNT_BLOCK_FAIL';
  16. export const ACCOUNT_UNBLOCK_REQUEST = 'ACCOUNT_UNBLOCK_REQUEST';
  17. export const ACCOUNT_UNBLOCK_SUCCESS = 'ACCOUNT_UNBLOCK_SUCCESS';
  18. export const ACCOUNT_UNBLOCK_FAIL = 'ACCOUNT_UNBLOCK_FAIL';
  19. export const ACCOUNT_MUTE_REQUEST = 'ACCOUNT_MUTE_REQUEST';
  20. export const ACCOUNT_MUTE_SUCCESS = 'ACCOUNT_MUTE_SUCCESS';
  21. export const ACCOUNT_MUTE_FAIL = 'ACCOUNT_MUTE_FAIL';
  22. export const ACCOUNT_UNMUTE_REQUEST = 'ACCOUNT_UNMUTE_REQUEST';
  23. export const ACCOUNT_UNMUTE_SUCCESS = 'ACCOUNT_UNMUTE_SUCCESS';
  24. export const ACCOUNT_UNMUTE_FAIL = 'ACCOUNT_UNMUTE_FAIL';
  25. export const FOLLOWERS_FETCH_REQUEST = 'FOLLOWERS_FETCH_REQUEST';
  26. export const FOLLOWERS_FETCH_SUCCESS = 'FOLLOWERS_FETCH_SUCCESS';
  27. export const FOLLOWERS_FETCH_FAIL = 'FOLLOWERS_FETCH_FAIL';
  28. export const FOLLOWERS_EXPAND_REQUEST = 'FOLLOWERS_EXPAND_REQUEST';
  29. export const FOLLOWERS_EXPAND_SUCCESS = 'FOLLOWERS_EXPAND_SUCCESS';
  30. export const FOLLOWERS_EXPAND_FAIL = 'FOLLOWERS_EXPAND_FAIL';
  31. export const FOLLOWING_FETCH_REQUEST = 'FOLLOWING_FETCH_REQUEST';
  32. export const FOLLOWING_FETCH_SUCCESS = 'FOLLOWING_FETCH_SUCCESS';
  33. export const FOLLOWING_FETCH_FAIL = 'FOLLOWING_FETCH_FAIL';
  34. export const FOLLOWING_EXPAND_REQUEST = 'FOLLOWING_EXPAND_REQUEST';
  35. export const FOLLOWING_EXPAND_SUCCESS = 'FOLLOWING_EXPAND_SUCCESS';
  36. export const FOLLOWING_EXPAND_FAIL = 'FOLLOWING_EXPAND_FAIL';
  37. export const RELATIONSHIPS_FETCH_REQUEST = 'RELATIONSHIPS_FETCH_REQUEST';
  38. export const RELATIONSHIPS_FETCH_SUCCESS = 'RELATIONSHIPS_FETCH_SUCCESS';
  39. export const RELATIONSHIPS_FETCH_FAIL = 'RELATIONSHIPS_FETCH_FAIL';
  40. export const FOLLOW_REQUESTS_FETCH_REQUEST = 'FOLLOW_REQUESTS_FETCH_REQUEST';
  41. export const FOLLOW_REQUESTS_FETCH_SUCCESS = 'FOLLOW_REQUESTS_FETCH_SUCCESS';
  42. export const FOLLOW_REQUESTS_FETCH_FAIL = 'FOLLOW_REQUESTS_FETCH_FAIL';
  43. export const FOLLOW_REQUESTS_EXPAND_REQUEST = 'FOLLOW_REQUESTS_EXPAND_REQUEST';
  44. export const FOLLOW_REQUESTS_EXPAND_SUCCESS = 'FOLLOW_REQUESTS_EXPAND_SUCCESS';
  45. export const FOLLOW_REQUESTS_EXPAND_FAIL = 'FOLLOW_REQUESTS_EXPAND_FAIL';
  46. export const FOLLOW_REQUEST_AUTHORIZE_REQUEST = 'FOLLOW_REQUEST_AUTHORIZE_REQUEST';
  47. export const FOLLOW_REQUEST_AUTHORIZE_SUCCESS = 'FOLLOW_REQUEST_AUTHORIZE_SUCCESS';
  48. export const FOLLOW_REQUEST_AUTHORIZE_FAIL = 'FOLLOW_REQUEST_AUTHORIZE_FAIL';
  49. export const FOLLOW_REQUEST_REJECT_REQUEST = 'FOLLOW_REQUEST_REJECT_REQUEST';
  50. export const FOLLOW_REQUEST_REJECT_SUCCESS = 'FOLLOW_REQUEST_REJECT_SUCCESS';
  51. export const FOLLOW_REQUEST_REJECT_FAIL = 'FOLLOW_REQUEST_REJECT_FAIL';
  52. function getFromDB(dispatch, getState, index, id) {
  53. return new Promise((resolve, reject) => {
  54. const request = index.get(id);
  55. request.onerror = reject;
  56. request.onsuccess = () => {
  57. if (!request.result) {
  58. reject();
  59. return;
  60. }
  61. dispatch(importAccount(request.result));
  62. resolve(request.result.moved && getFromDB(dispatch, getState, index, request.result.moved));
  63. };
  64. });
  65. }
  66. export function fetchAccount(id) {
  67. return (dispatch, getState) => {
  68. dispatch(fetchRelationships([id]));
  69. if (getState().getIn(['accounts', id], null) !== null) {
  70. return;
  71. }
  72. dispatch(fetchAccountRequest(id));
  73. openDB().then(db => getFromDB(
  74. dispatch,
  75. getState,
  76. db.transaction('accounts', 'read').objectStore('accounts').index('id'),
  77. id
  78. ).then(() => db.close(), error => {
  79. db.close();
  80. throw error;
  81. })).catch(() => api(getState).get(`/api/v1/accounts/${id}`).then(response => {
  82. dispatch(importFetchedAccount(response.data));
  83. })).then(() => {
  84. dispatch(fetchAccountSuccess());
  85. }).catch(error => {
  86. dispatch(fetchAccountFail(id, error));
  87. });
  88. };
  89. };
  90. export function fetchAccountRequest(id) {
  91. return {
  92. type: ACCOUNT_FETCH_REQUEST,
  93. id,
  94. };
  95. };
  96. export function fetchAccountSuccess() {
  97. return {
  98. type: ACCOUNT_FETCH_SUCCESS,
  99. };
  100. };
  101. export function fetchAccountFail(id, error) {
  102. return {
  103. type: ACCOUNT_FETCH_FAIL,
  104. id,
  105. error,
  106. skipAlert: true,
  107. };
  108. };
  109. export function followAccount(id, reblogs = true) {
  110. return (dispatch, getState) => {
  111. const alreadyFollowing = getState().getIn(['relationships', id, 'following']);
  112. dispatch(followAccountRequest(id));
  113. api(getState).post(`/api/v1/accounts/${id}/follow`, { reblogs }).then(response => {
  114. dispatch(followAccountSuccess(response.data, alreadyFollowing));
  115. }).catch(error => {
  116. dispatch(followAccountFail(error));
  117. });
  118. };
  119. };
  120. export function unfollowAccount(id) {
  121. return (dispatch, getState) => {
  122. dispatch(unfollowAccountRequest(id));
  123. api(getState).post(`/api/v1/accounts/${id}/unfollow`).then(response => {
  124. dispatch(unfollowAccountSuccess(response.data, getState().get('statuses')));
  125. }).catch(error => {
  126. dispatch(unfollowAccountFail(error));
  127. });
  128. };
  129. };
  130. export function followAccountRequest(id) {
  131. return {
  132. type: ACCOUNT_FOLLOW_REQUEST,
  133. id,
  134. };
  135. };
  136. export function followAccountSuccess(relationship, alreadyFollowing) {
  137. return {
  138. type: ACCOUNT_FOLLOW_SUCCESS,
  139. relationship,
  140. alreadyFollowing,
  141. };
  142. };
  143. export function followAccountFail(error) {
  144. return {
  145. type: ACCOUNT_FOLLOW_FAIL,
  146. error,
  147. };
  148. };
  149. export function unfollowAccountRequest(id) {
  150. return {
  151. type: ACCOUNT_UNFOLLOW_REQUEST,
  152. id,
  153. };
  154. };
  155. export function unfollowAccountSuccess(relationship, statuses) {
  156. return {
  157. type: ACCOUNT_UNFOLLOW_SUCCESS,
  158. relationship,
  159. statuses,
  160. };
  161. };
  162. export function unfollowAccountFail(error) {
  163. return {
  164. type: ACCOUNT_UNFOLLOW_FAIL,
  165. error,
  166. };
  167. };
  168. export function blockAccount(id) {
  169. return (dispatch, getState) => {
  170. dispatch(blockAccountRequest(id));
  171. api(getState).post(`/api/v1/accounts/${id}/block`).then(response => {
  172. // Pass in entire statuses map so we can use it to filter stuff in different parts of the reducers
  173. dispatch(blockAccountSuccess(response.data, getState().get('statuses')));
  174. }).catch(error => {
  175. dispatch(blockAccountFail(id, error));
  176. });
  177. };
  178. };
  179. export function unblockAccount(id) {
  180. return (dispatch, getState) => {
  181. dispatch(unblockAccountRequest(id));
  182. api(getState).post(`/api/v1/accounts/${id}/unblock`).then(response => {
  183. dispatch(unblockAccountSuccess(response.data));
  184. }).catch(error => {
  185. dispatch(unblockAccountFail(id, error));
  186. });
  187. };
  188. };
  189. export function blockAccountRequest(id) {
  190. return {
  191. type: ACCOUNT_BLOCK_REQUEST,
  192. id,
  193. };
  194. };
  195. export function blockAccountSuccess(relationship, statuses) {
  196. return {
  197. type: ACCOUNT_BLOCK_SUCCESS,
  198. relationship,
  199. statuses,
  200. };
  201. };
  202. export function blockAccountFail(error) {
  203. return {
  204. type: ACCOUNT_BLOCK_FAIL,
  205. error,
  206. };
  207. };
  208. export function unblockAccountRequest(id) {
  209. return {
  210. type: ACCOUNT_UNBLOCK_REQUEST,
  211. id,
  212. };
  213. };
  214. export function unblockAccountSuccess(relationship) {
  215. return {
  216. type: ACCOUNT_UNBLOCK_SUCCESS,
  217. relationship,
  218. };
  219. };
  220. export function unblockAccountFail(error) {
  221. return {
  222. type: ACCOUNT_UNBLOCK_FAIL,
  223. error,
  224. };
  225. };
  226. export function muteAccount(id, notifications) {
  227. return (dispatch, getState) => {
  228. dispatch(muteAccountRequest(id));
  229. api(getState).post(`/api/v1/accounts/${id}/mute`, { notifications }).then(response => {
  230. // Pass in entire statuses map so we can use it to filter stuff in different parts of the reducers
  231. dispatch(muteAccountSuccess(response.data, getState().get('statuses')));
  232. }).catch(error => {
  233. dispatch(muteAccountFail(id, error));
  234. });
  235. };
  236. };
  237. export function unmuteAccount(id) {
  238. return (dispatch, getState) => {
  239. dispatch(unmuteAccountRequest(id));
  240. api(getState).post(`/api/v1/accounts/${id}/unmute`).then(response => {
  241. dispatch(unmuteAccountSuccess(response.data));
  242. }).catch(error => {
  243. dispatch(unmuteAccountFail(id, error));
  244. });
  245. };
  246. };
  247. export function muteAccountRequest(id) {
  248. return {
  249. type: ACCOUNT_MUTE_REQUEST,
  250. id,
  251. };
  252. };
  253. export function muteAccountSuccess(relationship, statuses) {
  254. return {
  255. type: ACCOUNT_MUTE_SUCCESS,
  256. relationship,
  257. statuses,
  258. };
  259. };
  260. export function muteAccountFail(error) {
  261. return {
  262. type: ACCOUNT_MUTE_FAIL,
  263. error,
  264. };
  265. };
  266. export function unmuteAccountRequest(id) {
  267. return {
  268. type: ACCOUNT_UNMUTE_REQUEST,
  269. id,
  270. };
  271. };
  272. export function unmuteAccountSuccess(relationship) {
  273. return {
  274. type: ACCOUNT_UNMUTE_SUCCESS,
  275. relationship,
  276. };
  277. };
  278. export function unmuteAccountFail(error) {
  279. return {
  280. type: ACCOUNT_UNMUTE_FAIL,
  281. error,
  282. };
  283. };
  284. export function fetchFollowers(id) {
  285. return (dispatch, getState) => {
  286. dispatch(fetchFollowersRequest(id));
  287. api(getState).get(`/api/v1/accounts/${id}/followers`).then(response => {
  288. const next = getLinks(response).refs.find(link => link.rel === 'next');
  289. dispatch(importFetchedAccounts(response.data));
  290. dispatch(fetchFollowersSuccess(id, response.data, next ? next.uri : null));
  291. dispatch(fetchRelationships(response.data.map(item => item.id)));
  292. }).catch(error => {
  293. dispatch(fetchFollowersFail(id, error));
  294. });
  295. };
  296. };
  297. export function fetchFollowersRequest(id) {
  298. return {
  299. type: FOLLOWERS_FETCH_REQUEST,
  300. id,
  301. };
  302. };
  303. export function fetchFollowersSuccess(id, accounts, next) {
  304. return {
  305. type: FOLLOWERS_FETCH_SUCCESS,
  306. id,
  307. accounts,
  308. next,
  309. };
  310. };
  311. export function fetchFollowersFail(id, error) {
  312. return {
  313. type: FOLLOWERS_FETCH_FAIL,
  314. id,
  315. error,
  316. };
  317. };
  318. export function expandFollowers(id) {
  319. return (dispatch, getState) => {
  320. const url = getState().getIn(['user_lists', 'followers', id, 'next']);
  321. if (url === null) {
  322. return;
  323. }
  324. dispatch(expandFollowersRequest(id));
  325. api(getState).get(url).then(response => {
  326. const next = getLinks(response).refs.find(link => link.rel === 'next');
  327. dispatch(importFetchedAccounts(response.data));
  328. dispatch(expandFollowersSuccess(id, response.data, next ? next.uri : null));
  329. dispatch(fetchRelationships(response.data.map(item => item.id)));
  330. }).catch(error => {
  331. dispatch(expandFollowersFail(id, error));
  332. });
  333. };
  334. };
  335. export function expandFollowersRequest(id) {
  336. return {
  337. type: FOLLOWERS_EXPAND_REQUEST,
  338. id,
  339. };
  340. };
  341. export function expandFollowersSuccess(id, accounts, next) {
  342. return {
  343. type: FOLLOWERS_EXPAND_SUCCESS,
  344. id,
  345. accounts,
  346. next,
  347. };
  348. };
  349. export function expandFollowersFail(id, error) {
  350. return {
  351. type: FOLLOWERS_EXPAND_FAIL,
  352. id,
  353. error,
  354. };
  355. };
  356. export function fetchFollowing(id) {
  357. return (dispatch, getState) => {
  358. dispatch(fetchFollowingRequest(id));
  359. api(getState).get(`/api/v1/accounts/${id}/following`).then(response => {
  360. const next = getLinks(response).refs.find(link => link.rel === 'next');
  361. dispatch(importFetchedAccounts(response.data));
  362. dispatch(fetchFollowingSuccess(id, response.data, next ? next.uri : null));
  363. dispatch(fetchRelationships(response.data.map(item => item.id)));
  364. }).catch(error => {
  365. dispatch(fetchFollowingFail(id, error));
  366. });
  367. };
  368. };
  369. export function fetchFollowingRequest(id) {
  370. return {
  371. type: FOLLOWING_FETCH_REQUEST,
  372. id,
  373. };
  374. };
  375. export function fetchFollowingSuccess(id, accounts, next) {
  376. return {
  377. type: FOLLOWING_FETCH_SUCCESS,
  378. id,
  379. accounts,
  380. next,
  381. };
  382. };
  383. export function fetchFollowingFail(id, error) {
  384. return {
  385. type: FOLLOWING_FETCH_FAIL,
  386. id,
  387. error,
  388. };
  389. };
  390. export function expandFollowing(id) {
  391. return (dispatch, getState) => {
  392. const url = getState().getIn(['user_lists', 'following', id, 'next']);
  393. if (url === null) {
  394. return;
  395. }
  396. dispatch(expandFollowingRequest(id));
  397. api(getState).get(url).then(response => {
  398. const next = getLinks(response).refs.find(link => link.rel === 'next');
  399. dispatch(importFetchedAccounts(response.data));
  400. dispatch(expandFollowingSuccess(id, response.data, next ? next.uri : null));
  401. dispatch(fetchRelationships(response.data.map(item => item.id)));
  402. }).catch(error => {
  403. dispatch(expandFollowingFail(id, error));
  404. });
  405. };
  406. };
  407. export function expandFollowingRequest(id) {
  408. return {
  409. type: FOLLOWING_EXPAND_REQUEST,
  410. id,
  411. };
  412. };
  413. export function expandFollowingSuccess(id, accounts, next) {
  414. return {
  415. type: FOLLOWING_EXPAND_SUCCESS,
  416. id,
  417. accounts,
  418. next,
  419. };
  420. };
  421. export function expandFollowingFail(id, error) {
  422. return {
  423. type: FOLLOWING_EXPAND_FAIL,
  424. id,
  425. error,
  426. };
  427. };
  428. export function fetchRelationships(accountIds) {
  429. return (dispatch, getState) => {
  430. const loadedRelationships = getState().get('relationships');
  431. const newAccountIds = accountIds.filter(id => loadedRelationships.get(id, null) === null);
  432. if (newAccountIds.length === 0) {
  433. return;
  434. }
  435. dispatch(fetchRelationshipsRequest(newAccountIds));
  436. api(getState).get(`/api/v1/accounts/relationships?${newAccountIds.map(id => `id[]=${id}`).join('&')}`).then(response => {
  437. dispatch(fetchRelationshipsSuccess(response.data));
  438. }).catch(error => {
  439. dispatch(fetchRelationshipsFail(error));
  440. });
  441. };
  442. };
  443. export function fetchRelationshipsRequest(ids) {
  444. return {
  445. type: RELATIONSHIPS_FETCH_REQUEST,
  446. ids,
  447. skipLoading: true,
  448. };
  449. };
  450. export function fetchRelationshipsSuccess(relationships) {
  451. return {
  452. type: RELATIONSHIPS_FETCH_SUCCESS,
  453. relationships,
  454. skipLoading: true,
  455. };
  456. };
  457. export function fetchRelationshipsFail(error) {
  458. return {
  459. type: RELATIONSHIPS_FETCH_FAIL,
  460. error,
  461. skipLoading: true,
  462. };
  463. };
  464. export function fetchFollowRequests() {
  465. return (dispatch, getState) => {
  466. dispatch(fetchFollowRequestsRequest());
  467. api(getState).get('/api/v1/follow_requests').then(response => {
  468. const next = getLinks(response).refs.find(link => link.rel === 'next');
  469. dispatch(importFetchedAccounts(response.data));
  470. dispatch(fetchFollowRequestsSuccess(response.data, next ? next.uri : null));
  471. }).catch(error => dispatch(fetchFollowRequestsFail(error)));
  472. };
  473. };
  474. export function fetchFollowRequestsRequest() {
  475. return {
  476. type: FOLLOW_REQUESTS_FETCH_REQUEST,
  477. };
  478. };
  479. export function fetchFollowRequestsSuccess(accounts, next) {
  480. return {
  481. type: FOLLOW_REQUESTS_FETCH_SUCCESS,
  482. accounts,
  483. next,
  484. };
  485. };
  486. export function fetchFollowRequestsFail(error) {
  487. return {
  488. type: FOLLOW_REQUESTS_FETCH_FAIL,
  489. error,
  490. };
  491. };
  492. export function expandFollowRequests() {
  493. return (dispatch, getState) => {
  494. const url = getState().getIn(['user_lists', 'follow_requests', 'next']);
  495. if (url === null) {
  496. return;
  497. }
  498. dispatch(expandFollowRequestsRequest());
  499. api(getState).get(url).then(response => {
  500. const next = getLinks(response).refs.find(link => link.rel === 'next');
  501. dispatch(importFetchedAccounts(response.data));
  502. dispatch(expandFollowRequestsSuccess(response.data, next ? next.uri : null));
  503. }).catch(error => dispatch(expandFollowRequestsFail(error)));
  504. };
  505. };
  506. export function expandFollowRequestsRequest() {
  507. return {
  508. type: FOLLOW_REQUESTS_EXPAND_REQUEST,
  509. };
  510. };
  511. export function expandFollowRequestsSuccess(accounts, next) {
  512. return {
  513. type: FOLLOW_REQUESTS_EXPAND_SUCCESS,
  514. accounts,
  515. next,
  516. };
  517. };
  518. export function expandFollowRequestsFail(error) {
  519. return {
  520. type: FOLLOW_REQUESTS_EXPAND_FAIL,
  521. error,
  522. };
  523. };
  524. export function authorizeFollowRequest(id) {
  525. return (dispatch, getState) => {
  526. dispatch(authorizeFollowRequestRequest(id));
  527. api(getState)
  528. .post(`/api/v1/follow_requests/${id}/authorize`)
  529. .then(() => dispatch(authorizeFollowRequestSuccess(id)))
  530. .catch(error => dispatch(authorizeFollowRequestFail(id, error)));
  531. };
  532. };
  533. export function authorizeFollowRequestRequest(id) {
  534. return {
  535. type: FOLLOW_REQUEST_AUTHORIZE_REQUEST,
  536. id,
  537. };
  538. };
  539. export function authorizeFollowRequestSuccess(id) {
  540. return {
  541. type: FOLLOW_REQUEST_AUTHORIZE_SUCCESS,
  542. id,
  543. };
  544. };
  545. export function authorizeFollowRequestFail(id, error) {
  546. return {
  547. type: FOLLOW_REQUEST_AUTHORIZE_FAIL,
  548. id,
  549. error,
  550. };
  551. };
  552. export function rejectFollowRequest(id) {
  553. return (dispatch, getState) => {
  554. dispatch(rejectFollowRequestRequest(id));
  555. api(getState)
  556. .post(`/api/v1/follow_requests/${id}/reject`)
  557. .then(() => dispatch(rejectFollowRequestSuccess(id)))
  558. .catch(error => dispatch(rejectFollowRequestFail(id, error)));
  559. };
  560. };
  561. export function rejectFollowRequestRequest(id) {
  562. return {
  563. type: FOLLOW_REQUEST_REJECT_REQUEST,
  564. id,
  565. };
  566. };
  567. export function rejectFollowRequestSuccess(id) {
  568. return {
  569. type: FOLLOW_REQUEST_REJECT_SUCCESS,
  570. id,
  571. };
  572. };
  573. export function rejectFollowRequestFail(id, error) {
  574. return {
  575. type: FOLLOW_REQUEST_REJECT_FAIL,
  576. id,
  577. error,
  578. };
  579. };