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.

161 lines
4.9 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. 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. export function refreshNotifications() {
  41. return (dispatch, getState) => {
  42. dispatch(refreshNotificationsRequest());
  43. const params = {};
  44. const ids = getState().getIn(['notifications', 'items']);
  45. if (ids.size > 0) {
  46. params.since_id = ids.first().get('id');
  47. }
  48. api(getState).get('/api/v1/notifications', { params }).then(response => {
  49. const next = getLinks(response).refs.find(link => link.rel === 'next');
  50. dispatch(refreshNotificationsSuccess(response.data, next ? next.uri : null));
  51. fetchRelatedRelationships(dispatch, response.data);
  52. }).catch(error => {
  53. dispatch(refreshNotificationsFail(error));
  54. });
  55. };
  56. };
  57. export function refreshNotificationsRequest() {
  58. return {
  59. type: NOTIFICATIONS_REFRESH_REQUEST
  60. };
  61. };
  62. export function refreshNotificationsSuccess(notifications, next) {
  63. return {
  64. type: NOTIFICATIONS_REFRESH_SUCCESS,
  65. notifications,
  66. accounts: notifications.map(item => item.account),
  67. statuses: notifications.map(item => item.status).filter(status => !!status),
  68. next
  69. };
  70. };
  71. export function refreshNotificationsFail(error) {
  72. return {
  73. type: NOTIFICATIONS_REFRESH_FAIL,
  74. error
  75. };
  76. };
  77. export function expandNotifications() {
  78. return (dispatch, getState) => {
  79. const url = getState().getIn(['notifications', 'next'], null);
  80. if (url === null || getState().getIn(['notifications', 'isLoading'])) {
  81. return;
  82. }
  83. dispatch(expandNotificationsRequest());
  84. api(getState).get(url, {
  85. params: {
  86. limit: 5
  87. }
  88. }).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. };