notifications.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import api, { getLinks } from '../api'
  2. import Immutable from 'immutable';
  3. import IntlMessageFormat from 'intl-messageformat';
  4. import { fetchRelationships } from './accounts';
  5. export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
  6. export const NOTIFICATIONS_REFRESH_REQUEST = 'NOTIFICATIONS_REFRESH_REQUEST';
  7. export const NOTIFICATIONS_REFRESH_SUCCESS = 'NOTIFICATIONS_REFRESH_SUCCESS';
  8. export const NOTIFICATIONS_REFRESH_FAIL = 'NOTIFICATIONS_REFRESH_FAIL';
  9. export const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST';
  10. export const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS';
  11. export const NOTIFICATIONS_EXPAND_FAIL = 'NOTIFICATIONS_EXPAND_FAIL';
  12. export const NOTIFICATIONS_CLEAR = 'NOTIFICATIONS_CLEAR';
  13. export const NOTIFICATIONS_SCROLL_TOP = 'NOTIFICATIONS_SCROLL_TOP';
  14. const fetchRelatedRelationships = (dispatch, notifications) => {
  15. const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
  16. if (accountIds > 0) {
  17. dispatch(fetchRelationships(accountIds));
  18. }
  19. };
  20. export function updateNotifications(notification, intlMessages, intlLocale) {
  21. return (dispatch, getState) => {
  22. const showAlert = getState().getIn(['settings', 'notifications', 'alerts', notification.type], true);
  23. const playSound = getState().getIn(['settings', 'notifications', 'sounds', notification.type], true);
  24. dispatch({
  25. type: NOTIFICATIONS_UPDATE,
  26. notification,
  27. account: notification.account,
  28. status: notification.status,
  29. meta: playSound ? { sound: 'boop' } : undefined
  30. });
  31. fetchRelatedRelationships(dispatch, [notification]);
  32. // Desktop notifications
  33. if (typeof window.Notification !== 'undefined' && showAlert) {
  34. const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
  35. const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : $('<p>').html(notification.status ? notification.status.content : '').text();
  36. new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
  37. }
  38. };
  39. };
  40. const excludeTypesFromSettings = state => state.getIn(['settings', 'notifications', 'shows']).filter(enabled => !enabled).keySeq().toJS();
  41. export function refreshNotifications() {
  42. return (dispatch, getState) => {
  43. dispatch(refreshNotificationsRequest());
  44. const params = {};
  45. const ids = getState().getIn(['notifications', 'items']);
  46. if (ids.size > 0) {
  47. params.since_id = ids.first().get('id');
  48. }
  49. params.exclude_types = excludeTypesFromSettings(getState());
  50. api(getState).get('/api/v1/notifications', { params }).then(response => {
  51. const next = getLinks(response).refs.find(link => link.rel === 'next');
  52. dispatch(refreshNotificationsSuccess(response.data, next ? next.uri : null));
  53. fetchRelatedRelationships(dispatch, response.data);
  54. }).catch(error => {
  55. dispatch(refreshNotificationsFail(error));
  56. });
  57. };
  58. };
  59. export function refreshNotificationsRequest() {
  60. return {
  61. type: NOTIFICATIONS_REFRESH_REQUEST
  62. };
  63. };
  64. export function refreshNotificationsSuccess(notifications, next) {
  65. return {
  66. type: NOTIFICATIONS_REFRESH_SUCCESS,
  67. notifications,
  68. accounts: notifications.map(item => item.account),
  69. statuses: notifications.map(item => item.status).filter(status => !!status),
  70. next
  71. };
  72. };
  73. export function refreshNotificationsFail(error) {
  74. return {
  75. type: NOTIFICATIONS_REFRESH_FAIL,
  76. error
  77. };
  78. };
  79. export function expandNotifications() {
  80. return (dispatch, getState) => {
  81. const url = getState().getIn(['notifications', 'next'], null);
  82. if (url === null || getState().getIn(['notifications', 'isLoading'])) {
  83. return;
  84. }
  85. dispatch(expandNotificationsRequest());
  86. const params = {};
  87. params.exclude_types = excludeTypesFromSettings(getState());
  88. api(getState).get(url, params).then(response => {
  89. const next = getLinks(response).refs.find(link => link.rel === 'next');
  90. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
  91. fetchRelatedRelationships(dispatch, response.data);
  92. }).catch(error => {
  93. dispatch(expandNotificationsFail(error));
  94. });
  95. };
  96. };
  97. export function expandNotificationsRequest() {
  98. return {
  99. type: NOTIFICATIONS_EXPAND_REQUEST
  100. };
  101. };
  102. export function expandNotificationsSuccess(notifications, next) {
  103. return {
  104. type: NOTIFICATIONS_EXPAND_SUCCESS,
  105. notifications,
  106. accounts: notifications.map(item => item.account),
  107. statuses: notifications.map(item => item.status).filter(status => !!status),
  108. next
  109. };
  110. };
  111. export function expandNotificationsFail(error) {
  112. return {
  113. type: NOTIFICATIONS_EXPAND_FAIL,
  114. error
  115. };
  116. };
  117. export function clearNotifications() {
  118. return (dispatch, getState) => {
  119. dispatch({
  120. type: NOTIFICATIONS_CLEAR
  121. });
  122. api(getState).post('/api/v1/notifications/clear');
  123. };
  124. };
  125. export function scrollTopNotifications(top) {
  126. return {
  127. type: NOTIFICATIONS_SCROLL_TOP,
  128. top
  129. };
  130. };