added error messaging & extra privacy recalc when add to slate & conversions between version structures

This commit is contained in:
Martina 2021-09-08 12:30:59 -07:00
parent 18396439ef
commit d0d0d356c9
3 changed files with 178 additions and 0 deletions

169
common/conversions.js Normal file
View File

@ -0,0 +1,169 @@
import * as Strings from "~/common/strings";
//NOTE(martina): V1
export const convertToV1User = (user) => {
return {
id: user.id,
username: user.username,
data: {
name: user.name,
photo: user.photo,
body: user.body,
},
};
};
export const convertToV1Slate = (slate) => {
let reformattedSlate = {
id: slate.id,
updated_at: slate.updatedAt,
created_at: slate.createdAt,
slatename: slate.slatename,
data: {
name: slate.name,
public: slate.isPublic,
ownerId: slate.ownerId,
},
};
if (slate.objects) {
reformattedSlate.objects = slate.objects.map((obj) => convertToV1File(obj));
}
return reformattedSlate;
};
export const convertToV1File = (file) => {
return {
id: file.id,
cid: file.cid,
url: Strings.getURLfromCID(file.cid),
name: file.filename,
size: file.size,
type: file.type,
title: file.name,
ownerId: file.ownerId,
blurhash: file.blurhash,
source: file.source,
body: file.body,
author: file.author,
linkName: file.linkName,
linkBody: file.linkBody,
linkAuthor: file.linkAuthor,
linkSource: file.linkSource,
linkDomain: file.linkDomain,
linkImage: file.linkImage,
linkFavicon: file.linkFavicon,
};
};
//NOTE(martina): V2
export const convertToV2User = (user) => {
return {
id: user.id,
username: user.username,
data: {
name: user.name,
photo: user.photo,
body: user.body,
},
};
};
export const convertToV2Slate = (slate) => {
let reformattedSlate = {
id: slate.id,
updatedAt: slate.updatedAt,
createdAt: slate.createdAt,
slatename: slate.slatename,
isPublic: slate.isPublic,
ownerId: slate.ownerId,
data: {
name: slate.name,
body: slate.body,
},
};
if (slate.objects) {
reformattedSlate.objects = slate.objects.map((obj) => convertToV2File(obj));
}
return reformattedSlate;
};
export const convertToV2File = (file) => {
return {
id: file.id,
cid: file.cid,
url: Strings.getURLfromCID(file.cid),
filename: file.filename,
ownerId: file.ownerId,
data: {
name: file.name,
blurhash: file.blurhash,
size: file.size,
type: file.type,
source: file.source,
body: file.body,
author: file.author,
linkName: file.linkName,
linkBody: file.linkBody,
linkAuthor: file.linkAuthor,
linkSource: file.linkSource,
linkDomain: file.linkDomain,
linkImage: file.linkImage,
linkFavicon: file.linkFavicon,
},
};
};
//NOTE(martina): V3. Since this is the current version, it simply cleans any extraneous info rather than reformatting at all
export const convertToV3User = (user) => {
return {
id: user.id,
username: user.username,
name: user.name,
photo: user.photo,
body: user.body,
};
};
export const convertToV3Slate = (slate) => {
let reformattedSlate = {
id: slate.id,
updatedAt: slate.updatedAt,
createdAt: slate.createdAt,
slatename: slate.slatename,
isPublic: slate.isPublic,
ownerId: slate.ownerId,
name: slate.name,
body: slate.body,
};
if (slate.objects) {
reformattedSlate.objects = slate.objects.map((obj) => convertToV3File(obj));
}
return reformattedSlate;
};
export const convertToV3File = (file) => {
return {
id: file.id,
cid: file.cid,
url: Strings.getURLfromCID(file.cid),
filename: file.filename,
ownerId: file.ownerId,
name: file.name,
blurhash: file.blurhash,
size: file.size,
type: file.type,
source: file.source,
body: file.body,
author: file.author,
linkName: file.linkName,
linkBody: file.linkBody,
linkAuthor: file.linkAuthor,
linkSource: file.linkSource,
linkDomain: file.linkDomain,
linkImage: file.linkImage,
linkFavicon: file.linkFavicon,
};
};

View File

@ -46,6 +46,7 @@ export const checkAuthorizationExternal = async (req, res) => {
if (!key) {
return res.status(403).send({
decorator: "NO_MATCHING_API_KEY_FOUND",
message: "We could not find that API key in our records",
error: true,
});
}
@ -53,6 +54,7 @@ export const checkAuthorizationExternal = async (req, res) => {
if (key.error) {
return res.status(500).send({
decorator: "ERROR_WHILE_VERIFYING_API_KEY",
message: "We ran into an error while verifying that API key. Please try again",
error: true,
});
}
@ -64,6 +66,7 @@ export const checkAuthorizationExternal = async (req, res) => {
if (!user) {
return res.status(404).send({
decorator: "API_KEY_OWNER_NOT_FOUND",
message: "We were unable to find the owner of that API key",
error: true,
});
}
@ -71,6 +74,8 @@ export const checkAuthorizationExternal = async (req, res) => {
if (user.error) {
return res.status(500).send({
decorator: "ERROR_WHILE_LOCATING_API_KEY_OWNER",
message:
"We ran into an error while trying to find the owner of that API key. Please try again",
error: true,
});
}

View File

@ -277,6 +277,10 @@ export const addToSlate = async ({ slate, files, user, saveCopy = false }) => {
await Data.updateSlateById({ id: slate.id, updatedAt: new Date() });
if (slate.isPublic) {
addToPublicCollectionUpdatePrivacy({ filteredFiles });
}
return { added: response.length };
};