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.

165 lines
4.2 KiB

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