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.

55 lines
1.9 KiB

  1. import { connectStream } from 'flavours/glitch/util/stream';
  2. import {
  3. updateTimeline,
  4. deleteFromTimelines,
  5. refreshHomeTimeline,
  6. connectTimeline,
  7. disconnectTimeline,
  8. } from './timelines';
  9. import { updateNotifications, refreshNotifications } from './notifications';
  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. onConnect() {
  17. dispatch(connectTimeline(timelineId));
  18. },
  19. onDisconnect() {
  20. dispatch(disconnectTimeline(timelineId));
  21. },
  22. onReceive (data) {
  23. switch(data.event) {
  24. case 'update':
  25. dispatch(updateTimeline(timelineId, JSON.parse(data.payload)));
  26. break;
  27. case 'delete':
  28. dispatch(deleteFromTimelines(data.payload));
  29. break;
  30. case 'notification':
  31. dispatch(updateNotifications(JSON.parse(data.payload), messages, locale));
  32. break;
  33. }
  34. },
  35. };
  36. });
  37. }
  38. function refreshHomeTimelineAndNotification (dispatch) {
  39. dispatch(refreshHomeTimeline());
  40. dispatch(refreshNotifications());
  41. }
  42. export const connectUserStream = () => connectTimelineStream('home', 'user', refreshHomeTimelineAndNotification);
  43. export const connectCommunityStream = () => connectTimelineStream('community', 'public:local');
  44. export const connectMediaStream = () => connectTimelineStream('community', 'public:local');
  45. export const connectPublicStream = () => connectTimelineStream('public', 'public');
  46. export const connectHashtagStream = (tag) => connectTimelineStream(`hashtag:${tag}`, `hashtag&tag=${tag}`);
  47. export const connectDirectStream = () => connectTimelineStream('direct', 'direct');
  48. export const connectListStream = (id) => connectTimelineStream(`list:${id}`, `list&list=${id}`);