Skip to content

Commit 52895a2

Browse files
committed
feat: add registered bot weights
1 parent e58658b commit 52895a2

5 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/controllers/cron/commentreply.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ async function processCommentUpdates(comment: CommentReply) {
105105
// FIXME: due to an issue with snoowrap typings, the 'await' keyword causes compile errors. see https://github.com/DefinitelyTyped/DefinitelyTyped/issues/33139
106106
post = await redditapi.getSubmission(comment.redditPostId_Parent).fetch();
107107

108-
const mirrors = await AvailableMirror.find({
108+
let mirrors = await AvailableMirror.find({
109109
where: {
110110
redditPostId: comment.redditPostId_Parent,
111111
},
@@ -116,6 +116,14 @@ async function processCommentUpdates(comment: CommentReply) {
116116

117117
if (mirrors.length <= 0) return deleteComment(comment);
118118

119+
// This is a dirty workaround for sorting mirrors by the
120+
// weight of their registered bot. This really should be done
121+
// with an SQL query, but I opted to do this instead because
122+
// there is already a bunch of Active Record stuff in this
123+
// model and I don't want to break pattern. That's probably a
124+
// bad reason. In fact, I know it is.
125+
mirrors = mirrors.sort((a, b) => a.bot.weight - b.bot.weight);
126+
119127
let commentBody = generateFormattedMirrors(mirrors);
120128

121129
let reply: RedditComment;

src/db/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let db = getConnectionManager().create({
66
database: path.resolve(
77
process.env.DATABASE_LOCATION || "data/database.sqlite3"
88
),
9-
synchronize: true,
9+
synchronize: false,
1010
logging: true,
1111
entities: [__dirname + "/../entity/**{.ts,.js}"],
1212
migrations: [__dirname + "/../migration/**{.ts,.js}"],

src/entity/registeredbot.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export class RegisteredBot extends BaseEntity {
3232
})
3333
token: string;
3434

35+
@Column({
36+
default: 0,
37+
})
38+
weight: number;
39+
3540
@OneToMany((type) => AvailableMirror, (mirroredvideo) => mirroredvideo.bot)
3641
mirroredVideos: AvailableMirror[];
3742

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class AddWeightToBots1609376826000 implements MigrationInterface {
4+
async up(queryRunner: QueryRunner): Promise<any> {
5+
await queryRunner.query(`UPDATE registered_bot SET weight = 0`);
6+
7+
await queryRunner.query(
8+
`UPDATE registered_bot SET weight = -1 WHERE username = 'tuckbot'`
9+
);
10+
}
11+
12+
async down(queryRunner: QueryRunner): Promise<any> {
13+
await queryRunner.query(`UPDATE registered_bot SET weight = 0`);
14+
}
15+
}

src/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export class WebServer {
5151
async start() {
5252
try {
5353
await db.connect();
54+
await db.query("PRAGMA foreign_keys=OFF");
55+
await db.synchronize();
56+
await db.query("PRAGMA foreign_keys=ON");
5457

5558
logger.info(`database sucecssfully started`);
5659
} catch (err) {

0 commit comments

Comments
 (0)