The code powering m.abunchtell.com https://m.abunchtell.com
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

139 lines
4.7 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. if (usePendingItems) {
  36. return state.update('pendingItems', list => list.unshift(notificationToMap(notification))).update('unread', unread => unread + 1);
  37. }
  38. const top = state.get('top');
  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, 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. mutable.update(usePendingItems ? 'pendingItems' : 'items', list => {
  57. const lastIndex = 1 + list.findLastIndex(
  58. item => item !== null && (compareId(item.get('id'), items.last().get('id')) > 0 || item.get('id') === items.last().get('id'))
  59. );
  60. const firstIndex = 1 + list.take(lastIndex).findLastIndex(
  61. item => item !== null && compareId(item.get('id'), items.first().get('id')) > 0
  62. );
  63. return list.take(firstIndex).concat(items, list.skip(lastIndex));
  64. });
  65. }
  66. if (!next) {
  67. mutable.set('hasMore', false);
  68. }
  69. mutable.set('isLoading', false);
  70. });
  71. };
  72. const filterNotifications = (state, accountIds) => {
  73. const helper = list => list.filterNot(item => item !== null && accountIds.includes(item.get('account')));
  74. return state.update('items', helper).update('pendingItems', helper);
  75. };
  76. const updateTop = (state, top) => {
  77. if (top) {
  78. state = state.set('unread', state.get('pendingItems').size);
  79. }
  80. return state.set('top', top);
  81. };
  82. const deleteByStatus = (state, statusId) => {
  83. const helper = list => list.filterNot(item => item !== null && item.get('status') === statusId);
  84. return state.update('items', helper).update('pendingItems', helper);
  85. };
  86. export default function notifications(state = initialState, action) {
  87. switch(action.type) {
  88. case NOTIFICATIONS_LOAD_PENDING:
  89. return state.update('items', list => state.get('pendingItems').concat(list.take(40))).set('pendingItems', ImmutableList()).set('unread', 0);
  90. case NOTIFICATIONS_EXPAND_REQUEST:
  91. return state.set('isLoading', true);
  92. case NOTIFICATIONS_EXPAND_FAIL:
  93. return state.set('isLoading', false);
  94. case NOTIFICATIONS_FILTER_SET:
  95. return state.set('items', ImmutableList()).set('pendingItems', ImmutableList()).set('hasMore', true);
  96. case NOTIFICATIONS_SCROLL_TOP:
  97. return updateTop(state, action.top);
  98. case NOTIFICATIONS_UPDATE:
  99. return normalizeNotification(state, action.notification, action.usePendingItems);
  100. case NOTIFICATIONS_EXPAND_SUCCESS:
  101. return expandNormalizedNotifications(state, action.notifications, action.next, action.usePendingItems);
  102. case ACCOUNT_BLOCK_SUCCESS:
  103. return filterNotifications(state, [action.relationship.id]);
  104. case ACCOUNT_MUTE_SUCCESS:
  105. return action.relationship.muting_notifications ? filterNotifications(state, [action.relationship.id]) : state;
  106. case DOMAIN_BLOCK_SUCCESS:
  107. return filterNotifications(state, action.accounts);
  108. case NOTIFICATIONS_CLEAR:
  109. return state.set('items', ImmutableList()).set('pendingItems', ImmutableList()).set('hasMore', false);
  110. case TIMELINE_DELETE:
  111. return deleteByStatus(state, action.id);
  112. case TIMELINE_DISCONNECT:
  113. return action.timeline === 'home' ?
  114. state.update(action.usePendingItems ? 'pendingItems' : 'items', items => items.first() ? items.unshift(null) : items) :
  115. state;
  116. default:
  117. return state;
  118. }
  119. };