Merge pull request #354 from toastts/@toastts/randomized-avatar

added randomized avatar fucntion
This commit is contained in:
CAKE 2020-10-05 16:30:58 -07:00 committed by GitHub
commit 502dc9a856
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 25 deletions

View File

@ -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;

View File

@ -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;
};

View File

@ -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}`;