The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

181 lines
5.6 KiB

  1. import api, { getLinks } from '../api'
  2. import Immutable from 'immutable';
  3. import IntlMessageFormat from 'intl-messageformat';
  4. import { unescape } from 'lodash';
  5. import { fetchRelationships } from './accounts';
  6. export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
  7. export const NOTIFICATIONS_REFRESH_REQUEST = 'NOTIFICATIONS_REFRESH_REQUEST';
  8. export const NOTIFICATIONS_REFRESH_SUCCESS = 'NOTIFICATIONS_REFRESH_SUCCESS';
  9. export const NOTIFICATIONS_REFRESH_FAIL = 'NOTIFICATIONS_REFRESH_FAIL';
  10. export const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST';
  11. export const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS';
  12. export const NOTIFICATIONS_EXPAND_FAIL = 'NOTIFICATIONS_EXPAND_FAIL';
  13. export const NOTIFICATIONS_CLEAR = 'NOTIFICATIONS_CLEAR';
  14. export const NOTIFICATIONS_SCROLL_TOP = 'NOTIFICATIONS_SCROLL_TOP';
  15. const fetchRelatedRelationships = (dispatch, notifications) => {
  16. const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
  17. if (accountIds > 0) {
  18. dispatch(fetchRelationships(accountIds));
  19. }
  20. };
  21. const unescapeHTML = (html) => unescape(html).replace(/<\/?\w+(?:\s[^>]*)?>/g, '');
  22. export function updateNotifications(notification, intlMessages, intlLocale) {
  23. return (dispatch, getState) => {
  24. const showAlert = getState().getIn(['settings', 'notifications', 'alerts', notification.type], true);
  25. const playSound = getState().getIn(['settings', 'notifications', 'sounds', notification.type], true);
  26. dispatch({
  27. type: NOTIFICATIONS_UPDATE,
  28. notification,
  29. account: notification.account,
  30. status: notification.status,
  31. meta: playSound ? { sound: 'boop' } : undefined
  32. });
  33. fetchRelatedRelationships(dispatch, [notification]);
  34. // Desktop notifications
  35. if (typeof window.Notification !== 'undefined' && showAlert) {
  36. const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
  37. const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : unescapeHTML(notification.status ? notification.status.content : '');
  38. new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
  39. }
  40. };
  41. };
  42. const excludeTypesFromSettings = state => state.getIn(['settings', 'notifications', 'shows']).filter(enabled => !enabled).keySeq().toJS();
  43. export function refreshNotifications() {
  44. return (dispatch, getState) => {
  45. const params = {};
  46. const ids = getState().getIn(['notifications', 'items']);
  47. let skipLoading = false;
  48. if (ids.size > 0) {
  49. params.since_id = ids.first().get('id');
  50. }
  51. if (getState().getIn(['notifications', 'loaded'])) {
  52. skipLoading = true;
  53. }
  54. params.exclude_types = excludeTypesFromSettings(getState());
  55. dispatch(refreshNotificationsRequest(skipLoading));
  56. api(getState).get('/api/v1/notifications', { params }).then(response => {
  57. const next = getLinks(response).refs.find(link => link.rel === 'next');
  58. dispatch(refreshNotificationsSuccess(response.data, skipLoading, next ? next.uri : null));
  59. fetchRelatedRelationships(dispatch, response.data);
  60. }).catch(error => {
  61. dispatch(refreshNotificationsFail(error, skipLoading));
  62. });
  63. };
  64. };
  65. export function refreshNotificationsRequest(skipLoading) {
  66. return {
  67. type: NOTIFICATIONS_REFRESH_REQUEST,
  68. skipLoading
  69. };
  70. };
  71. export function refreshNotificationsSuccess(notifications, skipLoading, next) {
  72. return {
  73. type: NOTIFICATIONS_REFRESH_SUCCESS,
  74. notifications,
  75. accounts: notifications.map(item => item.account),
  76. statuses: notifications.map(item => item.status).filter(status => !!status),
  77. skipLoading,
  78. next
  79. };
  80. };
  81. export function refreshNotificationsFail(error, skipLoading) {
  82. return {
  83. type: NOTIFICATIONS_REFRESH_FAIL,
  84. error,
  85. skipLoading
  86. };
  87. };
  88. export function expandNotifications() {
  89. return (dispatch, getState) => {
  90. const url = getState().getIn(['notifications', 'next'], null);
  91. const lastId = getState().getIn(['notifications', 'items']).last();
  92. if (url === null || getState().getIn(['notifications', 'isLoading'])) {
  93. return;
  94. }
  95. dispatch(expandNotificationsRequest());
  96. const params = {
  97. max_id: lastId,
  98. limit: 20
  99. };
  100. params.exclude_types = excludeTypesFromSettings(getState());
  101. api(getState).get(url, params).then(response => {
  102. const next = getLinks(response).refs.find(link => link.rel === 'next');
  103. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null));
  104. fetchRelatedRelationships(dispatch, response.data);
  105. }).catch(error => {
  106. dispatch(expandNotificationsFail(error));
  107. });
  108. };
  109. };
  110. export function expandNotificationsRequest() {
  111. return {
  112. type: NOTIFICATIONS_EXPAND_REQUEST
  113. };
  114. };
  115. export function expandNotificationsSuccess(notifications, next) {
  116. return {
  117. type: NOTIFICATIONS_EXPAND_SUCCESS,
  118. notifications,
  119. accounts: notifications.map(item => item.account),
  120. statuses: notifications.map(item => item.status).filter(status => !!status),
  121. next
  122. };
  123. };
  124. export function expandNotificationsFail(error) {
  125. return {
  126. type: NOTIFICATIONS_EXPAND_FAIL,
  127. error
  128. };
  129. };
  130. export function clearNotifications() {
  131. return (dispatch, getState) => {
  132. dispatch({
  133. type: NOTIFICATIONS_CLEAR
  134. });
  135. api(getState).post('/api/v1/notifications/clear');
  136. };
  137. };
  138. export function scrollTopNotifications(top) {
  139. return {
  140. type: NOTIFICATIONS_SCROLL_TOP,
  141. top
  142. };
  143. };