logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

notifications.js (4811B)


  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. export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
  12. export const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP';
  13. export const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST';
  14. export const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS';
  15. export const NOTIFICATIONS_EXPAND_FAIL = 'NOTIFICATIONS_EXPAND_FAIL';
  16. export const NOTIFICATIONS_CLEAR = 'NOTIFICATIONS_CLEAR';
  17. export const NOTIFICATIONS_SCROLL_TOP = 'NOTIFICATIONS_SCROLL_TOP';
  18. defineMessages({
  19. mention: { id: 'notification.mention', defaultMessage: '{name} mentioned you' },
  20. });
  21. const fetchRelatedRelationships = (dispatch, notifications) => {
  22. const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
  23. if (accountIds.length > 0) {
  24. dispatch(fetchRelationships(accountIds));
  25. }
  26. };
  27. const unescapeHTML = (html) => {
  28. const wrapper = document.createElement('div');
  29. html = html.replace(/<br \/>|<br>|\n/g, ' ');
  30. wrapper.innerHTML = html;
  31. return wrapper.textContent;
  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. if (showInColumn) {
  39. dispatch(importFetchedAccount(notification.account));
  40. if (notification.status) {
  41. dispatch(importFetchedStatus(notification.status));
  42. }
  43. dispatch({
  44. type: NOTIFICATIONS_UPDATE,
  45. notification,
  46. meta: playSound ? { sound: 'boop' } : undefined,
  47. });
  48. fetchRelatedRelationships(dispatch, [notification]);
  49. } else if (playSound) {
  50. dispatch({
  51. type: NOTIFICATIONS_UPDATE_NOOP,
  52. meta: { sound: 'boop' },
  53. });
  54. }
  55. // Desktop notifications
  56. if (typeof window.Notification !== 'undefined' && showAlert) {
  57. const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
  58. const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : unescapeHTML(notification.status ? notification.status.content : '');
  59. const notify = new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
  60. notify.addEventListener('click', () => {
  61. window.focus();
  62. notify.close();
  63. });
  64. }
  65. };
  66. };
  67. const excludeTypesFromSettings = state => state.getIn(['settings', 'notifications', 'shows']).filter(enabled => !enabled).keySeq().toJS();
  68. export function expandNotifications({ maxId } = {}) {
  69. return (dispatch, getState) => {
  70. if (getState().getIn(['notifications', 'isLoading'])) {
  71. return;
  72. }
  73. const params = {
  74. max_id: maxId,
  75. exclude_types: excludeTypesFromSettings(getState()),
  76. };
  77. dispatch(expandNotificationsRequest());
  78. api(getState).get('/api/v1/notifications', { params }).then(response => {
  79. const next = getLinks(response).refs.find(link => link.rel === 'next');
  80. dispatch(importFetchedAccounts(response.data.map(item => item.account)));
  81. dispatch(importFetchedStatuses(response.data.map(item => item.status).filter(status => !!status)));
  82. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
  83. fetchRelatedRelationships(dispatch, response.data);
  84. }).catch(error => {
  85. dispatch(expandNotificationsFail(error));
  86. });
  87. };
  88. };
  89. export function expandNotificationsRequest() {
  90. return {
  91. type: NOTIFICATIONS_EXPAND_REQUEST,
  92. };
  93. };
  94. export function expandNotificationsSuccess(notifications, next) {
  95. return {
  96. type: NOTIFICATIONS_EXPAND_SUCCESS,
  97. notifications,
  98. next,
  99. };
  100. };
  101. export function expandNotificationsFail(error) {
  102. return {
  103. type: NOTIFICATIONS_EXPAND_FAIL,
  104. error,
  105. };
  106. };
  107. export function clearNotifications() {
  108. return (dispatch, getState) => {
  109. dispatch({
  110. type: NOTIFICATIONS_CLEAR,
  111. });
  112. api(getState).post('/api/v1/notifications/clear');
  113. };
  114. };
  115. export function scrollTopNotifications(top) {
  116. return {
  117. type: NOTIFICATIONS_SCROLL_TOP,
  118. top,
  119. };
  120. };