Merge pull request #434 from TheArcaneBrony/remove-vanity-url

Fix duplicate key - remove vanity url column
This commit is contained in:
Flam3rboy 2021-10-10 19:02:22 +02:00 committed by GitHub
commit d04690ff08
8 changed files with 54 additions and 37 deletions

View File

@ -10,10 +10,10 @@ const InviteRegex = /\W/g;
router.get("/", route({ permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => { router.get("/", route({ permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => {
const { guild_id } = req.params; const { guild_id } = req.params;
const guild = await Guild.findOneOrFail({ where: { id: guild_id }, relations: ["vanity_url"] }); const invite = await Invite.findOne({ where: { guild_id: guild_id, vanity_url: true } });
if (!guild.vanity_url) return res.json({ code: null }); if (!invite) return res.json({ code: null });
return res.json({ code: guild.vanity_url_code, uses: guild.vanity_url.uses }); return res.json({ code: invite.code, uses: invite.uses });
}); });
export interface VanityUrlSchema { export interface VanityUrlSchema {
@ -33,20 +33,9 @@ router.patch("/", route({ body: "VanityUrlSchema", permission: "MANAGE_GUILD" })
const invite = await Invite.findOne({ code }); const invite = await Invite.findOne({ code });
if (invite) throw new HTTPError("Invite already exists"); if (invite) throw new HTTPError("Invite already exists");
const guild = await Guild.findOneOrFail({ id: guild_id });
const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT }); const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT });
Promise.all([ await Invite.update({ vanity_url: true, guild_id }, { code: code, channel_id: id });
Guild.update({ id: guild_id }, { vanity_url_code: code }),
Invite.delete({ code: guild.vanity_url_code }),
new Invite({
code: code,
uses: 0,
created_at: new Date(),
guild_id,
channel_id: id
}).save()
]);
return res.json({ code: code }); return res.json({ code: code });
}); });

View File

