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.

191 lines
6.3 KiB

  1. import api, { getLinks } from '../api';
  2. import IntlMessageFormat from 'intl-messageformat';
  3. import { fetchRelationships } from './accounts';
  4. import {
  5. importFetchedAccount,
  6. importFetchedAccounts,
  7. importFetchedStatus,
  8. importFetchedStatuses,
  9. } from './importer';
  10. import { defineMessages } from 'react-intl';
  11. import { List as ImmutableList } from 'immutable';
  12. import { unescapeHTML } from '../utils/html';
  13. import { getFilters, regexFromFilters } from '../selectors';
  14. export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
  15. export const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP';
  16. export const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST';
  17. export const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS';
  18. export const NOTIFICATIONS_EXPAND_FAIL = 'NOTIFICATIONS_EXPAND_FAIL';
  19. export const NOTIFICATIONS_FILTER_SET = 'NOTIFICATIONS_FILTER_SET';
  20. export const NOTIFICATIONS_CLEAR = 'NOTIFICATIONS_CLEAR';
  21. export const NOTIFICATIONS_SCROLL_TOP = 'NOTIFICATIONS_SCROLL_TOP';
  22. defineMessages({
  23. mention: { id: 'notification.mention', defaultMessage: '{name} mentioned you' },
  24. group: { id: 'notifications.group', defaultMessage: '{count} notifications' },
  25. });
  26. const fetchRelatedRelationships = (dispatch, notifications) => {
  27. const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
  28. if (accountIds.length > 0) {
  29. dispatch(fetchRelationships(accountIds));
  30. }
  31. };
  32. export function updateNotifications(notification, intlMessages, intlLocale) {
  33. return (dispatch, getState) => {
  34. const showInColumn = getState().getIn(['settings', 'notifications', 'shows', notification.type], true);
  35. const showAlert = getState().getIn(['settings', 'notifications', 'alerts', notification.type], true);
  36. const playSound = getState().getIn(['settings', 'notifications', 'sounds', notification.type], true);
  37. const filters = getFilters(getState(), { contextType: 'notifications' });
  38. let filtered = false;
  39. if (notification.type === 'mention') {
  40. const regex = regexFromFilters(filters);
  41. const searchIndex = notification.status.spoiler_text + '\n' + unescapeHTML(notification.status.content);
  42. filtered = regex && regex.test(searchIndex);
  43. }
  44. if (showInColumn) {
  45. dispatch(importFetchedAccount(notification.account));
  46. if (notification.status) {
  47. dispatch(importFetchedStatus(notification.status));
  48. }
  49. dispatch({
  50. type: NOTIFICATIONS_UPDATE,
  51. notification,
  52. meta: (playSound && !filtered) ? { sound: 'boop' } : undefined,
  53. });
  54. fetchRelatedRelationships(dispatch, [notification]);
  55. } else if (playSound && !filtered) {
  56. dispatch({
  57. type: NOTIFICATIONS_UPDATE_NOOP,
  58. meta: { sound: 'boop' },
  59. });
  60. }
  61. // Desktop notifications
  62. if (typeof window.Notification !== 'undefined' && showAlert && !filtered) {
  63. const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
  64. const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : unescapeHTML(notification.status ? notification.status.content : '');
  65. const notify = new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
  66. notify.addEventListener('click', () => {
  67. window.focus();
  68. notify.close();
  69. });
  70. }
  71. };
  72. };
  73. const excludeTypesFromSettings = state => state.getIn(['settings', 'notifications', 'shows']).filter(enabled => !enabled).keySeq().toJS();
  74. const excludeTypesFromFilter = filter => {
  75. const allTypes = ImmutableList(['follow', 'favourite', 'reblog', 'mention']);
  76. return allTypes.filterNot(item => item === filter).toJS();
  77. };
  78. const noOp = () => {};
  79. export function expandNotifications({ maxId } = {}, done = noOp) {
  80. return (dispatch, getState) => {
  81. const activeFilter = getState().getIn(['settings', 'notifications', 'quickFilter', 'active']);
  82. const notifications = getState().get('notifications');
  83. const isLoadingMore = !!maxId;
  84. if (notifications.get('isLoading')) {
  85. done();
  86. return;
  87. }
  88. const params = {
  89. max_id: maxId,
  90. exclude_types: activeFilter === 'all'
  91. ? excludeTypesFromSettings(getState())
  92. : excludeTypesFromFilter(activeFilter),
  93. };
  94. if (!maxId && notifications.get('items').size > 0) {
  95. params.since_id = notifications.getIn(['items', 0, 'id']);
  96. }
  97. dispatch(expandNotificationsRequest(isLoadingMore));
  98. api(getState).get('/api/v1/notifications', { params }).then(response => {
  99. const next = getLinks(response).refs.find(link => link.rel === 'next');
  100. dispatch(importFetchedAccounts(response.data.map(item => item.account)));
  101. dispatch(importFetchedStatuses(response.data.map(item => item.status).filter(status => !!status)));
  102. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null, isLoadingMore));
  103. fetchRelatedRelationships(dispatch, response.data);
  104. done();
  105. }).catch(error => {
  106. dispatch(expandNotificationsFail(error, isLoadingMore));
  107. done();
  108. });
  109. };
  110. };
  111. export function expandNotificationsRequest(isLoadingMore) {
  112. return {
  113. type: NOTIFICATIONS_EXPAND_REQUEST,
  114. skipLoading: !isLoadingMore,
  115. };
  116. };
  117. export function expandNotificationsSuccess(notifications, next, isLoadingMore) {
  118. return {
  119. type: NOTIFICATIONS_EXPAND_SUCCESS,
  120. notifications,
  121. next,
  122. skipLoading: !isLoadingMore,
  123. };
  124. };
  125. export function expandNotificationsFail(error, isLoadingMore) {
  126. return {
  127. type: NOTIFICATIONS_EXPAND_FAIL,
  128. error,
  129. skipLoading: !isLoadingMore,
  130. };
  131. };
  132. export function clearNotifications() {
  133. return (dispatch, getState) => {
  134. dispatch({
  135. type: NOTIFICATIONS_CLEAR,
  136. });
  137. api(getState).post('/api/v1/notifications/clear');
  138. };
  139. };
  140. export function scrollTopNotifications(top) {
  141. return {
  142. type: NOTIFICATIONS_SCROLL_TOP,
  143. top,
  144. };
  145. };
  146. export function setFilter (filterType) {
  147. return dispatch => {
  148. dispatch({
  149. type: NOTIFICATIONS_FILTER_SET,
  150. path: ['notifications', 'quickFilter', 'active'],
  151. value: filterType,
  152. });
  153. dispatch(expandNotifications());
  154. };
  155. };