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.

145 lines
3.7 KiB

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