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

184 lines
5.9 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 eo from 'react-intl/locale-data/eo';
  45. import es from 'react-intl/locale-data/es';
  46. import fi from 'react-intl/locale-data/fi';
  47. import fr from 'react-intl/locale-data/fr';
  48. import hu from 'react-intl/locale-data/hu';
  49. import ja from 'react-intl/locale-data/ja';
  50. import pt from 'react-intl/locale-data/pt';
  51. import no from 'react-intl/locale-data/no';
  52. import ru from 'react-intl/locale-data/ru';
  53. import uk from 'react-intl/locale-data/uk';
  54. import zh from 'react-intl/locale-data/zh';
  55. import bg from 'react-intl/locale-data/bg';
  56. import { localeData as zh_hk } from '../locales/zh-hk';
  57. import getMessagesForLocale from '../locales';
  58. import { hydrateStore } from '../actions/store';
  59. import createStream from '../stream';
  60. const store = configureStore();
  61. store.dispatch(hydrateStore(window.INITIAL_STATE));
  62. const browserHistory = useRouterHistory(createBrowserHistory)({
  63. basename: '/web'
  64. });
  65. addLocaleData([
  66. ...en,
  67. ...de,
  68. ...eo,
  69. ...es,
  70. ...fi,
  71. ...fr,
  72. ...hu,
  73. ...ja,
  74. ...pt,
  75. ...no,
  76. ...ru,
  77. ...uk,
  78. ...zh,
  79. ...zh_hk,
  80. ...bg,
  81. ]);
  82. const Mastodon = React.createClass({
  83. propTypes: {
  84. locale: React.PropTypes.string.isRequired
  85. },
  86. componentDidMount() {
  87. const { locale } = this.props;
  88. const accessToken = store.getState().getIn(['meta', 'access_token']);
  89. this.subscription = createStream(accessToken, 'user', {
  90. connected () {
  91. store.dispatch(connectTimeline('home'));
  92. },
  93. disconnected () {
  94. store.dispatch(disconnectTimeline('home'));
  95. },
  96. received (data) {
  97. switch(data.event) {
  98. case 'update':
  99. store.dispatch(updateTimeline('home', JSON.parse(data.payload)));
  100. break;
  101. case 'delete':
  102. store.dispatch(deleteFromTimelines(data.payload));
  103. break;
  104. case 'notification':
  105. store.dispatch(updateNotifications(JSON.parse(data.payload), getMessagesForLocale(locale), locale));
  106. break;
  107. }
  108. },
  109. reconnected () {
  110. store.dispatch(connectTimeline('home'));
  111. store.dispatch(refreshTimeline('home'));
  112. store.dispatch(refreshNotifications());
  113. }
  114. });
  115. // Desktop notifications
  116. if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
  117. Notification.requestPermission();
  118. }
  119. },
  120. componentWillUnmount () {
  121. if (typeof this.subscription !== 'undefined') {
  122. this.subscription.close();
  123. this.subscription = null;
  124. }
  125. },
  126. render () {
  127. const { locale } = this.props;
  128. return (
  129. <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
  130. <Provider store={store}>
  131. <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
  132. <Route path='/' component={UI}>
  133. <IndexRedirect to="/getting-started" />
  134. <Route path='getting-started' component={GettingStarted} />
  135. <Route path='timelines/home' component={HomeTimeline} />
  136. <Route path='timelines/public' component={PublicTimeline} />
  137. <Route path='timelines/public/local' component={CommunityTimeline} />
  138. <Route path='timelines/tag/:id' component={HashtagTimeline} />
  139. <Route path='notifications' component={Notifications} />
  140. <Route path='favourites' component={FavouritedStatuses} />
  141. <Route path='statuses/new' component={Compose} />
  142. <Route path='statuses/:statusId' component={Status} />
  143. <Route path='statuses/:statusId/reblogs' component={Reblogs} />
  144. <Route path='statuses/:statusId/favourites' component={Favourites} />
  145. <Route path='accounts/:accountId' component={AccountTimeline} />
  146. <Route path='accounts/:accountId/followers' component={Followers} />
  147. <Route path='accounts/:accountId/following' component={Following} />
  148. <Route path='follow_requests' component={FollowRequests} />
  149. <Route path='blocks' component={Blocks} />
  150. <Route path='report' component={Report} />
  151. <Route path='*' component={GenericNotFound} />
  152. </Route>
  153. </Router>
  154. </Provider>
  155. </IntlProvider>
  156. );
  157. }
  158. });
  159. export default Mastodon;