slate/node_common/serializers.js

350 lines
8.7 KiB
JavaScript
Raw Normal View History

import DB from "~/node_common/database";
export const user = (entity) => {
return {
type: "USER",
id: entity.id,
username: entity.username,
slates: entity.slates ? entity.slates : [],
data: {
name: entity.data.name ? entity.data.name : "",
photo: entity.data.photo ? entity.data.photo : "",
body: entity.data.body ? entity.data.body : "",
},
};
};
export const slate = (entity) => {
return {
type: "SLATE",
id: entity.id,
slatename: entity.slatename,
data: {
ownerId: entity.data.ownerId,
name: entity.data.name ? entity.data.name : "",
body: entity.data.body ? entity.data.body : "",
objects: entity.data.objects,
},
};
};
export const doSlates = async ({ serializedUsers, slates }) => {
const userToSlatesMap = {};
const sanitized = slates.map((d) => {
let o = null;
if (userToSlatesMap[d.data.ownerId]) {
userToSlatesMap[d.data.ownerId].push(d);
}
if (!userToSlatesMap[d.data.ownerId]) {
userToSlatesMap[d.data.ownerId] = [d];
}
if (d.data.ownerId) {
o = serializedUsers.find((e) => d.data.ownerId === e.id);
}
return { ...d, owner: o };
});
2020-08-31 21:10:42 +03:00
return {
serializedSlates: JSON.parse(JSON.stringify(sanitized)),
userToSlatesMap,
};
};
2020-08-31 21:10:42 +03:00
export const doTrusted = async ({
users,
trusted,
serializedUsersMap,
serializedSlatesMap,
}) => {
trusted.forEach((each) => {
if (each.target_user_id && !serializedUsersMap[each.target_user_id]) {
users.push(each.target_user_id);
}
});
let userEntities = [];
2020-08-30 22:56:45 +03:00
try {
console.log({ query: `CHECK_TO_SERIALIZE` });
if (users.length) {
console.log({ query: `(${users.length}) USERS_FOR_SERIALIZATION` });
2020-08-31 21:10:42 +03:00
userEntities = await DB.select("id", "username", "data")
.from("users")
.whereIn("id", users);
2020-08-30 22:56:45 +03:00
}
} catch (e) {
console.log("FAILED TO SERIALIZE");
2020-08-31 21:10:42 +03:00
return {
serializedTrusted: trusted,
serializedUsersMap,
serializedSlatesMap,
};
}
const sanitized = trusted.map((data) => {
let u = null;
let o = null;
if (data.target_user_id) {
if (serializedUsersMap[data.target_user_id]) {
u = serializedUsersMap[data.target_user_id];
} else {
u = userEntities.find((e) => data.target_user_id === e.id);
u = user(u);
serializedUsersMap[data.target_user_id] = u;
}
}
if (data.owner_user_id) {
if (serializedUsersMap[data.owner_user_id]) {
o = serializedUsersMap[data.owner_user_id];
} else {
o = userEntities.find((e) => data.owner_user_id === e.id);
o = user(o);
serializedUsersMap[data.owner_user_id] = o;
}
}
return { ...data, user: u, owner: o };
});
2020-08-31 21:10:42 +03:00
return {
serializedTrusted: JSON.parse(JSON.stringify(sanitized)),
serializedUsersMap,
serializedSlatesMap,
};
};
2020-08-31 21:10:42 +03:00
export const doPendingTrusted = async ({
users,
pendingTrusted,
serializedUsersMap,
serializedSlatesMap,
}) => {
pendingTrusted.forEach((each) => {
2020-08-31 21:10:42 +03:00
if (each.owner_user_id && !serializedUsersMap[each.owner_user_id]) {
users.push(each.owner_user_id);
}
});
let userEntities = [];
2020-08-30 22:56:45 +03:00
try {
console.log({ query: `CHECK_TO_SERIALIZE` });
if (users.length) {
console.log({ query: `(${users.length}) USERS_FOR_SERIALIZATION` });
2020-08-31 21:10:42 +03:00
userEntities = await DB.select("id", "username", "data")
.from("users")
.whereIn("id", users);
2020-08-30 22:56:45 +03:00
}
} catch (e) {
console.log("FAILED TO SERIALIZE");
2020-08-31 21:10:42 +03:00
return {
serializedPendingTrusted: pendingTrusted,
serializedUsersMap,
serializedSlatesMap,
};
}
const sanitized = pendingTrusted.map((data) => {
let u = null;
let o = null;
if (data.target_user_id) {
if (serializedUsersMap[data.target_user_id]) {
u = serializedUsersMap[data.target_user_id];
} else {
u = userEntities.find((e) => data.target_user_id === e.id);
u = user(u);
serializedUsersMap[data.target_user_id] = u;
}
}
if (data.owner_user_id) {
if (serializedUsersMap[data.owner_user_id]) {
o = serializedUsersMap[data.owner_user_id];
} else {
o = userEntities.find((e) => data.owner_user_id === e.id);
o = user(o);
serializedUsersMap[data.owner_user_id] = o;
}
}
return { ...data, user: u, owner: o };
});
2020-08-31 21:10:42 +03:00
return {
serializedPendingTrusted: JSON.parse(JSON.stringify(sanitized)),
serializedUsersMap,
serializedSlatesMap,
};
};
2020-08-31 21:10:42 +03:00
export const doSubscriptions = async ({
users,
slates,
subscriptions,
serializedUsersMap,
serializedSlatesMap,
}) => {
subscriptions.forEach((each) => {
if (each.target_user_id && !serializedUsersMap[each.target_user_id]) {
users.push(each.target_user_id);
}
if (each.target_slate_id && !serializedSlatesMap[each.target_slate_id]) {
slates.push(each.target_slate_id);
}
});
let userEntities = [];
2020-08-30 22:56:45 +03:00
try {
console.log({ query: `CHECK_TO_SERIALIZE` });
if (users.length) {
console.log({ query: `(${users.length}) USERS_FOR_SERIALIZATION` });
2020-08-31 21:10:42 +03:00
userEntities = await DB.select("id", "username", "data")
.from("users")
.whereIn("id", users);
2020-08-30 22:56:45 +03:00
}
} catch (e) {
console.log("FAILED TO SERIALIZE");
2020-08-31 21:10:42 +03:00
return {
serializedSubscriptions: subscriptions,
serializedUsersMap,
serializedSlatesMap,
};
}
let slateEntities = [];
2020-08-30 22:56:45 +03:00
try {
console.log({ query: `CHECK_TO_SERIALIZE` });
if (slates.length) {
console.log({ query: `(${slates.length}) SLATES_FOR_SERIALIZATION` });
2020-08-31 21:10:42 +03:00
slateEntities = await DB.select("id", "slatename", "data")
.from("slates")
.whereIn("id", slates);
2020-08-30 22:56:45 +03:00
}
} catch (e) {
console.log("FAILED TO SERIALIZE");
2020-08-31 21:10:42 +03:00
return {
serializedSubscriptions: subscriptions,
serializedUsersMap,
serializedSlatesMap,
};
}
const sanitized = subscriptions.map((data) => {
let u = null;
let o = null;
let s = null;
if (data.target_user_id) {
if (serializedUsersMap[data.target_user_id]) {
u = serializedUsersMap[data.target_user_id];
} else {
u = userEntities.find((e) => data.target_user_id === e.id);
u = user(u);
serializedUsersMap[data.target_user_id] = u;
}
}
if (data.owner_user_id) {
if (serializedUsersMap[data.owner_user_id]) {
o = serializedUsersMap[data.owner_user_id];
} else {
o = userEntities.find((e) => data.owner_user_id === e.id);
o = user(o);
serializedUsersMap[data.owner_user_id] = o;
}
}
if (data.target_slate_id) {
if (serializedSlatesMap[data.target_slate_id]) {
s = serializedSlatesMap[data.target_slate_id];
} else {
s = slateEntities.find((e) => data.target_slate_id === e.id);
s = slate(s);
serializedSlatesMap[data.target_slate_id] = s;
}
}
return { ...data, user: u, owner: o, slate: s };
});
2020-08-31 21:10:42 +03:00
return {
serializedSubscriptions: JSON.parse(JSON.stringify(sanitized)),
serializedUsersMap,
serializedSlatesMap,
};
};
2020-09-07 01:30:33 +03:00
export const doSubscribers = async ({
users,
slates,
subscribers,
serializedUsersMap,
serializedSlatesMap,
}) => {
subscribers.forEach((each) => {
if (each.owner_user_id && !serializedUsersMap[each.owner_user_id]) {
users.push(each.owner_user_id);
}
});
let userEntities = [];
try {
console.log({ query: `CHECK_TO_SERIALIZE` });
if (users.length) {
console.log({ query: `(${users.length}) USERS_FOR_SERIALIZATION` });
userEntities = await DB.select("id", "username", "data")
.from("users")
.whereIn("id", users);
}
} catch (e) {
console.log("FAILED TO SERIALIZE");
return {
serializedSubscribers: subscribers,
serializedUsersMap,
serializedSlatesMap,
};
}
const sanitized = subscribers.map((data) => {
let u = null;
let o = null;
let s = null;
if (data.owner_user_id) {
if (serializedUsersMap[data.owner_user_id]) {
o = serializedUsersMap[data.owner_user_id];
} else {
o = userEntities.find((e) => data.owner_user_id === e.id);
o = user(o);
serializedUsersMap[data.owner_user_id] = o;
}
}
if (data.target_user_id) {
if (serializedUsersMap[data.target_user_id]) {
u = serializedUsersMap[data.target_user_id];
} else {
u = userEntities.find((e) => data.target_user_id === e.id);
u = user(u);
serializedUsersMap[data.target_user_id] = u;
}
}
return { ...data, user: u, owner: o, slate: s };
});
return {
serializedSubscribers: JSON.parse(JSON.stringify(sanitized)),
serializedUsersMap,
serializedSlatesMap,
};
};