闭社主体 forked from https://github.com/tootsuite/mastodon
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.

186 lines
6.0 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 const TIMELINE_CONNECT = 'TIMELINE_CONNECT';
  13. export const TIMELINE_DISCONNECT = 'TIMELINE_DISCONNECT';
  14. export function refreshTimelineSuccess(timeline, statuses, skipLoading, next) {
  15. return {
  16. type: TIMELINE_REFRESH_SUCCESS,
  17. timeline,
  18. statuses,
  19. skipLoading,
  20. next,
  21. };
  22. };
  23. export function updateTimeline(timeline, status) {
  24. return (dispatch, getState) => {
  25. const references = status.reblog ? getState().get('statuses').filter((item, itemId) => (itemId === status.reblog.id || item.get('reblog') === status.reblog.id)).map((_, itemId) => itemId) : [];
  26. dispatch({
  27. type: TIMELINE_UPDATE,
  28. timeline,
  29. status,
  30. references,
  31. });
  32. };
  33. };
  34. export function deleteFromTimelines(id) {
  35. return (dispatch, getState) => {
  36. const accountId = getState().getIn(['statuses', id, 'account']);
  37. const references = getState().get('statuses').filter(status => status.get('reblog') === id).map(status => [status.get('id'), status.get('account')]);
  38. const reblogOf = getState().getIn(['statuses', id, 'reblog'], null);
  39. dispatch({
  40. type: TIMELINE_DELETE,
  41. id,
  42. accountId,
  43. references,
  44. reblogOf,
  45. });
  46. };
  47. };
  48. export function refreshTimelineRequest(timeline, skipLoading) {
  49. return {
  50. type: TIMELINE_REFRESH_REQUEST,
  51. timeline,
  52. skipLoading,
  53. };
  54. };
  55. export function refreshTimeline(timelineId, path, params = {}) {
  56. return function (dispatch, getState) {
  57. const timeline = getState().getIn(['timelines', timelineId], Immutable.Map());
  58. if (timeline.get('isLoading') || timeline.get('online')) {
  59. return;
  60. }
  61. const ids = timeline.get('items', Immutable.List());
  62. const newestId = ids.size > 0 ? ids.first() : null;
  63. let skipLoading = timeline.get('loaded');
  64. if (newestId !== null) {
  65. params.since_id = newestId;
  66. }
  67. dispatch(refreshTimelineRequest(timelineId, skipLoading));
  68. api(getState).get(path, { params }).then(response => {
  69. const next = getLinks(response).refs.find(link => link.rel === 'next');
  70. dispatch(refreshTimelineSuccess(timelineId, response.data, skipLoading, next ? next.uri : null));
  71. }).catch(error => {
  72. dispatch(refreshTimelineFail(timelineId, error, skipLoading));
  73. });
  74. };
  75. };
  76. export const refreshHomeTimeline = () => refreshTimeline('home', '/api/v1/timelines/home');
  77. export const refreshPublicTimeline = () => refreshTimeline('public', '/api/v1/timelines/public');
  78. export const refreshCommunityTimeline = () => refreshTimeline('community', '/api/v1/timelines/public', { local: true });
  79. export const refreshAccountTimeline = accountId => refreshTimeline(`account:${accountId}`, `/api/v1/accounts/${accountId}/statuses`);
  80. export const refreshAccountMediaTimeline = accountId => refreshTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { only_media: true });
  81. export const refreshHashtagTimeline = hashtag => refreshTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`);
  82. export function refreshTimelineFail(timeline, error, skipLoading) {
  83. return {
  84. type: TIMELINE_REFRESH_FAIL,
  85. timeline,
  86. error,
  87. skipLoading,
  88. skipAlert: error.response.status === 404,
  89. };
  90. };
  91. export function expandTimeline(timelineId, path, params = {}) {
  92. return (dispatch, getState) => {
  93. const timeline = getState().getIn(['timelines', timelineId], Immutable.Map());
  94. const ids = timeline.get('items', Immutable.List());
  95. if (timeline.get('isLoading') || ids.size === 0) {
  96. return;
  97. }
  98. params.max_id = ids.last();
  99. params.limit = 10;
  100. dispatch(expandTimelineRequest(timelineId));
  101. api(getState).get(path, { params }).then(response => {
  102. const next = getLinks(response).refs.find(link => link.rel === 'next');
  103. dispatch(expandTimelineSuccess(timelineId, response.data, next ? next.uri : null));
  104. }).catch(error => {
  105. dispatch(expandTimelineFail(timelineId, error));
  106. });
  107. };
  108. };
  109. export const expandHomeTimeline = () => expandTimeline('home', '/api/v1/timelines/home');
  110. export const expandPublicTimeline = () => expandTimeline('public', '/api/v1/timelines/public');
  111. export const expandCommunityTimeline = () => expandTimeline('community', '/api/v1/timelines/public', { local: true });
  112. export const expandAccountTimeline = accountId => expandTimeline(`account:${accountId}`, `/api/v1/accounts/${accountId}/statuses`);
  113. export const expandAccountMediaTimeline = accountId => expandTimeline(`account:${accountId}:media`, `/api/v1/accounts/${accountId}/statuses`, { only_media: true });
  114. export const expandHashtagTimeline = hashtag => expandTimeline(`hashtag:${hashtag}`, `/api/v1/timelines/tag/${hashtag}`);
  115. export function expandTimelineRequest(timeline) {
  116. return {
  117. type: TIMELINE_EXPAND_REQUEST,
  118. timeline,
  119. };
  120. };
  121. export function expandTimelineSuccess(timeline, statuses, next) {
  122. return {
  123. type: TIMELINE_EXPAND_SUCCESS,
  124. timeline,
  125. statuses,
  126. next,
  127. };
  128. };
  129. export function expandTimelineFail(timeline, error) {
  130. return {
  131. type: TIMELINE_EXPAND_FAIL,
  132. timeline,
  133. error,
  134. };
  135. };
  136. export function scrollTopTimeline(timeline, top) {
  137. return {
  138. type: TIMELINE_SCROLL_TOP,
  139. timeline,
  140. top,
  141. };
  142. };
  143. export function connectTimeline(timeline) {
  144. return {
  145. type: TIMELINE_CONNECT,
  146. timeline,
  147. };
  148. };
  149. export function disconnectTimeline(timeline) {
  150. return {
  151. type: TIMELINE_DISCONNECT,
  152. timeline,
  153. };
  154. };