mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-08-22 02:53:38 +00:00
populate block fields when registering a new instance
This commit is contained in:
parent
b422d5bc9d
commit
f3eca0b5cf
1 changed files with 17 additions and 21 deletions
|
@ -5,9 +5,9 @@
|
||||||
|
|
||||||
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
||||||
import * as Redis from 'ioredis';
|
import * as Redis from 'ioredis';
|
||||||
import { QueryFailedError } from 'typeorm';
|
import { DataSource, QueryFailedError } from 'typeorm';
|
||||||
import type { InstancesRepository } from '@/models/_.js';
|
import type { InstancesRepository } from '@/models/_.js';
|
||||||
import type { MiInstance } from '@/models/Instance.js';
|
import { MiInstance } from '@/models/Instance.js';
|
||||||
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
|
import { MemoryKVCache, RedisKVCache } from '@/misc/cache.js';
|
||||||
import { IdService } from '@/core/IdService.js';
|
import { IdService } from '@/core/IdService.js';
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
|
@ -26,6 +26,9 @@ export class FederatedInstanceService implements OnApplicationShutdown {
|
||||||
@Inject(DI.instancesRepository)
|
@Inject(DI.instancesRepository)
|
||||||
private instancesRepository: InstancesRepository,
|
private instancesRepository: InstancesRepository,
|
||||||
|
|
||||||
|
@Inject(DI.db)
|
||||||
|
private readonly db: DataSource,
|
||||||
|
|
||||||
private utilityService: UtilityService,
|
private utilityService: UtilityService,
|
||||||
private idService: IdService,
|
private idService: IdService,
|
||||||
) {
|
) {
|
||||||
|
@ -55,34 +58,27 @@ export class FederatedInstanceService implements OnApplicationShutdown {
|
||||||
const cached = await this.federatedInstanceCache.get(host);
|
const cached = await this.federatedInstanceCache.get(host);
|
||||||
if (cached) return cached;
|
if (cached) return cached;
|
||||||
|
|
||||||
const index = await this.instancesRepository.findOneBy({ host });
|
return await this.db.transaction(async tem => {
|
||||||
|
let index = await tem.findOneBy(MiInstance, { host });
|
||||||
|
|
||||||
if (index == null) {
|
if (index == null) {
|
||||||
let i;
|
await tem.insert(MiInstance, {
|
||||||
try {
|
|
||||||
i = await this.instancesRepository.insertOne({
|
|
||||||
id: this.idService.gen(),
|
id: this.idService.gen(),
|
||||||
host,
|
host,
|
||||||
firstRetrievedAt: new Date(),
|
firstRetrievedAt: new Date(),
|
||||||
|
isBlocked: this.utilityService.isBlockedHost(host),
|
||||||
|
isSilenced: this.utilityService.isSilencedHost(host),
|
||||||
|
isMediaSilenced: this.utilityService.isMediaSilencedHost(host),
|
||||||
|
isAllowListed: this.utilityService.isAllowListedHost(host),
|
||||||
|
isBubbled: this.utilityService.isBubbledHost(host),
|
||||||
});
|
});
|
||||||
} catch (e: unknown) {
|
|
||||||
if (e instanceof QueryFailedError) {
|
index = await tem.findOneByOrFail(MiInstance, { host });
|
||||||
if (isDuplicateKeyValueError(e)) {
|
|
||||||
i = await this.instancesRepository.findOneBy({ host });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i == null) {
|
await this.federatedInstanceCache.set(host, index);
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.federatedInstanceCache.set(host, i);
|
|
||||||
return i;
|
|
||||||
} else {
|
|
||||||
this.federatedInstanceCache.set(host, index);
|
|
||||||
return index;
|
return index;
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
|
|
Loading…
Add table
Reference in a new issue