Skip to content

Commit

Permalink
fix(eventlist): cleanup of events just hides from widget (#4164)
Browse files Browse the repository at this point in the history
* fix(eventlist): cleanup of events just hides from widget

This will ensure we still keep all data for users

Fixes #3653

* fix
  • Loading branch information
sogehige committed Sep 29, 2020
1 parent 6f8f8a3 commit 956d4fe
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/bot/database/entity/eventList.ts
Expand Up @@ -7,6 +7,7 @@ export interface EventListInterface {
userId: string;
timestamp: number;
isTest: boolean;
isHidden?: boolean;
values_json: string;
}

Expand All @@ -18,6 +19,7 @@ export const EventList = new EntitySchema<Readonly<Required<EventListInterface>>
userId: { type: String },
timestamp: { type: 'bigint', transformer: new ColumnNumericTransformer() },
isTest: { type: 'boolean' },
isHidden: { type: 'boolean', default: false },
values_json: { type: 'text' },
},
indices: [
Expand Down
@@ -0,0 +1,14 @@
import {MigrationInterface, QueryRunner} from 'typeorm';

export class eventListIsHiddenAttr1601297968526 implements MigrationInterface {
name = 'eventListIsHiddenAttr1601297968526';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`event_list\` ADD \`isHidden\` tinyint NOT NULL DEFAULT 0`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`event_list\` DROP COLUMN \`isHidden\``, undefined);
}

}
@@ -0,0 +1,14 @@
import {MigrationInterface, QueryRunner} from 'typeorm';

export class eventListIsHiddenAttr1601297968526 implements MigrationInterface {
name = 'eventListIsHiddenAttr1601297968526';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "event_list" ADD "isHidden" boolean NOT NULL DEFAULT false`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "event_list" DROP COLUMN "isHidden"`, undefined);
}

}
@@ -0,0 +1,24 @@
import {MigrationInterface, QueryRunner} from 'typeorm';

export class eventListIsHiddenAttr1601297968526 implements MigrationInterface {
name = 'eventListIsHiddenAttr1601297968526';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "IDX_8a80a3cf6b2d815920a390968a"`);
await queryRunner.query(`CREATE TABLE "temporary_event_list" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "event" varchar NOT NULL, "userId" varchar NOT NULL, "timestamp" bigint NOT NULL, "values_json" text NOT NULL, "isTest" boolean NOT NULL, "isHidden" boolean NOT NULL DEFAULT (0))`);
await queryRunner.query(`INSERT INTO "temporary_event_list"("id", "event", "userId", "timestamp", "values_json", "isTest") SELECT "id", "event", "userId", "timestamp", "values_json", "isTest" FROM "event_list"`);
await queryRunner.query(`DROP TABLE "event_list"`);
await queryRunner.query(`ALTER TABLE "temporary_event_list" RENAME TO "event_list"`);
await queryRunner.query(`CREATE INDEX "IDX_8a80a3cf6b2d815920a390968a" ON "event_list" ("userId") `);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "IDX_8a80a3cf6b2d815920a390968a"`);
await queryRunner.query(`ALTER TABLE "event_list" RENAME TO "temporary_event_list"`);
await queryRunner.query(`CREATE TABLE "event_list" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "event" varchar NOT NULL, "userId" varchar NOT NULL, "timestamp" bigint NOT NULL, "values_json" text NOT NULL, "isTest" boolean NOT NULL)`);
await queryRunner.query(`INSERT INTO "event_list"("id", "event", "userId", "timestamp", "values_json", "isTest") SELECT "id", "event", "userId", "timestamp", "values_json", "isTest" FROM "temporary_event_list"`);
await queryRunner.query(`DROP TABLE "temporary_event_list"`);
await queryRunner.query(`CREATE INDEX "IDX_8a80a3cf6b2d815920a390968a" ON "event_list" ("userId") `);
}

}
15 changes: 11 additions & 4 deletions src/bot/widgets/eventlist.ts
Expand Up @@ -14,9 +14,13 @@ class EventList extends Widget {
}

public sockets() {
adminEndpoint(this.nsp, 'eventlist::removeById', async (id, cb) => {
const ids = Array.isArray(id) ? [...id] : [id];
await getRepository(EventListDB).delete(ids);
adminEndpoint(this.nsp, 'eventlist::removeById', async (idList, cb) => {
const ids = Array.isArray(idList) ? [...idList] : [idList];
for (const id of ids) {
await getRepository(EventListDB).update(id, {
isHidden: true,
});
}
if (cb) {
cb(null);
}
Expand All @@ -26,7 +30,7 @@ class EventList extends Widget {
});

adminEndpoint(this.nsp, 'cleanup', () => {
getRepository(EventListDB).delete({});
getRepository(EventListDB).update({ isHidden: false }, { isHidden: true });
});

adminEndpoint(this.nsp, 'eventlist::resend', async (id) => {
Expand Down Expand Up @@ -120,6 +124,9 @@ class EventList extends Widget {
public async update(count: number) {
try {
const events = await getRepository(EventListDB).find({
where: {
isHidden: false,
},
order: { timestamp: 'DESC' },
take: count,
});
Expand Down

0 comments on commit 956d4fe

Please sign in to comment.