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.

143 lines
3.6 KiB

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