Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: badges #1144

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/api/routes/users/#id/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import { route } from "@spacebar/api";
import {
Badge,
Member,
PrivateUserProjection,
User,
Expand Down Expand Up @@ -98,6 +99,9 @@ router.get(
bio: guild_member?.bio || "",
guild_id,
};

const badges = await Badge.find();

res.json({
connected_accounts: user.connected_accounts.filter(
(x) => x.visibility != 0,
Expand All @@ -111,6 +115,7 @@ router.get(
user_profile: userProfile,
guild_member: guild_member?.toPublicMember(),
guild_member_profile: guild_id && guildMemberProfile,
badges: badges.filter((x) => user.badge_ids?.includes(x.id)),
});
},
);
Expand Down
40 changes: 40 additions & 0 deletions src/cdn/routes/badge-icons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { Router, Response, Request } from "express";
import { storage } from "../util/Storage";
import FileType from "file-type";
import { HTTPError } from "lambert-server";

const router = Router();

router.get("/:badge_id", async (req: Request, res: Response) => {
const { badge_id } = req.params;
const path = `badge-icons/${badge_id}`;

const file = await storage.get(path);
if (!file) throw new HTTPError("not found", 404);
const type = await FileType.fromBuffer(file);

res.set("Content-Type", type?.mime);
res.set("Cache-Control", "public, max-age=31536000, must-revalidate");

return res.send(file);
});

export default router;
2 changes: 2 additions & 0 deletions src/util/dtos/UserDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ export class MinimalPublicUserDTO {
id: string;
public_flags: number;
username: string;
badge_ids?: string[] | null;

constructor(user: User) {
this.avatar = user.avatar;
this.discriminator = user.discriminator;
this.id = user.id;
this.public_flags = user.public_flags;
this.username = user.username;
this.badge_ids = user.badge_ids;
}
}
35 changes: 35 additions & 0 deletions src/util/entities/Badge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { Column, Entity } from "typeorm";
import { BaseClassWithoutId } from "./BaseClass";

@Entity("badges")
export class Badge extends BaseClassWithoutId {
@Column({ primary: true })
id: string;

@Column()
description: string;

@Column()
icon: string;

@Column({ nullable: true })
link?: string;
}
4 changes: 4 additions & 0 deletions src/util/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export enum PublicUserEnum {
premium_type,
theme_colors,
pronouns,
badge_ids,
}
export type PublicUserKeys = keyof typeof PublicUserEnum;

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

@Column({ type: "simple-array", nullable: true })
badge_ids?: string[];

// TODO: I don't like this method?
validate() {
if (this.discriminator) {
Expand Down
1 change: 1 addition & 0 deletions src/util/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from "./Application";
export * from "./Attachment";
export * from "./AuditLog";
export * from "./BackupCodes";
export * from "./Badge";
export * from "./Ban";
export * from "./BaseClass";
export * from "./Categories";
Expand Down
21 changes: 21 additions & 0 deletions src/util/migration/mariadb/1720628601997-badges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class Badges1720628601997 implements MigrationInterface {
name = "Badges1720628601997";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`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`,
);
await queryRunner.query(
`ALTER TABLE \`users\` ADD \`badge_ids\` text NULL`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`users\` DROP COLUMN \`badge_ids\``,
);
await queryRunner.query(`DROP TABLE \`badges\``);
}
}
21 changes: 21 additions & 0 deletions src/util/migration/mysql/1720628601997-badges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class Badges1720628601997 implements MigrationInterface {
name = "Badges1720628601997";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`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`,
);
await queryRunner.query(
`ALTER TABLE \`users\` ADD \`badge_ids\` text NULL`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`users\` DROP COLUMN \`badge_ids\``,
);
await queryRunner.query(`DROP TABLE \`badges\``);
}
}
16 changes: 16 additions & 0 deletions src/util/migration/postgres/1720628601997-badges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class Badges1720628601997 implements MigrationInterface {
name = "Badges1720628601997";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`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"))`,
);
await queryRunner.query(`ALTER TABLE "users" ADD "badge_ids" text`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "badge_ids"`);
}
}
2 changes: 2 additions & 0 deletions src/util/schemas/responses/UserProfileResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import {
Badge,
Member,
PublicConnectedAccount,
PublicMember,
Expand Down Expand Up @@ -52,4 +53,5 @@ export interface UserProfileResponse {
user_profile: UserProfile;
guild_member?: PublicMember;
guild_member_profile?: PublicMemberProfile;
badges: Badge[];
}
Loading