@ -33,7 +33,6 @@ router.delete("/:code", route({}), async (req: Request, res: Response) => {
await Promise.all([ await Promise.all([
Invite.delete({ code }), Invite.delete({ code }),
Guild.update({ vanity_url_code: code }, { vanity_url_code: undefined }),
emitEvent({ emitEvent({
event: "INVITE_DELETE", event: "INVITE_DELETE",
guild_id: guild_id, guild_id: guild_id,

View File

@ -9,7 +9,7 @@
"start": "node scripts/build.js && node dist/bundle/src/start.js", "start": "node scripts/build.js && node dist/bundle/src/start.js",
"start:bundle": "node dist/bundle/src/start.js", "start:bundle": "node dist/bundle/src/start.js",
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"migrate": "node --require ts-node/register node_modules/typeorm/cli.js -f ../util/ormconfig.json migration:run" "migrate": "cd ../util/ && npm i && node --require ts-node/register node_modules/typeorm/cli.js -f ../util/ormconfig.json migration:run"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -1,6 +1,22 @@
const { execSync } = require("child_process"); const { execSync } = require("child_process");
const path = require("path"); const path = require("path");
const fse = require("fs-extra"); const fse = require("fs-extra");
const { getSystemErrorMap } = require("util");
const { argv } = require("process");
const dirs = ["api", "util", "cdn", "gateway", "bundle"];
const verbose = argv.includes("verbose") || argv.includes("v");
if(argv.includes("clean")){
dirs.forEach(a=>{
var d = "../"+a+"/dist";
if(fse.existsSync(d)) {
fse.rmSync(d,{recursive: true});
if(verbose) console.log(`Deleted ${d}!`);
}
});
}
fse.copySync(path.join(__dirname, "..", "..", "api", "assets"), path.join(__dirname, "..", "dist", "api", "assets")); fse.copySync(path.join(__dirname, "..", "..", "api", "assets"), path.join(__dirname, "..", "dist", "api", "assets"));
fse.copySync( fse.copySync(
@ -8,13 +24,12 @@ fse.copySync(
path.join(__dirname, "..", "dist", "api", "client_test") path.join(__dirname, "..", "dist", "api", "client_test")
); );
fse.copySync(path.join(__dirname, "..", "..", "api", "locales"), path.join(__dirname, "..", "dist", "api", "locales")); fse.copySync(path.join(__dirname, "..", "..", "api", "locales"), path.join(__dirname, "..", "dist", "api", "locales"));
fse.copySync(path.join(__dirname, "..", "..", "api", "src"), path.join(__dirname, "..", "dist", "api", "src")); dirs.forEach(a=>{
fse.copySync(path.join(__dirname, "..", "..", "util", "src"), path.join(__dirname, "..", "dist", "util", "src")); fse.copySync("../"+a+"/src", "dist/"+a+"/src");
fse.copySync(path.join(__dirname, "..", "..", "cdn", "src"), path.join(__dirname, "..", "dist", "cdn", "src")); if(verbose) console.log(`Copied ${"../"+a+"/dist"} -> ${"dist/"+a+"/src"}!`);
fse.copySync(path.join(__dirname, "..", "..", "gateway", "src"), path.join(__dirname, "..", "dist", "gateway", "src")); });
fse.copySync(path.join(__dirname, "..", "..", "bundle", "src"), path.join(__dirname, "..", "dist", "bundle", "src"));
console.log("Copying src files done"); console.log("Copying src files done");
console.log("Compiling src files ..."); console.log("Compiling src files ...");
console.log( console.log(

View File

@ -257,14 +257,6 @@ export class Guild extends BaseClass {
@Column({ nullable: true }) @Column({ nullable: true })
unavailable?: boolean; unavailable?: boolean;
@Column({ nullable: true })
@RelationId((guild: Guild) => guild.vanity_url)
vanity_url_code?: string;
@JoinColumn({ name: "vanity_url_code" })
@ManyToOne(() => Invite)
vanity_url?: Invite;
@Column({ nullable: true }) @Column({ nullable: true })
verification_level?: number; verification_level?: number;

View File

@ -1,6 +1,6 @@
import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm"; import { Column, Entity, JoinColumn, ManyToOne, RelationId, PrimaryColumn } from "typeorm";
import { Member } from "./Member"; import { Member } from "./Member";
import { BaseClass, PrimaryIdColumn } from "./BaseClass"; import { BaseClassWithoutId } from "./BaseClass";
import { Channel } from "./Channel"; import { Channel } from "./Channel";
import { Guild } from "./Guild"; import { Guild } from "./Guild";
import { User } from "./User"; import { User } from "./User";
@ -8,8 +8,8 @@ import { User } from "./User";
export const PublicInviteRelation = ["inviter", "guild", "channel"]; export const PublicInviteRelation = ["inviter", "guild", "channel"];
@Entity("invites") @Entity("invites")
export class Invite extends BaseClass { export class Invite extends BaseClassWithoutId {
@PrimaryIdColumn() @PrimaryColumn()
code: string; code: string;
@Column() @Column()
@ -71,6 +71,9 @@ export class Invite extends BaseClass {
@Column({ nullable: true }) @Column({ nullable: true })
target_user_type?: number; target_user_type?: number;
@Column({ nullable: true})
vanity_url?: boolean;
static async joinGuild(user_id: string, code: string) { static async joinGuild(user_id: string, code: string) {
const invite = await Invite.findOneOrFail({ code }); const invite = await Invite.findOneOrFail({ code });
if (invite.uses++ >= invite.max_uses && invite.max_uses !== 0) await Invite.delete({ code }); if (invite.uses++ >= invite.max_uses && invite.max_uses !== 0) await Invite.delete({ code });

View File

@ -0,0 +1,17 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class VanityInvite1633881705509 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
try {
await queryRunner.query(`ALTER TABLE "emojis" DROP COLUMN vanity_url_code`);
await queryRunner.query(`ALTER TABLE "emojis" DROP CONSTRAINT FK_c2c1809d79eb120ea0cb8d342ad`);
} catch (error) {}
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "emojis" ADD vanity_url_code varchar`);
await queryRunner.query(
`ALTER TABLE "emojis" ADD CONSTRAINT FK_c2c1809d79eb120ea0cb8d342ad FOREIGN KEY ("vanity_url_code") REFERENCES "invites"("code") ON DELETE NO ACTION ON UPDATE NO ACTION`
);
}
}

View File

@ -30,7 +30,7 @@ const {
async function main() { async function main() {
if (!process.env.TO) throw new Error("TO database env connection string not set"); if (!process.env.TO) throw new Error("TO database env connection string not set");
// manually arrange them because of foreign key // manually arrange them because of foreign keys
const entities = [ const entities = [
User, User,
Guild, Guild,
@ -55,7 +55,7 @@ async function main() {
Attachment, Attachment,
]; ];
const newDB = await initDatabase(); const oldDB = await initDatabase();
const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite"; const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite";
const isSqlite = type.includes("sqlite"); const isSqlite = type.includes("sqlite");
@ -73,6 +73,7 @@ async function main() {
try { try {
for (const entity of entities) { for (const entity of entities) {
const entries = await oldDB.manager.find(entity); const entries = await oldDB.manager.find(entity);
// @ts-ignore // @ts-ignore
console.log("migrating " + entries.length + " " + entity.name + " ..."); console.log("migrating " + entries.length + " " + entity.name + " ...");
@ -90,6 +91,7 @@ async function main() {
} }
} }
} }
// @ts-ignore // @ts-ignore
console.log("migrated " + entries.length + " " + entity.name); console.log("migrated " + entries.length + " " + entity.name);
} }