iceshrimp/packages/backend/src/migration/1644010796173-convert-hard-mutes.ts
Laura Hausmann 7cf00d1503
[backend] Move to our own fork of the re2 package
This fixes builds on alpine-arm64 (and presumably also other musl-arm64 distros)
2023-11-05 16:54:00 +01:00

72 lines
1.9 KiB
TypeScript

import { MigrationInterface, QueryRunner } from "typeorm";
import RE2 from "@iceshrimp/re2";
export class convertHardMutes1644010796173 implements MigrationInterface {
name = "convertHardMutes1644010796173";
async up(queryRunner: QueryRunner): Promise<void> {
let entries = await queryRunner.query(
`SELECT "userId", "mutedWords" FROM "user_profile" WHERE "userHost" IS NULL`,
);
for (let i = 0; i < entries.length; i++) {
let words = entries[i].mutedWords
.map((line) => {
if (typeof line === "string") return [];
const regexp = line.join(" ").match(/^\/(.+)\/(.*)$/);
if (regexp) {
// convert regexp's
try {
new RE2(regexp[1], regexp[2]);
return `/${regexp[1]}/${regexp[2]}`;
} catch (err) {
// invalid regex, ignore it
return [];
}
} else {
// remove empty segments
return line.filter((x) => x !== "");
}
})
// remove empty lines
.filter((x) => !(Array.isArray(x) && x.length === 0));
await queryRunner.connection
.createQueryBuilder()
.update("user_profile")
.set({
mutedWords: words,
})
.where("userId = :id", { id: entries[i].userId })
.execute();
}
}
async down(queryRunner: QueryRunner): Promise<void> {
let entries = await queryRunner.query(
`SELECT "userId", "mutedWords" FROM "user_profile"`,
);
for (let i = 0; i < entries.length; i++) {
let words = entries[i].mutedWords
.map((line) => {
if (Array.isArray(line)) {
return line;
} else {
// do not split regex at spaces again
return [line];
}
})
// remove empty lines
.filter((x) => !(Array.isArray(x) && x.length === 0));
await queryRunner.connection
.createQueryBuilder()
.update("user_profile")
.set({
mutedWords: words,
})
.where("userId = :id", { id: entries[i].userId })
.execute();
}
}
}