mirror of
https://github.com/filecoin-project/slate.git
synced 2024-11-22 12:24:02 +03:00
added updating slate coverImage upon add/remove from slate
This commit is contained in:
parent
9f6334ddcb
commit
abf34ab852
@ -373,6 +373,8 @@ export const addToSlate = async ({ slate, files, user, saveCopy = false }) => {
|
||||
addToPublicCollectionUpdatePrivacy({ filteredFiles });
|
||||
}
|
||||
|
||||
addToSlateCheckCoverImage(slate, files);
|
||||
|
||||
return { added: response.length };
|
||||
};
|
||||
|
||||
@ -401,3 +403,56 @@ export const addToPublicCollectionUpdatePrivacy = async ({ files }) => {
|
||||
}
|
||||
return madePublic;
|
||||
};
|
||||
|
||||
const selectSlateCoverImage = (objects) => {
|
||||
if (!objects.length) return null;
|
||||
for (let object of objects) {
|
||||
if (
|
||||
Validations.isPreviewableImage(object.type) ||
|
||||
(object.coverImage?.type && Validations.isPreviewableImage(object.coverImage.type))
|
||||
) {
|
||||
return object;
|
||||
}
|
||||
}
|
||||
return objects[0];
|
||||
};
|
||||
|
||||
export const addToSlateCheckCoverImage = async (slate, filesAdded) => {
|
||||
if (slate.coverImage) {
|
||||
if (Validations.isPreviewableImage(slate.coverImage.type)) return;
|
||||
let imageCoverImage = slate.coverImage.coverImage;
|
||||
if (imageCoverImage && Validations.isPreviewableImage(imageCoverImage.type)) return;
|
||||
}
|
||||
if (!filesAdded) return;
|
||||
let files;
|
||||
if (Array.isArray(filesAdded)) {
|
||||
files = filesAdded;
|
||||
} else {
|
||||
files = [filesAdded];
|
||||
}
|
||||
let newObjects = (slate.objects || []).push(...files);
|
||||
let coverImage = selectSlateCoverImage(newObjects);
|
||||
if (coverImage.id !== slate.coverImage?.id) {
|
||||
slate.coverImage = coverImage;
|
||||
await Data.updateSlateById(slate);
|
||||
}
|
||||
};
|
||||
|
||||
export const removeFromSlateCheckCoverImage = async (slate, fileIdsRemoved) => {
|
||||
if (!slate.coverImage) return;
|
||||
let fileIds;
|
||||
if (Array.isArray(fileIdsRemoved)) {
|
||||
fileIds = fileIdsRemoved;
|
||||
} else {
|
||||
fileIds = [fileIdsRemoved];
|
||||
}
|
||||
for (let fileId of fileIds) {
|
||||
if (fileId === slate.coverImage.id) {
|
||||
let newObjects = slate.objects.filter((obj) => !fileIds.includes(obj.id));
|
||||
let coverImage = selectSlateCoverImage(newObjects);
|
||||
slate.coverImage = coverImage;
|
||||
await Data.updateSlateById(slate);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -105,11 +105,6 @@ export default async (req, res) => {
|
||||
added = addedToSlate;
|
||||
}
|
||||
|
||||
//NOTE(martina): leaving createdFiles out of the privacy recalculation since those should already have the correct privacy
|
||||
if (slate?.isPublic) {
|
||||
await Utilities.addToPublicCollectionUpdatePrivacy({ files: duplicateFiles });
|
||||
}
|
||||
|
||||
let updatedFiles = await Data.getFilesByIds({ ids: filesToAddToSlate.map((file) => file.id) });
|
||||
SearchManager.indexFile(updatedFiles); //NOTE(martina): using createFile instead of updateFile b/c createFile also works for existing files (it just overwrites)
|
||||
|
||||
|
@ -58,6 +58,8 @@ export default async (req, res) => {
|
||||
SearchManager.updateFile(updatedFiles);
|
||||
}
|
||||
|
||||
Utilities.removeFromSlateCheckCoverImage(slate, fileIds);
|
||||
|
||||
ViewerManager.hydratePartial(id, { slates: true });
|
||||
|
||||
return res.status(200).send({
|
||||
|
Loading…
Reference in New Issue
Block a user