receptor/src/actions/selection.js

65 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

import { filter_subscribe, filter_unsubscribe } from './filter_subscribe';
import { unsubscribe } from './subscribe';
2017-12-30 20:00:42 +01:00
import { push_path } from './routing';
export const UNION = 'UNION';
export const SUBTRACT = 'SUBTRACT';
export const EXCLUSIVE = 'EXCLUSIVE';
export default function selectTorrent(ids, action, navigate=true) {
return (dispatch, getState) => {
const previous = new Set(getState().selection);
2017-08-26 01:59:53 +02:00
dispatch({ type: action, ids });
const state = getState();
const next = new Set(state.selection);
const filter_subscriptions = state.filter_subscribe;
const subscriptions = state.subscribe;
2017-09-12 20:33:40 +02:00
const { peers, files, trackers } = state;
const added = next.difference(previous);
const removed = previous.difference(next);
added.forEach(t => {
const criteria = [
2017-08-26 01:59:53 +02:00
{ field: "torrent_id", op: "==", value: t }
];
dispatch(filter_subscribe("peer", criteria));
dispatch(filter_subscribe("file", criteria));
dispatch(filter_subscribe("tracker", criteria));
});
removed.forEach(t => {
/* Remove filter subscriptions */
const serials = filter_subscriptions
.filter(sub => sub.criteria[0] && sub.criteria[0].value === t)
.map(sub => sub.serial);
serials.forEach(serial => dispatch(filter_unsubscribe(serial)));
/* Remove resource subscriptions */
2017-08-26 01:59:53 +02:00
const _ids = [
...Object.values(files)
.filter(file => file.torrent_id === t)
2017-12-29 07:19:24 +01:00
.map(file => file.id),
...Object.values(peers)
.filter(peer => peer.torrent_id === t)
.map(peer => peer.id),
...Object.values(trackers)
.filter(tracker => tracker.torrent_id === t)
.map(tracker => tracker.id)
];
2017-08-26 01:59:53 +02:00
if (_ids.length > 0) {
dispatch(unsubscribe(..._ids));
}
});
if (navigate) {
const url_torrents = state.selection.slice(0, 3);
if (url_torrents.length > 0) {
2017-12-30 20:00:42 +01:00
dispatch(push_path(`/torrents/${url_torrents}`));
} else {
2017-12-30 20:00:42 +01:00
dispatch(push_path("/"));
}
}
};
}