2021-07-03 03:18:48 +03:00
|
|
|
import * as Monitor from "~/node_common/monitor";
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
//NOTE(martina): when you add any new variable to the user, file, or slate objects, add it in these structures
|
|
|
|
//add it to sanitize___ if it should be sent to the front end
|
|
|
|
//add it to clean____ if it should be saved to the database
|
2020-08-28 07:34:50 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
//NOTE(martina): these functions are to remove sensitive information before sending the data to the front end
|
|
|
|
//only variables listed here will be sent to the front end
|
|
|
|
|
|
|
|
export const sanitizeUser = (entity) => {
|
2020-08-28 07:34:50 +03:00
|
|
|
return {
|
|
|
|
id: entity.id,
|
|
|
|
username: entity.username,
|
2021-03-07 23:53:54 +03:00
|
|
|
slates: entity.slates, //NOTE(martina): this is not in the database. It is added after
|
|
|
|
library: entity.library, //NOTE(martina): this is not in the database. It is added after
|
2021-06-09 01:53:30 +03:00
|
|
|
twitterId: entity.twitterId,
|
|
|
|
email: entity.email,
|
2020-08-28 07:34:50 +03:00
|
|
|
data: {
|
2021-03-07 23:53:54 +03:00
|
|
|
name: entity.data?.name,
|
|
|
|
photo: entity.data?.photo,
|
|
|
|
body: entity.data?.body,
|
2020-08-28 07:34:50 +03:00
|
|
|
},
|
2021-06-01 08:14:22 +03:00
|
|
|
fileCount: entity.fileCount,
|
|
|
|
followerCount: entity.followerCount,
|
|
|
|
slateCount: entity.slateCount,
|
2020-08-28 07:34:50 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
export const sanitizeSlate = (entity) => {
|
2020-08-28 07:34:50 +03:00
|
|
|
return {
|
|
|
|
id: entity.id,
|
|
|
|
slatename: entity.slatename,
|
2021-03-07 23:53:54 +03:00
|
|
|
ownerId: entity.ownerId,
|
|
|
|
isPublic: entity.isPublic,
|
|
|
|
objects: entity.objects,
|
|
|
|
user: entity.user, //NOTE(martina): this is not in the database. It is added after
|
2020-08-28 07:34:50 +03:00
|
|
|
data: {
|
2021-03-07 23:53:54 +03:00
|
|
|
name: entity.data?.name,
|
|
|
|
body: entity.data?.body,
|
|
|
|
preview: entity.data?.preview,
|
|
|
|
layouts: entity.data?.layouts,
|
|
|
|
tags: entity.data?.tags,
|
2020-08-28 07:34:50 +03:00
|
|
|
},
|
2021-06-01 08:14:22 +03:00
|
|
|
fileCount: entity.fileCount,
|
|
|
|
subscriberCount: entity.subscriberCount,
|
2020-08-28 07:34:50 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
export const sanitizeFile = (entity) => {
|
2020-08-31 21:10:42 +03:00
|
|
|
return {
|
2021-03-07 23:53:54 +03:00
|
|
|
id: entity.id,
|
|
|
|
cid: entity.cid,
|
|
|
|
ownerId: entity.ownerId,
|
|
|
|
isPublic: entity.isPublic,
|
|
|
|
filename: entity.filename,
|
2021-04-30 22:14:14 +03:00
|
|
|
createdAt: entity.createdAt,
|
2021-03-07 23:53:54 +03:00
|
|
|
data: {
|
|
|
|
type: entity.data?.type,
|
|
|
|
name: entity.data?.name,
|
|
|
|
size: entity.data?.size,
|
|
|
|
body: entity.data?.body,
|
|
|
|
source: entity.data?.source,
|
|
|
|
author: entity.data?.author,
|
|
|
|
blurhash: entity.data?.blurhash,
|
|
|
|
coverImage: entity.data?.coverImage,
|
|
|
|
downloads: entity.data?.downloads, //NOTE(martina): newly added
|
|
|
|
tags: entity.data?.tags, //NOTE(martina): newly added
|
|
|
|
unity: entity.data?.unity, //NOTE(martina): newly added
|
|
|
|
link: entity.data?.link, //NOTE(martina): newly added
|
|
|
|
},
|
2021-06-01 08:14:22 +03:00
|
|
|
likeCount: entity.likeCount,
|
|
|
|
downloadCount: entity.downloadCount,
|
|
|
|
saveCount: entity.saveCount,
|
2020-08-31 21:10:42 +03:00
|
|
|
};
|
2020-08-30 12:07:21 +03:00
|
|
|
};
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
//NOTE(martina): these functions are to remove extraneous information before updating the database entry.
|
|
|
|
//Only variables included here will be added to the database
|
2020-08-28 07:34:50 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
export const cleanUser = (entity) => {
|
2020-08-31 21:10:42 +03:00
|
|
|
return {
|
2021-03-07 23:53:54 +03:00
|
|
|
id: entity.id,
|
|
|
|
username: entity.username,
|
|
|
|
createdAt: entity.createdAt,
|
|
|
|
lastActive: entity.lastActive,
|
|
|
|
salt: entity.salt,
|
|
|
|
password: entity.password,
|
|
|
|
email: entity.email,
|
2021-06-09 01:53:30 +03:00
|
|
|
twitterId: entity.twitterId,
|
|
|
|
authVersion: entity.authVersion,
|
2021-03-07 23:53:54 +03:00
|
|
|
data: entity.data,
|
2021-07-03 04:30:43 +03:00
|
|
|
revertedVersion: entity.revertedVersion,
|
2021-03-07 23:53:54 +03:00
|
|
|
// data: {
|
|
|
|
// name: entity.data?.name,
|
|
|
|
// photo: entity.data?.photo,
|
|
|
|
// body: entity.data?.body,
|
|
|
|
// tokens: entity.data?.tokens,
|
|
|
|
// settings: entity.data?.settings,
|
|
|
|
// onboarding: entity.data?.onboarding,
|
|
|
|
// status: entity.data?.status,
|
|
|
|
// },
|
2020-08-31 21:10:42 +03:00
|
|
|
};
|
2020-08-28 07:34:50 +03:00
|
|
|
};
|
2020-09-07 01:30:33 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
export const cleanSlate = (entity) => {
|
2020-09-07 01:30:33 +03:00
|
|
|
return {
|
2021-03-07 23:53:54 +03:00
|
|
|
id: entity.id,
|
|
|
|
createdAt: entity.createdAt,
|
|
|
|
updatedAt: entity.updatedAt,
|
|
|
|
slatename: entity.slatename,
|
|
|
|
isPublic: entity.isPublic,
|
|
|
|
ownerId: entity.ownerId,
|
|
|
|
data: entity.data,
|
|
|
|
// data: {
|
|
|
|
// name: entity.data?.name,
|
|
|
|
// body: entity.data?.body,
|
|
|
|
// preview: entity.data?.preview,
|
|
|
|
// layouts: entity.data?.layouts,
|
|
|
|
// tags: entity.data?.tags,
|
|
|
|
// },
|
2020-09-07 01:30:33 +03:00
|
|
|
};
|
|
|
|
};
|
2021-01-14 09:30:26 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
export const cleanFile = (entity) => {
|
|
|
|
return {
|
|
|
|
id: entity.id,
|
|
|
|
cid: entity.cid,
|
|
|
|
createdAt: entity.createdAt,
|
|
|
|
ownerId: entity.ownerId,
|
|
|
|
isPublic: entity.isPublic,
|
|
|
|
filename: entity.filename,
|
|
|
|
data: entity.data,
|
|
|
|
// data: {
|
|
|
|
// type: entity.data?.type,
|
|
|
|
// name: entity.data?.name,
|
|
|
|
// size: entity.data?.size,
|
|
|
|
// body: entity.data?.body,
|
|
|
|
// source: entity.data?.source,
|
|
|
|
// author: entity.data?.author,
|
|
|
|
// blurhash: entity.data?.blurhash,
|
|
|
|
// coverImage: entity.data?.coverImage,
|
|
|
|
// downloads: entity.data?.downloads,
|
|
|
|
// tags: entity.data?.tags,
|
|
|
|
// unity: entity.data?.unity,
|
|
|
|
// link: entity.data?.link,
|
|
|
|
// },
|
|
|
|
};
|
|
|
|
};
|
2021-01-14 09:30:26 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
//NOTE(martina): these functions are used to get the updated object that is obtained by merging the old and new objects
|
|
|
|
// and using the above cleaning functions to strip out things that should not be in the database or should not be mutated
|
2021-01-14 09:30:26 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
export const getUpdatedSlate = (oldSlate, updates) => {
|
|
|
|
let updatedSlate = cleanSlate(updates);
|
|
|
|
return { ...oldSlate, ...updatedSlate, data: { ...oldSlate.data, ...updatedSlate.data } };
|
|
|
|
};
|
2021-01-14 09:30:26 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
export const getUpdatedFile = (oldFile, updates) => {
|
|
|
|
let updatedFile = cleanFile(updates);
|
|
|
|
return { ...oldFile, ...updatedFile, data: { ...oldFile.data, ...updatedFile.data } };
|
|
|
|
};
|
2021-01-14 09:30:26 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
export const getUpdatedUser = (oldUser, updates) => {
|
2021-07-03 03:18:48 +03:00
|
|
|
//NOTE(martina): we have this check here to make sure we never accidentally update the auth version without updating the password as well
|
2021-07-03 04:03:47 +03:00
|
|
|
if (
|
|
|
|
!oldUser.revertedVersion &&
|
|
|
|
updates.authVersion &&
|
|
|
|
updates.authVersion > oldUser.authVersion
|
|
|
|
) {
|
|
|
|
if (!updates.password) {
|
|
|
|
delete updates.authVersion;
|
|
|
|
Monitor.message(
|
|
|
|
"node_common/serializers.js",
|
|
|
|
`Tried to update authVersion but missing a password update. Update blocked for user ${oldUser.username}`
|
|
|
|
);
|
|
|
|
} else if (updates.password === oldUser.password) {
|
|
|
|
delete updates.authVersion;
|
|
|
|
Monitor.message(
|
|
|
|
"node_common/serializers.js",
|
|
|
|
`Tried to update authVersion but has the same password hash as before. Update blocked for user ${oldUser.username}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-03-07 23:53:54 +03:00
|
|
|
let updatedUser = cleanUser(updates);
|
|
|
|
return { ...oldUser, ...updatedUser, data: { ...oldUser.data, ...updatedUser.data } };
|
2021-01-14 09:30:26 +03:00
|
|
|
};
|