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.

290 lines
7.1 KiB

  1. import api from '../api'
  2. import axios from 'axios';
  3. import Immutable from 'immutable';
  4. export const ACCOUNT_SET_SELF = 'ACCOUNT_SET_SELF';
  5. export const ACCOUNT_FETCH_REQUEST = 'ACCOUNT_FETCH_REQUEST';
  6. export const ACCOUNT_FETCH_SUCCESS = 'ACCOUNT_FETCH_SUCCESS';
  7. export const ACCOUNT_FETCH_FAIL = 'ACCOUNT_FETCH_FAIL';
  8. export const ACCOUNT_FOLLOW_REQUEST = 'ACCOUNT_FOLLOW_REQUEST';
  9. export const ACCOUNT_FOLLOW_SUCCESS = 'ACCOUNT_FOLLOW_SUCCESS';
  10. export const ACCOUNT_FOLLOW_FAIL = 'ACCOUNT_FOLLOW_FAIL';
  11. export const ACCOUNT_UNFOLLOW_REQUEST = 'ACCOUNT_UNFOLLOW_REQUEST';
  12. export const ACCOUNT_UNFOLLOW_SUCCESS = 'ACCOUNT_UNFOLLOW_SUCCESS';
  13. export const ACCOUNT_UNFOLLOW_FAIL = 'ACCOUNT_UNFOLLOW_FAIL';
  14. export const ACCOUNT_BLOCK_REQUEST = 'ACCOUNT_BLOCK_REQUEST';
  15. export const ACCOUNT_BLOCK_SUCCESS = 'ACCOUNT_BLOCK_SUCCESS';
  16. export const ACCOUNT_BLOCK_FAIL = 'ACCOUNT_BLOCK_FAIL';
  17. export const ACCOUNT_UNBLOCK_REQUEST = 'ACCOUNT_UNBLOCK_REQUEST';
  18. export const ACCOUNT_UNBLOCK_SUCCESS = 'ACCOUNT_UNBLOCK_SUCCESS';
  19. export const ACCOUNT_UNBLOCK_FAIL = 'ACCOUNT_UNBLOCK_FAIL';
  20. export const ACCOUNT_TIMELINE_FETCH_REQUEST = 'ACCOUNT_TIMELINE_FETCH_REQUEST';
  21. export const ACCOUNT_TIMELINE_FETCH_SUCCESS = 'ACCOUNT_TIMELINE_FETCH_SUCCESS';
  22. export const ACCOUNT_TIMELINE_FETCH_FAIL = 'ACCOUNT_TIMELINE_FETCH_FAIL';
  23. export const ACCOUNT_TIMELINE_EXPAND_REQUEST = 'ACCOUNT_TIMELINE_EXPAND_REQUEST';
  24. export const ACCOUNT_TIMELINE_EXPAND_SUCCESS = 'ACCOUNT_TIMELINE_EXPAND_SUCCESS';
  25. export const ACCOUNT_TIMELINE_EXPAND_FAIL = 'ACCOUNT_TIMELINE_EXPAND_FAIL';
  26. export function setAccountSelf(account) {
  27. return {
  28. type: ACCOUNT_SET_SELF,
  29. account: account
  30. };
  31. };
  32. export function fetchAccount(id) {
  33. return (dispatch, getState) => {
  34. const boundApi = api(getState);
  35. dispatch(fetchAccountRequest(id));
  36. axios.all([boundApi.get(`/api/v1/accounts/${id}`), boundApi.get(`/api/v1/accounts/relationships?id=${id}`)]).then(values => {
  37. dispatch(fetchAccountSuccess(values[0].data, values[1].data[0]));
  38. }).catch(error => {
  39. dispatch(fetchAccountFail(id, error));
  40. });
  41. };
  42. };
  43. export function fetchAccountTimeline(id) {
  44. return (dispatch, getState) => {
  45. dispatch(fetchAccountTimelineRequest(id));
  46. const ids = getState().getIn(['timelines', 'accounts_timelines', id], Immutable.List());
  47. const newestId = ids.size > 0 ? ids.first() : null;
  48. let params = '';
  49. if (newestId !== null) {
  50. params = `?since_id=${newestId}`;
  51. }
  52. api(getState).get(`/api/v1/accounts/${id}/statuses${params}`).then(response => {
  53. dispatch(fetchAccountTimelineSuccess(id, response.data));
  54. }).catch(error => {
  55. dispatch(fetchAccountTimelineFail(id, error));
  56. });
  57. };
  58. };
  59. export function expandAccountTimeline(id) {
  60. return (dispatch, getState) => {
  61. const lastId = getState().getIn(['timelines', 'accounts_timelines', id], Immutable.List()).last();
  62. dispatch(expandAccountTimelineRequest(id));
  63. api(getState).get(`/api/v1/accounts/${id}/statuses?max_id=${lastId}`).then(response => {
  64. dispatch(expandAccountTimelineSuccess(id, response.data));
  65. }).catch(error => {
  66. dispatch(expandAccountTimelineFail(id, error));
  67. });
  68. };
  69. };
  70. export function fetchAccountRequest(id) {
  71. return {
  72. type: ACCOUNT_FETCH_REQUEST,
  73. id: id
  74. };
  75. };
  76. export function fetchAccountSuccess(account, relationship) {
  77. return {
  78. type: ACCOUNT_FETCH_SUCCESS,
  79. account: account,
  80. relationship: relationship
  81. };
  82. };
  83. export function fetchAccountFail(id, error) {
  84. return {
  85. type: ACCOUNT_FETCH_FAIL,
  86. id: id,
  87. error: error
  88. };
  89. };
  90. export function followAccount(id) {
  91. return (dispatch, getState) => {
  92. dispatch(followAccountRequest(id));
  93. api(getState).post(`/api/v1/accounts/${id}/follow`).then(response => {
  94. dispatch(followAccountSuccess(response.data));
  95. }).catch(error => {
  96. dispatch(followAccountFail(error));
  97. });
  98. };
  99. };
  100. export function unfollowAccount(id) {
  101. return (dispatch, getState) => {
  102. dispatch(unfollowAccountRequest(id));
  103. api(getState).post(`/api/v1/accounts/${id}/unfollow`).then(response => {
  104. dispatch(unfollowAccountSuccess(response.data));
  105. }).catch(error => {
  106. dispatch(unfollowAccountFail(error));
  107. });
  108. }
  109. };
  110. export function followAccountRequest(id) {
  111. return {
  112. type: ACCOUNT_FOLLOW_REQUEST,
  113. id: id
  114. };
  115. };
  116. export function followAccountSuccess(relationship) {
  117. return {
  118. type: ACCOUNT_FOLLOW_SUCCESS,
  119. relationship: relationship
  120. };
  121. };
  122. export function followAccountFail(error) {
  123. return {
  124. type: ACCOUNT_FOLLOW_FAIL,
  125. error: error
  126. };
  127. };
  128. export function unfollowAccountRequest(id) {
  129. return {
  130. type: ACCOUNT_UNFOLLOW_REQUEST,
  131. id: id
  132. };
  133. };
  134. export function unfollowAccountSuccess(relationship) {
  135. return {
  136. type: ACCOUNT_UNFOLLOW_SUCCESS,
  137. relationship: relationship
  138. };
  139. };
  140. export function unfollowAccountFail(error) {
  141. return {
  142. type: ACCOUNT_UNFOLLOW_FAIL,
  143. error: error
  144. };
  145. };
  146. export function fetchAccountTimelineRequest(id) {
  147. return {
  148. type: ACCOUNT_TIMELINE_FETCH_REQUEST,
  149. id: id
  150. };
  151. };
  152. export function fetchAccountTimelineSuccess(id, statuses) {
  153. return {
  154. type: ACCOUNT_TIMELINE_FETCH_SUCCESS,
  155. id: id,
  156. statuses: statuses
  157. };
  158. };
  159. export function fetchAccountTimelineFail(id, error) {
  160. return {
  161. type: ACCOUNT_TIMELINE_FETCH_FAIL,
  162. id: id,
  163. error: error
  164. };
  165. };
  166. export function expandAccountTimelineRequest(id) {
  167. return {
  168. type: ACCOUNT_TIMELINE_EXPAND_REQUEST,
  169. id: id
  170. };
  171. };
  172. export function expandAccountTimelineSuccess(id, statuses) {
  173. return {
  174. type: ACCOUNT_TIMELINE_EXPAND_SUCCESS,
  175. id: id,
  176. statuses: statuses
  177. };
  178. };
  179. export function expandAccountTimelineFail(id, error) {
  180. return {
  181. type: ACCOUNT_TIMELINE_EXPAND_FAIL,
  182. id: id,
  183. error: error
  184. };
  185. };
  186. export function blockAccount(id) {
  187. return (dispatch, getState) => {
  188. dispatch(blockAccountRequest(id));
  189. api(getState).post(`/api/v1/accounts/${id}/block`).then(response => {
  190. dispatch(blockAccountSuccess(response.data));
  191. }).catch(error => {
  192. dispatch(blockAccountFail(id, error));
  193. });
  194. };
  195. };
  196. export function unblockAccount(id) {
  197. return (dispatch, getState) => {
  198. dispatch(unblockAccountRequest(id));
  199. api(getState).post(`/api/v1/accounts/${id}/unblock`).then(response => {
  200. dispatch(unblockAccountSuccess(response.data));
  201. }).catch(error => {
  202. dispatch(unblockAccountFail(id, error));
  203. });
  204. };
  205. };
  206. export function blockAccountRequest(id) {
  207. return {
  208. type: ACCOUNT_BLOCK_REQUEST,
  209. id: id
  210. };
  211. };
  212. export function blockAccountSuccess(relationship) {
  213. return {
  214. type: ACCOUNT_BLOCK_SUCCESS,
  215. relationship: relationship
  216. };
  217. };
  218. export function blockAccountFail(error) {
  219. return {
  220. type: ACCOUNT_BLOCK_FAIL,
  221. error: error
  222. };
  223. };
  224. export function unblockAccountRequest(id) {
  225. return {
  226. type: ACCOUNT_UNBLOCK_REQUEST,
  227. id: id
  228. };
  229. };
  230. export function unblockAccountSuccess(relationship) {
  231. return {
  232. type: ACCOUNT_UNBLOCK_SUCCESS,
  233. relationship: relationship
  234. };
  235. };
  236. export function unblockAccountFail(error) {
  237. return {
  238. type: ACCOUNT_UNBLOCK_FAIL,
  239. error: error
  240. };
  241. };