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.

206 lines
5.0 KiB

  1. import api from '../api'
  2. import axios from 'axios';
  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_TIMELINE_FETCH_REQUEST = 'ACCOUNT_TIMELINE_FETCH_REQUEST';
  14. export const ACCOUNT_TIMELINE_FETCH_SUCCESS = 'ACCOUNT_TIMELINE_FETCH_SUCCESS';
  15. export const ACCOUNT_TIMELINE_FETCH_FAIL = 'ACCOUNT_TIMELINE_FETCH_FAIL';
  16. export const ACCOUNT_TIMELINE_EXPAND_REQUEST = 'ACCOUNT_TIMELINE_EXPAND_REQUEST';
  17. export const ACCOUNT_TIMELINE_EXPAND_SUCCESS = 'ACCOUNT_TIMELINE_EXPAND_SUCCESS';
  18. export const ACCOUNT_TIMELINE_EXPAND_FAIL = 'ACCOUNT_TIMELINE_EXPAND_FAIL';
  19. export function setAccountSelf(account) {
  20. return {
  21. type: ACCOUNT_SET_SELF,
  22. account: account
  23. };
  24. };
  25. export function fetchAccount(id) {
  26. return (dispatch, getState) => {
  27. const boundApi = api(getState);
  28. dispatch(fetchAccountRequest(id));
  29. axios.all([boundApi.get(`/api/v1/accounts/${id}`), boundApi.get(`/api/v1/accounts/relationships?id=${id}`)]).then(values => {
  30. dispatch(fetchAccountSuccess(values[0].data, values[1].data[0]));
  31. }).catch(error => {
  32. dispatch(fetchAccountFail(id, error));
  33. });
  34. };
  35. };
  36. export function fetchAccountTimeline(id) {
  37. return (dispatch, getState) => {
  38. dispatch(fetchAccountTimelineRequest(id));
  39. api(getState).get(`/api/v1/accounts/${id}/statuses`).then(response => {
  40. dispatch(fetchAccountTimelineSuccess(id, response.data));
  41. }).catch(error => {
  42. dispatch(fetchAccountTimelineFail(id, error));
  43. });
  44. };
  45. };
  46. export function expandAccountTimeline(id) {
  47. return (dispatch, getState) => {
  48. const lastId = getState().getIn(['timelines', 'accounts_timelines', id]).last();
  49. dispatch(expandAccountTimelineRequest(id));
  50. api(getState).get(`/api/v1/accounts/${id}/statuses?max_id=${lastId}`).then(response => {
  51. dispatch(expandAccountTimelineSuccess(id, response.data));
  52. }).catch(error => {
  53. dispatch(expandAccountTimelineFail(id, error));
  54. });
  55. };
  56. };
  57. export function fetchAccountRequest(id) {
  58. return {
  59. type: ACCOUNT_FETCH_REQUEST,
  60. id: id
  61. };
  62. };
  63. export function fetchAccountSuccess(account, relationship) {
  64. return {
  65. type: ACCOUNT_FETCH_SUCCESS,
  66. account: account,
  67. relationship: relationship
  68. };
  69. };
  70. export function fetchAccountFail(id, error) {
  71. return {
  72. type: ACCOUNT_FETCH_FAIL,
  73. id: id,
  74. error: error
  75. };
  76. };
  77. export function followAccount(id) {
  78. return (dispatch, getState) => {
  79. dispatch(followAccountRequest(id));
  80. api(getState).post(`/api/v1/accounts/${id}/follow`).then(response => {
  81. dispatch(followAccountSuccess(response.data));
  82. }).catch(error => {
  83. dispatch(followAccountFail(error));
  84. });
  85. };
  86. };
  87. export function unfollowAccount(id) {
  88. return (dispatch, getState) => {
  89. dispatch(unfollowAccountRequest(id));
  90. api(getState).post(`/api/v1/accounts/${id}/unfollow`).then(response => {
  91. dispatch(unfollowAccountSuccess(response.data));
  92. }).catch(error => {
  93. dispatch(unfollowAccountFail(error));
  94. });
  95. }
  96. };
  97. export function followAccountRequest(id) {
  98. return {
  99. type: ACCOUNT_FOLLOW_REQUEST,
  100. id: id
  101. };
  102. };
  103. export function followAccountSuccess(relationship) {
  104. return {
  105. type: ACCOUNT_FOLLOW_SUCCESS,
  106. relationship: relationship
  107. };
  108. };
  109. export function followAccountFail(error) {
  110. return {
  111. type: ACCOUNT_FOLLOW_FAIL,
  112. error: error
  113. };
  114. };
  115. export function unfollowAccountRequest(id) {
  116. return {
  117. type: ACCOUNT_UNFOLLOW_REQUEST,
  118. id: id
  119. };
  120. };
  121. export function unfollowAccountSuccess(relationship) {
  122. return {
  123. type: ACCOUNT_UNFOLLOW_SUCCESS,
  124. relationship: relationship
  125. };
  126. };
  127. export function unfollowAccountFail(error) {
  128. return {
  129. type: ACCOUNT_UNFOLLOW_FAIL,
  130. error: error
  131. };
  132. };
  133. export function fetchAccountTimelineRequest(id) {
  134. return {
  135. type: ACCOUNT_TIMELINE_FETCH_REQUEST,
  136. id: id
  137. };
  138. };
  139. export function fetchAccountTimelineSuccess(id, statuses) {
  140. return {
  141. type: ACCOUNT_TIMELINE_FETCH_SUCCESS,
  142. id: id,
  143. statuses: statuses
  144. };
  145. };
  146. export function fetchAccountTimelineFail(id, error) {
  147. return {
  148. type: ACCOUNT_TIMELINE_FETCH_FAIL,
  149. id: id,
  150. error: error
  151. };
  152. };
  153. export function expandAccountTimelineRequest(id) {
  154. return {
  155. type: ACCOUNT_TIMELINE_EXPAND_REQUEST,
  156. id: id
  157. };
  158. };
  159. export function expandAccountTimelineSuccess(id, statuses) {
  160. return {
  161. type: ACCOUNT_TIMELINE_EXPAND_SUCCESS,
  162. id: id,
  163. statuses: statuses
  164. };
  165. };
  166. export function expandAccountTimelineFail(id, error) {
  167. return {
  168. type: ACCOUNT_TIMELINE_EXPAND_FAIL,
  169. id: id,
  170. error: error
  171. };
  172. };