1
Fork 0
mirror of https://github.com/diced/zipline.git synced 2025-04-04 23:21:17 -05:00

feat: default avatar stuffs

This commit is contained in:
diced 2022-12-15 20:05:33 -08:00
parent d85211a145
commit bb28f49cf5
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1
7 changed files with 53 additions and 10 deletions

View file

@ -106,6 +106,8 @@ export interface ConfigFeatures {
user_registration: boolean;
headless: boolean;
default_avatar: string;
}
export interface ConfigOAuth {

View file

@ -143,6 +143,8 @@ export default function readConfig() {
map('FEATURES_HEADLESS', 'boolean', 'features.headless'),
map('FEATURES_DEFAULT_AVATAR', 'path', 'features.default_avatar'),
map('CHUNKS_MAX_SIZE', 'human-to-byte', 'chunks.max_size'),
map('CHUNKS_CHUNKS_SIZE', 'human-to-byte', 'chunks.chunks_size'),

View file

@ -170,6 +170,7 @@ const validator = s.object({
oauth_login_only: s.boolean.default(false),
user_registration: s.boolean.default(false),
headless: s.boolean.default(false),
default_avatar: s.string.nullable.default(null),
})
.default({
invites: false,
@ -178,6 +179,7 @@ const validator = s.object({
oauth_login_only: false,
user_registration: false,
headless: false,
default_avatar: null,
}),
chunks: s
.object({

View file

@ -5,6 +5,12 @@ import ms, { StringValue } from 'ms';
dayjs.extend(duration);
dayjs.extend(dayjsRelativeTime);
export function jsonUserReplacer(key: string, value: any) {
if (key === 'avatar') return 'data:image/*;base64,***';
return value;
}
export function randomChars(length: number) {
const charset = 'QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890';

View file

@ -1,8 +1,12 @@
import { readFile } from 'fs/promises';
import config from 'lib/config';
import Logger from 'lib/logger';
import { NextApiReq, NextApiRes, withZipline } from 'lib/middleware/withZipline';
import { guess } from 'lib/mimes';
import prisma from 'lib/prisma';
import { createToken, hashPassword } from 'lib/util';
import { jsonUserReplacer } from 'lib/utils/client';
import { extname } from 'path';
const logger = Logger.get('user');
@ -29,12 +33,25 @@ async function handler(req: NextApiReq, res: NextApiRes) {
if (user) return res.badRequest('username already exists');
const hashed = await hashPassword(password);
let avatar;
if (config.features.default_avatar) {
logger.debug(`using default avatar ${config.features.default_avatar}`);
const buf = await readFile(config.features.default_avatar);
const mimetype = await guess(extname(config.features.default_avatar));
logger.debug(`guessed mimetype ${mimetype} for ${config.features.default_avatar}`);
avatar = `data:${mimetype};base64,${buf.toString('base64')}`;
}
const newUser = await prisma.user.create({
data: {
password: hashed,
username,
token: createToken(),
administrator: false,
avatar,
},
});
@ -49,7 +66,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
});
}
logger.debug(`created user via invite ${code} ${JSON.stringify(newUser)}`);
logger.debug(`created user via invite ${code} ${JSON.stringify(newUser, jsonUserReplacer)}`);
logger.info(
`Created user ${newUser.username} (${newUser.id}) ${
@ -82,16 +99,28 @@ async function handler(req: NextApiReq, res: NextApiRes) {
const hashed = await hashPassword(password);
let avatar;
if (config.features.default_avatar) {
logger.debug(`using default avatar ${config.features.default_avatar}`);
const buf = await readFile(config.features.default_avatar);
const mimetype = await guess(extname(config.features.default_avatar));
logger.debug(`guessed mimetype ${mimetype} for ${config.features.default_avatar}`);
avatar = `data:${mimetype};base64,${buf.toString('base64')}`;
}
const newUser = await prisma.user.create({
data: {
password: hashed,
username,
token: createToken(),
administrator,
avatar,
},
});
logger.debug(`created user ${JSON.stringify(newUser)}`);
logger.debug(`created user ${JSON.stringify(newUser, jsonUserReplacer)}`);
delete newUser.password;

View file

@ -2,6 +2,7 @@ import datasource from 'lib/datasource';
import Logger from 'lib/logger';
import prisma from 'lib/prisma';
import { hashPassword } from 'lib/util';
import { jsonUserReplacer } from 'lib/utils/client';
import { NextApiReq, NextApiRes, UserExtended, withZipline } from 'middleware/withZipline';
const logger = Logger.get('user');
@ -159,7 +160,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
},
});
logger.debug(`updated user ${id} with ${JSON.stringify(newUser)}`);
logger.debug(`updated user ${id} with ${JSON.stringify(newUser, jsonUserReplacer)}`);
logger.info(
`User ${user.username} (${user.id}) updated ${target.username} (${newUser.username}) (${newUser.id})`

View file

@ -3,6 +3,7 @@ import Logger from 'lib/logger';
import { discord_auth, github_auth, google_auth } from 'lib/oauth';
import prisma from 'lib/prisma';
import { hashPassword } from 'lib/util';
import { jsonUserReplacer } from 'lib/utils/client';
import { NextApiReq, NextApiRes, UserExtended, withZipline } from 'middleware/withZipline';
const logger = Logger.get('user');
@ -13,7 +14,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
if (user.oauth.find((o) => o.provider === 'GITHUB')) {
const resp = await github_auth.oauth_user(user.oauth.find((o) => o.provider === 'GITHUB').token);
if (!resp) {
logger.debug(`oauth expired for ${JSON.stringify(user)}`);
logger.debug(`oauth expired for ${JSON.stringify(user, jsonUserReplacer)}`);
return res.json({
error: 'oauth token expired',
@ -29,7 +30,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
if (!resp.ok) {
const provider = user.oauth.find((o) => o.provider === 'DISCORD');
if (!provider.refresh) {
logger.debug(`couldn't find a refresh token for ${JSON.stringify(user)}`);
logger.debug(`couldn't find a refresh token for ${JSON.stringify(user, jsonUserReplacer)}`);
return res.json({
error: 'oauth token expired',
@ -53,7 +54,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
}),
});
if (!resp2.ok) {
logger.debug(`oauth expired for ${JSON.stringify(user)}`);
logger.debug(`oauth expired for ${JSON.stringify(user, jsonUserReplacer)}`);
return res.json({
error: 'oauth token expired',
@ -84,7 +85,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
if (!resp.ok) {
const provider = user.oauth.find((o) => o.provider === 'GOOGLE');
if (!provider.refresh) {
logger.debug(`couldn't find a refresh token for ${JSON.stringify(user)}`);
logger.debug(`couldn't find a refresh token for ${JSON.stringify(user, jsonUserReplacer)}`);
return res.json({
error: 'oauth token expired',
@ -107,7 +108,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
}),
});
if (!resp2.ok) {
logger.debug(`oauth expired for ${JSON.stringify(user)}`);
logger.debug(`oauth expired for ${JSON.stringify(user, jsonUserReplacer)}`);
return res.json({
error: 'oauth token expired',
@ -134,7 +135,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
}
if (req.method === 'PATCH') {
logger.debug(`attempting to update user ${JSON.stringify(user)}`);
logger.debug(`attempting to update user ${JSON.stringify(user, jsonUserReplacer)}`);
if (req.body.password) {
const hashed = await hashPassword(req.body.password);
@ -236,7 +237,7 @@ async function handler(req: NextApiReq, res: NextApiRes, user: UserExtended) {
},
});
logger.debug(`updated user ${JSON.stringify(newUser)}`);
logger.debug(`updated user ${JSON.stringify(newUser, jsonUserReplacer)}`);
logger.info(`User ${user.username} (${newUser.username}) (${newUser.id}) was updated`);