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.

248 lines
7.7 KiB

  1. import {
  2. NOTIFICATIONS_MOUNT,
  3. NOTIFICATIONS_UNMOUNT,
  4. NOTIFICATIONS_SET_VISIBILITY,
  5. NOTIFICATIONS_UPDATE,
  6. NOTIFICATIONS_EXPAND_SUCCESS,
  7. NOTIFICATIONS_EXPAND_REQUEST,
  8. NOTIFICATIONS_EXPAND_FAIL,
  9. NOTIFICATIONS_FILTER_SET,
  10. NOTIFICATIONS_CLEAR,
  11. NOTIFICATIONS_SCROLL_TOP,
  12. NOTIFICATIONS_DELETE_MARKED_REQUEST,
  13. NOTIFICATIONS_DELETE_MARKED_SUCCESS,
  14. NOTIFICATION_MARK_FOR_DELETE,
  15. NOTIFICATIONS_DELETE_MARKED_FAIL,
  16. NOTIFICATIONS_ENTER_CLEARING_MODE,
  17. NOTIFICATIONS_MARK_ALL_FOR_DELETE,
  18. } from 'flavours/glitch/actions/notifications';
  19. import {
  20. ACCOUNT_BLOCK_SUCCESS,
  21. ACCOUNT_MUTE_SUCCESS,
  22. } from 'flavours/glitch/actions/accounts';
  23. import { TIMELINE_DELETE, TIMELINE_DISCONNECT } from 'flavours/glitch/actions/timelines';
  24. import { Map as ImmutableMap, List as ImmutableList } from 'immutable';
  25. import compareId from 'flavours/glitch/util/compare_id';
  26. const initialState = ImmutableMap({
  27. items: ImmutableList(),
  28. hasMore: true,
  29. top: true,
  30. mounted: 0,
  31. unread: 0,
  32. lastReadId: '0',
  33. isLoading: false,
  34. cleaningMode: false,
  35. isTabVisible: true,
  36. // notification removal mark of new notifs loaded whilst cleaningMode is true.
  37. markNewForDelete: false,
  38. });
  39. const notificationToMap = (state, notification) => ImmutableMap({
  40. id: notification.id,
  41. type: notification.type,
  42. account: notification.account.id,
  43. markedForDelete: state.get('markNewForDelete'),
  44. status: notification.status ? notification.status.id : null,
  45. });
  46. const normalizeNotification = (state, notification) => {
  47. const top = !shouldCountUnreadNotifications(state);
  48. if (top) {
  49. state = state.set('lastReadId', notification.id);
  50. } else {
  51. state = state.update('unread', unread => unread + 1);
  52. }
  53. return state.update('items', list => {
  54. if (top && list.size > 40) {
  55. list = list.take(20);
  56. }
  57. return list.unshift(notificationToMap(state, notification));
  58. });
  59. };
  60. const expandNormalizedNotifications = (state, notifications, next) => {
  61. const top = !(shouldCountUnreadNotifications(state));
  62. const lastReadId = state.get('lastReadId');
  63. let items = ImmutableList();
  64. notifications.forEach((n, i) => {
  65. items = items.set(i, notificationToMap(state, n));
  66. });
  67. return state.withMutations(mutable => {
  68. if (!items.isEmpty()) {
  69. mutable.update('items', list => {
  70. const lastIndex = 1 + list.findLastIndex(
  71. item => item !== null && (compareId(item.get('id'), items.last().get('id')) > 0 || item.get('id') === items.last().get('id'))
  72. );
  73. const firstIndex = 1 + list.take(lastIndex).findLastIndex(
  74. item => item !== null && compareId(item.get('id'), items.first().get('id')) > 0
  75. );
  76. return list.take(firstIndex).concat(items, list.skip(lastIndex));
  77. });
  78. }
  79. if (top) {
  80. if (!items.isEmpty()) {
  81. mutable.update('lastReadId', id => compareId(id, items.first().get('id')) > 0 ? id : items.first().get('id'));
  82. }
  83. } else {
  84. mutable.update('unread', unread => unread + items.filter(item => compareId(item.get('id'), lastReadId) > 0).size);
  85. }
  86. if (!next) {
  87. mutable.set('hasMore', false);
  88. }
  89. mutable.set('isLoading', false);
  90. });
  91. };
  92. const filterNotifications = (state, relationship) => {
  93. return state.update('items', list => list.filterNot(item => item !== null && item.get('account') === relationship.id));
  94. };
  95. const clearUnread = (state) => {
  96. state = state.set('unread', 0);
  97. const lastNotification = state.get('items').find(item => item !== null);
  98. return state.set('lastReadId', lastNotification ? lastNotification.get('id') : '0');
  99. }
  100. const updateTop = (state, top) => {
  101. state = state.set('top', top);
  102. if (!shouldCountUnreadNotifications(state)) {
  103. state = clearUnread(state);
  104. }
  105. return state.set('top', top);
  106. };
  107. const deleteByStatus = (state, statusId) => {
  108. const top = !(shouldCountUnreadNotifications(state));
  109. if (!top) {
  110. const lastReadId = state.get('lastReadId');
  111. const deletedUnread = state.get('items').filter(item => item !== null && item.get('status') === statusId && compareId(item.get('id'), lastReadId) > 0);
  112. state = state.update('unread', unread => unread - deletedUnread.size);
  113. }
  114. return state.update('items', list => list.filterNot(item => item !== null && item.get('status') === statusId));
  115. };
  116. const markForDelete = (state, notificationId, yes) => {
  117. return state.update('items', list => list.map(item => {
  118. if(item.get('id') === notificationId) {
  119. return item.set('markedForDelete', yes);
  120. } else {
  121. return item;
  122. }
  123. }));
  124. };
  125. const markAllForDelete = (state, yes) => {
  126. return state.update('items', list => list.map(item => {
  127. if(yes !== null) {
  128. return item.set('markedForDelete', yes);
  129. } else {
  130. return item.set('markedForDelete', !item.get('markedForDelete'));
  131. }
  132. }));
  133. };
  134. const unmarkAllForDelete = (state) => {
  135. return state.update('items', list => list.map(item => item.set('markedForDelete', false)));
  136. };
  137. const deleteMarkedNotifs = (state) => {
  138. return state.update('items', list => list.filterNot(item => item.get('markedForDelete')));
  139. };
  140. const updateMounted = (state) => {
  141. state = state.update('mounted', count => count + 1);
  142. if (!shouldCountUnreadNotifications(state)) {
  143. state = clearUnread(state);
  144. }
  145. return state;
  146. };
  147. const updateVisibility = (state, visibility) => {
  148. state = state.set('isTabVisible', visibility);
  149. if (!shouldCountUnreadNotifications(state)) {
  150. state = clearUnread(state);
  151. }
  152. return state;
  153. };
  154. const shouldCountUnreadNotifications = (state) => {
  155. return !(state.get('isTabVisible') && state.get('top') && state.get('mounted') > 0);
  156. };
  157. export default function notifications(state = initialState, action) {
  158. let st;
  159. switch(action.type) {
  160. case NOTIFICATIONS_MOUNT:
  161. return updateMounted(state);
  162. case NOTIFICATIONS_UNMOUNT:
  163. return state.update('mounted', count => count - 1);
  164. case NOTIFICATIONS_SET_VISIBILITY:
  165. return updateVisibility(state, action.visibility);
  166. case NOTIFICATIONS_EXPAND_REQUEST:
  167. case NOTIFICATIONS_DELETE_MARKED_REQUEST:
  168. return state.set('isLoading', true);
  169. case NOTIFICATIONS_DELETE_MARKED_FAIL:
  170. case NOTIFICATIONS_EXPAND_FAIL:
  171. return state.set('isLoading', false);
  172. case NOTIFICATIONS_FILTER_SET:
  173. return state.set('items', ImmutableList()).set('hasMore', true);
  174. case NOTIFICATIONS_SCROLL_TOP:
  175. return updateTop(state, action.top);
  176. case NOTIFICATIONS_UPDATE:
  177. return normalizeNotification(state, action.notification);
  178. case NOTIFICATIONS_EXPAND_SUCCESS:
  179. return expandNormalizedNotifications(state, action.notifications, action.next);
  180. case ACCOUNT_BLOCK_SUCCESS:
  181. case ACCOUNT_MUTE_SUCCESS:
  182. return filterNotifications(state, action.relationship);
  183. case NOTIFICATIONS_CLEAR:
  184. return state.set('items', ImmutableList()).set('hasMore', false);
  185. case TIMELINE_DELETE:
  186. return deleteByStatus(state, action.id);
  187. case TIMELINE_DISCONNECT:
  188. return action.timeline === 'home' ?
  189. state.update('items', items => items.first() ? items.unshift(null) : items) :
  190. state;
  191. case NOTIFICATION_MARK_FOR_DELETE:
  192. return markForDelete(state, action.id, action.yes);
  193. case NOTIFICATIONS_DELETE_MARKED_SUCCESS:
  194. return deleteMarkedNotifs(state).set('isLoading', false);
  195. case NOTIFICATIONS_ENTER_CLEARING_MODE:
  196. st = state.set('cleaningMode', action.yes);
  197. if (!action.yes) {
  198. return unmarkAllForDelete(st).set('markNewForDelete', false);
  199. } else {
  200. return st;
  201. }
  202. case NOTIFICATIONS_MARK_ALL_FOR_DELETE:
  203. st = state;
  204. if (action.yes === null) {
  205. // Toggle - this is a bit confusing, as it toggles the all-none mode
  206. //st = st.set('markNewForDelete', !st.get('markNewForDelete'));
  207. } else {
  208. st = st.set('markNewForDelete', action.yes);
  209. }
  210. return markAllForDelete(st, action.yes);
  211. default:
  212. return state;
  213. }
  214. };