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.

153 lines
4.7 KiB

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