The code powering m.abunchtell.com https://m.abunchtell.com
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

156 righe
4.0 KiB

  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. const ids = getState().getIn(['timelines', timeline, 'items'], Immutable.List());
  56. const newestId = ids.size > 0 ? ids.first() : null;
  57. let params = '';
  58. let path = timeline;
  59. let skipLoading = false;
  60. if (newestId !== null && getState().getIn(['timelines', timeline, 'loaded'])) {
  61. params = `?since_id=${newestId}`;
  62. skipLoading = true;
  63. }
  64. if (id) {
  65. path = `${path}/${id}`
  66. }
  67. dispatch(refreshTimelineRequest(timeline, id, skipLoading));
  68. api(getState).get(`/api/v1/timelines/${path}${params}`).then(function (response) {
  69. dispatch(refreshTimelineSuccess(timeline, response.data, skipLoading));
  70. }).catch(function (error) {
  71. dispatch(refreshTimelineFail(timeline, error, skipLoading));
  72. });
  73. };
  74. };
  75. export function refreshTimelineFail(timeline, error, skipLoading) {
  76. return {
  77. type: TIMELINE_REFRESH_FAIL,
  78. timeline,
  79. error,
  80. skipLoading
  81. };
  82. };
  83. export function expandTimeline(timeline, id = null) {
  84. return (dispatch, getState) => {
  85. const lastId = getState().getIn(['timelines', timeline, 'items'], Immutable.List()).last();
  86. if (!lastId) {
  87. // If timeline is empty, don't try to load older posts since there are none
  88. return;
  89. }
  90. dispatch(expandTimelineRequest(timeline));
  91. let path = timeline;
  92. if (id) {
  93. path = `${path}/${id}`
  94. }
  95. api(getState).get(`/api/v1/timelines/${path}?max_id=${lastId}`).then(response => {
  96. dispatch(expandTimelineSuccess(timeline, response.data));
  97. }).catch(error => {
  98. dispatch(expandTimelineFail(timeline, error));
  99. });
  100. };
  101. };
  102. export function expandTimelineRequest(timeline) {
  103. return {
  104. type: TIMELINE_EXPAND_REQUEST,
  105. timeline
  106. };
  107. };
  108. export function expandTimelineSuccess(timeline, statuses) {
  109. return {
  110. type: TIMELINE_EXPAND_SUCCESS,
  111. timeline,
  112. statuses
  113. };
  114. };
  115. export function expandTimelineFail(timeline, error) {
  116. return {
  117. type: TIMELINE_EXPAND_FAIL,
  118. timeline,
  119. error
  120. };
  121. };
  122. export function scrollTopTimeline(timeline, top) {
  123. return {
  124. type: TIMELINE_SCROLL_TOP,
  125. timeline,
  126. top
  127. };
  128. };