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.

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