The code powering m.abunchtell.com https://m.abunchtell.com
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

218 wiersze
7.2 KiB

  1. import api, { getLinks } from '../api';
  2. import IntlMessageFormat from 'intl-messageformat';
  3. import { fetchRelationships } from './accounts';
  4. import {
  5. importFetchedAccount,
  6. importFetchedAccounts,
  7. importFetchedStatus,
  8. importFetchedStatuses,
  9. } from './importer';
  10. import { saveSettings } from './settings';
  11. import { defineMessages } from 'react-intl';
  12. import { List as ImmutableList } from 'immutable';
  13. import { unescapeHTML } from '../utils/html';
  14. import { getFiltersRegex } from '../selectors';
  15. import { usePendingItems as preferPendingItems } from 'mastodon/initial_state';
  16. import compareId from 'mastodon/compare_id';
  17. export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
  18. export const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP';
  19. export const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST';
  20. export const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS';
  21. export const NOTIFICATIONS_EXPAND_FAIL = 'NOTIFICATIONS_EXPAND_FAIL';
  22. export const NOTIFICATIONS_FILTER_SET = 'NOTIFICATIONS_FILTER_SET';
  23. export const NOTIFICATIONS_CLEAR = 'NOTIFICATIONS_CLEAR';
  24. export const NOTIFICATIONS_SCROLL_TOP = 'NOTIFICATIONS_SCROLL_TOP';
  25. export const NOTIFICATIONS_LOAD_PENDING = 'NOTIFICATIONS_LOAD_PENDING';
  26. defineMessages({
  27. mention: { id: 'notification.mention', defaultMessage: '{name} mentioned you' },
  28. group: { id: 'notifications.group', defaultMessage: '{count} notifications' },
  29. });
  30. const fetchRelatedRelationships = (dispatch, notifications) => {
  31. const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
  32. if (accountIds.length > 0) {
  33. dispatch(fetchRelationships(accountIds));
  34. }
  35. };
  36. export const loadPending = () => ({
  37. type: NOTIFICATIONS_LOAD_PENDING,
  38. });
  39. export function updateNotifications(notification, intlMessages, intlLocale) {
  40. return (dispatch, getState) => {
  41. const showInColumn = getState().getIn(['settings', 'notifications', 'shows', notification.type], true);
  42. const showAlert = getState().getIn(['settings', 'notifications', 'alerts', notification.type], true);
  43. const playSound = getState().getIn(['settings', 'notifications', 'sounds', notification.type], true);
  44. const filters = getFiltersRegex(getState(), { contextType: 'notifications' });
  45. let filtered = false;
  46. if (notification.type === 'mention') {
  47. const dropRegex = filters[0];
  48. const regex = filters[1];
  49. const searchIndex = notification.status.spoiler_text + '\n' + unescapeHTML(notification.status.content);
  50. if (dropRegex && dropRegex.test(searchIndex)) {
  51. return;
  52. }
  53. filtered = regex && regex.test(searchIndex);
  54. }
  55. if (showInColumn) {
  56. dispatch(importFetchedAccount(notification.account));
  57. if (notification.status) {
  58. dispatch(importFetchedStatus(notification.status));
  59. }
  60. dispatch({
  61. type: NOTIFICATIONS_UPDATE,
  62. notification,
  63. usePendingItems: preferPendingItems,
  64. meta: (playSound && !filtered) ? { sound: 'boop' } : undefined,
  65. });
  66. fetchRelatedRelationships(dispatch, [notification]);
  67. } else if (playSound && !filtered) {
  68. dispatch({
  69. type: NOTIFICATIONS_UPDATE_NOOP,
  70. meta: { sound: 'boop' },
  71. });
  72. }
  73. // Desktop notifications
  74. if (typeof window.Notification !== 'undefined' && showAlert && !filtered) {
  75. const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
  76. const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : unescapeHTML(notification.status ? notification.status.content : '');
  77. const notify = new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
  78. notify.addEventListener('click', () => {
  79. window.focus();
  80. notify.close();
  81. });
  82. }
  83. };
  84. };
  85. const excludeTypesFromSettings = state => state.getIn(['settings', 'notifications', 'shows']).filter(enabled => !enabled).keySeq().toJS();
  86. const excludeTypesFromFilter = filter => {
  87. const allTypes = ImmutableList(['follow', 'favourite', 'reblog', 'mention', 'poll']);
  88. return allTypes.filterNot(item => item === filter).toJS();
  89. };
  90. const noOp = () => {};
  91. export function expandNotifications({ maxId } = {}, done = noOp) {
  92. return (dispatch, getState) => {
  93. const activeFilter = getState().getIn(['settings', 'notifications', 'quickFilter', 'active']);
  94. const notifications = getState().get('notifications');
  95. const isLoadingMore = !!maxId;
  96. if (notifications.get('isLoading')) {
  97. done();
  98. return;
  99. }
  100. const params = {
  101. max_id: maxId,
  102. exclude_types: activeFilter === 'all'
  103. ? excludeTypesFromSettings(getState())
  104. : excludeTypesFromFilter(activeFilter),
  105. };
  106. if (!params.max_id && (notifications.get('items', ImmutableList()).size + notifications.get('pendingItems', ImmutableList()).size) > 0) {
  107. const a = notifications.getIn(['pendingItems', 0, 'id']);
  108. const b = notifications.getIn(['items', 0, 'id']);
  109. if (a && b && compareId(a, b) > 0) {
  110. params.since_id = a;
  111. } else {
  112. params.since_id = b || a;
  113. }
  114. }
  115. const isLoadingRecent = !!params.since_id;
  116. dispatch(expandNotificationsRequest(isLoadingMore));
  117. api(getState).get('/api/v1/notifications', { params }).then(response => {
  118. const next = getLinks(response).refs.find(link => link.rel === 'next');
  119. dispatch(importFetchedAccounts(response.data.map(item => item.account)));
  120. dispatch(importFetchedStatuses(response.data.map(item => item.status).filter(status => !!status)));
  121. dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null, isLoadingMore, isLoadingRecent, isLoadingRecent && preferPendingItems));
  122. fetchRelatedRelationships(dispatch, response.data);
  123. done();
  124. }).catch(error => {
  125. dispatch(expandNotificationsFail(error, isLoadingMore));
  126. done();
  127. });
  128. };
  129. };
  130. export function expandNotificationsRequest(isLoadingMore) {
  131. return {
  132. type: NOTIFICATIONS_EXPAND_REQUEST,
  133. skipLoading: !isLoadingMore,
  134. };
  135. };
  136. export function expandNotificationsSuccess(notifications, next, isLoadingMore, isLoadingRecent, usePendingItems) {
  137. return {
  138. type: NOTIFICATIONS_EXPAND_SUCCESS,
  139. notifications,
  140. next,
  141. isLoadingRecent: isLoadingRecent,
  142. usePendingItems,
  143. skipLoading: !isLoadingMore,
  144. };
  145. };
  146. export function expandNotificationsFail(error, isLoadingMore) {
  147. return {
  148. type: NOTIFICATIONS_EXPAND_FAIL,
  149. error,
  150. skipLoading: !isLoadingMore,
  151. };
  152. };
  153. export function clearNotifications() {
  154. return (dispatch, getState) => {
  155. dispatch({
  156. type: NOTIFICATIONS_CLEAR,
  157. });
  158. api(getState).post('/api/v1/notifications/clear');
  159. };
  160. };
  161. export function scrollTopNotifications(top) {
  162. return {
  163. type: NOTIFICATIONS_SCROLL_TOP,
  164. top,
  165. };
  166. };
  167. export function setFilter (filterType) {
  168. return dispatch => {
  169. dispatch({
  170. type: NOTIFICATIONS_FILTER_SET,
  171. path: ['notifications', 'quickFilter', 'active'],
  172. value: filterType,
  173. });
  174. dispatch(expandNotifications());
  175. dispatch(saveSettings());
  176. };
  177. };