Skip to content

Commit

Permalink
added svg head, connections -> settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Aug 7, 2024
1 parent 96b5f53 commit 0736a4a
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/dist
/node_modules
/build
/migrate

# Logs
logs
Expand Down
16 changes: 12 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ model User {
discordId String @unique @default("")
sessions Sessions[]
profile Minecraft?
UserSettings UserSettings?
Bandage Bandage[] @relation("UserBandages")
stars Bandage[] @relation("UserStars")
admin Boolean @default(false)
banned Boolean @default(false)
joined_at DateTime @default(now())
autoload Boolean @default(true)
notifications Notifications[] @relation("Notifications")
has_unreaded_notifications Boolean @default(false)
profile_theme Int @default(0)
}

model UserSettings {
id Int @id @default(autoincrement())
User User @relation(fields: [userId], references: [id])
userId Int @unique
admin Boolean @default(false)
banned Boolean @default(false)
profile_theme Int @default(0)
autoload Boolean @default(true)
public_profile Boolean @default(true)
}

model Sessions {
Expand Down
10 changes: 4 additions & 6 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ interface Bandage {
username: string;
name: string;
discordId: string;
admin: boolean;
banned: boolean;
joined_at: Date;
UserSettings: UserSettings | null
} | null;
stars: {
id: number;
username: string;
name: string;
discordId: string;
admin: boolean;
banned: boolean;
joined_at: Date;
}[];
categories: {
Expand All @@ -36,7 +33,7 @@ export const generate_response = (data: Bandage[], session: Session | null) => {
/* generate list of works response by provided array of bandages */

const result = data.map((el) => {
if (el.User?.banned) return undefined;
if (el.User?.UserSettings?.banned) return undefined;
const categories = el.categories.map((cat) => {
return {
id: cat.id,
Expand All @@ -57,7 +54,8 @@ export const generate_response = (data: Bandage[], session: Session | null) => {
author: {
id: el.User?.id,
name: el.User?.name,
username: el.User?.username
username: el.User?.username,
public: el.User?.UserSettings?.public_profile
},
categories: categories.filter((el) => el !== undefined)
}
Expand Down
14 changes: 11 additions & 3 deletions src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ interface User {
username: string;
name: string;
discordId: string;
admin: boolean;
banned: boolean;
joined_at: Date;
profile: {
id: number;
Expand All @@ -114,8 +112,18 @@ interface User {
valid: boolean;
userId: number | null;
} | null;
autoload: boolean;
notifications: Notifications[];
UserSettings: UserSettings | null
}

interface UserSettings {
id: number,
userId: number,
admin: boolean,
banned: boolean,
profile_theme: number,
autoload: boolean,
public_profile: boolean
}

interface Session {
Expand Down
15 changes: 15 additions & 0 deletions src/minecraft/minecraft.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Controller, Get, HttpStatus, Param, Query, Res, StreamableFile, HttpException } from '@nestjs/common';
import type { Response } from 'express'
import { MinecraftService } from 'src/minecraft/minecraft.service';
import { generateSvg } from './svg.module';
import * as sharp from 'sharp';

@Controller('api')
export class minecraftController {
Expand Down Expand Up @@ -74,4 +76,17 @@ export class minecraftController {
}
return cache;
}

@Get('/beta/head/:name')
async beta_head(@Param('name') name: string, @Res({ passthrough: true }) res: Response, @Query() query: { pixel_width: number }) {
const pixel_width = Number(query.pixel_width) || 50;

const cache = await this.minecraftService.updateSkinCache(name);
if (!cache) {
throw new HttpException({ message: 'Profile not found' }, HttpStatus.NOT_FOUND);
}
const result = await generateSvg(sharp(Buffer.from(cache.data, "base64")), pixel_width);
res.set({ 'Content-Type': 'image/svg+xml' });
return result;
}
}
17 changes: 1 addition & 16 deletions src/minecraft/minecraft.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as sharp from 'sharp';
import { PrismaService } from "../prisma/prisma.service";
import { Injectable } from '@nestjs/common';
import { Buffer } from "buffer";
import { Prisma } from "@prisma/client";

@Injectable()
export class MinecraftService {
Expand Down Expand Up @@ -174,7 +173,7 @@ export class MinecraftService {
if (fragment.length < 3) {
return null;
}
const filter_rule = { OR: [{ nickname: { contains: fragment } }, { uuid: { contains: fragment } }], valid: true }
const filter_rule = { OR: [{ nickname: { contains: fragment } }], valid: true }
const cache = await this.prisma.minecraft.findMany({
where: filter_rule,
orderBy: { default_nick: "asc" },
Expand Down Expand Up @@ -217,20 +216,6 @@ export class MinecraftService {
return { statusCode: 200, new_data: result.valid };
}


async changeAutoload(session: Session, state: boolean) {
/* switch skin autoload in editor */

const result = await this.prisma.user.update({
where: {
id: session.user.id
}, data: {
autoload: state
}
})
return { statusCode: 200, new_data: result.autoload };
}

async connect(session: Session, code: string) {
/* connect minecraft account to a user profile */

Expand Down
29 changes: 29 additions & 0 deletions src/minecraft/svg.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as sharp from 'sharp';

export const generateSvg = async (image: sharp.Sharp, pixel_width: number) => {
const { data, info } = await image.raw()
.ensureAlpha()
.toBuffer({ resolveWithObject: true });

const pixels = [];
for (let x = 8; x < 16; x++) {
for (let y = 8; y < 16; y++) {
const pixelIndex = (y * info.width + x) * info.channels;
pixels.push(`<rect x="${(x - 8) * (pixel_width * 0.865) + (pixel_width / 2)}" y="${(y - 8) * (pixel_width * 0.865) + (pixel_width / 2)}" width="${pixel_width}" height="${pixel_width}" fill="rgba(${data[pixelIndex]}, ${data[pixelIndex + 1]}, ${data[pixelIndex + 2]}, ${data[pixelIndex + 3]})" />`);
}
}

for (let x = 40; x < 48; x++) {
for (let y = 8; y < 16; y++) {
const pixelIndex = (y * info.width + x) * info.channels;
pixels.push(`<rect x="${(x - 40) * pixel_width}" y="${(y - 8) * pixel_width}" width="${pixel_width}" height="${pixel_width}" fill="rgba(${data[pixelIndex]}, ${data[pixelIndex + 1]}, ${data[pixelIndex + 2]}, ${data[pixelIndex + 3]})" />`);
}
}

const result =
`<svg width="${pixel_width * 8}" height="${pixel_width * 8}" xmlns="http://www.w3.org/2000/svg">\n` +
`${pixels.join('\n')}\n` +
`</svg>`;

return result;
}
4 changes: 2 additions & 2 deletions src/root/root.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class RootController {
{ loc: 'https://pplbandage.ru/me', priority: 0.5 },
{ loc: 'https://pplbandage.ru/me/stars', priority: 0.5 },
{ loc: 'https://pplbandage.ru/me/notifications', priority: 0.5 },
{ loc: 'https://pplbandage.ru/me/connections', priority: 0.5 },
{ loc: 'https://pplbandage.ru/me/settings', priority: 0.5 },
{ loc: 'https://pplbandage.ru/tos', priority: 0.5 },
{ loc: 'https://pplbandage.ru/contacts', priority: 0.5 }
]
Expand All @@ -60,7 +60,7 @@ export class RootController {
priority: 0.6
})));

const users = await this.prisma.user.findMany({ where: { Bandage: { some: {} } } });
const users = await this.prisma.user.findMany({ where: { Bandage: { some: {} }, UserSettings: { banned: false } } });
urls = urls.concat(users.map((user) => ({
loc: `https://pplbandage.ru/users/${user.username}`,
priority: 0.5
Expand Down
23 changes: 20 additions & 3 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ export class UserController {
res.status(data.statusCode).send(data.data);
}

@Get("/user/me/connections")
@Get("/user/me/settings")
@UseGuards(AuthGuard)
async minecraft(@Req() request: RequestSession, @Res() res: Response): Promise<void> {
/* get user's connections */

const data = await this.userService.getConnections(request.session);
const data = await this.userService.getUserSettings(request.session);
res.status(data.statusCode).send(data);
}

Expand Down Expand Up @@ -127,7 +127,7 @@ export class UserController {
});
return;
}
const data = await this.minecraftService.changeAutoload(request.session, query.state === "true");
const data = await this.userService.changeAutoload(request.session, query.state === "true");
res.status(data.statusCode).send(data);
}

Expand Down Expand Up @@ -236,4 +236,21 @@ export class UserController {
const data = await this.bandageService.setStar(request.session, query.set === "true", id);
res.status(data.statusCode).send(data);
}

@Put("/user/me/settings/set_public")
@UseGuards(AuthGuard)
async set_public(@Req() request: RequestSession, @Res() res: Response, @Query() query: SearchQuery): Promise<void> {
/* set skin autoload in editor */

if (!query.state || !["true", "false"].includes(query.state)) {
res.status(HttpStatus.BAD_REQUEST).send({
status: "error",
message: "`State` query param invalid",
statusCode: 400
});
return;
}
const data = await this.userService.setPublic(request.session, query.state === "true");
res.status(data.statusCode).send(data);
}
}
Loading

0 comments on commit 0736a4a

Please sign in to comment.