闭社主体 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.

180 lines
6.4 KiB

  1. import React from 'react';
  2. import { Provider } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import configureStore from '../store/configureStore';
  5. import {
  6. refreshTimelineSuccess,
  7. updateTimeline,
  8. deleteFromTimelines,
  9. refreshHomeTimeline,
  10. connectTimeline,
  11. disconnectTimeline,
  12. } from '../actions/timelines';
  13. import { showOnboardingOnce } from '../actions/onboarding';
  14. import { updateNotifications, refreshNotifications } from '../actions/notifications';
  15. import createBrowserHistory from 'history/lib/createBrowserHistory';
  16. import applyRouterMiddleware from 'react-router/lib/applyRouterMiddleware';
  17. import useRouterHistory from 'react-router/lib/useRouterHistory';
  18. import Router from 'react-router/lib/Router';
  19. import Route from 'react-router/lib/Route';
  20. import IndexRedirect from 'react-router/lib/IndexRedirect';
  21. import IndexRoute from 'react-router/lib/IndexRoute';
  22. import { useScroll } from 'react-router-scroll';
  23. import UI from '../features/ui';
  24. import Status from '../features/status';
  25. import GettingStarted from '../features/getting_started';
  26. import PublicTimeline from '../features/public_timeline';
  27. import CommunityTimeline from '../features/community_timeline';
  28. import AccountTimeline from '../features/account_timeline';
  29. import AccountGallery from '../features/account_gallery';
  30. import HomeTimeline from '../features/home_timeline';
  31. import Compose from '../features/compose';
  32. import Followers from '../features/followers';
  33. import Following from '../features/following';
  34. import Reblogs from '../features/reblogs';
  35. import Favourites from '../features/favourites';
  36. import HashtagTimeline from '../features/hashtag_timeline';
  37. import Notifications from '../features/notifications';
  38. import FollowRequests from '../features/follow_requests';
  39. import GenericNotFound from '../features/generic_not_found';
  40. import FavouritedStatuses from '../features/favourited_statuses';
  41. import Blocks from '../features/blocks';
  42. import Mutes from '../features/mutes';
  43. import Report from '../features/report';
  44. import { hydrateStore } from '../actions/store';
  45. import createStream from '../stream';
  46. import { IntlProvider, addLocaleData } from 'react-intl';
  47. import { getLocale } from '../locales';
  48. const { localeData, messages } = getLocale();
  49. addLocaleData(localeData);
  50. const store = configureStore();
  51. const initialState = JSON.parse(document.getElementById('initial-state').textContent);
  52. store.dispatch(hydrateStore(initialState));
  53. const browserHistory = useRouterHistory(createBrowserHistory)({
  54. basename: '/web',
  55. });
  56. class Mastodon extends React.PureComponent {
  57. componentDidMount() {
  58. const { locale } = this.props;
  59. const streamingAPIBaseURL = store.getState().getIn(['meta', 'streaming_api_base_url']);
  60. const accessToken = store.getState().getIn(['meta', 'access_token']);
  61. const setupPolling = () => {
  62. this.polling = setInterval(() => {
  63. store.dispatch(refreshHomeTimeline());
  64. store.dispatch(refreshNotifications());
  65. }, 20000);
  66. };
  67. const clearPolling = () => {
  68. clearInterval(this.polling);
  69. this.polling = undefined;
  70. };
  71. this.subscription = createStream(streamingAPIBaseURL, accessToken, 'user', {
  72. connected () {
  73. clearPolling();
  74. store.dispatch(connectTimeline('home'));
  75. },
  76. disconnected () {
  77. setupPolling();
  78. store.dispatch(disconnectTimeline('home'));
  79. },
  80. received (data) {
  81. switch(data.event) {
  82. case 'update':
  83. store.dispatch(updateTimeline('home', JSON.parse(data.payload)));
  84. break;
  85. case 'delete':
  86. store.dispatch(deleteFromTimelines(data.payload));
  87. break;
  88. case 'notification':
  89. store.dispatch(updateNotifications(JSON.parse(data.payload), messages, locale));
  90. break;
  91. }
  92. },
  93. reconnected () {
  94. clearPolling();
  95. store.dispatch(connectTimeline('home'));
  96. store.dispatch(refreshHomeTimeline());
  97. store.dispatch(refreshNotifications());
  98. },
  99. });
  100. // Desktop notifications
  101. if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
  102. Notification.requestPermission();
  103. }
  104. store.dispatch(showOnboardingOnce());
  105. }
  106. componentWillUnmount () {
  107. if (typeof this.subscription !== 'undefined') {
  108. this.subscription.close();
  109. this.subscription = null;
  110. }
  111. if (typeof this.polling !== 'undefined') {
  112. clearInterval(this.polling);
  113. this.polling = null;
  114. }
  115. }
  116. render () {
  117. const { locale } = this.props;
  118. return (
  119. <IntlProvider locale={locale} messages={messages}>
  120. <Provider store={store}>
  121. <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
  122. <Route path='/' component={UI}>
  123. <IndexRedirect to='/getting-started' />
  124. <Route path='getting-started' component={GettingStarted} />
  125. <Route path='timelines/home' component={HomeTimeline} />
  126. <Route path='timelines/public' component={PublicTimeline} />
  127. <Route path='timelines/public/local' component={CommunityTimeline} />
  128. <Route path='timelines/tag/:id' component={HashtagTimeline} />
  129. <Route path='notifications' component={Notifications} />
  130. <Route path='favourites' component={FavouritedStatuses} />
  131. <Route path='statuses/new' component={Compose} />
  132. <Route path='statuses/:statusId' component={Status} />
  133. <Route path='statuses/:statusId/reblogs' component={Reblogs} />
  134. <Route path='statuses/:statusId/favourites' component={Favourites} />
  135. <Route path='accounts/:accountId' component={AccountTimeline} />
  136. <Route path='accounts/:accountId/followers' component={Followers} />
  137. <Route path='accounts/:accountId/following' component={Following} />
  138. <Route path='accounts/:accountId/media' component={AccountGallery} />
  139. <Route path='follow_requests' component={FollowRequests} />
  140. <Route path='blocks' component={Blocks} />
  141. <Route path='mutes' component={Mutes} />
  142. <Route path='report' component={Report} />
  143. <Route path='*' component={GenericNotFound} />
  144. </Route>
  145. </Router>
  146. </Provider>
  147. </IntlProvider>
  148. );
  149. }
  150. }
  151. Mastodon.propTypes = {
  152. locale: PropTypes.string.isRequired,
  153. };
  154. export default Mastodon;