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.

281 lines
6.8 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. api(getState).get(`/api/v1/accounts/${id}/statuses`).then(response => {
  47. dispatch(fetchAccountTimelineSuccess(id, response.data));
  48. }).catch(error => {
  49. dispatch(fetchAccountTimelineFail(id, error));
  50. });
  51. };
  52. };
  53. export function expandAccountTimeline(id) {
  54. return (dispatch, getState) => {
  55. const lastId = getState().getIn(['timelines', 'accounts_timelines', id], Immutable.List()).last();
  56. dispatch(expandAccountTimelineRequest(id));
  57. api(getState).get(`/api/v1/accounts/${id}/statuses?max_id=${lastId}`).then(response => {
  58. dispatch(expandAccountTimelineSuccess(id, response.data));
  59. }).catch(error => {
  60. dispatch(expandAccountTimelineFail(id, error));
  61. });
  62. };
  63. };
  64. export function fetchAccountRequest(id) {
  65. return {
  66. type: ACCOUNT_FETCH_REQUEST,
  67. id: id
  68. };
  69. };
  70. export function fetchAccountSuccess(account, relationship) {
  71. return {
  72. type: ACCOUNT_FETCH_SUCCESS,
  73. account: account,
  74. relationship: relationship
  75. };
  76. };
  77. export function fetchAccountFail(id, error) {
  78. return {
  79. type: ACCOUNT_FETCH_FAIL,
  80. id: id,
  81. error: error
  82. };
  83. };
  84. export function followAccount(id) {
  85. return (dispatch, getState) => {
  86. dispatch(followAccountRequest(id));
  87. api(getState).post(`/api/v1/accounts/${id}/follow`).then(response => {
  88. dispatch(followAccountSuccess(response.data));
  89. }).catch(error => {
  90. dispatch(followAccountFail(error));
  91. });
  92. };
  93. };
  94. export function unfollowAccount(id) {
  95. return (dispatch, getState) => {
  96. dispatch(unfollowAccountRequest(id));
  97. api(getState).post(`/api/v1/accounts/${id}/unfollow`).then(response => {
  98. dispatch(unfollowAccountSuccess(response.data));
  99. }).catch(error => {
  100. dispatch(unfollowAccountFail(error));
  101. });
  102. }
  103. };
  104. export function followAccountRequest(id) {
  105. return {
  106. type: ACCOUNT_FOLLOW_REQUEST,
  107. id: id
  108. };
  109. };
  110. export function followAccountSuccess(relationship) {
  111. return {
  112. type: ACCOUNT_FOLLOW_SUCCESS,
  113. relationship: relationship
  114. };
  115. };
  116. export function followAccountFail(error) {
  117. return {
  118. type: ACCOUNT_FOLLOW_FAIL,
  119. error: error
  120. };
  121. };
  122. export function unfollowAccountRequest(id) {
  123. return {
  124. type: ACCOUNT_UNFOLLOW_REQUEST,
  125. id: id
  126. };
  127. };
  128. export function unfollowAccountSuccess(relationship) {
  129. return {
  130. type: ACCOUNT_UNFOLLOW_SUCCESS,
  131. relationship: relationship
  132. };
  133. };
  134. export function unfollowAccountFail(error) {
  135. return {
  136. type: ACCOUNT_UNFOLLOW_FAIL,
  137. error: error
  138. };
  139. };
  140. export function fetchAccountTimelineRequest(id) {
  141. return {
  142. type: ACCOUNT_TIMELINE_FETCH_REQUEST,
  143. id: id
  144. };
  145. };
  146. export function fetchAccountTimelineSuccess(id, statuses) {
  147. return {
  148. type: ACCOUNT_TIMELINE_FETCH_SUCCESS,
  149. id: id,
  150. statuses: statuses
  151. };
  152. };
  153. export function fetchAccountTimelineFail(id, error) {
  154. return {
  155. type: ACCOUNT_TIMELINE_FETCH_FAIL,
  156. id: id,
  157. error: error
  158. };
  159. };
  160. export function expandAccountTimelineRequest(id) {
  161. return {
  162. type: ACCOUNT_TIMELINE_EXPAND_REQUEST,
  163. id: id
  164. };
  165. };
  166. export function expandAccountTimelineSuccess(id, statuses) {
  167. return {
  168. type: ACCOUNT_TIMELINE_EXPAND_SUCCESS,
  169. id: id,
  170. statuses: statuses
  171. };
  172. };
  173. export function expandAccountTimelineFail(id, error) {
  174. return {
  175. type: ACCOUNT_TIMELINE_EXPAND_FAIL,
  176. id: id,
  177. error: error
  178. };
  179. };
  180. export function blockAccount(id) {
  181. return (dispatch, getState) => {
  182. dispatch(blockAccountRequest(id));
  183. api(getState).post(`/api/v1/accounts/${id}/block`).then(response => {
  184. dispatch(blockAccountSuccess(response.data));
  185. }).catch(error => {
  186. dispatch(blockAccountFail(id, error));
  187. });
  188. };
  189. };
  190. export function unblockAccount(id) {
  191. return (dispatch, getState) => {
  192. dispatch(unblockAccountRequest(id));
  193. api(getState).post(`/api/v1/accounts/${id}/unblock`).then(response => {
  194. dispatch(unblockAccountSuccess(response.data));
  195. }).catch(error => {
  196. dispatch(unblockAccountFail(id, error));
  197. });
  198. };
  199. };
  200. export function blockAccountRequest(id) {
  201. return {
  202. type: ACCOUNT_BLOCK_REQUEST,
  203. id: id
  204. };
  205. };
  206. export function blockAccountSuccess(relationship) {
  207. return {
  208. type: ACCOUNT_BLOCK_SUCCESS,
  209. relationship: relationship
  210. };
  211. };
  212. export function blockAccountFail(error) {
  213. return {
  214. type: ACCOUNT_BLOCK_FAIL,
  215. error: error
  216. };
  217. };
  218. export function unblockAccountRequest(id) {
  219. return {
  220. type: ACCOUNT_UNBLOCK_REQUEST,
  221. id: id
  222. };
  223. };
  224. export function unblockAccountSuccess(relationship) {
  225. return {
  226. type: ACCOUNT_UNBLOCK_SUCCESS,
  227. relationship: relationship
  228. };
  229. };
  230. export function unblockAccountFail(error) {
  231. return {
  232. type: ACCOUNT_UNBLOCK_FAIL,
  233. error: error
  234. };
  235. };