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.

189 lines
5.9 KiB

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