2020-03-27 13:59:38 -07:00
|
|
|
import api, { getLinks } from '../api';
|
|
|
|
import IntlMessageFormat from 'intl-messageformat';
|
|
|
|
import 'intl-pluralrules';
|
|
|
|
import { fetchRelationships } from './accounts';
|
|
|
|
import {
|
|
|
|
importFetchedAccount,
|
|
|
|
importFetchedAccounts,
|
|
|
|
importFetchedStatus,
|
|
|
|
importFetchedStatuses,
|
|
|
|
} from './importer';
|
2020-04-28 11:49:39 -07:00
|
|
|
import { getSettings, saveSettings } from './settings';
|
2020-03-27 13:59:38 -07:00
|
|
|
import { defineMessages } from 'react-intl';
|
2020-09-22 11:42:08 -07:00
|
|
|
import {
|
|
|
|
List as ImmutableList,
|
|
|
|
Map as ImmutableMap,
|
2020-09-23 16:57:10 -07:00
|
|
|
OrderedMap as ImmutableOrderedMap,
|
2020-09-22 11:42:08 -07:00
|
|
|
} from 'immutable';
|
2020-03-27 13:59:38 -07:00
|
|
|
import { unescapeHTML } from '../utils/html';
|
|
|
|
import { getFilters, regexFromFilters } from '../selectors';
|
2021-03-25 10:25:45 -07:00
|
|
|
import { isLoggedIn } from 'soapbox/utils/auth';
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export const NOTIFICATIONS_UPDATE = 'NOTIFICATIONS_UPDATE';
|
|
|
|
export const NOTIFICATIONS_UPDATE_NOOP = 'NOTIFICATIONS_UPDATE_NOOP';
|
|
|
|
export const NOTIFICATIONS_UPDATE_QUEUE = 'NOTIFICATIONS_UPDATE_QUEUE';
|
|
|
|
export const NOTIFICATIONS_DEQUEUE = 'NOTIFICATIONS_DEQUEUE';
|
|
|
|
|
|
|
|
export const NOTIFICATIONS_EXPAND_REQUEST = 'NOTIFICATIONS_EXPAND_REQUEST';
|
|
|
|
export const NOTIFICATIONS_EXPAND_SUCCESS = 'NOTIFICATIONS_EXPAND_SUCCESS';
|
|
|
|
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';
|
|
|
|
|
2020-04-27 18:51:17 -07:00
|
|
|
export const NOTIFICATIONS_MARK_READ_REQUEST = 'NOTIFICATIONS_MARK_READ_REQUEST';
|
|
|
|
export const NOTIFICATIONS_MARK_READ_SUCCESS = 'NOTIFICATIONS_MARK_READ_SUCCESS';
|
|
|
|
export const NOTIFICATIONS_MARK_READ_FAIL = 'NOTIFICATIONS_MARK_READ_FAIL';
|
|
|
|
|
2020-03-27 13:59:38 -07:00
|
|
|
export const MAX_QUEUED_NOTIFICATIONS = 40;
|
|
|
|
|
|
|
|
defineMessages({
|
|
|
|
mention: { id: 'notification.mention', defaultMessage: '{name} mentioned you' },
|
|
|
|
group: { id: 'notifications.group', defaultMessage: '{count} notifications' },
|
|
|
|
});
|
|
|
|
|
|
|
|
const fetchRelatedRelationships = (dispatch, notifications) => {
|
|
|
|
const accountIds = notifications.filter(item => item.type === 'follow').map(item => item.account.id);
|
|
|
|
|
|
|
|
if (accountIds.length > 0) {
|
|
|
|
dispatch(fetchRelationships(accountIds));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export function updateNotifications(notification, intlMessages, intlLocale) {
|
|
|
|
return (dispatch, getState) => {
|
2020-04-28 11:49:39 -07:00
|
|
|
const showInColumn = getSettings(getState()).getIn(['notifications', 'shows', notification.type], true);
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-07-15 08:52:23 -07:00
|
|
|
if (notification.account) {
|
2020-03-27 13:59:38 -07:00
|
|
|
dispatch(importFetchedAccount(notification.account));
|
2021-07-15 08:52:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Used by Move notification
|
|
|
|
if (notification.target) {
|
|
|
|
dispatch(importFetchedAccount(notification.target));
|
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-07-15 08:52:23 -07:00
|
|
|
if (notification.status) {
|
|
|
|
dispatch(importFetchedStatus(notification.status));
|
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-07-15 08:52:23 -07:00
|
|
|
if (showInColumn) {
|
2020-03-27 13:59:38 -07:00
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_UPDATE,
|
|
|
|
notification,
|
|
|
|
});
|
|
|
|
|
|
|
|
fetchRelatedRelationships(dispatch, [notification]);
|
|
|
|
}
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export function updateNotificationsQueue(notification, intlMessages, intlLocale, curPath) {
|
|
|
|
return (dispatch, getState) => {
|
2020-08-27 12:35:28 -07:00
|
|
|
if (notification.type === 'pleroma:chat_mention') return; // Drop chat notifications, handle them per-chat
|
|
|
|
|
2020-05-18 17:43:58 -07:00
|
|
|
const showAlert = getSettings(getState()).getIn(['notifications', 'alerts', notification.type]);
|
2020-03-27 13:59:38 -07:00
|
|
|
const filters = getFilters(getState(), { contextType: 'notifications' });
|
2020-05-18 17:43:58 -07:00
|
|
|
const playSound = getSettings(getState()).getIn(['notifications', 'sounds', notification.type]);
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
let filtered = false;
|
|
|
|
|
|
|
|
const isOnNotificationsPage = curPath === '/notifications';
|
|
|
|
|
|
|
|
if (notification.type === 'mention') {
|
|
|
|
const regex = regexFromFilters(filters);
|
|
|
|
const searchIndex = notification.status.spoiler_text + '\n' + unescapeHTML(notification.status.content);
|
|
|
|
filtered = regex && regex.test(searchIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Desktop notifications
|
2021-06-17 00:18:17 -07:00
|
|
|
try {
|
|
|
|
if (typeof window.Notification !== 'undefined' && showAlert && !filtered) {
|
|
|
|
const title = new IntlMessageFormat(intlMessages[`notification.${notification.type}`], intlLocale).format({ name: notification.account.display_name.length > 0 ? notification.account.display_name : notification.account.username });
|
|
|
|
const body = (notification.status && notification.status.spoiler_text.length > 0) ? notification.status.spoiler_text : unescapeHTML(notification.status ? notification.status.content : '');
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-06-17 00:18:17 -07:00
|
|
|
const notify = new Notification(title, { body, icon: notification.account.avatar, tag: notification.id });
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2021-06-17 00:18:17 -07:00
|
|
|
notify.addEventListener('click', () => {
|
|
|
|
window.focus();
|
|
|
|
notify.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
console.warn(e);
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (playSound && !filtered) {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_UPDATE_NOOP,
|
2020-04-10 17:54:33 -07:00
|
|
|
meta: { sound: 'boop' },
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isOnNotificationsPage) {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_UPDATE_QUEUE,
|
|
|
|
notification,
|
|
|
|
intlMessages,
|
|
|
|
intlLocale,
|
|
|
|
});
|
2020-04-14 11:44:40 -07:00
|
|
|
} else {
|
2020-03-27 13:59:38 -07:00
|
|
|
dispatch(updateNotifications(notification, intlMessages, intlLocale));
|
|
|
|
}
|
2020-04-14 11:44:40 -07:00
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export function dequeueNotifications() {
|
|
|
|
return (dispatch, getState) => {
|
2020-09-23 16:57:10 -07:00
|
|
|
const queuedNotifications = getState().getIn(['notifications', 'queuedNotifications'], ImmutableOrderedMap());
|
2020-03-27 13:59:38 -07:00
|
|
|
const totalQueuedNotificationsCount = getState().getIn(['notifications', 'totalQueuedNotificationsCount'], 0);
|
|
|
|
|
2020-04-14 13:45:38 -07:00
|
|
|
if (totalQueuedNotificationsCount === 0) {
|
2020-03-27 13:59:38 -07:00
|
|
|
return;
|
2020-04-14 11:44:40 -07:00
|
|
|
} else if (totalQueuedNotificationsCount > 0 && totalQueuedNotificationsCount <= MAX_QUEUED_NOTIFICATIONS) {
|
2020-03-27 13:59:38 -07:00
|
|
|
queuedNotifications.forEach(block => {
|
|
|
|
dispatch(updateNotifications(block.notification, block.intlMessages, block.intlLocale));
|
|
|
|
});
|
2020-04-14 11:44:40 -07:00
|
|
|
} else {
|
2020-03-27 13:59:38 -07:00
|
|
|
dispatch(expandNotifications());
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_DEQUEUE,
|
|
|
|
});
|
|
|
|
dispatch(markReadNotifications());
|
2020-04-14 11:44:40 -07:00
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2020-04-28 11:49:39 -07:00
|
|
|
const excludeTypesFromSettings = getState => getSettings(getState()).getIn(['notifications', 'shows']).filter(enabled => !enabled).keySeq().toJS();
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
const excludeTypesFromFilter = filter => {
|
2021-06-30 04:41:06 -07:00
|
|
|
const allTypes = ImmutableList(['follow', 'follow_request', 'favourite', 'reblog', 'mention', 'poll', 'move', 'pleroma:emoji_reaction']);
|
2020-03-27 13:59:38 -07:00
|
|
|
return allTypes.filterNot(item => item === filter).toJS();
|
|
|
|
};
|
|
|
|
|
|
|
|
const noOp = () => {};
|
|
|
|
|
|
|
|
export function expandNotifications({ maxId } = {}, done = noOp) {
|
|
|
|
return (dispatch, getState) => {
|
2021-03-24 09:44:51 -07:00
|
|
|
if (!isLoggedIn(getState)) return;
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2020-04-28 11:49:39 -07:00
|
|
|
const activeFilter = getSettings(getState()).getIn(['notifications', 'quickFilter', 'active']);
|
2020-03-27 13:59:38 -07:00
|
|
|
const notifications = getState().get('notifications');
|
|
|
|
const isLoadingMore = !!maxId;
|
|
|
|
|
|
|
|
if (notifications.get('isLoading')) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
max_id: maxId,
|
|
|
|
exclude_types: activeFilter === 'all'
|
2020-04-28 11:49:39 -07:00
|
|
|
? excludeTypesFromSettings(getState)
|
2020-03-27 13:59:38 -07:00
|
|
|
: excludeTypesFromFilter(activeFilter),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!maxId && notifications.get('items').size > 0) {
|
|
|
|
params.since_id = notifications.getIn(['items', 0, 'id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(expandNotificationsRequest(isLoadingMore));
|
|
|
|
|
|
|
|
api(getState).get('/api/v1/notifications', { params }).then(response => {
|
|
|
|
const next = getLinks(response).refs.find(link => link.rel === 'next');
|
|
|
|
|
2021-07-09 15:56:25 -07:00
|
|
|
const entries = response.data.reduce((acc, item) => {
|
2021-07-09 15:40:15 -07:00
|
|
|
if (item.account && item.account.id) {
|
|
|
|
acc.accounts[item.account.id] = item.account;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used by Move notification
|
|
|
|
if (item.target && item.target.id) {
|
|
|
|
acc.accounts[item.target.id] = item.target;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.status && item.status.id) {
|
|
|
|
acc.statuses[item.status.id] = item.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, { accounts: {}, statuses: {} });
|
|
|
|
|
|
|
|
dispatch(importFetchedAccounts(Object.values(entries.accounts)));
|
|
|
|
dispatch(importFetchedStatuses(Object.values(entries.statuses)));
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
dispatch(expandNotificationsSuccess(response.data, next ? next.uri : null, isLoadingMore));
|
|
|
|
fetchRelatedRelationships(dispatch, response.data);
|
|
|
|
done();
|
|
|
|
}).catch(error => {
|
|
|
|
dispatch(expandNotificationsFail(error, isLoadingMore));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export function expandNotificationsRequest(isLoadingMore) {
|
|
|
|
return {
|
|
|
|
type: NOTIFICATIONS_EXPAND_REQUEST,
|
|
|
|
skipLoading: !isLoadingMore,
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export function expandNotificationsSuccess(notifications, next, isLoadingMore) {
|
|
|
|
return {
|
|
|
|
type: NOTIFICATIONS_EXPAND_SUCCESS,
|
|
|
|
notifications,
|
|
|
|
next,
|
|
|
|
skipLoading: !isLoadingMore,
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export function expandNotificationsFail(error, isLoadingMore) {
|
|
|
|
return {
|
|
|
|
type: NOTIFICATIONS_EXPAND_FAIL,
|
|
|
|
error,
|
|
|
|
skipLoading: !isLoadingMore,
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export function clearNotifications() {
|
|
|
|
return (dispatch, getState) => {
|
2021-03-24 09:44:51 -07:00
|
|
|
if (!isLoggedIn(getState)) return;
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_CLEAR,
|
|
|
|
});
|
|
|
|
|
|
|
|
api(getState).post('/api/v1/notifications/clear');
|
|
|
|
};
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|
2020-03-27 13:59:38 -07:00
|
|
|
|
|
|
|
export function scrollTopNotifications(top) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_SCROLL_TOP,
|
|
|
|
top,
|
|
|
|
});
|
|
|
|
dispatch(markReadNotifications());
|
2020-04-14 11:44:40 -07:00
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
|
|
|
|
2020-04-14 14:47:35 -07:00
|
|
|
export function setFilter(filterType) {
|
2020-03-27 13:59:38 -07:00
|
|
|
return dispatch => {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_FILTER_SET,
|
|
|
|
path: ['notifications', 'quickFilter', 'active'],
|
|
|
|
value: filterType,
|
|
|
|
});
|
|
|
|
dispatch(expandNotifications());
|
|
|
|
dispatch(saveSettings());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function markReadNotifications() {
|
|
|
|
return (dispatch, getState) => {
|
2021-03-24 09:52:12 -07:00
|
|
|
if (!isLoggedIn(getState)) return;
|
2020-09-22 11:42:08 -07:00
|
|
|
|
2021-03-24 09:52:12 -07:00
|
|
|
const state = getState();
|
2020-09-23 16:57:10 -07:00
|
|
|
const topNotification = state.getIn(['notifications', 'items'], ImmutableOrderedMap()).first(ImmutableMap()).get('id');
|
2020-09-22 11:42:08 -07:00
|
|
|
const lastRead = state.getIn(['notifications', 'lastRead']);
|
|
|
|
|
2020-04-27 18:51:17 -07:00
|
|
|
if (!(topNotification && topNotification > lastRead)) return;
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_MARK_READ_REQUEST,
|
|
|
|
lastRead: topNotification,
|
|
|
|
});
|
|
|
|
|
|
|
|
api(getState).post('/api/v1/pleroma/notifications/read', {
|
|
|
|
max_id: topNotification,
|
|
|
|
}).then(response => {
|
|
|
|
dispatch({
|
|
|
|
type: NOTIFICATIONS_MARK_READ_SUCCESS,
|
|
|
|
notifications: response.data,
|
2020-03-27 13:59:38 -07:00
|
|
|
});
|
2020-04-27 18:51:17 -07:00
|
|
|
}).catch(e => {
|
|
|
|
dispatch({ type: NOTIFICATIONS_MARK_READ_FAIL });
|
|
|
|
});
|
2020-04-14 11:44:40 -07:00
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|