From 09ad9cfa553444b63e4c4ab041cf11befe64b5de Mon Sep 17 00:00:00 2001 From: toast Date: Mon, 5 Oct 2020 15:33:37 -0700 Subject: [PATCH] added randomized avatar function --- node_common/environment.js | 2 ++ node_common/managers/slate.js | 15 ++++++++++++++ pages/api/users/create.js | 38 ++++++++++++----------------------- 3 files changed, 30 insertions(+), 25 deletions(-) create mode 100644 node_common/managers/slate.js diff --git a/node_common/environment.js b/node_common/environment.js index c7a63acd..331eb0e2 100644 --- a/node_common/environment.js +++ b/node_common/environment.js @@ -25,6 +25,8 @@ export const LOCAL_PASSWORD_SECRET = `$2b$${LOCAL_PASSWORD_ROUNDS}$${ process.env.LOCAL_PASSWORD_SECRET }`; +export const AVATAR_SLATE_ID = process.env.AVATAR_SLATE_ID; + export const TEXTILE_HUB_KEY = process.env.TEXTILE_HUB_KEY; export const TEXTILE_HUB_SECRET = process.env.TEXTILE_HUB_SECRET; export const TEXTILE_HUB_STAGING_HOST = process.env.TEXTILE_HUB_STAGING_HOST; diff --git a/node_common/managers/slate.js b/node_common/managers/slate.js new file mode 100644 index 00000000..ea80bc6f --- /dev/null +++ b/node_common/managers/slate.js @@ -0,0 +1,15 @@ +import * as Data from "~/node_common/data"; + +export const getRandomSlateElementURL = async ({ id, fallback }) => { + if (!{ id }) { + return { fallback }; + } + + const query = await Data.getSlateById({ id }); + + if (!query || query.error) { + return { fallback }; + } + + return query.data.objects[Math.floor(Math.random() * query.data.objects.length)].url; +}; diff --git a/pages/api/users/create.js b/pages/api/users/create.js index 51e1ba65..eff29291 100644 --- a/pages/api/users/create.js +++ b/pages/api/users/create.js @@ -1,6 +1,7 @@ import * as Environment from "~/node_common/environment"; import * as Data from "~/node_common/data"; import * as Utilities from "~/node_common/utilities"; +import * as SlateManager from "~/node_common/managers/slate"; import * as LibraryManager from "~/node_common/managers/library"; import * as Social from "~/node_common/social"; import * as Validations from "~/common/validations"; @@ -15,21 +16,15 @@ export default async (req, res) => { }); if (existing) { - return res - .status(403) - .send({ decorator: "SERVER_EXISTING_USER_ALREADY", error: true }); + return res.status(403).send({ decorator: "SERVER_EXISTING_USER_ALREADY", error: true }); } if (!Validations.username(req.body.data.username)) { - return res - .status(500) - .send({ decorator: "SERVER_INVALID_USERNAME", error: true }); + return res.status(500).send({ decorator: "SERVER_INVALID_USERNAME", error: true }); } if (!Validations.password(req.body.data.password)) { - return res - .status(500) - .send({ decorator: "SERVER_INVALID_PASSWORD", error: true }); + return res.status(500).send({ decorator: "SERVER_INVALID_PASSWORD", error: true }); } const rounds = Number(Environment.LOCAL_PASSWORD_ROUNDS); @@ -46,11 +41,7 @@ export default async (req, res) => { // Don't do this once you refactor. const newUsername = req.body.data.username.toLowerCase(); - const { - buckets, - bucketKey, - bucketName, - } = await Utilities.getBucketAPIFromUserToken({ + const { buckets, bucketKey, bucketName } = await Utilities.getBucketAPIFromUserToken({ user: { username: newUsername, data: { tokens: { api } }, @@ -58,9 +49,7 @@ export default async (req, res) => { }); if (!buckets) { - return res - .status(500) - .send({ decorator: "SERVER_BUCKET_INIT_FAILURE", error: true }); + return res.status(500).send({ decorator: "SERVER_BUCKET_INIT_FAILURE", error: true }); } const user = await Data.createUser({ @@ -68,8 +57,11 @@ export default async (req, res) => { salt, username: newUsername, data: { - photo: - "https://slate.textile.io/ipfs/bafkreiexygfz4e5resu66xfviokddariztq4onuai5wii5mdd7syshftca", + photo: SlateManager.getRandomSlateElementURL({ + id: Environment.AVATAR_SLATE_ID, + fallback: + "https://slate.textile.io/ipfs/bafkreick3nscgixwfpq736forz7kzxvvhuej6kszevpsgmcubyhsx2pf7i", + }), body: "A user of Slate.", settings_deals_auto_approve: false, allow_filecoin_directory_listing: false, @@ -81,15 +73,11 @@ export default async (req, res) => { }); if (!user) { - return res - .status(404) - .send({ decorator: "SERVER_USER_CREATE_USER_NOT_FOUND", error: true }); + return res.status(404).send({ decorator: "SERVER_USER_CREATE_USER_NOT_FOUND", error: true }); } if (user.error) { - return res - .status(500) - .send({ decorator: "SERVER_USER_CREATE_USER_NOT_FOUND", error: true }); + return res.status(500).send({ decorator: "SERVER_USER_CREATE_USER_NOT_FOUND", error: true }); } const userProfileURL = `https://slate.host/${user.username}`;