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

161 lines
5.5 KiB

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