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.

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