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.

178 lines
5.5 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. const excludeTypesFromSettings = state => state.getIn(['settings', 'notifications', 'shows']).filter(enabled => !enabled).keySeq().toJS();
  41. export function refreshNotifications() {
  42. return (dispatch, getState) => {
  43. const params = {};
  44. const ids = getState().getIn(['notifications', 'items']);
  45. let skipLoading = false;
  46. if (ids.size > 0) {
  47. params.since_id = ids.first().get('id');
  48. }
  49. if (getState().getIn(['notifications', 'loaded'])) {
  50. skipLoading = true;
  51. }
  52. params.exclude_types = excludeTypesFromSettings(getState());
  53. dispatch(refreshNotificationsRequest(skipLoading));
  54. api(getState).get('/api/v1/notifications', { params }).then(response => {
  55. const next = getLinks(response).refs.find(link => link.rel === 'next');
  56. dispatch(refreshNotificationsSuccess(response.data, skipLoading, next ? next.uri : null));
  57. fetchRelatedRelationships(dispatch, response.data);
  58. }).catch(error => {
  59. dispatch(refreshNotificationsFail(error, skipLoading));
  60. });
  61. };
  62. };
  63. export function refreshNotificationsRequest(skipLoading) {
  64. return {
  65. type: NOTIFICATIONS_REFRESH_REQUEST,
  66. skipLoading
  67. };
  68. };
  69. export function refreshNotificationsSuccess(notifications, skipLoading, next) {
  70. return {
  71. type: NOTIFICATIONS_REFRESH_SUCCESS,
  72. notifications,
  73. accounts: notifications.map(item => item.account),
  74. statuses: notifications.map(item => item.status).filter(status => !!status),
  75. skipLoading,
  76. next
  77. };
  78. };
  79. export function refreshNotificationsFail(error, skipLoading) {
  80. return {
  81. type: NOTIFICATIONS_REFRESH_FAIL,
  82. error,
  83. skipLoading
  84. };
  85. };
  86. export function expandNotifications() {
  87. return (dispatch, getState) => {
  88. const url = getState().getIn(['notifications', 'next'], null);
  89. const lastId = getState().getIn(['notifications', 'items']).last();
  90. if (url === null || getState().getIn(['notifications', 'isLoading'])) {
  91. return;
  92. }
  93. dispatch(expandNotificationsRequest());
  94. const params = {
  95. max_id: lastId,
  96. limit: 20
  97. };
  98. params.exclude_types = excludeTypesFromSettings(getState());
  99. api(getState).get(url, params).then(response => {
  100. const next = getLinks(response).refs.find(link => link.rel === 'next');
  101. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
  102. fetchRelatedRelationships(dispatch, response.data);
  103. }).catch(error => {
  104. dispatch(expandNotificationsFail(error));
  105. });
  106. };
  107. };
  108. export function expandNotificationsRequest() {
  109. return {
  110. type: NOTIFICATIONS_EXPAND_REQUEST
  111. };
  112. };
  113. export function expandNotificationsSuccess(notifications, next) {
  114. return {
  115. type: NOTIFICATIONS_EXPAND_SUCCESS,
  116. notifications,
  117. accounts: notifications.map(item => item.account),
  118. statuses: notifications.map(item => item.status).filter(status => !!status),
  119. next
  120. };
  121. };
  122. export function expandNotificationsFail(error) {
  123. return {
  124. type: NOTIFICATIONS_EXPAND_FAIL,
  125. error
  126. };
  127. };
  128. export function clearNotifications() {
  129. return (dispatch, getState) => {
  130. dispatch({
  131. type: NOTIFICATIONS_CLEAR
  132. });
  133. api(getState).post('/api/v1/notifications/clear');
  134. };
  135. };
  136. export function scrollTopNotifications(top) {
  137. return {
  138. type: NOTIFICATIONS_SCROLL_TOP,
  139. top
  140. };
  141. };