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.

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