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.

53 lines
2.0 KiB

  1. import { connectStream } from 'flavours/glitch/util/stream';
  2. import {
  3. updateTimeline,
  4. deleteFromTimelines,
  5. expandHomeTimeline,
  6. disconnectTimeline,
  7. } from './timelines';
  8. import { updateNotifications, expandNotifications } from './notifications';
  9. import { fetchFilters } from './filters';
  10. import { getLocale } from 'mastodon/locales';
  11. const { messages } = getLocale();
  12. export function connectTimelineStream (timelineId, path, pollingRefresh = null) {
  13. return connectStream (path, pollingRefresh, (dispatch, getState) => {
  14. const locale = getState().getIn(['meta', 'locale']);
  15. return {
  16. onDisconnect() {
  17. dispatch(disconnectTimeline(timelineId));
  18. },
  19. onReceive (data) {
  20. switch(data.event) {
  21. case 'update':
  22. dispatch(updateTimeline(timelineId, JSON.parse(data.payload)));
  23. break;
  24. case 'delete':
  25. dispatch(deleteFromTimelines(data.payload));
  26. break;
  27. case 'notification':
  28. dispatch(updateNotifications(JSON.parse(data.payload), messages, locale));
  29. break;
  30. case 'filters_changed':
  31. dispatch(fetchFilters());
  32. break;
  33. }
  34. },
  35. };
  36. });
  37. }
  38. const refreshHomeTimelineAndNotification = (dispatch, done) => {
  39. dispatch(expandHomeTimeline({}, () => dispatch(expandNotifications({}, done))));
  40. };
  41. export const connectUserStream = () => connectTimelineStream('home', 'user', refreshHomeTimelineAndNotification);
  42. export const connectCommunityStream = () => connectTimelineStream('community', 'public:local');
  43. export const connectMediaStream = () => connectTimelineStream('community', 'public:local');
  44. export const connectPublicStream = () => connectTimelineStream('public', 'public');
  45. export const connectHashtagStream = (tag) => connectTimelineStream(`hashtag:${tag}`, `hashtag&tag=${tag}`);
  46. export const connectDirectStream = () => connectTimelineStream('direct', 'direct');
  47. export const connectListStream = (id) => connectTimelineStream(`list:${id}`, `list&list=${id}`);