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.

133 lines
3.5 KiB

7 years ago
7 years ago
7 years ago
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 function refreshTimelineSuccess(timeline, statuses, replace) {
  12. return {
  13. type: TIMELINE_REFRESH_SUCCESS,
  14. timeline: timeline,
  15. statuses: statuses,
  16. replace: replace
  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) {
  43. return {
  44. type: TIMELINE_REFRESH_REQUEST,
  45. timeline: timeline
  46. };
  47. };
  48. export function refreshTimeline(timeline, replace = false, id = null) {
  49. return function (dispatch, getState) {
  50. dispatch(refreshTimelineRequest(timeline));
  51. const ids = getState().getIn(['timelines', timeline], Immutable.List());
  52. const newestId = ids.size > 0 ? ids.first() : null;
  53. let params = '';
  54. let path = timeline;
  55. if (newestId !== null && !replace) {
  56. params = `?since_id=${newestId}`;
  57. }
  58. if (id) {
  59. path = `${path}/${id}`
  60. }
  61. api(getState).get(`/api/v1/timelines/${path}${params}`).then(function (response) {
  62. dispatch(refreshTimelineSuccess(timeline, response.data, replace));
  63. }).catch(function (error) {
  64. dispatch(refreshTimelineFail(timeline, error));
  65. });
  66. };
  67. };
  68. export function refreshTimelineFail(timeline, error) {
  69. return {
  70. type: TIMELINE_REFRESH_FAIL,
  71. timeline: timeline,
  72. error: error
  73. };
  74. };
  75. export function expandTimeline(timeline, id = null) {
  76. return (dispatch, getState) => {
  77. const lastId = getState().getIn(['timelines', timeline], Immutable.List()).last();
  78. dispatch(expandTimelineRequest(timeline));
  79. let path = timeline;
  80. if (id) {
  81. path = `${path}/${id}`
  82. }
  83. api(getState).get(`/api/v1/timelines/${path}?max_id=${lastId}`).then(response => {
  84. dispatch(expandTimelineSuccess(timeline, response.data));
  85. }).catch(error => {
  86. dispatch(expandTimelineFail(timeline, error));
  87. });
  88. };
  89. };
  90. export function expandTimelineRequest(timeline) {
  91. return {
  92. type: TIMELINE_EXPAND_REQUEST,
  93. timeline: timeline
  94. };
  95. };
  96. export function expandTimelineSuccess(timeline, statuses) {
  97. return {
  98. type: TIMELINE_EXPAND_SUCCESS,
  99. timeline: timeline,
  100. statuses: statuses
  101. };
  102. };
  103. export function expandTimelineFail(timeline, error) {
  104. return {
  105. type: TIMELINE_EXPAND_FAIL,
  106. timeline: timeline,
  107. error: error
  108. };
  109. };