2020-06-12 22:44:25 +02:00
|
|
|
// @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';
|
2021-04-29 21:10:20 +02:00
|
|
|
import {
|
|
|
|
selectNotifications,
|
|
|
|
selectNotificationsFiltered,
|
|
|
|
selectNotificationCategories,
|
|
|
|
} from 'redux/selectors/notifications';
|
2020-08-21 21:44:54 +02:00
|
|
|
import { doResolveUris } from 'lbry-redux';
|
2020-06-12 22:44:25 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-04-29 21:10:20 +02:00
|
|
|
export function doNotificationList(types?: Array<string>) {
|
|
|
|
return async (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const notificationTypes = selectNotificationCategories(state);
|
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
dispatch({ type: ACTIONS.NOTIFICATION_LIST_STARTED });
|
2021-03-24 08:45:41 +01:00
|
|
|
|
|
|
|
let params: any = { is_app_readable: true };
|
2021-04-29 21:10:20 +02:00
|
|
|
if (types) {
|
|
|
|
params.type = types.join(',');
|
2021-03-24 08:45:41 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 21:10:20 +02:00
|
|
|
try {
|
|
|
|
if (!notificationTypes) {
|
|
|
|
const notificationCategories = await Lbryio.call('notification', 'categories');
|
|
|
|
if (notificationCategories) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.NOTIFICATION_CATEGORIES_COMPLETED,
|
|
|
|
data: {
|
|
|
|
notificationCategories: notificationCategories.reverse(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Lbryio.call('notification', 'list', params).then((response) => {
|
2020-07-23 16:22:57 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
})
|
2021-03-04 12:03:18 +01:00
|
|
|
.map((notification) => {
|
2020-11-02 17:51:08 +01:00
|
|
|
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));
|
2021-03-24 08:45:41 +01:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.NOTIFICATION_LIST_COMPLETED,
|
|
|
|
data: {
|
|
|
|
newNotifications: notifications,
|
2021-04-29 21:10:20 +02:00
|
|
|
filterRule: Boolean(types),
|
2021-03-24 08:45:41 +01:00
|
|
|
},
|
|
|
|
});
|
2020-07-23 16:22:57 +02:00
|
|
|
});
|
2021-04-29 21:10:20 +02:00
|
|
|
} catch (error) {
|
|
|
|
dispatch({ type: ACTIONS.NOTIFICATION_LIST_FAILED, data: { error } });
|
|
|
|
}
|
2020-07-23 16:22:57 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-04 12:00:15 +01:00
|
|
|
export function doReadNotifications(notificationsIds: Array<number>) {
|
2020-11-16 20:09:00 +01:00
|
|
|
return (dispatch: Dispatch, getState: GetState) => {
|
2020-07-23 16:22:57 +02:00
|
|
|
const state = getState();
|
|
|
|
const notifications = selectNotifications(state);
|
2021-03-24 08:45:41 +01:00
|
|
|
const notificationsFiltered = selectNotificationsFiltered(state);
|
|
|
|
|
|
|
|
if (!notifications || !notificationsFiltered) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-04 12:00:15 +01:00
|
|
|
|
|
|
|
let ids;
|
|
|
|
if (notificationsIds && Array.isArray(notificationsIds) && notificationsIds.length !== 0) {
|
2021-03-24 08:45:41 +01:00
|
|
|
// Wipe specified notifications.
|
|
|
|
ids = notificationsIds;
|
2021-03-04 12:00:15 +01:00
|
|
|
} else {
|
|
|
|
// A null or invalid argument will wipe all unread notifications.
|
2021-03-24 08:45:41 +01:00
|
|
|
const getUnreadIds = (list) => list.filter((n) => !n.is_read).map((n) => n.id);
|
2021-07-06 04:53:30 +02:00
|
|
|
ids = Array.from(new Set([...getUnreadIds(notifications), ...getUnreadIds(notificationsFiltered)]));
|
2021-03-04 12:00:15 +01:00
|
|
|
}
|
2020-07-23 16:22:57 +02:00
|
|
|
|
|
|
|
dispatch({ type: ACTIONS.NOTIFICATION_READ_STARTED });
|
2021-03-04 12:00:15 +01:00
|
|
|
return Lbryio.call('notification', 'edit', { notification_ids: ids.join(','), is_read: true })
|
2020-07-23 16:22:57 +02:00
|
|
|
.then(() => {
|
2021-03-04 12:00:15 +01:00
|
|
|
dispatch({ type: ACTIONS.NOTIFICATION_READ_COMPLETED, data: { notificationIds: ids } });
|
2020-07-23 16:22:57 +02:00
|
|
|
})
|
2021-03-04 12:03:18 +01:00
|
|
|
.catch((error) => {
|
2020-07-23 16:22:57 +02:00
|
|
|
dispatch({ type: ACTIONS.NOTIFICATION_READ_FAILED, data: { error } });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2020-08-21 21:44:54 +02:00
|
|
|
|
|
|
|
export function doSeeNotifications(notificationIds: Array<string>) {
|
2020-11-16 20:09:00 +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,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
2021-03-04 12:03:18 +01:00
|
|
|
.catch((error) => {
|
2020-08-21 21:44:54 +02:00
|
|
|
dispatch({ type: ACTIONS.NOTIFICATION_SEEN_FAILED, data: { error } });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doSeeAllNotifications() {
|
2020-11-16 20:09:00 +01:00
|
|
|
return (dispatch: Dispatch, getState: GetState) => {
|
2020-08-21 21:44:54 +02:00
|
|
|
const state = getState();
|
|
|
|
const notifications = selectNotifications(state);
|
2021-03-24 08:45:41 +01:00
|
|
|
const notificationsFiltered = selectNotificationsFiltered(state);
|
|
|
|
|
|
|
|
if (!notifications || !notificationsFiltered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const getUnseenIds = (list) => list.filter((n) => !n.is_seen).map((n) => n.id);
|
2021-07-06 04:53:30 +02:00
|
|
|
const unseenIds = Array.from(new Set([...getUnseenIds(notifications), ...getUnseenIds(notificationsFiltered)]));
|
2020-08-21 21:44:54 +02:00
|
|
|
|
2021-03-24 08:45:41 +01:00
|
|
|
dispatch(doSeeNotifications(unseenIds));
|
2020-08-21 21:44:54 +02:00
|
|
|
};
|
|
|
|
}
|
2020-11-03 21:09:56 +01:00
|
|
|
|
|
|
|
export function doDeleteNotification(notificationId: number) {
|
2020-11-16 20:09:00 +01:00
|
|
|
return (dispatch: Dispatch) => {
|
2020-11-03 21:09:56 +01:00
|
|
|
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.') }));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|