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.

498 lines
12 KiB

  1. import api, { getLinks } from '../api'
  2. import Immutable from 'immutable';
  3. export const ACCOUNT_SET_SELF = 'ACCOUNT_SET_SELF';
  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_TIMELINE_FETCH_REQUEST = 'ACCOUNT_TIMELINE_FETCH_REQUEST';
  20. export const ACCOUNT_TIMELINE_FETCH_SUCCESS = 'ACCOUNT_TIMELINE_FETCH_SUCCESS';
  21. export const ACCOUNT_TIMELINE_FETCH_FAIL = 'ACCOUNT_TIMELINE_FETCH_FAIL';
  22. export const ACCOUNT_TIMELINE_EXPAND_REQUEST = 'ACCOUNT_TIMELINE_EXPAND_REQUEST';
  23. export const ACCOUNT_TIMELINE_EXPAND_SUCCESS = 'ACCOUNT_TIMELINE_EXPAND_SUCCESS';
  24. export const ACCOUNT_TIMELINE_EXPAND_FAIL = 'ACCOUNT_TIMELINE_EXPAND_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 function setAccountSelf(account) {
  41. return {
  42. type: ACCOUNT_SET_SELF,
  43. account
  44. };
  45. };
  46. export function fetchAccount(id) {
  47. return (dispatch, getState) => {
  48. dispatch(fetchAccountRequest(id));
  49. api(getState).get(`/api/v1/accounts/${id}`).then(response => {
  50. dispatch(fetchAccountSuccess(response.data));
  51. dispatch(fetchRelationships([id]));
  52. }).catch(error => {
  53. dispatch(fetchAccountFail(id, error));
  54. });
  55. };
  56. };
  57. export function fetchAccountTimeline(id, replace = false) {
  58. return (dispatch, getState) => {
  59. dispatch(fetchAccountTimelineRequest(id));
  60. const ids = getState().getIn(['timelines', 'accounts_timelines', id], Immutable.List());
  61. const newestId = ids.size > 0 ? ids.first() : null;
  62. let params = '';
  63. if (newestId !== null && !replace) {
  64. params = `?since_id=${newestId}`;
  65. }
  66. api(getState).get(`/api/v1/accounts/${id}/statuses${params}`).then(response => {
  67. dispatch(fetchAccountTimelineSuccess(id, response.data, replace));
  68. }).catch(error => {
  69. dispatch(fetchAccountTimelineFail(id, error));
  70. });
  71. };
  72. };
  73. export function expandAccountTimeline(id) {
  74. return (dispatch, getState) => {
  75. const lastId = getState().getIn(['timelines', 'accounts_timelines', id], Immutable.List()).last();
  76. dispatch(expandAccountTimelineRequest(id));
  77. api(getState).get(`/api/v1/accounts/${id}/statuses?max_id=${lastId}`).then(response => {
  78. dispatch(expandAccountTimelineSuccess(id, response.data));
  79. }).catch(error => {
  80. dispatch(expandAccountTimelineFail(id, error));
  81. });
  82. };
  83. };
  84. export function fetchAccountRequest(id) {
  85. return {
  86. type: ACCOUNT_FETCH_REQUEST,
  87. id
  88. };
  89. };
  90. export function fetchAccountSuccess(account) {
  91. return {
  92. type: ACCOUNT_FETCH_SUCCESS,
  93. account
  94. };
  95. };
  96. export function fetchAccountFail(id, error) {
  97. return {
  98. type: ACCOUNT_FETCH_FAIL,
  99. id,
  100. error
  101. };
  102. };
  103. export function followAccount(id) {
  104. return (dispatch, getState) => {
  105. dispatch(followAccountRequest(id));
  106. api(getState).post(`/api/v1/accounts/${id}/follow`).then(response => {
  107. dispatch(followAccountSuccess(response.data));
  108. }).catch(error => {
  109. dispatch(followAccountFail(error));
  110. });
  111. };
  112. };
  113. export function unfollowAccount(id) {
  114. return (dispatch, getState) => {
  115. dispatch(unfollowAccountRequest(id));
  116. api(getState).post(`/api/v1/accounts/${id}/unfollow`).then(response => {
  117. dispatch(unfollowAccountSuccess(response.data));
  118. }).catch(error => {
  119. dispatch(unfollowAccountFail(error));
  120. });
  121. }
  122. };
  123. export function followAccountRequest(id) {
  124. return {
  125. type: ACCOUNT_FOLLOW_REQUEST,
  126. id
  127. };
  128. };
  129. export function followAccountSuccess(relationship) {
  130. return {
  131. type: ACCOUNT_FOLLOW_SUCCESS,
  132. relationship
  133. };
  134. };
  135. export function followAccountFail(error) {
  136. return {
  137. type: ACCOUNT_FOLLOW_FAIL,
  138. error
  139. };
  140. };
  141. export function unfollowAccountRequest(id) {
  142. return {
  143. type: ACCOUNT_UNFOLLOW_REQUEST,
  144. id
  145. };
  146. };
  147. export function unfollowAccountSuccess(relationship) {
  148. return {
  149. type: ACCOUNT_UNFOLLOW_SUCCESS,
  150. relationship
  151. };
  152. };
  153. export function unfollowAccountFail(error) {
  154. return {
  155. type: ACCOUNT_UNFOLLOW_FAIL,
  156. error
  157. };
  158. };
  159. export function fetchAccountTimelineRequest(id) {
  160. return {
  161. type: ACCOUNT_TIMELINE_FETCH_REQUEST,
  162. id
  163. };
  164. };
  165. export function fetchAccountTimelineSuccess(id, statuses, replace) {
  166. return {
  167. type: ACCOUNT_TIMELINE_FETCH_SUCCESS,
  168. id,
  169. statuses,
  170. replace
  171. };
  172. };
  173. export function fetchAccountTimelineFail(id, error) {
  174. return {
  175. type: ACCOUNT_TIMELINE_FETCH_FAIL,
  176. id,
  177. error
  178. };
  179. };
  180. export function expandAccountTimelineRequest(id) {
  181. return {
  182. type: ACCOUNT_TIMELINE_EXPAND_REQUEST,
  183. id
  184. };
  185. };
  186. export function expandAccountTimelineSuccess(id, statuses) {
  187. return {
  188. type: ACCOUNT_TIMELINE_EXPAND_SUCCESS,
  189. id,
  190. statuses
  191. };
  192. };
  193. export function expandAccountTimelineFail(id, error) {
  194. return {
  195. type: ACCOUNT_TIMELINE_EXPAND_FAIL,
  196. id,
  197. error
  198. };
  199. };
  200. export function blockAccount(id) {
  201. return (dispatch, getState) => {
  202. dispatch(blockAccountRequest(id));
  203. api(getState).post(`/api/v1/accounts/${id}/block`).then(response => {
  204. dispatch(blockAccountSuccess(response.data));
  205. }).catch(error => {
  206. dispatch(blockAccountFail(id, error));
  207. });
  208. };
  209. };
  210. export function unblockAccount(id) {
  211. return (dispatch, getState) => {
  212. dispatch(unblockAccountRequest(id));
  213. api(getState).post(`/api/v1/accounts/${id}/unblock`).then(response => {
  214. dispatch(unblockAccountSuccess(response.data));
  215. }).catch(error => {
  216. dispatch(unblockAccountFail(id, error));
  217. });
  218. };
  219. };
  220. export function blockAccountRequest(id) {
  221. return {
  222. type: ACCOUNT_BLOCK_REQUEST,
  223. id
  224. };
  225. };
  226. export function blockAccountSuccess(relationship) {
  227. return {
  228. type: ACCOUNT_BLOCK_SUCCESS,
  229. relationship
  230. };
  231. };
  232. export function blockAccountFail(error) {
  233. return {
  234. type: ACCOUNT_BLOCK_FAIL,
  235. error
  236. };
  237. };
  238. export function unblockAccountRequest(id) {
  239. return {
  240. type: ACCOUNT_UNBLOCK_REQUEST,
  241. id
  242. };
  243. };
  244. export function unblockAccountSuccess(relationship) {
  245. return {
  246. type: ACCOUNT_UNBLOCK_SUCCESS,
  247. relationship
  248. };
  249. };
  250. export function unblockAccountFail(error) {
  251. return {
  252. type: ACCOUNT_UNBLOCK_FAIL,
  253. error
  254. };
  255. };
  256. export function fetchFollowers(id) {
  257. return (dispatch, getState) => {
  258. dispatch(fetchFollowersRequest(id));
  259. api(getState).get(`/api/v1/accounts/${id}/followers`).then(response => {
  260. const prev = getLinks(response).refs.find(link => link.rel === 'prev').uri;
  261. dispatch(fetchFollowersSuccess(id, response.data, prev));
  262. dispatch(fetchRelationships(response.data.map(item => item.id)));
  263. }).catch(error => {
  264. dispatch(fetchFollowersFail(id, error));
  265. });
  266. };
  267. };
  268. export function fetchFollowersRequest(id) {
  269. return {
  270. type: FOLLOWERS_FETCH_REQUEST,
  271. id
  272. };
  273. };
  274. export function fetchFollowersSuccess(id, accounts, prev) {
  275. return {
  276. type: FOLLOWERS_FETCH_SUCCESS,
  277. id,
  278. accounts,
  279. prev
  280. };
  281. };
  282. export function fetchFollowersFail(id, error) {
  283. return {
  284. type: FOLLOWERS_FETCH_FAIL,
  285. id,
  286. error
  287. };
  288. };
  289. export function expandFollowers(id) {
  290. return (dispatch, getState) => {
  291. const url = getState().getIn(['user_lists', 'followers', id, 'prev']);
  292. dispatch(expandFollowersRequest(id));
  293. api(getState).get(url).then(response => {
  294. const prev = getLinks(response).refs.find(link => link.rel === 'prev').uri;
  295. dispatch(expandFollowersSuccess(id, response.data, prev));
  296. dispatch(fetchRelationships(response.data.map(item => item.id)));
  297. }).catch(error => {
  298. dispatch(expandFollowersFail(id, error));
  299. });
  300. };
  301. };
  302. export function expandFollowersRequest(id) {
  303. return {
  304. type: FOLLOWERS_EXPAND_REQUEST,
  305. id
  306. };
  307. };
  308. export function expandFollowersSuccess(id, accounts, prev) {
  309. return {
  310. type: FOLLOWERS_EXPAND_SUCCESS,
  311. id,
  312. accounts,
  313. prev
  314. };
  315. };
  316. export function expandFollowersFail(id, error) {
  317. return {
  318. type: FOLLOWERS_EXPAND_FAIL,
  319. id,
  320. error
  321. };
  322. };
  323. export function fetchFollowing(id) {
  324. return (dispatch, getState) => {
  325. dispatch(fetchFollowingRequest(id));
  326. api(getState).get(`/api/v1/accounts/${id}/following`).then(response => {
  327. dispatch(fetchFollowingSuccess(id, response.data));
  328. dispatch(fetchRelationships(response.data.map(item => item.id)));
  329. }).catch(error => {
  330. dispatch(fetchFollowingFail(id, error));
  331. });
  332. };
  333. };
  334. export function fetchFollowingRequest(id) {
  335. return {
  336. type: FOLLOWING_FETCH_REQUEST,
  337. id
  338. };
  339. };
  340. export function fetchFollowingSuccess(id, accounts) {
  341. return {
  342. type: FOLLOWING_FETCH_SUCCESS,
  343. id,
  344. accounts
  345. };
  346. };
  347. export function fetchFollowingFail(id, error) {
  348. return {
  349. type: FOLLOWING_FETCH_FAIL,
  350. id,
  351. error
  352. };
  353. };
  354. export function expandFollowing(id) {
  355. return (dispatch, getState) => {
  356. const url = getState().getIn(['user_lists', 'following', id, 'prev']);
  357. dispatch(expandFollowingRequest(id));
  358. api(getState).get(url).then(response => {
  359. const prev = getLinks(response).refs.find(link => link.rel === 'prev').uri;
  360. dispatch(expandFollowingSuccess(id, response.data, prev));
  361. dispatch(fetchRelationships(response.data.map(item => item.id)));
  362. }).catch(error => {
  363. dispatch(expandFollowingFail(id, error));
  364. });
  365. };
  366. };
  367. export function expandFollowingRequest(id) {
  368. return {
  369. type: FOLLOWING_EXPAND_REQUEST,
  370. id
  371. };
  372. };
  373. export function expandFollowingSuccess(id, accounts, prev) {
  374. return {
  375. type: FOLLOWING_EXPAND_SUCCESS,
  376. id,
  377. accounts,
  378. prev
  379. };
  380. };
  381. export function expandFollowingFail(id, error) {
  382. return {
  383. type: FOLLOWING_EXPAND_FAIL,
  384. id,
  385. error
  386. };
  387. };
  388. export function fetchRelationships(account_ids) {
  389. return (dispatch, getState) => {
  390. dispatch(fetchRelationshipsRequest(account_ids));
  391. api(getState).get(`/api/v1/accounts/relationships?${account_ids.map(id => `id[]=${id}`).join('&')}`).then(response => {
  392. dispatch(fetchRelationshipsSuccess(response.data));
  393. }).catch(error => {
  394. dispatch(fetchRelationshipsFail(error));
  395. });
  396. };
  397. };
  398. export function fetchRelationshipsRequest(ids) {
  399. return {
  400. type: RELATIONSHIPS_FETCH_REQUEST,
  401. ids
  402. };
  403. };
  404. export function fetchRelationshipsSuccess(relationships) {
  405. return {
  406. type: RELATIONSHIPS_FETCH_SUCCESS,
  407. relationships
  408. };
  409. };
  410. export function fetchRelationshipsFail(error) {
  411. return {
  412. type: RELATIONSHIPS_FETCH_FAIL,
  413. error
  414. };
  415. };