Skip to content

Commit 01ca7b7

Browse files
CyberL1MaddyUnderStars
authored andcommitted
feat: badges
1 parent 2f679fd commit 01ca7b7

File tree

9 files changed

+107
-0
lines changed

9 files changed

+107
-0
lines changed

src/api/routes/users/#id/profile.ts

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import { route } from "@spacebar/api";
2020
import {
21+
Badge,
2122
Member,
2223
PrivateUserProjection,
2324
User,
@@ -98,6 +99,9 @@ router.get(
9899
bio: guild_member?.bio || "",
99100
guild_id,
100101
};
102+
103+
const badges = await Badge.find();
104+
101105
res.json({
102106
connected_accounts: user.connected_accounts.filter(
103107
(x) => x.visibility != 0,
@@ -111,6 +115,7 @@ router.get(
111115
user_profile: userProfile,
112116
guild_member: guild_member?.toPublicMember(),
113117
guild_member_profile: guild_id && guildMemberProfile,
118+
badges: badges.filter((x) => user.badge_ids?.includes(x.id)),
114119
});
115120
},
116121
);

src/util/dtos/UserDTO.ts

+2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ export class MinimalPublicUserDTO {
2424
id: string;
2525
public_flags: number;
2626
username: string;
27+
badge_ids?: string[] | null;
2728

2829
constructor(user: User) {
2930
this.avatar = user.avatar;
3031
this.discriminator = user.discriminator;
3132
this.id = user.id;
3233
this.public_flags = user.public_flags;
3334
this.username = user.username;
35+
this.badge_ids = user.badge_ids;
3436
}
3537
}

src/util/entities/Badge.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
3+
Copyright (C) 2023 Spacebar and Spacebar Contributors
4+
5+
This program is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU Affero General Public License as published
7+
by the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Affero General Public License for more details.
14+
15+
You should have received a copy of the GNU Affero General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
import { Column, Entity } from "typeorm";
20+
import { BaseClassWithoutId } from "./BaseClass";
21+
22+
@Entity("badges")
23+
export class Badge extends BaseClassWithoutId {
24+
@Column({ primary: true })
25+
id: string;
26+
27+
@Column()
28+
description: string;
29+
30+
@Column()
31+
icon: string;
32+
33+
@Column({ nullable: true })
34+
link?: string;
35+
}

src/util/entities/User.ts

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export enum PublicUserEnum {
4949
premium_type,
5050
theme_colors,
5151
pronouns,
52+
badge_ids,
5253
}
5354
export type PublicUserKeys = keyof typeof PublicUserEnum;
5455

@@ -231,6 +232,9 @@ export class User extends BaseClass {
231232
@OneToMany(() => SecurityKey, (key: SecurityKey) => key.user)
232233
security_keys: SecurityKey[];
233234

235+
@Column({ type: "simple-array", nullable: true })
236+
badge_ids?: string[];
237+
234238
// TODO: I don't like this method?
235239
validate() {
236240
if (this.discriminator) {

src/util/entities/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export * from "./Application";
2020
export * from "./Attachment";
2121
export * from "./AuditLog";
2222
export * from "./BackupCodes";
23+
export * from "./Badge";
2324
export * from "./Ban";
2425
export * from "./BaseClass";
2526
export * from "./Categories";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class Badges1720628601997 implements MigrationInterface {
4+
name = "Badges1720628601997";
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(
8+
`CREATE TABLE \`badges\` (\`id\` varchar(255) NOT NULL, \`description\` varchar(255) NOT NULL, \`icon\` varchar(255) NOT NULL, \`link\` varchar(255) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
9+
);
10+
await queryRunner.query(
11+
`ALTER TABLE \`users\` ADD \`badge_ids\` text NULL`,
12+
);
13+
}
14+
15+
public async down(queryRunner: QueryRunner): Promise<void> {
16+
await queryRunner.query(
17+
`ALTER TABLE \`users\` DROP COLUMN \`badge_ids\``,
18+
);
19+
await queryRunner.query(`DROP TABLE \`badges\``);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class Badges1720628601997 implements MigrationInterface {
4+
name = "Badges1720628601997";
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(
8+
`CREATE TABLE \`badges\` (\`id\` varchar(255) NOT NULL, \`description\` varchar(255) NOT NULL, \`icon\` varchar(255) NOT NULL, \`link\` varchar(255) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
9+
);
10+
await queryRunner.query(
11+
`ALTER TABLE \`users\` ADD \`badge_ids\` text NULL`,
12+
);
13+
}
14+
15+
public async down(queryRunner: QueryRunner): Promise<void> {
16+
await queryRunner.query(
17+
`ALTER TABLE \`users\` DROP COLUMN \`badge_ids\``,
18+
);
19+
await queryRunner.query(`DROP TABLE \`badges\``);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class Badges1720628601997 implements MigrationInterface {
4+
name = "Badges1720628601997";
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(
8+
`CREATE TABLE "badges" ("id" character varying NOT NULL, "description" character varying NOT NULL, "icon" character varying NOT NULL, "link" character varying, CONSTRAINT "PK_8a651318b8de577e8e217676466" PRIMARY KEY ("id"))`,
9+
);
10+
await queryRunner.query(`ALTER TABLE "users" ADD "badge_ids" text`);
11+
}
12+
13+
public async down(queryRunner: QueryRunner): Promise<void> {
14+
await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "badge_ids"`);
15+
}
16+
}

src/util/schemas/responses/UserProfileResponse.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
import {
20+
Badge,
2021
Member,
2122
PublicConnectedAccount,
2223
PublicMember,
@@ -52,4 +53,5 @@ export interface UserProfileResponse {
5253
user_profile: UserProfile;
5354
guild_member?: PublicMember;
5455
guild_member_profile?: PublicMemberProfile;
56+
badges: Badge[];
5557
}

0 commit comments

Comments
 (0)