闭社主体 forked from https://github.com/tootsuite/mastodon
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.

192 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. const messages = 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 url = getState().getIn(['notifications', 'next'], null);
  102. const lastId = getState().getIn(['notifications', 'items']).last();
  103. if (url === null || getState().getIn(['notifications', 'isLoading'])) {
  104. return;
  105. }
  106. dispatch(expandNotificationsRequest());
  107. const params = {
  108. max_id: lastId,
  109. limit: 20,
  110. };
  111. params.exclude_types = excludeTypesFromSettings(getState());
  112. api(getState).get(url, 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. };