Sharkey/packages/backend/src/core/entities/InstanceEntityService.ts

61 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-09-17 21:27:08 +03:00
import { Inject, Injectable } from '@nestjs/common';
import { DI } from '@/di-symbols.js';
2022-09-20 23:33:11 +03:00
import type { InstancesRepository } from '@/models/index.js';
2022-09-17 21:27:08 +03:00
import type { Packed } from '@/misc/schema.js';
import type { } from '@/models/entities/Blocking.js';
import type { Instance } from '@/models/entities/Instance.js';
2022-12-04 06:16:25 +02:00
import { MetaService } from '@/core/MetaService.js';
import { UtilityService } from '../UtilityService.js';
import { bindThis } from '@/decorators.js';
2022-02-18 13:43:50 +02:00
2022-09-17 21:27:08 +03:00
@Injectable()
export class InstanceEntityService {
constructor(
@Inject(DI.instancesRepository)
private instancesRepository: InstancesRepository,
private metaService: MetaService,
private utilityService: UtilityService,
2022-09-17 21:27:08 +03:00
) {
}
@bindThis
2022-09-17 21:27:08 +03:00
public async pack(
2022-02-18 13:43:50 +02:00
instance: Instance,
): Promise<Packed<'FederationInstance'>> {
2022-09-17 21:27:08 +03:00
const meta = await this.metaService.fetch();
2022-02-18 13:43:50 +02:00
return {
id: instance.id,
firstRetrievedAt: instance.firstRetrievedAt.toISOString(),
2022-02-18 13:43:50 +02:00
host: instance.host,
usersCount: instance.usersCount,
notesCount: instance.notesCount,
followingCount: instance.followingCount,
followersCount: instance.followersCount,
isNotResponding: instance.isNotResponding,
isSuspended: instance.isSuspended,
isBlocked: this.utilityService.isBlockedHost(meta.blockedHosts, instance.host),
2022-02-18 13:43:50 +02:00
softwareName: instance.softwareName,
softwareVersion: instance.softwareVersion,
openRegistrations: instance.openRegistrations,
name: instance.name,
description: instance.description,
maintainerName: instance.maintainerName,
maintainerEmail: instance.maintainerEmail,
iconUrl: instance.iconUrl,
faviconUrl: instance.faviconUrl,
themeColor: instance.themeColor,
2022-02-18 13:43:50 +02:00
infoUpdatedAt: instance.infoUpdatedAt ? instance.infoUpdatedAt.toISOString() : null,
};
2022-09-17 21:27:08 +03:00
}
2022-02-18 13:43:50 +02:00
@bindThis
2022-09-17 21:27:08 +03:00
public packMany(
2022-02-18 13:43:50 +02:00
instances: Instance[],
) {
return Promise.all(instances.map(x => this.pack(x)));
2022-09-17 21:27:08 +03:00
}
}