rudeshark.net/packages/backend/migration/1644010796173-convert-hard-mutes.js

71 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2023-04-07 03:56:46 +02:00
import RE2 from "re2";
2022-02-28 16:07:03 +01:00
export class convertHardMutes1644010796173 {
2023-04-07 03:56:46 +02:00
name = "convertHardMutes1644010796173";
2023-04-07 03:56:46 +02:00
async up(queryRunner) {
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));
2023-04-07 03:56:46 +02:00
await queryRunner.connection
.createQueryBuilder()
.update("user_profile")
.set({
mutedWords: words,
})
.where("userId = :id", { id: entries[i].userId })
.execute();
}
}
2023-04-07 03:56:46 +02:00
async down(queryRunner) {
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));
2023-04-07 03:56:46 +02:00
await queryRunner.connection
.createQueryBuilder()
.update("user_profile")
.set({
mutedWords: words,
})
.where("userId = :id", { id: entries[i].userId })
.execute();
}
}
}