The code powering m.abunchtell.com https://m.abunchtell.com
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

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