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.

190 lines
6.0 KiB

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