The code powering m.abunchtell.com https://m.abunchtell.com
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

201 righe
6.2 KiB

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