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.

162 lines
4.4 KiB

7 years ago
7 years ago
7 years ago
  1. import api, { getLinks } from '../api'
  2. import Immutable from 'immutable';
  3. export const TIMELINE_UPDATE = 'TIMELINE_UPDATE';
  4. export const TIMELINE_DELETE = 'TIMELINE_DELETE';
  5. export const TIMELINE_REFRESH_REQUEST = 'TIMELINE_REFRESH_REQUEST';
  6. export const TIMELINE_REFRESH_SUCCESS = 'TIMELINE_REFRESH_SUCCESS';
  7. export const TIMELINE_REFRESH_FAIL = 'TIMELINE_REFRESH_FAIL';
  8. export const TIMELINE_EXPAND_REQUEST = 'TIMELINE_EXPAND_REQUEST';
  9. export const TIMELINE_EXPAND_SUCCESS = 'TIMELINE_EXPAND_SUCCESS';
  10. export const TIMELINE_EXPAND_FAIL = 'TIMELINE_EXPAND_FAIL';
  11. export const TIMELINE_SCROLL_TOP = 'TIMELINE_SCROLL_TOP';
  12. export function refreshTimelineSuccess(timeline, statuses, skipLoading, next) {
  13. return {
  14. type: TIMELINE_REFRESH_SUCCESS,
  15. timeline,
  16. statuses,
  17. skipLoading,
  18. next
  19. };
  20. };
  21. export function updateTimeline(timeline, status) {
  22. return (dispatch, getState) => {
  23. const references = status.reblog ? getState().get('statuses').filter((item, itemId) => (itemId === status.reblog.id || item.get('reblog') === status.reblog.id)).map((_, itemId) => itemId) : [];
  24. dispatch({
  25. type: TIMELINE_UPDATE,
  26. timeline,
  27. status,
  28. references
  29. });
  30. };
  31. };
  32. export function deleteFromTimelines(id) {
  33. return (dispatch, getState) => {
  34. const accountId = getState().getIn(['statuses', id, 'account']);
  35. const references = getState().get('statuses').filter(status => status.get('reblog') === id).map(status => [status.get('id'), status.get('account')]);
  36. const reblogOf = getState().getIn(['statuses', id, 'reblog'], null);
  37. dispatch({
  38. type: TIMELINE_DELETE,
  39. id,
  40. accountId,
  41. references,
  42. reblogOf
  43. });
  44. };
  45. };
  46. export function refreshTimelineRequest(timeline, id, skipLoading) {
  47. return {
  48. type: TIMELINE_REFRESH_REQUEST,
  49. timeline,
  50. id,
  51. skipLoading
  52. };
  53. };
  54. export function refreshTimeline(timeline, id = null) {
  55. return function (dispatch, getState) {
  56. if (getState().getIn(['timelines', timeline, 'isLoading'])) {
  57. return;
  58. }
  59. const ids = getState().getIn(['timelines', timeline, 'items'], Immutable.List());
  60. const newestId = ids.size > 0 ? ids.first() : null;
  61. const params = getState().getIn(['timelines', timeline, 'params'], {});
  62. const path = getState().getIn(['timelines', timeline, 'path'])(id);
  63. let skipLoading = false;
  64. if (newestId !== null && getState().getIn(['timelines', timeline, 'loaded']) && (id === null || getState().getIn(['timelines', timeline, 'id']) === id)) {
  65. params.since_id = newestId;
  66. skipLoading = true;
  67. }
  68. dispatch(refreshTimelineRequest(timeline, id, skipLoading));
  69. api(getState).get(path, { params }).then(response => {
  70. const next = getLinks(response).refs.find(link => link.rel === 'next');
  71. dispatch(refreshTimelineSuccess(timeline, response.data, skipLoading, next ? next.uri : null));
  72. }).catch(error => {
  73. dispatch(refreshTimelineFail(timeline, error, skipLoading));
  74. });
  75. };
  76. };
  77. export function refreshTimelineFail(timeline, error, skipLoading) {
  78. return {
  79. type: TIMELINE_REFRESH_FAIL,
  80. timeline,
  81. error,
  82. skipLoading
  83. };
  84. };
  85. export function expandTimeline(timeline) {
  86. return (dispatch, getState) => {
  87. if (getState().getIn(['timelines', timeline, 'isLoading'])) {
  88. return;
  89. }
  90. const next = getState().getIn(['timelines', timeline, 'next']);
  91. const params = getState().getIn(['timelines', timeline, 'params'], {});
  92. if (next === null) {
  93. return;
  94. }
  95. dispatch(expandTimelineRequest(timeline));
  96. api(getState).get(next, {
  97. params: {
  98. ...params,
  99. limit: 10
  100. }
  101. }).then(response => {
  102. const next = getLinks(response).refs.find(link => link.rel === 'next');
  103. dispatch(expandTimelineSuccess(timeline, response.data, next ? next.uri : null));
  104. }).catch(error => {
  105. dispatch(expandTimelineFail(timeline, error));
  106. });
  107. };
  108. };
  109. export function expandTimelineRequest(timeline) {
  110. return {
  111. type: TIMELINE_EXPAND_REQUEST,
  112. timeline
  113. };
  114. };
  115. export function expandTimelineSuccess(timeline, statuses, next) {
  116. return {
  117. type: TIMELINE_EXPAND_SUCCESS,
  118. timeline,
  119. statuses,
  120. next
  121. };
  122. };
  123. export function expandTimelineFail(timeline, error) {
  124. return {
  125. type: TIMELINE_EXPAND_FAIL,
  126. timeline,
  127. error
  128. };
  129. };
  130. export function scrollTopTimeline(timeline, top) {
  131. return {
  132. type: TIMELINE_SCROLL_TOP,
  133. timeline,
  134. top
  135. };
  136. };