-
Notifications
You must be signed in to change notification settings - Fork 1
/
meilisearch.ts
161 lines (159 loc) · 4.93 KB
/
meilisearch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {
EnqueuedTask,
Index,
MeiliSearch,
MeiliSearchApiError,
} from "meilisearch";
import { sleep, toJSON } from "./utils.ts";
import type { EhDb } from "./db.ts";
import isEqual from "lodash/isEqual";
const GMetaSettings: Record<string, unknown> = {
displayedAttributes: ["*"],
searchableAttributes: [
"title",
"title_jpn",
"uploader",
"tags.tag",
"tags.translated",
],
filterableAttributes: [
"category",
"expunged",
"filecount",
"filesize",
"gid",
"posted",
"rating",
"tags.id",
"tags.tag",
"tags.translated",
"uploader",
],
sortableAttributes: ["filecount", "filesize", "gid", "posted", "rating"],
};
export class MeiliSearchServer {
client;
db;
handle;
handle2;
#gmeta?: Index;
#inited = false;
target;
constructor(host: string, api_key: string, db: EhDb, signal?: AbortSignal) {
this.client = new MeiliSearch({
host,
apiKey: api_key,
requestConfig: { signal },
});
this.db = db;
this.handle = (e: Event) => {
this.#gallery_update(e);
};
this.handle2 = (e: Event) => {
this.#gallery_remove(e);
};
this.target = new EventTarget();
this.target.addEventListener("gallery_update", this.handle);
this.target.addEventListener("gallery_remove", this.handle2);
}
#gallery_remove(e: Event) {
const ev = e as CustomEvent<number>;
this.removeGallery(ev.detail).catch((e) => {
console.log(e);
});
}
#gallery_update(e: Event) {
const ev = e as CustomEvent<number>;
this.updateGallery(ev.detail).catch((e) => {
console.log(e);
});
}
async #updateGMetaSettings() {
if (this.#gmeta) {
const o: Record<string, unknown> = await this.#gmeta.getSettings();
const u: Record<string, unknown> = {};
let need_update = false;
Object.getOwnPropertyNames(GMetaSettings).forEach((k) => {
if (!isEqual(o[k], GMetaSettings[k])) {
u[k] = GMetaSettings[k];
need_update = true;
}
});
if (need_update) {
console.log(u);
await this.waitTask(this.#gmeta.updateSettings(u));
}
}
}
close() {
this.target.removeEventListener("gallery_update", this.handle);
this.target.removeEventListener("gallery_remove", this.handle2);
}
async getIndex(uid: string, primaryKey?: string) {
try {
return await this.client.getIndex(uid);
} catch (e) {
if (e instanceof MeiliSearchApiError) {
if (e.code === "index_not_found") {
await this.waitTask(
this.client.createIndex(uid, { primaryKey }),
);
return await this.client.getIndex(uid);
}
}
throw e;
}
}
get gmeta(): Promise<Index> {
return new Promise((resolve, reject) => {
const check = () => {
if (!this.#gmeta) reject(new Error("gmeta not found."));
else resolve(this.#gmeta);
};
if (!this.#inited) {
this.init().then(check).catch(reject);
} else {
check();
}
});
}
async init() {
this.#gmeta = await this.getIndex("gmeta", "gid");
this.#updateGMetaSettings();
this.#inited = true;
}
async removeGallery(gid: number) {
const gmeta = await this.gmeta;
await this.waitTask(gmeta.deleteDocument(gid));
}
async removeAllGallery() {
const gmeta = await this.gmeta;
await this.waitTask(gmeta.deleteAllDocuments());
}
async updateGallery(...gids: (number | bigint)[]) {
const gmeta = await this.gmeta;
const datas = gids.map((gid) => {
const d = this.db.get_gmeta_by_gid(gid);
if (!d) throw Error("Gallery not found.");
const e = <Record<string, unknown>> d;
e.tags = this.db.get_gtags_full(gid);
return e;
});
const d = JSON.parse(toJSON(datas));
await this.waitTask(gmeta.updateDocuments(d));
}
async waitTask(task: EnqueuedTask | Promise<EnqueuedTask>) {
if (task instanceof Promise) {
task = await task;
}
let status = await this.client.getTask(task.taskUid);
while (status.status === "enqueued" || status.status === "processing") {
await sleep(100);
status = await this.client.getTask(task.taskUid);
}
if (status.status === "failed") {
throw status.error ? status.error : new Error("Task failed.");
}
return status;
}
}