The code powering m.abunchtell.com https://m.abunchtell.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

136 linhas
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. dispatch(importFetchedStatus(notification.status));
  38. dispatch({
  39. type: NOTIFICATIONS_UPDATE,
  40. notification,
  41. meta: playSound ? { sound: 'boop' } : undefined,
  42. });
  43. fetchRelatedRelationships(dispatch, [notification]);
  44. // Desktop notifications
  45. if (typeof window.Notification !== 'undefined' && showAlert) {
  46. const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
  47. const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : unescapeHTML(notification.status ? notification.status.content : '');
  48. const notify = new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
  49. notify.addEventListener('click', () => {
  50. window.focus();
  51. notify.close();
  52. });
  53. }
  54. };
  55. };
  56. const excludeTypesFromSettings = state => state.getIn(['settings', 'notifications', 'shows']).filter(enabled => !enabled).keySeq().toJS();
  57. export function expandNotifications({ maxId } = {}) {
  58. return (dispatch, getState) => {
  59. if (getState().getIn(['notifications', 'isLoading'])) {
  60. return;
  61. }
  62. const params = {
  63. max_id: maxId,
  64. exclude_types: excludeTypesFromSettings(getState()),
  65. };
  66. dispatch(expandNotificationsRequest());
  67. api(getState).get('/api/v1/notifications', { params }).then(response => {
  68. const next = getLinks(response).refs.find(link => link.rel === 'next');
  69. dispatch(importFetchedAccounts(response.data.map(item => item.account)));
  70. dispatch(importFetchedStatuses(response.data.map(item => item.status).filter(status => !!status)));
  71. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
  72. fetchRelatedRelationships(dispatch, response.data);
  73. }).catch(error => {
  74. dispatch(expandNotificationsFail(error));
  75. });
  76. };
  77. };
  78. export function expandNotificationsRequest() {
  79. return {
  80. type: NOTIFICATIONS_EXPAND_REQUEST,
  81. };
  82. };
  83. export function expandNotificationsSuccess(notifications, next) {
  84. return {
  85. type: NOTIFICATIONS_EXPAND_SUCCESS,
  86. notifications,
  87. next,
  88. };
  89. };
  90. export function expandNotificationsFail(error) {
  91. return {
  92. type: NOTIFICATIONS_EXPAND_FAIL,
  93. error,
  94. };
  95. };
  96. export function clearNotifications() {
  97. return (dispatch, getState) => {
  98. dispatch({
  99. type: NOTIFICATIONS_CLEAR,
  100. });
  101. api(getState).post('/api/v1/notifications/clear');
  102. };
  103. };
  104. export function scrollTopNotifications(top) {
  105. return {
  106. type: NOTIFICATIONS_SCROLL_TOP,
  107. top,
  108. };
  109. };