diff options
author | Volpeon <git@volpeon.ink> | 2021-10-19 19:25:03 +0200 |
---|---|---|
committer | Volpeon <git@volpeon.ink> | 2021-10-19 19:25:03 +0200 |
commit | 163d8119c109c42e64ab37b01dec131f2cb5bf61 (patch) | |
tree | ee0abce3ce349482ca63b4064fcbebc0e5d2f4ff /src/services/dedupe.ts | |
parent | Code improvements (diff) | |
download | feralbot-163d8119c109c42e64ab37b01dec131f2cb5bf61.tar.gz feralbot-163d8119c109c42e64ab37b01dec131f2cb5bf61.tar.bz2 feralbot-163d8119c109c42e64ab37b01dec131f2cb5bf61.zip |
Code improvements, support a manual post queue
Diffstat (limited to 'src/services/dedupe.ts')
-rw-r--r-- | src/services/dedupe.ts | 65 |
1 files changed, 0 insertions, 65 deletions
diff --git a/src/services/dedupe.ts b/src/services/dedupe.ts deleted file mode 100644 index 2eeb5ee..0000000 --- a/src/services/dedupe.ts +++ /dev/null | |||
@@ -1,65 +0,0 @@ | |||
1 | import fs from "fs/promises"; | ||
2 | import path from "path"; | ||
3 | import * as f from "fp-ts"; | ||
4 | import * as t from "io-ts"; | ||
5 | |||
6 | export const E926DedupeEntryC = t.type({ | ||
7 | provider: t.literal("e926"), | ||
8 | id: t.number, | ||
9 | }); | ||
10 | |||
11 | export const DedupeEntryC = E926DedupeEntryC; | ||
12 | |||
13 | export type E926DedupeEntry = t.TypeOf<typeof E926DedupeEntryC>; | ||
14 | |||
15 | export type DedupeEntry = t.TypeOf<typeof DedupeEntryC>; | ||
16 | |||
17 | export class Dedupe { | ||
18 | private entries: DedupeEntry[] = []; | ||
19 | |||
20 | private readonly filePath: string; | ||
21 | |||
22 | private isLoaded = false; | ||
23 | |||
24 | constructor(private max: number, filename: string) { | ||
25 | this.filePath = path.join(process.cwd(), filename); | ||
26 | } | ||
27 | |||
28 | private async load() { | ||
29 | if (this.isLoaded) { | ||
30 | return; | ||
31 | } | ||
32 | |||
33 | try { | ||
34 | await fs.stat(this.filePath); | ||
35 | } catch { | ||
36 | await this.save(); | ||
37 | } | ||
38 | |||
39 | const fileContent = await fs.readFile(this.filePath, "utf8"); | ||
40 | const entries = t.array(DedupeEntryC).decode(fileContent); | ||
41 | |||
42 | if (f.either.isRight(entries)) { | ||
43 | this.entries = entries.right; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | private async save() { | ||
48 | await fs.writeFile(this.filePath, JSON.stringify(this.entries ?? []), "utf8"); | ||
49 | } | ||
50 | |||
51 | async check(entry: DedupeEntry) { | ||
52 | await this.load(); | ||
53 | |||
54 | const has = !!this.entries.find((e) => e.provider === entry.provider && e.id === entry.id); | ||
55 | |||
56 | if (!has) { | ||
57 | this.entries.push(entry); | ||
58 | await this.save(); | ||
59 | } | ||
60 | |||
61 | return has; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | export default new Dedupe(50, "dedupe.json"); | ||