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.

164 lines
4.6 KiB

8 years ago
8 years ago
8 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. let 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 = { ...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. if (getState().getIn(['timelines', timeline, 'items']).size === 0) {
  91. return;
  92. }
  93. const path = getState().getIn(['timelines', timeline, 'path'])(getState().getIn(['timelines', timeline, 'id']));
  94. const params = getState().getIn(['timelines', timeline, 'params'], {});
  95. const lastId = getState().getIn(['timelines', timeline, 'items']).last();
  96. dispatch(expandTimelineRequest(timeline));
  97. api(getState).get(path, {
  98. params: {
  99. ...params,
  100. max_id: lastId,
  101. limit: 10
  102. }
  103. }).then(response => {
  104. const next = getLinks(response).refs.find(link => link.rel === 'next');
  105. dispatch(expandTimelineSuccess(timeline, response.data, next ? next.uri : null));
  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, next) {
  118. return {
  119. type: TIMELINE_EXPAND_SUCCESS,
  120. timeline,
  121. statuses,
  122. next
  123. };
  124. };
  125. export function expandTimelineFail(timeline, error) {
  126. return {
  127. type: TIMELINE_EXPAND_FAIL,
  128. timeline,
  129. error
  130. };
  131. };
  132. export function scrollTopTimeline(timeline, top) {
  133. return {
  134. type: TIMELINE_SCROLL_TOP,
  135. timeline,
  136. top
  137. };
  138. };