summaryrefslogtreecommitdiff
path: root/packages/backend/src/services/register-or-fetch-instance-doc.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-11-12 02:02:25 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2021-11-12 02:02:25 +0900
commit0e4a111f81cceed275d9bec2695f6e401fb654d8 (patch)
tree40874799472fa07416f17b50a398ac33b7771905 /packages/backend/src/services/register-or-fetch-instance-doc.ts
parentupdate deps (diff)
downloadmisskey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.gz
misskey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.bz2
misskey-0e4a111f81cceed275d9bec2695f6e401fb654d8.zip
refactoring
Resolve #7779
Diffstat (limited to 'packages/backend/src/services/register-or-fetch-instance-doc.ts')
-rw-r--r--packages/backend/src/services/register-or-fetch-instance-doc.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/backend/src/services/register-or-fetch-instance-doc.ts b/packages/backend/src/services/register-or-fetch-instance-doc.ts
new file mode 100644
index 0000000000..a548ab0497
--- /dev/null
+++ b/packages/backend/src/services/register-or-fetch-instance-doc.ts
@@ -0,0 +1,34 @@
+import { Instance } from '@/models/entities/instance';
+import { Instances } from '@/models/index';
+import { federationChart } from '@/services/chart/index';
+import { genId } from '@/misc/gen-id';
+import { toPuny } from '@/misc/convert-host';
+import { Cache } from '@/misc/cache';
+
+const cache = new Cache<Instance>(1000 * 60 * 60);
+
+export async function registerOrFetchInstanceDoc(host: string): Promise<Instance> {
+ host = toPuny(host);
+
+ const cached = cache.get(host);
+ if (cached) return cached;
+
+ const index = await Instances.findOne({ host });
+
+ if (index == null) {
+ const i = await Instances.save({
+ id: genId(),
+ host,
+ caughtAt: new Date(),
+ lastCommunicatedAt: new Date(),
+ });
+
+ federationChart.update(true);
+
+ cache.set(host, i);
+ return i;
+ } else {
+ cache.set(host, index);
+ return index;
+ }
+}