lbry-desktop/ui/redux/actions/notifications.js

141 lines
4.3 KiB
JavaScript
Raw Normal View History

// @flow
import * as ACTIONS from 'constants/action_types';
2020-11-02 17:51:08 +01:00
import * as NOTIFICATIONS from 'constants/notifications';
2020-07-23 16:22:57 +02:00
import { Lbryio } from 'lbryinc';
2020-10-02 20:16:37 +02:00
import { v4 as uuid } from 'uuid';
2020-07-23 16:22:57 +02:00
import { selectNotifications } from 'redux/selectors/notifications';
2020-08-21 21:44:54 +02:00
import { doResolveUris } from 'lbry-redux';
export function doToast(params: ToastParams) {
if (!params) {
throw Error("'params' object is required to create a toast notification");
}
return {
type: ACTIONS.CREATE_TOAST,
data: {
id: uuid(),
params,
},
};
}
export function doDismissToast() {
return {
type: ACTIONS.DISMISS_TOAST,
};
}
export function doError(error: string | {}) {
return {
type: ACTIONS.CREATE_ERROR,
data: {
error,
},
};
}
export function doDismissError() {
return {
type: ACTIONS.DISMISS_ERROR,
};
}
2020-07-23 16:22:57 +02:00
export function doNotificationList() {
2020-11-02 17:51:08 +01:00
return (dispatch: Dispatch<*>) => {
2020-07-23 16:22:57 +02:00
dispatch({ type: ACTIONS.NOTIFICATION_LIST_STARTED });
return Lbryio.call('notification', 'list', { is_app_readable: true })
2020-07-23 16:22:57 +02:00
.then(response => {
const notifications = response || [];
2020-08-21 21:44:54 +02:00
const channelsToResolve = notifications
.filter((notification: WebNotification) => {
if (
2020-11-02 17:51:08 +01:00
(notification.notification_parameters.dynamic &&
notification.notification_parameters.dynamic.comment_author) ||
notification.notification_rule === NOTIFICATIONS.NEW_CONTENT
2020-08-21 21:44:54 +02:00
) {
return true;
} else {
return false;
}
})
2020-11-02 17:51:08 +01:00
.map(notification => {
if (notification.notification_rule === NOTIFICATIONS.NEW_CONTENT) {
return notification.notification_parameters.device.target;
} else {
return notification.notification_parameters.dynamic.comment_author;
}
});
2020-08-21 21:44:54 +02:00
dispatch(doResolveUris(channelsToResolve));
2020-07-23 16:22:57 +02:00
dispatch({ type: ACTIONS.NOTIFICATION_LIST_COMPLETED, data: { notifications } });
})
.catch(error => {
dispatch({ type: ACTIONS.NOTIFICATION_LIST_FAILED, data: { error } });
});
};
}
export function doReadNotifications() {
2020-11-02 17:51:08 +01:00
return (dispatch: Dispatch<*>, getState: GetState) => {
2020-07-23 16:22:57 +02:00
const state = getState();
const notifications = selectNotifications(state);
const unreadNotifications =
notifications &&
notifications
.filter(notification => !notification.is_read)
.map(notification => notification.id)
.join(',');
dispatch({ type: ACTIONS.NOTIFICATION_READ_STARTED });
return Lbryio.call('notification', 'edit', { notification_ids: unreadNotifications, is_read: true })
.then(() => {
dispatch({ type: ACTIONS.NOTIFICATION_READ_COMPLETED });
})
.catch(error => {
dispatch({ type: ACTIONS.NOTIFICATION_READ_FAILED, data: { error } });
});
};
}
2020-08-21 21:44:54 +02:00
export function doSeeNotifications(notificationIds: Array<string>) {
2020-11-02 17:51:08 +01:00
return (dispatch: Dispatch<*>) => {
2020-08-21 21:44:54 +02:00
dispatch({ type: ACTIONS.NOTIFICATION_SEEN_STARTED });
return Lbryio.call('notification', 'edit', { notification_ids: notificationIds.join(','), is_seen: true })
.then(() => {
dispatch({
type: ACTIONS.NOTIFICATION_SEEN_COMPLETED,
data: {
notificationIds,
},
});
})
.catch(error => {
dispatch({ type: ACTIONS.NOTIFICATION_SEEN_FAILED, data: { error } });
});
};
}
export function doSeeAllNotifications() {
2020-11-02 17:51:08 +01:00
return (dispatch: Dispatch<*>, getState: GetState) => {
2020-08-21 21:44:54 +02:00
const state = getState();
const notifications = selectNotifications(state);
const unSeenNotifications =
notifications && notifications.filter(notification => !notification.is_seen).map(notification => notification.id);
dispatch(doSeeNotifications(unSeenNotifications));
};
}
export function doDeleteNotification(notificationId: number) {
return (dispatch: Dispatch<*>) => {
Lbryio.call('notification', 'delete', { notification_ids: notificationId })
.then(() => {
dispatch({ type: ACTIONS.NOTIFICATION_DELETE_COMPLETED, data: { notificationId } });
})
.catch(() => {
dispatch(doToast({ isError: true, message: __('Unable to delete this right now. Please try again later.') }));
});
};
}