Add option to disable real-time updates in web UI (#9984)
Fix #9031 Fix #7913
This commit is contained in:
		@@ -12,6 +12,8 @@ import { defineMessages } from 'react-intl';
 | 
			
		||||
import { List as ImmutableList } from 'immutable';
 | 
			
		||||
import { unescapeHTML } from '../utils/html';
 | 
			
		||||
import { getFiltersRegex } from '../selectors';
 | 
			
		||||
import { usePendingItems as preferPendingItems } from 'mastodon/initial_state';
 | 
			
		||||
import compareId from 'mastodon/compare_id';
 | 
			
		||||
 | 
			
		||||
export const NOTIFICATIONS_UPDATE      = 'NOTIFICATIONS_UPDATE';
 | 
			
		||||
export const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP';
 | 
			
		||||
@@ -22,8 +24,9 @@ export const NOTIFICATIONS_EXPAND_FAIL    = 'NOTIFICATIONS_EXPAND_FAIL';
 | 
			
		||||
 | 
			
		||||
export const NOTIFICATIONS_FILTER_SET = 'NOTIFICATIONS_FILTER_SET';
 | 
			
		||||
 | 
			
		||||
export const NOTIFICATIONS_CLEAR      = 'NOTIFICATIONS_CLEAR';
 | 
			
		||||
export const NOTIFICATIONS_SCROLL_TOP = 'NOTIFICATIONS_SCROLL_TOP';
 | 
			
		||||
export const NOTIFICATIONS_CLEAR        = 'NOTIFICATIONS_CLEAR';
 | 
			
		||||
export const NOTIFICATIONS_SCROLL_TOP   = 'NOTIFICATIONS_SCROLL_TOP';
 | 
			
		||||
export const NOTIFICATIONS_LOAD_PENDING = 'NOTIFICATIONS_LOAD_PENDING';
 | 
			
		||||
 | 
			
		||||
defineMessages({
 | 
			
		||||
  mention: { id: 'notification.mention', defaultMessage: '{name} mentioned you' },
 | 
			
		||||
@@ -38,6 +41,10 @@ const fetchRelatedRelationships = (dispatch, notifications) => {
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export const loadPending = () => ({
 | 
			
		||||
  type: NOTIFICATIONS_LOAD_PENDING,
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
export function updateNotifications(notification, intlMessages, intlLocale) {
 | 
			
		||||
  return (dispatch, getState) => {
 | 
			
		||||
    const showInColumn = getState().getIn(['settings', 'notifications', 'shows', notification.type], true);
 | 
			
		||||
@@ -69,6 +76,7 @@ export function updateNotifications(notification, intlMessages, intlLocale) {
 | 
			
		||||
      dispatch({
 | 
			
		||||
        type: NOTIFICATIONS_UPDATE,
 | 
			
		||||
        notification,
 | 
			
		||||
        usePendingItems: preferPendingItems,
 | 
			
		||||
        meta: (playSound && !filtered) ? { sound: 'boop' } : undefined,
 | 
			
		||||
      });
 | 
			
		||||
 | 
			
		||||
@@ -122,10 +130,19 @@ export function expandNotifications({ maxId } = {}, done = noOp) {
 | 
			
		||||
        : excludeTypesFromFilter(activeFilter),
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    if (!maxId && notifications.get('items').size > 0) {
 | 
			
		||||
      params.since_id = notifications.getIn(['items', 0, 'id']);
 | 
			
		||||
    if (!params.max_id && (notifications.get('items', ImmutableList()).size + notifications.get('pendingItems', ImmutableList()).size) > 0) {
 | 
			
		||||
      const a = notifications.getIn(['pendingItems', 0, 'id']);
 | 
			
		||||
      const b = notifications.getIn(['items', 0, 'id']);
 | 
			
		||||
 | 
			
		||||
      if (a && b && compareId(a, b) > 0) {
 | 
			
		||||
        params.since_id = a;
 | 
			
		||||
      } else {
 | 
			
		||||
        params.since_id = b || a;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const isLoadingRecent = !!params.since_id;
 | 
			
		||||
 | 
			
		||||
    dispatch(expandNotificationsRequest(isLoadingMore));
 | 
			
		||||
 | 
			
		||||
    api(getState).get('/api/v1/notifications', { params }).then(response => {
 | 
			
		||||
@@ -134,7 +151,7 @@ export function expandNotifications({ maxId } = {}, done = noOp) {
 | 
			
		||||
      dispatch(importFetchedAccounts(response.data.map(item => item.account)));
 | 
			
		||||
      dispatch(importFetchedStatuses(response.data.map(item => item.status).filter(status => !!status)));
 | 
			
		||||
 | 
			
		||||
      dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null, isLoadingMore));
 | 
			
		||||
      dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null, isLoadingMore, isLoadingRecent && preferPendingItems));
 | 
			
		||||
      fetchRelatedRelationships(dispatch, response.data);
 | 
			
		||||
      done();
 | 
			
		||||
    }).catch(error => {
 | 
			
		||||
@@ -151,11 +168,12 @@ export function expandNotificationsRequest(isLoadingMore) {
 | 
			
		||||
  };
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export function expandNotificationsSuccess(notifications, next, isLoadingMore) {
 | 
			
		||||
export function expandNotificationsSuccess(notifications, next, isLoadingMore, usePendingItems) {
 | 
			
		||||
  return {
 | 
			
		||||
    type: NOTIFICATIONS_EXPAND_SUCCESS,
 | 
			
		||||
    notifications,
 | 
			
		||||
    next,
 | 
			
		||||
    usePendingItems,
 | 
			
		||||
    skipLoading: !isLoadingMore,
 | 
			
		||||
  };
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user