added updating slate coverImage upon add/remove from slate

This commit is contained in:
Martina 2021-11-12 13:14:07 -08:00
parent 9f6334ddcb
commit abf34ab852
3 changed files with 57 additions and 5 deletions

View File

@ -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;
}
}
};

View File

@ -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)

View File

@ -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({