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

83 lines
2.4 KiB

  1. import {
  2. NOTIFICATIONS_UPDATE,
  3. NOTIFICATIONS_REFRESH_SUCCESS,
  4. NOTIFICATIONS_EXPAND_SUCCESS,
  5. NOTIFICATIONS_SETTING_CHANGE
  6. } from '../actions/notifications';
  7. import { ACCOUNT_BLOCK_SUCCESS } from '../actions/accounts';
  8. import Immutable from 'immutable';
  9. const initialState = Immutable.Map({
  10. items: Immutable.List(),
  11. next: null,
  12. loaded: false,
  13. settings: Immutable.Map({
  14. alerts: Immutable.Map({
  15. follow: true,
  16. favourite: true,
  17. reblog: true,
  18. mention: true
  19. }),
  20. shows: Immutable.Map({
  21. follow: true,
  22. favourite: true,
  23. reblog: true,
  24. mention: true
  25. })
  26. })
  27. });
  28. const notificationToMap = notification => Immutable.Map({
  29. id: notification.id,
  30. type: notification.type,
  31. account: notification.account.id,
  32. status: notification.status ? notification.status.id : null
  33. });
  34. const normalizeNotification = (state, notification) => {
  35. return state.update('items', list => list.unshift(notificationToMap(notification)));
  36. };
  37. const normalizeNotifications = (state, notifications, next) => {
  38. let items = Immutable.List();
  39. const loaded = state.get('loaded');
  40. notifications.forEach((n, i) => {
  41. items = items.set(i, notificationToMap(n));
  42. });
  43. return state.update('items', list => loaded ? list.unshift(...items) : list.push(...items)).set('next', next).set('loaded', true);
  44. };
  45. const appendNormalizedNotifications = (state, notifications, next) => {
  46. let items = Immutable.List();
  47. notifications.forEach((n, i) => {
  48. items = items.set(i, notificationToMap(n));
  49. });
  50. return state.update('items', list => list.push(...items)).set('next', next);
  51. };
  52. const filterNotifications = (state, relationship) => {
  53. return state.update('items', list => list.filterNot(item => item.get('account') === relationship.id));
  54. };
  55. export default function notifications(state = initialState, action) {
  56. switch(action.type) {
  57. case NOTIFICATIONS_UPDATE:
  58. return normalizeNotification(state, action.notification);
  59. case NOTIFICATIONS_REFRESH_SUCCESS:
  60. return normalizeNotifications(state, action.notifications, action.next);
  61. case NOTIFICATIONS_EXPAND_SUCCESS:
  62. return appendNormalizedNotifications(state, action.notifications, action.next);
  63. case ACCOUNT_BLOCK_SUCCESS:
  64. return filterNotifications(state, action.relationship);
  65. case NOTIFICATIONS_SETTING_CHANGE:
  66. return state.setIn(['settings', ...action.key], action.checked);
  67. default:
  68. return state;
  69. }
  70. };