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

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