slate/node_common/managers/library.js

129 lines
3.1 KiB
JavaScript
Raw Normal View History

import { v4 as uuid } from "uuid";
import { getFileExtension } from "../../common/strings";
// NOTE(jim):
// There is some Navigation specific data here for folders.
export const createBucket = ({ id, name }) => {
return {
id,
decorator: "FOLDER",
icon: "FOLDER",
name: name,
pageTitle: `Exploring ${name}`,
date: new Date(),
size: null,
children: [],
};
};
// NOTE(jim):
// Every root level user gets a bucket.
export const init = ({ bucketName, readableName }) => [
createBucket({ id: bucketName, name: readableName }),
];
export const createLocalDataIncomplete = ({ type, size, name }, id = null) => {
return {
id: !id ? `data-${uuid()}` : id,
name: name,
decorator: "FILE",
icon: type,
size: size,
file: name,
type: type,
date: new Date(),
networks: [],
job: null,
ipfs: null,
storage: 0,
retrieval: 0,
};
};
export const updateDataIPFS = (d, { ipfs }) => {
// TODO(jim): DELETE_THIS_GUARD_CODE
if (!d.networks) {
d.networks = [];
delete d.network;
}
if (!d.networks.includes("IPFS")) {
d.networks.push("IPFS");
}
2020-09-22 02:20:24 +03:00
return { ...d, cid: ipfs.replace("/ipfs/", ""), ipfs };
};
export const updateDataById = ({ user, id, data }) => {
const { library } = user.data;
for (let i = 0; i < library.length; i++) {
for (let j = 0; j < library[i].children.length; j++) {
if (library[i].children[j].id === id) {
library[i].children[j] = data;
2020-08-02 03:13:19 +03:00
return { ...user.data, library };
}
}
}
return { ...user.data, library };
};
export const getDataByIPFS = (user, ipfs) => {
const { library } = user.data;
2020-07-24 06:15:32 +03:00
// TODO(jim): Totally purges the ID.
for (let i = 0; i < library.length; i++) {
for (let j = 0; j < library[i].children.length; j++) {
if (library[i].children[j].ipfs === ipfs) {
return library[i].children[j];
}
}
}
return null;
};
export const addData = ({ user, files }) => {
const { library } = user.data;
// TODO(jim): Since we don't support bucket organization... yet.
// Add just pushes to the first set. But we can change this easily later.
let noRepeats = [...files];
2020-10-05 00:30:28 +03:00
for (let i = 0; i < library.length; i++) {
let cids = library[i].children.map((file) => file.ipfs);
for (let j = 0; j < files.length; j++) {
2020-10-05 00:30:28 +03:00
if (cids.includes(files[j].ipfs)) {
noRepeats[j] = null;
}
}
}
noRepeats = noRepeats.filter((file) => {
return !!file;
});
for (let i = 0; i < library.length; i++) {
library[i].children = [...noRepeats, ...library[i].children];
break;
}
return {
updatedUserDataFields: { ...user.data, library },
added: noRepeats.length,
skipped: files.length - noRepeats.length,
};
};
2020-11-16 03:29:13 +03:00
export const editItem = ({ user, update }) => {
const { library } = user.data;
2020-11-19 08:13:50 +03:00
const previewImage = update.data.previewImage;
2020-11-16 03:29:13 +03:00
for (let i = 0; i < library[0].children.length; i++) {
2020-11-19 08:13:50 +03:00
if (library[0].children[i].id === update.data.id) {
library[0].children[i] = { ...library[0].children[i], ...{ previewImage } };
2020-11-16 03:29:13 +03:00
break;
}
}
return user.data;
};