You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.3 KiB

  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. const fetchRelatedRelationships = (dispatch, notifications) => {
  13. const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
  14. if (accountIds > 0) {
  15. dispatch(fetchRelationships(accountIds));
  16. }
  17. };
  18. export function updateNotifications(notification, intlMessages, intlLocale) {
  19. return (dispatch, getState) => {
  20. const showAlert = getState().getIn(['notifications', 'settings', 'alerts', notification.type], false);
  21. const playSound = getState().getIn(['notifications', 'settings', 'sounds', notification.type], false);
  22. dispatch({
  23. type: NOTIFICATIONS_UPDATE,
  24. notification,
  25. account: notification.account,
  26. status: notification.status,
  27. meta: playSound ? { sound: 'boop' } : null
  28. });
  29. fetchRelatedRelationships(dispatch, [notification]);
  30. // Desktop notifications
  31. if (typeof window.Notification !== 'undefined' && showAlert) {
  32. const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
  33. const body = $('<p>').html(notification.status ? notification.status.content : '').text();
  34. new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
  35. }
  36. };
  37. };
  38. export function refreshNotifications() {
  39. return (dispatch, getState) => {
  40. dispatch(refreshNotificationsRequest());
  41. const params = {};
  42. const ids = getState().getIn(['notifications', 'items']);
  43. if (ids.size > 0) {
  44. params.since_id = ids.first().get('id');
  45. }
  46. api(getState).get('/api/v1/notifications', { params }).then(response => {
  47. const next = getLinks(response).refs.find(link => link.rel === 'next');
  48. dispatch(refreshNotificationsSuccess(response.data, next ? next.uri : null));
  49. fetchRelatedRelationships(dispatch, response.data);
  50. }).catch(error => {
  51. dispatch(refreshNotificationsFail(error));
  52. });
  53. };
  54. };
  55. export function refreshNotificationsRequest() {
  56. return {
  57. type: NOTIFICATIONS_REFRESH_REQUEST
  58. };
  59. };
  60. export function refreshNotificationsSuccess(notifications, next) {
  61. return {
  62. type: NOTIFICATIONS_REFRESH_SUCCESS,
  63. notifications,
  64. accounts: notifications.map(item => item.account),
  65. statuses: notifications.map(item => item.status).filter(status => !!status),
  66. next
  67. };
  68. };
  69. export function refreshNotificationsFail(error) {
  70. return {
  71. type: NOTIFICATIONS_REFRESH_FAIL,
  72. error
  73. };
  74. };
  75. export function expandNotifications() {
  76. return (dispatch, getState) => {
  77. const url = getState().getIn(['notifications', 'next'], null);
  78. if (url === null) {
  79. return;
  80. }
  81. dispatch(expandNotificationsRequest());
  82. api(getState).get(url).then(response => {
  83. const next = getLinks(response).refs.find(link => link.rel === 'next');
  84. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
  85. fetchRelatedRelationships(dispatch, response.data);
  86. }).catch(error => {
  87. dispatch(expandNotificationsFail(error));
  88. });
  89. };
  90. };
  91. export function expandNotificationsRequest() {
  92. return {
  93. type: NOTIFICATIONS_EXPAND_REQUEST
  94. };
  95. };
  96. export function expandNotificationsSuccess(notifications, next) {
  97. return {
  98. type: NOTIFICATIONS_EXPAND_SUCCESS,
  99. notifications,
  100. accounts: notifications.map(item => item.account),
  101. statuses: notifications.map(item => item.status).filter(status => !!status),
  102. next
  103. };
  104. };
  105. export function expandNotificationsFail(error) {
  106. return {
  107. type: NOTIFICATIONS_EXPAND_FAIL,
  108. error
  109. };
  110. };