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.

405 lines
9.9 KiB

  1. import api 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 FOLLOWING_FETCH_REQUEST = 'FOLLOWING_FETCH_REQUEST';
  29. export const FOLLOWING_FETCH_SUCCESS = 'FOLLOWING_FETCH_SUCCESS';
  30. export const FOLLOWING_FETCH_FAIL = 'FOLLOWING_FETCH_FAIL';
  31. export const RELATIONSHIPS_FETCH_REQUEST = 'RELATIONSHIPS_FETCH_REQUEST';
  32. export const RELATIONSHIPS_FETCH_SUCCESS = 'RELATIONSHIPS_FETCH_SUCCESS';
  33. export const RELATIONSHIPS_FETCH_FAIL = 'RELATIONSHIPS_FETCH_FAIL';
  34. export function setAccountSelf(account) {
  35. return {
  36. type: ACCOUNT_SET_SELF,
  37. account: account
  38. };
  39. };
  40. export function fetchAccount(id) {
  41. return (dispatch, getState) => {
  42. dispatch(fetchAccountRequest(id));
  43. api(getState).get(`/api/v1/accounts/${id}`).then(response => {
  44. dispatch(fetchAccountSuccess(response.data));
  45. dispatch(fetchRelationships([id]));
  46. }).catch(error => {
  47. dispatch(fetchAccountFail(id, error));
  48. });
  49. };
  50. };
  51. export function fetchAccountTimeline(id, replace = false) {
  52. return (dispatch, getState) => {
  53. dispatch(fetchAccountTimelineRequest(id));
  54. const ids = getState().getIn(['timelines', 'accounts_timelines', id], Immutable.List());
  55. const newestId = ids.size > 0 ? ids.first() : null;
  56. let params = '';
  57. if (newestId !== null && !replace) {
  58. params = `?since_id=${newestId}`;
  59. }
  60. api(getState).get(`/api/v1/accounts/${id}/statuses${params}`).then(response => {
  61. dispatch(fetchAccountTimelineSuccess(id, response.data, replace));
  62. }).catch(error => {
  63. dispatch(fetchAccountTimelineFail(id, error));
  64. });
  65. };
  66. };
  67. export function expandAccountTimeline(id) {
  68. return (dispatch, getState) => {
  69. const lastId = getState().getIn(['timelines', 'accounts_timelines', id], Immutable.List()).last();
  70. dispatch(expandAccountTimelineRequest(id));
  71. api(getState).get(`/api/v1/accounts/${id}/statuses?max_id=${lastId}`).then(response => {
  72. dispatch(expandAccountTimelineSuccess(id, response.data));
  73. }).catch(error => {
  74. dispatch(expandAccountTimelineFail(id, error));
  75. });
  76. };
  77. };
  78. export function fetchAccountRequest(id) {
  79. return {
  80. type: ACCOUNT_FETCH_REQUEST,
  81. id: id
  82. };
  83. };
  84. export function fetchAccountSuccess(account) {
  85. return {
  86. type: ACCOUNT_FETCH_SUCCESS,
  87. account: account
  88. };
  89. };
  90. export function fetchAccountFail(id, error) {
  91. return {
  92. type: ACCOUNT_FETCH_FAIL,
  93. id: id,
  94. error: error
  95. };
  96. };
  97. export function followAccount(id) {
  98. return (dispatch, getState) => {
  99. dispatch(followAccountRequest(id));
  100. api(getState).post(`/api/v1/accounts/${id}/follow`).then(response => {
  101. dispatch(followAccountSuccess(response.data));
  102. }).catch(error => {
  103. dispatch(followAccountFail(error));
  104. });
  105. };
  106. };
  107. export function unfollowAccount(id) {
  108. return (dispatch, getState) => {
  109. dispatch(unfollowAccountRequest(id));
  110. api(getState).post(`/api/v1/accounts/${id}/unfollow`).then(response => {
  111. dispatch(unfollowAccountSuccess(response.data));
  112. }).catch(error => {
  113. dispatch(unfollowAccountFail(error));
  114. });
  115. }
  116. };
  117. export function followAccountRequest(id) {
  118. return {
  119. type: ACCOUNT_FOLLOW_REQUEST,
  120. id: id
  121. };
  122. };
  123. export function followAccountSuccess(relationship) {
  124. return {
  125. type: ACCOUNT_FOLLOW_SUCCESS,
  126. relationship: relationship
  127. };
  128. };
  129. export function followAccountFail(error) {
  130. return {
  131. type: ACCOUNT_FOLLOW_FAIL,
  132. error: error
  133. };
  134. };
  135. export function unfollowAccountRequest(id) {
  136. return {
  137. type: ACCOUNT_UNFOLLOW_REQUEST,
  138. id: id
  139. };
  140. };
  141. export function unfollowAccountSuccess(relationship) {
  142. return {
  143. type: ACCOUNT_UNFOLLOW_SUCCESS,
  144. relationship: relationship
  145. };
  146. };
  147. export function unfollowAccountFail(error) {
  148. return {
  149. type: ACCOUNT_UNFOLLOW_FAIL,
  150. error: error
  151. };
  152. };
  153. export function fetchAccountTimelineRequest(id) {
  154. return {
  155. type: ACCOUNT_TIMELINE_FETCH_REQUEST,
  156. id: id
  157. };
  158. };
  159. export function fetchAccountTimelineSuccess(id, statuses, replace) {
  160. return {
  161. type: ACCOUNT_TIMELINE_FETCH_SUCCESS,
  162. id: id,
  163. statuses: statuses,
  164. replace: replace
  165. };
  166. };
  167. export function fetchAccountTimelineFail(id, error) {
  168. return {
  169. type: ACCOUNT_TIMELINE_FETCH_FAIL,
  170. id: id,
  171. error: error
  172. };
  173. };
  174. export function expandAccountTimelineRequest(id) {
  175. return {
  176. type: ACCOUNT_TIMELINE_EXPAND_REQUEST,
  177. id: id
  178. };
  179. };
  180. export function expandAccountTimelineSuccess(id, statuses) {
  181. return {
  182. type: ACCOUNT_TIMELINE_EXPAND_SUCCESS,
  183. id: id,
  184. statuses: statuses
  185. };
  186. };
  187. export function expandAccountTimelineFail(id, error) {
  188. return {
  189. type: ACCOUNT_TIMELINE_EXPAND_FAIL,
  190. id: id,
  191. error: error
  192. };
  193. };
  194. export function blockAccount(id) {
  195. return (dispatch, getState) => {
  196. dispatch(blockAccountRequest(id));
  197. api(getState).post(`/api/v1/accounts/${id}/block`).then(response => {
  198. dispatch(blockAccountSuccess(response.data));
  199. }).catch(error => {
  200. dispatch(blockAccountFail(id, error));
  201. });
  202. };
  203. };
  204. export function unblockAccount(id) {
  205. return (dispatch, getState) => {
  206. dispatch(unblockAccountRequest(id));
  207. api(getState).post(`/api/v1/accounts/${id}/unblock`).then(response => {
  208. dispatch(unblockAccountSuccess(response.data));
  209. }).catch(error => {
  210. dispatch(unblockAccountFail(id, error));
  211. });
  212. };
  213. };
  214. export function blockAccountRequest(id) {
  215. return {
  216. type: ACCOUNT_BLOCK_REQUEST,
  217. id: id
  218. };
  219. };
  220. export function blockAccountSuccess(relationship) {
  221. return {
  222. type: ACCOUNT_BLOCK_SUCCESS,
  223. relationship: relationship
  224. };
  225. };
  226. export function blockAccountFail(error) {
  227. return {
  228. type: ACCOUNT_BLOCK_FAIL,
  229. error: error
  230. };
  231. };
  232. export function unblockAccountRequest(id) {
  233. return {
  234. type: ACCOUNT_UNBLOCK_REQUEST,
  235. id: id
  236. };
  237. };
  238. export function unblockAccountSuccess(relationship) {
  239. return {
  240. type: ACCOUNT_UNBLOCK_SUCCESS,
  241. relationship: relationship
  242. };
  243. };
  244. export function unblockAccountFail(error) {
  245. return {
  246. type: ACCOUNT_UNBLOCK_FAIL,
  247. error: error
  248. };
  249. };
  250. export function fetchFollowers(id) {
  251. return (dispatch, getState) => {
  252. dispatch(fetchFollowersRequest(id));
  253. api(getState).get(`/api/v1/accounts/${id}/followers`).then(response => {
  254. dispatch(fetchFollowersSuccess(id, response.data));
  255. dispatch(fetchRelationships(response.data.map(item => item.id)));
  256. }).catch(error => {
  257. dispatch(fetchFollowersFail(id, error));
  258. });
  259. };
  260. };
  261. export function fetchFollowersRequest(id) {
  262. return {
  263. type: FOLLOWERS_FETCH_REQUEST,
  264. id: id
  265. };
  266. };
  267. export function fetchFollowersSuccess(id, accounts) {
  268. return {
  269. type: FOLLOWERS_FETCH_SUCCESS,
  270. id: id,
  271. accounts: accounts
  272. };
  273. };
  274. export function fetchFollowersFail(id, error) {
  275. return {
  276. type: FOLLOWERS_FETCH_FAIL,
  277. id: id,
  278. error: error
  279. };
  280. };
  281. export function fetchFollowing(id) {
  282. return (dispatch, getState) => {
  283. dispatch(fetchFollowingRequest(id));
  284. api(getState).get(`/api/v1/accounts/${id}/following`).then(response => {
  285. dispatch(fetchFollowingSuccess(id, response.data));
  286. dispatch(fetchRelationships(response.data.map(item => item.id)));
  287. }).catch(error => {
  288. dispatch(fetchFollowingFail(id, error));
  289. });
  290. };
  291. };
  292. export function fetchFollowingRequest(id) {
  293. return {
  294. type: FOLLOWING_FETCH_REQUEST,
  295. id: id
  296. };
  297. };
  298. export function fetchFollowingSuccess(id, accounts) {
  299. return {
  300. type: FOLLOWING_FETCH_SUCCESS,
  301. id: id,
  302. accounts: accounts
  303. };
  304. };
  305. export function fetchFollowingFail(id, error) {
  306. return {
  307. type: FOLLOWING_FETCH_FAIL,
  308. id: id,
  309. error: error
  310. };
  311. };
  312. export function fetchRelationships(account_ids) {
  313. return (dispatch, getState) => {
  314. dispatch(fetchRelationshipsRequest(account_ids));
  315. api(getState).get(`/api/v1/accounts/relationships?${account_ids.map(id => `id[]=${id}`).join('&')}`).then(response => {
  316. dispatch(fetchRelationshipsSuccess(response.data));
  317. }).catch(error => {
  318. dispatch(fetchRelationshipsFail(error));
  319. });
  320. };
  321. };
  322. export function fetchRelationshipsRequest(ids) {
  323. return {
  324. type: RELATIONSHIPS_FETCH_REQUEST,
  325. ids: ids
  326. };
  327. };
  328. export function fetchRelationshipsSuccess(relationships) {
  329. return {
  330. type: RELATIONSHIPS_FETCH_SUCCESS,
  331. relationships: relationships
  332. };
  333. };
  334. export function fetchRelationshipsFail(error) {
  335. return {
  336. type: RELATIONSHIPS_FETCH_FAIL,
  337. error: error
  338. };
  339. };