summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/FederatedInstanceService.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-09-18 03:27:08 +0900
committerGitHub <noreply@github.com>2022-09-18 03:27:08 +0900
commitb75184ec8e3436200bacdcd832e3324702553d20 (patch)
tree8b7e316f29e95df921db57289c8b8da476d18f07 /packages/backend/src/core/FederatedInstanceService.ts
parentUpdate ROADMAP.md (diff)
downloadsharkey-b75184ec8e3436200bacdcd832e3324702553d20.tar.gz
sharkey-b75184ec8e3436200bacdcd832e3324702553d20.tar.bz2
sharkey-b75184ec8e3436200bacdcd832e3324702553d20.zip
なんかもうめっちゃ変えた
Diffstat (limited to 'packages/backend/src/core/FederatedInstanceService.ts')
-rw-r--r--packages/backend/src/core/FederatedInstanceService.ts46
1 files changed, 46 insertions, 0 deletions
diff --git a/packages/backend/src/core/FederatedInstanceService.ts b/packages/backend/src/core/FederatedInstanceService.ts
new file mode 100644
index 0000000000..24bedd8192
--- /dev/null
+++ b/packages/backend/src/core/FederatedInstanceService.ts
@@ -0,0 +1,46 @@
+import { Inject, Injectable } from '@nestjs/common';
+import { InstancesRepository } from '@/models/index.js';
+import type { Instance } from '@/models/entities/Instance.js';
+import { Cache } from '@/misc/cache.js';
+import { IdService } from '@/core/IdService.js';
+import { DI } from '@/di-symbols.js';
+import { UtilityService } from './UtilityService.js';
+
+@Injectable()
+export class FederatedInstanceService {
+ #cache: Cache<Instance>;
+
+ constructor(
+ @Inject(DI.instancesRepository)
+ private instancesRepository: InstancesRepository,
+
+ private utilityService: UtilityService,
+ private idService: IdService,
+ ) {
+ this.#cache = new Cache<Instance>(1000 * 60 * 60);
+ }
+
+ public async registerOrFetchInstanceDoc(host: string): Promise<Instance> {
+ host = this.utilityService.toPuny(host);
+
+ const cached = this.#cache.get(host);
+ if (cached) return cached;
+
+ const index = await this.instancesRepository.findOneBy({ host });
+
+ if (index == null) {
+ const i = await this.instancesRepository.insert({
+ id: this.idService.genId(),
+ host,
+ caughtAt: new Date(),
+ lastCommunicatedAt: new Date(),
+ }).then(x => this.instancesRepository.findOneByOrFail(x.identifiers[0]));
+
+ this.#cache.set(host, i);
+ return i;
+ } else {
+ this.#cache.set(host, index);
+ return index;
+ }
+ }
+}