This commit is contained in:
2025-09-11 19:47:33 -04:00
parent 317da3aaf7
commit c2eb111291
14 changed files with 217 additions and 44 deletions

View File

@@ -1,11 +1,12 @@
import { createHonoRoute } from "../../../util/import.ts";
import { authenticate, galvanicError, GalvanicErrors, RateLimiter, recNetError, statusResponse } from "../../../util/api.ts";
import { RateLimiter, recNetResultResponse, statusResponse } from "../../../util/api.ts";
import Server from "../../../server/server.ts";
import z from "zod";
import { transformCheckEnum, typedZValidator } from "../../../util/validators.ts";
import { PlatformType } from "../../../server/platforms/types.ts";
import Steam from "../../../util/steam/steam.ts";
import { HTTPStatus } from "@oneday/http-status";
import { accountRoute } from "./me/root.ts";
export const route = createHonoRoute('/account');
@@ -35,20 +36,22 @@ const createAccountBodySchema = z.object({
});
route.app.post('/create', postCreateRateLimiter.middle(), typedZValidator('form', createAccountBodySchema), async c => {
if (c.req.header("User-Agent") !== "BestHTTP") return recNetResultResponse(c, HTTPStatus.OK, false, "Platform error");
const form = c.req.valid('form');
if (typeof form.platform == 'undefined')
return c.json(galvanicError(GalvanicErrors.jex));
if (form.platform == null)
return recNetResultResponse(c, HTTPStatus.OK, false, "Platform error");
else if (form.platform == PlatformType.Steam) {
const steam = await Steam.GetPlayerSummaries([form.platformId]);
if (steam.length == 0)
return c.json(galvanicError(GalvanicErrors.sploot));
return recNetResultResponse(c, HTTPStatus.OK, false, "Steam profile could not be fetched");
const cachedlogins = await Server.Platforms.getCachedLogins(form.platform, form.platformId, true);
if (cachedlogins.length == 0) {
const profile = await Server.Profiles.create(form.platform, form.platformId, steam[0].realname ?? steam[0].personaname);
if (!profile) return c.json(galvanicError(GalvanicErrors.sploot));
if (!profile) return recNetResultResponse(c, HTTPStatus.OK, false, "Account could not be created");
Server.Content.steamAvatarDownloadForProfile(profile, steam[0].avatarfull);
@@ -60,7 +63,7 @@ route.app.post('/create', postCreateRateLimiter.middle(), typedZValidator('form'
} else {
const profile = await Server.Profiles.create(form.platform, form.platformId);
if (!profile) return c.json(galvanicError(GalvanicErrors.sploot));
if (!profile) return recNetResultResponse(c, HTTPStatus.OK, false, "Account could not be created");
return c.json({
success: true,
@@ -68,14 +71,11 @@ route.app.post('/create', postCreateRateLimiter.middle(), typedZValidator('form'
});
}
} else return c.json(recNetError("Not a Steam user"));
} else return recNetResultResponse(c, HTTPStatus.OK, false, "Not a Steam user");
});
route.app.get('/me', authenticate, c => {
const profile = c.get('profile');
return c.json(profile.selfExport());
});
route.app.route('/', accountRoute.app);
const getAccountByIdParamSchema = z.object({
id: z.coerce.number().max(Math.pow(2, 31))

View File

@@ -0,0 +1,38 @@
import { HTTPStatus } from "@oneday/http-status";
import { authenticate, recNetResultResponse } from "../../../../util/api.ts";
import { createHonoRoute } from "../../../../util/import.ts";
import z from "zod";
import { typedZValidator } from "../../../../util/validators.ts";
import Server from "../../../../server/server.ts";
export const accountRoute = createHonoRoute("/");
accountRoute.app.get('/me', authenticate, c => {
const profile = c.get('profile');
return c.json(profile.selfExport());
});
const changeUsernameFormSchema = z.object({
username: z.string().min(4).max(48).regex(/^[A-Za-z0-9._-]+$/)
});
accountRoute.app.put('/me/username',
authenticate,
typedZValidator('form', changeUsernameFormSchema, true, "Username must only contain letters, numbers, and any of these symbols: ._-"),
async c => {
const newUsername = c.req.valid('form').username;
const takenProf = await Server.Profiles.getByUsername(newUsername);
if (takenProf) return recNetResultResponse(c, HTTPStatus.OK, false, "Username taken");
else {
await c.get('profile').setUsername(newUsername);
return recNetResultResponse(c, HTTPStatus.OK, true, "Username updated successfully");
}
}
);
// birthday: 1970-09-10T00:00:00.0000000Z (based on age entered in OOBE)
accountRoute.app.put('/me/birthday', authenticate, c => {
return recNetResultResponse(c, HTTPStatus.OK, true, "Stub. Birthdays are not saved in Galvanic Corrosion.");
});