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.

154 lines
5.4 KiB

  1. import {
  2. NOTIFICATIONS_UPDATE,
  3. NOTIFICATIONS_EXPAND_SUCCESS,
  4. NOTIFICATIONS_EXPAND_REQUEST,
  5. NOTIFICATIONS_EXPAND_FAIL,
  6. NOTIFICATIONS_FILTER_SET,
  7. NOTIFICATIONS_CLEAR,
  8. NOTIFICATIONS_SCROLL_TOP,
  9. NOTIFICATIONS_LOAD_PENDING,
  10. NOTIFICATIONS_MOUNT,
  11. NOTIFICATIONS_UNMOUNT,
  12. } from '../actions/notifications';
  13. import {
  14. ACCOUNT_BLOCK_SUCCESS,
  15. ACCOUNT_MUTE_SUCCESS,
  16. FOLLOW_REQUEST_AUTHORIZE_SUCCESS,
  17. FOLLOW_REQUEST_REJECT_SUCCESS,
  18. } from '../actions/accounts';
  19. import { DOMAIN_BLOCK_SUCCESS } from 'mastodon/actions/domain_blocks';
  20. import { TIMELINE_DELETE, TIMELINE_DISCONNECT } from '../actions/timelines';
  21. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  22. import compareId from '../compare_id';
  23. const initialState = ImmutableMap({
  24. pendingItems: ImmutableList(),
  25. items: ImmutableList(),
  26. hasMore: true,
  27. top: false,
  28. mounted: false,
  29. unread: 0,
  30. isLoading: false,
  31. });
  32. const notificationToMap = notification => ImmutableMap({
  33. id: notification.id,
  34. type: notification.type,
  35. account: notification.account.id,
  36. created_at: notification.created_at,
  37. status: notification.status ? notification.status.id : null,
  38. });
  39. const normalizeNotification = (state, notification, usePendingItems) => {
  40. const top = state.get('top');
  41. if (usePendingItems || !state.get('pendingItems').isEmpty()) {
  42. return state.update('pendingItems', list => list.unshift(notificationToMap(notification))).update('unread', unread => unread + 1);
  43. }
  44. if (!top) {
  45. state = state.update('unread', unread => unread + 1);
  46. }
  47. return state.update('items', list => {
  48. if (top && list.size > 40) {
  49. list = list.take(20);
  50. }
  51. return list.unshift(notificationToMap(notification));
  52. });
  53. };
  54. const expandNormalizedNotifications = (state, notifications, next, isLoadingRecent, usePendingItems) => {
  55. let items = ImmutableList();
  56. notifications.forEach((n, i) => {
  57. items = items.set(i, notificationToMap(n));
  58. });
  59. return state.withMutations(mutable => {
  60. if (!items.isEmpty()) {
  61. usePendingItems = isLoadingRecent && (usePendingItems || !mutable.get('pendingItems').isEmpty());
  62. mutable.update(usePendingItems ? 'pendingItems' : 'items', list => {
  63. const lastIndex = 1 + list.findLastIndex(
  64. item => item !== null && (compareId(item.get('id'), items.last().get('id')) > 0 || item.get('id') === items.last().get('id')),
  65. );
  66. const firstIndex = 1 + list.take(lastIndex).findLastIndex(
  67. item => item !== null && compareId(item.get('id'), items.first().get('id')) > 0,
  68. );
  69. return list.take(firstIndex).concat(items, list.skip(lastIndex));
  70. });
  71. }
  72. if (!next) {
  73. mutable.set('hasMore', false);
  74. }
  75. mutable.set('isLoading', false);
  76. });
  77. };
  78. const filterNotifications = (state, accountIds, type) => {
  79. const helper = list => list.filterNot(item => item !== null && accountIds.includes(item.get('account')) && (type === undefined || type === item.get('type')));
  80. return state.update('items', helper).update('pendingItems', helper);
  81. };
  82. const updateTop = (state, top) => {
  83. if (top) {
  84. state = state.set('unread', state.get('pendingItems').size);
  85. }
  86. return state.set('top', top);
  87. };
  88. const deleteByStatus = (state, statusId) => {
  89. const helper = list => list.filterNot(item => item !== null && item.get('status') === statusId);
  90. return state.update('items', helper).update('pendingItems', helper);
  91. };
  92. export default function notifications(state = initialState, action) {
  93. switch(action.type) {
  94. case NOTIFICATIONS_LOAD_PENDING:
  95. return state.update('items', list => state.get('pendingItems').concat(list.take(40))).set('pendingItems', ImmutableList()).set('unread', 0);
  96. case NOTIFICATIONS_EXPAND_REQUEST:
  97. return state.set('isLoading', true);
  98. case NOTIFICATIONS_EXPAND_FAIL:
  99. return state.set('isLoading', false);
  100. case NOTIFICATIONS_FILTER_SET:
  101. return state.set('items', ImmutableList()).set('pendingItems', ImmutableList()).set('hasMore', true);
  102. case NOTIFICATIONS_SCROLL_TOP:
  103. return updateTop(state, action.top);
  104. case NOTIFICATIONS_UPDATE:
  105. return normalizeNotification(state, action.notification, action.usePendingItems);
  106. case NOTIFICATIONS_EXPAND_SUCCESS:
  107. return expandNormalizedNotifications(state, action.notifications, action.next, action.isLoadingRecent, action.usePendingItems);
  108. case ACCOUNT_BLOCK_SUCCESS:
  109. return filterNotifications(state, [action.relationship.id]);
  110. case ACCOUNT_MUTE_SUCCESS:
  111. return action.relationship.muting_notifications ? filterNotifications(state, [action.relationship.id]) : state;
  112. case DOMAIN_BLOCK_SUCCESS:
  113. return filterNotifications(state, action.accounts);
  114. case FOLLOW_REQUEST_AUTHORIZE_SUCCESS:
  115. case FOLLOW_REQUEST_REJECT_SUCCESS:
  116. return filterNotifications(state, [action.id], 'follow_request');
  117. case ACCOUNT_MUTE_SUCCESS:
  118. return action.relationship.muting_notifications ? filterNotifications(state, [action.relationship.id]) : state;
  119. case NOTIFICATIONS_CLEAR:
  120. return state.set('items', ImmutableList()).set('pendingItems', ImmutableList()).set('hasMore', false);
  121. case TIMELINE_DELETE:
  122. return deleteByStatus(state, action.id);
  123. case TIMELINE_DISCONNECT:
  124. return action.timeline === 'home' ?
  125. state.update(action.usePendingItems ? 'pendingItems' : 'items', items => items.first() ? items.unshift(null) : items) :
  126. state;
  127. case NOTIFICATIONS_MOUNT:
  128. return state.set('mounted', true);
  129. case NOTIFICATIONS_UNMOUNT:
  130. return state.set('mounted', false);
  131. default:
  132. return state;
  133. }
  134. };