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.

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