The code powering m.abunchtell.com https://m.abunchtell.com
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

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