1e4c79cf06
## Issue: Closes 5515: All videos marked as read when clicking a single notification from notification list ## Change: - Augment `doReadNotifications` to only clear the given IDs. If the argument is `null` or is not a valid array (e.g. when used as a click handlers, the click event object is passed in), all notifications will be cleared. - Augment `NOTIFICATION_READ_COMPLETED` to only clear the given IDs. ## Notes: - Wasn't sure of the API will fail if the ID is invalid, so I start from `unreadNotifications` first, then only filtering it further with the given ID. Otherwise, we could just skip the `unreadNotifications` filtering.
147 lines
4.7 KiB
JavaScript
147 lines
4.7 KiB
JavaScript
// @flow
|
|
import * as ACTIONS from 'constants/action_types';
|
|
import * as NOTIFICATIONS from 'constants/notifications';
|
|
import { Lbryio } from 'lbryinc';
|
|
import { v4 as uuid } from 'uuid';
|
|
import { selectNotifications } from 'redux/selectors/notifications';
|
|
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,
|
|
};
|
|
}
|
|
|
|
export function doNotificationList() {
|
|
return (dispatch: Dispatch) => {
|
|
dispatch({ type: ACTIONS.NOTIFICATION_LIST_STARTED });
|
|
return Lbryio.call('notification', 'list', { is_app_readable: true })
|
|
.then((response) => {
|
|
const notifications = response || [];
|
|
const channelsToResolve = notifications
|
|
.filter((notification: WebNotification) => {
|
|
if (
|
|
(notification.notification_parameters.dynamic &&
|
|
notification.notification_parameters.dynamic.comment_author) ||
|
|
notification.notification_rule === NOTIFICATIONS.NEW_CONTENT
|
|
) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
})
|
|
.map((notification) => {
|
|
if (notification.notification_rule === NOTIFICATIONS.NEW_CONTENT) {
|
|
return notification.notification_parameters.device.target;
|
|
} else {
|
|
return notification.notification_parameters.dynamic.comment_author;
|
|
}
|
|
});
|
|
|
|
dispatch(doResolveUris(channelsToResolve));
|
|
dispatch({ type: ACTIONS.NOTIFICATION_LIST_COMPLETED, data: { notifications } });
|
|
})
|
|
.catch((error) => {
|
|
dispatch({ type: ACTIONS.NOTIFICATION_LIST_FAILED, data: { error } });
|
|
});
|
|
};
|
|
}
|
|
|
|
export function doReadNotifications(notificationsIds: Array<number>) {
|
|
return (dispatch: Dispatch, getState: GetState) => {
|
|
const state = getState();
|
|
const notifications = selectNotifications(state);
|
|
const unreadNotifications = notifications && notifications.filter((notification) => !notification.is_read);
|
|
|
|
let ids;
|
|
if (notificationsIds && Array.isArray(notificationsIds) && notificationsIds.length !== 0) {
|
|
// Wipe specified notications.
|
|
ids = unreadNotifications
|
|
.filter((notification) => notificationsIds.includes(notification.id))
|
|
.map((notification) => notification.id);
|
|
} else {
|
|
// A null or invalid argument will wipe all unread notifications.
|
|
ids = unreadNotifications.map((notification) => notification.id);
|
|
}
|
|
|
|
dispatch({ type: ACTIONS.NOTIFICATION_READ_STARTED });
|
|
return Lbryio.call('notification', 'edit', { notification_ids: ids.join(','), is_read: true })
|
|
.then(() => {
|
|
dispatch({ type: ACTIONS.NOTIFICATION_READ_COMPLETED, data: { notificationIds: ids } });
|
|
})
|
|
.catch((error) => {
|
|
dispatch({ type: ACTIONS.NOTIFICATION_READ_FAILED, data: { error } });
|
|
});
|
|
};
|
|
}
|
|
|
|
export function doSeeNotifications(notificationIds: Array<string>) {
|
|
return (dispatch: Dispatch) => {
|
|
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() {
|
|
return (dispatch: Dispatch, getState: GetState) => {
|
|
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.') }));
|
|
});
|
|
};
|
|
}
|