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.

229 lines
7.6 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. refreshTimeline,
  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 { IntlProvider, addLocaleData } from 'react-intl';
  45. import ar from 'react-intl/locale-data/ar';
  46. import bg from 'react-intl/locale-data/bg';
  47. import ca from 'react-intl/locale-data/ca';
  48. import de from 'react-intl/locale-data/de';
  49. import en from 'react-intl/locale-data/en';
  50. import eo from 'react-intl/locale-data/eo';
  51. import es from 'react-intl/locale-data/es';
  52. import fa from 'react-intl/locale-data/fa';
  53. import fi from 'react-intl/locale-data/fi';
  54. import fr from 'react-intl/locale-data/fr';
  55. import he from 'react-intl/locale-data/he';
  56. import hr from 'react-intl/locale-data/hr';
  57. import hu from 'react-intl/locale-data/hu';
  58. import id from 'react-intl/locale-data/id';
  59. import it from 'react-intl/locale-data/it';
  60. import ja from 'react-intl/locale-data/ja';
  61. import nl from 'react-intl/locale-data/nl';
  62. import no from 'react-intl/locale-data/no';
  63. import oc from '../locales/locale-data/oc';
  64. import pt from 'react-intl/locale-data/pt';
  65. import ru from 'react-intl/locale-data/ru';
  66. import uk from 'react-intl/locale-data/uk';
  67. import zh from 'react-intl/locale-data/zh';
  68. import tr from 'react-intl/locale-data/tr';
  69. import getMessagesForLocale from '../locales';
  70. import { hydrateStore } from '../actions/store';
  71. import createStream from '../stream';
  72. const store = configureStore();
  73. const initialState = JSON.parse(document.getElementById("initial-state").textContent);
  74. store.dispatch(hydrateStore(initialState));
  75. const browserHistory = useRouterHistory(createBrowserHistory)({
  76. basename: '/web',
  77. });
  78. addLocaleData([
  79. ...ar,
  80. ...bg,
  81. ...ca,
  82. ...de,
  83. ...en,
  84. ...eo,
  85. ...es,
  86. ...fa,
  87. ...fi,
  88. ...fr,
  89. ...he,
  90. ...hr,
  91. ...hu,
  92. ...id,
  93. ...it,
  94. ...ja,
  95. ...nl,
  96. ...no,
  97. ...oc,
  98. ...pt,
  99. ...ru,
  100. ...uk,
  101. ...zh,
  102. ...tr,
  103. ]);
  104. class Mastodon extends React.PureComponent {
  105. componentDidMount() {
  106. const { locale } = this.props;
  107. const streamingAPIBaseURL = store.getState().getIn(['meta', 'streaming_api_base_url']);
  108. const accessToken = store.getState().getIn(['meta', 'access_token']);
  109. const setupPolling = () => {
  110. this.polling = setInterval(() => {
  111. store.dispatch(refreshTimeline('home'));
  112. store.dispatch(refreshNotifications());
  113. }, 20000);
  114. };
  115. const clearPolling = () => {
  116. clearInterval(this.polling);
  117. this.polling = undefined;
  118. };
  119. this.subscription = createStream(streamingAPIBaseURL, accessToken, 'user', {
  120. connected () {
  121. clearPolling();
  122. store.dispatch(connectTimeline('home'));
  123. },
  124. disconnected () {
  125. setupPolling();
  126. store.dispatch(disconnectTimeline('home'));
  127. },
  128. received (data) {
  129. switch(data.event) {
  130. case 'update':
  131. store.dispatch(updateTimeline('home', JSON.parse(data.payload)));
  132. break;
  133. case 'delete':
  134. store.dispatch(deleteFromTimelines(data.payload));
  135. break;
  136. case 'notification':
  137. store.dispatch(updateNotifications(JSON.parse(data.payload), getMessagesForLocale(locale), locale));
  138. break;
  139. }
  140. },
  141. reconnected () {
  142. clearPolling();
  143. store.dispatch(connectTimeline('home'));
  144. store.dispatch(refreshTimeline('home'));
  145. store.dispatch(refreshNotifications());
  146. },
  147. });
  148. // Desktop notifications
  149. if (typeof window.Notification !== 'undefined' && Notification.permission === 'default') {
  150. Notification.requestPermission();
  151. }
  152. store.dispatch(showOnboardingOnce());
  153. }
  154. componentWillUnmount () {
  155. if (typeof this.subscription !== 'undefined') {
  156. this.subscription.close();
  157. this.subscription = null;
  158. }
  159. if (typeof this.polling !== 'undefined') {
  160. clearInterval(this.polling);
  161. this.polling = null;
  162. }
  163. }
  164. render () {
  165. const { locale } = this.props;
  166. return (
  167. <IntlProvider locale={locale} messages={getMessagesForLocale(locale)}>
  168. <Provider store={store}>
  169. <Router history={browserHistory} render={applyRouterMiddleware(useScroll())}>
  170. <Route path='/' component={UI}>
  171. <IndexRedirect to='/getting-started' />
  172. <Route path='getting-started' component={GettingStarted} />
  173. <Route path='timelines/home' component={HomeTimeline} />
  174. <Route path='timelines/public' component={PublicTimeline} />
  175. <Route path='timelines/public/local' component={CommunityTimeline} />
  176. <Route path='timelines/tag/:id' component={HashtagTimeline} />
  177. <Route path='notifications' component={Notifications} />
  178. <Route path='favourites' component={FavouritedStatuses} />
  179. <Route path='statuses/new' component={Compose} />
  180. <Route path='statuses/:statusId' component={Status} />
  181. <Route path='statuses/:statusId/reblogs' component={Reblogs} />
  182. <Route path='statuses/:statusId/favourites' component={Favourites} />
  183. <Route path='accounts/:accountId' component={AccountTimeline} />
  184. <Route path='accounts/:accountId/followers' component={Followers} />
  185. <Route path='accounts/:accountId/following' component={Following} />
  186. <Route path='accounts/:accountId/media' component={AccountGallery} />
  187. <Route path='follow_requests' component={FollowRequests} />
  188. <Route path='blocks' component={Blocks} />
  189. <Route path='mutes' component={Mutes} />
  190. <Route path='report' component={Report} />
  191. <Route path='*' component={GenericNotFound} />
  192. </Route>
  193. </Router>
  194. </Provider>
  195. </IntlProvider>
  196. );
  197. }
  198. }
  199. Mastodon.propTypes = {
  200. locale: PropTypes.string.isRequired,
  201. };
  202. export default Mastodon;