fixed some edge cases that weren't updating correctly

This commit is contained in:
Martina 2021-11-15 14:11:22 -08:00
parent a7f629d370
commit caa9193c0c
15 changed files with 48 additions and 29 deletions

View File

@ -169,3 +169,16 @@ export const clamp = (value, min, max) => {
if (value > max) return max;
return value;
};
export const getCoverImageUrlIfExists = (coverImage) => {
if (!coverImage) return;
if (Validations.isPreviewableImage(coverImage.type)) {
return Strings.getURLfromCID(coverImage.cid);
}
if (coverImage.coverImage) {
return Strings.getURLfromCID(coverImage.coverImage.cid);
}
if (coverImage.linkImage) {
return coverImage.linkImage;
}
};

View File

@ -7,7 +7,7 @@ import { runQuery } from "~/node_common/data/utilities";
//NOTE(martina): you can pass in an update object whose data component is incomplete (e.g. data: { name: "New name" }) without accidentally
//wiping the rest of data b/c this updater method overlays it over the current value for data
export default async (slate) => {
export default async ({ objects, ...slate }) => {
return await runQuery({
label: "UPDATE_SLATE_BY_ID",
queryFn: async (DB) => {

View File

@ -2,7 +2,7 @@ import * as Serializers from "~/node_common/serializers";
import { runQuery } from "~/node_common/data/utilities";
export default async (user) => {
export default async ({ library, slates, ...user }) => {
return await runQuery({
label: "UPDATE_USER_BY_ID",
queryFn: async (DB) => {

View File

@ -104,7 +104,6 @@ const cleanFile = ({
const indexObject = async (objects, cleanObject, index) => {
console.log("index object");
console.log(objects);
console.log(index);
try {
if (Array.isArray(objects)) {
let body = [];
@ -146,7 +145,6 @@ export const indexFile = async (files) => {
const updateObject = async (objects, cleanObject, index) => {
console.log("update object");
console.log(objects);
console.log(index);
try {
if (Array.isArray(objects)) {
let body = [];
@ -189,7 +187,6 @@ export const updateFile = async (files) => {
const deleteObject = async (objects, index) => {
console.log("delete object");
console.log(objects);
console.log(index);
try {
if (Array.isArray(objects)) {
let body = [];

View File

@ -8,6 +8,8 @@ import * as Logging from "~/common/logging";
import * as ArrayUtilities from "~/node_common/array-utilities";
import * as Monitor from "~/node_common/monitor";
import * as Arrays from "~/common/arrays";
import * as Utilities from "~/common/utilities";
import SearchManager from "~/node_common/managers/search";
import crypto from "crypto";
@ -409,11 +411,7 @@ export const selectSlateCoverImage = (objects) => {
let selectedObject;
if (!objects.length) return null;
for (let object of objects) {
if (
Validations.isPreviewableImage(object.type) ||
(object.coverImage?.type && Validations.isPreviewableImage(object.coverImage.type)) ||
object.linkImage
) {
if (Utilities.getCoverImageUrlIfExists(object)) {
selectedObject = object;
break;
}
@ -424,10 +422,8 @@ export const selectSlateCoverImage = (objects) => {
};
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 (Utilities.getCoverImageUrlIfExists(slate.coverImage)) {
return;
}
if (!filesAdded) return;
let files;
@ -436,11 +432,12 @@ export const addToSlateCheckCoverImage = async (slate, filesAdded) => {
} else {
files = [filesAdded];
}
let newObjects = (slate.objects || []).push(...files);
let newObjects = (slate.objects || []).concat(files);
let coverImage = selectSlateCoverImage(newObjects);
if (coverImage.id !== slate.coverImage?.id) {
if (coverImage?.id !== slate.coverImage?.id) {
slate.coverImage = coverImage;
await Data.updateSlateById(slate);
let updatedSlate = await Data.updateSlateById(slate);
await SearchManager.updateSlate(updatedSlate);
}
};
@ -457,7 +454,8 @@ export const removeFromSlateCheckCoverImage = async (slate, fileIdsRemoved) => {
let newObjects = slate.objects.filter((obj) => !fileIds.includes(obj.id));
let coverImage = selectSlateCoverImage(newObjects);
slate.coverImage = coverImage;
await Data.updateSlateById(slate);
let updatedSlate = await Data.updateSlateById(slate);
await SearchManager.updateSlate(updatedSlate);
return;
}
}

View File

@ -17,7 +17,7 @@ export default async (req, res) => {
const slateId = req.body.data.slate?.id;
let slate;
if (slateId) {
slate = await Data.getSlateById({ id: slateId });
slate = await Data.getSlateById({ id: slateId, includeFiles: true });
if (!slate || slate.error) {
slate = null;

View File

@ -15,7 +15,7 @@ export default async (req, res) => {
const slateId = req.body.data.slate?.id;
let slate;
if (slateId) {
slate = await Data.getSlateById({ id: slateId });
slate = await Data.getSlateById({ id: slateId, includeFiles: true });
if (!slate || slate.error) {
slate = null;

View File

@ -37,7 +37,7 @@ export default async (req, res) => {
const slateId = req.body.data.slate?.id;
let slate;
if (slateId) {
slate = await Data.getSlateById({ id: slateId });
slate = await Data.getSlateById({ id: slateId, includeFiles: true });
if (!slate || slate.error) {
slate = null;

View File

@ -42,7 +42,9 @@ export default async (req, res) => {
let updatedFiles = await Utilities.removeFromPublicCollectionUpdatePrivacy({
files: slate.objects,
});
SearchManager.updateFile(updatedFiles);
if (updatedFiles.length) {
SearchManager.updateFile(updatedFiles);
}
}
return res.status(200).send({ decorator: "SERVER_DELETE_SLATE", error: false });

View File

@ -55,7 +55,9 @@ export default async (req, res) => {
let updatedFiles = await Utilities.removeFromPublicCollectionUpdatePrivacy({
files: slate.objects,
});
SearchManager.updateFile(updatedFiles);
if (updatedFiles.length) {
SearchManager.updateFile(updatedFiles);
}
}
Utilities.removeFromSlateCheckCoverImage(slate, fileIds);

View File

@ -20,7 +20,7 @@ export default async (req, res) => {
});
}
const slate = await Data.getSlateById({ id: req.body.data.id });
const slate = await Data.getSlateById({ id: req.body.data.id, includeFiles: true });
if (!slate) {
return res.status(404).send({ decorator: "SLATE_NOT_FOUND", error: true });
@ -70,7 +70,9 @@ export default async (req, res) => {
updatedFiles = await Utilities.addToPublicCollectionUpdatePrivacy({ files: slate.objects });
}
SearchManager.updateFile(updatedFiles);
if (updatedFiles.length) {
SearchManager.updateFile(updatedFiles);
}
}
if (updates.name && updates.name !== slate.name) {

View File

@ -19,7 +19,7 @@ export default async (req, res) => {
const slateId = req.body.data.slate?.id;
let slate;
if (slateId) {
slate = await Data.getSlateById({ id: slateId });
slate = await Data.getSlateById({ id: slateId, includeFiles: true });
if (!slate || slate.error) {
slate = null;

View File

@ -103,7 +103,10 @@ export default async (req, res) => {
} else if (!slate.isPublic && updates.isPublic) {
updatedFiles = await Utilities.addToPublicCollectionUpdatePrivacy({ files: slate.objects });
}
SearchManager.updateFile(updatedFiles);
if (updatedFiles.length) {
SearchManager.updateFile(updatedFiles);
}
SearchManager.updateSlate(updatedSlate);

View File

@ -19,7 +19,7 @@ export default async (req, res) => {
const slateId = req.body.data.slate?.id;
let slate;
if (slateId) {
slate = await Data.getSlateById({ id: slateId });
slate = await Data.getSlateById({ id: slateId, includeFiles: true });
if (!slate || slate.error) {
slate = null;

View File

@ -103,7 +103,9 @@ export default async (req, res) => {
} else if (!slate.isPublic && updates.isPublic) {
updatedFiles = await Utilities.addToPublicCollectionUpdatePrivacy({ files: slate.objects });
}
SearchManager.updateFile(updatedFiles);
if (updatedFiles.length) {
SearchManager.updateFile(updatedFiles);
}
SearchManager.updateSlate(updatedSlate);