Skip to content

Commit

Permalink
added profile theme endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Jul 30, 2024
1 parent ca953f2 commit ae03b68
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ model User {
autoload Boolean @default(true)
notifications Notifications[] @relation("Notifications")
has_unreaded_notifications Boolean @default(false)
profile_theme Int @default(0)
}

model Sessions {
Expand Down
20 changes: 20 additions & 0 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,26 @@ export class AppController {
res.send(data);
}

@Post("/user/me/profile_theme")
@UseGuards(AuthGuard)
async profile_theme(@Req() request: RequestSession, @Res() res: Response, @Body() body: { theme: string }): Promise<void> {
/* update profile theme */

const theme = Number(body.theme);
if (isNaN(theme) || theme < 0 || theme > 2) {
res.status(400).send({
status: 'error',
message: 'invalid profile theme'
})
}

await this.userService.setProfileTheme(request.session, theme);
res.status(201).send({
status: 'success',
new_theme: theme
})
}

@Put("/user/me/connections/minecraft/set_valid")
@UseGuards(AuthGuard)
async set_valid(@Req() request: RequestSession, @Res() res: Response, @Query() query: SearchQuery): Promise<void> {
Expand Down
18 changes: 12 additions & 6 deletions src/user.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import axios from 'axios';
import { generate_response } from './app.service';

const discord_url = "https://discord.com/api/v10";
const token_ttl = Number(process.env.SESSION_TTL);
const pwgood = "447699225078136832"; // pwgood server id

interface DiscordResponse {
Expand Down Expand Up @@ -136,7 +135,8 @@ export class UserService {
await this.prisma.sessions.deleteMany({ where: { userId: user_db.id } });
return { message: "Unable to login", statusCode: 403 };
}
const sessionId = sign({ userId: user_db.id }, 'ppl_super_secret', { expiresIn: token_ttl });

const sessionId = sign({ userId: user_db.id }, 'ppl_super_secret', { expiresIn: Number(process.env.SESSION_TTL) });
const token_record = await this.prisma.sessions.create({
data: {
'sessionId': sessionId,
Expand Down Expand Up @@ -167,9 +167,9 @@ export class UserService {
const decoded = verify(session, 'ppl_super_secret') as SessionToken;
const seconds = Math.round(Date.now() / 1000);
if (decoded.iat + ((decoded.exp - decoded.iat) / 2) < seconds) {
const sessionId = sign({ userId: sessionDB.userId }, 'ppl_super_secret', { expiresIn: token_ttl });
const sessionId = sign({ userId: sessionDB.userId }, 'ppl_super_secret', { expiresIn: Number(process.env.SESSION_TTL) });
const new_tokens = await this.prisma.sessions.update({ where: { id: sessionDB.id }, data: { sessionId: sessionId }, include: { User: { include: { profile: true, notifications: true } } } });
const cookie = generateCookie(sessionId, seconds + token_ttl);
const cookie = generateCookie(sessionId, seconds + Number(process.env.SESSION_TTL));

return { sessionId: sessionId, cookie: cookie, user: new_tokens.User };
} else {
Expand Down Expand Up @@ -229,7 +229,8 @@ export class UserService {
avatar: response_data.avatar ? `https://cdn.discordapp.com/avatars/${response_data.id}/${response_data.avatar}` : `/static/favicon.ico`,
banner_color: response_data.banner_color,
has_unreaded_notifications: sessionDB.User.has_unreaded_notifications,
permissions: permissions
permissions: permissions,
profile_theme: sessionDB.User.profile_theme
};
}

Expand Down Expand Up @@ -348,8 +349,13 @@ export class UserService {
avatar: current_discord.avatar ? `https://cdn.discordapp.com/avatars/${current_discord.id}/${current_discord.avatar}` : `/static/favicon.ico`,
banner_color: current_discord.banner_color,
works: generate_response(bandages, session),
is_self: user.id == session?.user?.id
is_self: user.id == session?.user?.id,
profile_theme: user.profile_theme
}
}

async setProfileTheme(session: Session, theme: number) {
await this.prisma.user.update({ where: { id: session.user.id }, data: { profile_theme: theme } });
}
}

0 comments on commit ae03b68

Please sign in to comment.