2023-05-17 19:07:17 +02:00
|
|
|
import { BadRequestException, Inject, Injectable, Logger, NotFoundException } from '@nestjs/common';
|
2023-09-18 23:22:44 +02:00
|
|
|
import { AccessCore, IAccessRepository, Permission } from '../access';
|
2023-07-11 23:52:41 +02:00
|
|
|
import { AssetResponseDto, BulkIdErrorReason, BulkIdResponseDto, mapAsset } from '../asset';
|
2023-05-17 19:07:17 +02:00
|
|
|
import { AuthUserDto } from '../auth';
|
2023-07-10 19:56:45 +02:00
|
|
|
import { mimeTypes } from '../domain.constant';
|
2023-05-17 19:07:17 +02:00
|
|
|
import { IJobRepository, JobName } from '../job';
|
2023-09-04 21:45:59 +02:00
|
|
|
import { IStorageRepository, ImmichReadStream } from '../storage';
|
2023-09-18 06:05:35 +02:00
|
|
|
import { ISystemConfigRepository, SystemConfigCore } from '../system-config';
|
2023-07-18 20:09:43 +02:00
|
|
|
import {
|
|
|
|
MergePersonDto,
|
|
|
|
PeopleResponseDto,
|
2023-07-23 05:00:43 +02:00
|
|
|
PeopleUpdateDto,
|
2023-07-18 20:09:43 +02:00
|
|
|
PersonResponseDto,
|
|
|
|
PersonSearchDto,
|
|
|
|
PersonUpdateDto,
|
2023-09-04 21:45:59 +02:00
|
|
|
mapPerson,
|
2023-07-18 20:09:43 +02:00
|
|
|
} from './person.dto';
|
2023-07-11 23:52:41 +02:00
|
|
|
import { IPersonRepository, UpdateFacesData } from './person.repository';
|
2023-05-17 19:07:17 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class PersonService {
|
2023-09-18 23:22:44 +02:00
|
|
|
private access: AccessCore;
|
2023-09-18 06:05:35 +02:00
|
|
|
private configCore: SystemConfigCore;
|
2023-05-17 19:07:17 +02:00
|
|
|
readonly logger = new Logger(PersonService.name);
|
|
|
|
|
|
|
|
constructor(
|
2023-09-18 23:22:44 +02:00
|
|
|
@Inject(IAccessRepository) private accessRepository: IAccessRepository,
|
2023-05-17 19:07:17 +02:00
|
|
|
@Inject(IPersonRepository) private repository: IPersonRepository,
|
2023-09-18 06:05:35 +02:00
|
|
|
@Inject(ISystemConfigRepository) configRepository: ISystemConfigRepository,
|
2023-05-17 19:07:17 +02:00
|
|
|
@Inject(IStorageRepository) private storageRepository: IStorageRepository,
|
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2023-09-18 06:05:35 +02:00
|
|
|
) {
|
2023-09-18 23:22:44 +02:00
|
|
|
this.access = new AccessCore(accessRepository);
|
2023-09-18 06:05:35 +02:00
|
|
|
this.configCore = new SystemConfigCore(configRepository);
|
|
|
|
}
|
2023-05-17 19:07:17 +02:00
|
|
|
|
2023-07-18 20:09:43 +02:00
|
|
|
async getAll(authUser: AuthUserDto, dto: PersonSearchDto): Promise<PeopleResponseDto> {
|
2023-09-18 06:05:35 +02:00
|
|
|
const { machineLearning } = await this.configCore.getConfig();
|
2023-09-08 08:49:43 +02:00
|
|
|
const people = await this.repository.getAllForUser(authUser.id, {
|
2023-09-18 06:05:35 +02:00
|
|
|
minimumFaceCount: machineLearning.facialRecognition.minFaces,
|
2023-08-16 02:06:49 +02:00
|
|
|
withHidden: dto.withHidden || false,
|
|
|
|
});
|
2023-08-14 18:09:26 +02:00
|
|
|
const persons: PersonResponseDto[] = people
|
2023-07-18 20:09:43 +02:00
|
|
|
// with thumbnails
|
|
|
|
.filter((person) => !!person.thumbnailPath)
|
|
|
|
.map((person) => mapPerson(person));
|
|
|
|
|
|
|
|
return {
|
|
|
|
people: persons.filter((person) => dto.withHidden || !person.isHidden),
|
|
|
|
total: persons.length,
|
|
|
|
visible: persons.filter((person: PersonResponseDto) => !person.isHidden).length,
|
|
|
|
};
|
2023-05-17 19:07:17 +02:00
|
|
|
}
|
|
|
|
|
2023-09-18 23:22:44 +02:00
|
|
|
async getById(authUser: AuthUserDto, id: string): Promise<PersonResponseDto> {
|
|
|
|
await this.access.requirePermission(authUser, Permission.PERSON_READ, id);
|
|
|
|
return this.findOrFail(id).then(mapPerson);
|
2023-05-17 19:07:17 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
async getThumbnail(authUser: AuthUserDto, id: string): Promise<ImmichReadStream> {
|
2023-09-18 23:22:44 +02:00
|
|
|
await this.access.requirePermission(authUser, Permission.PERSON_READ, id);
|
|
|
|
const person = await this.repository.getById(id);
|
2023-05-17 19:07:17 +02:00
|
|
|
if (!person || !person.thumbnailPath) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
2023-07-10 19:56:45 +02:00
|
|
|
return this.storageRepository.createReadStream(person.thumbnailPath, mimeTypes.lookup(person.thumbnailPath));
|
2023-05-17 19:07:17 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
async getAssets(authUser: AuthUserDto, id: string): Promise<AssetResponseDto[]> {
|
2023-09-18 23:22:44 +02:00
|
|
|
await this.access.requirePermission(authUser, Permission.PERSON_READ, id);
|
|
|
|
const assets = await this.repository.getAssets(id);
|
2023-05-17 19:07:17 +02:00
|
|
|
return assets.map(mapAsset);
|
|
|
|
}
|
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
async update(authUser: AuthUserDto, id: string, dto: PersonUpdateDto): Promise<PersonResponseDto> {
|
2023-09-18 23:22:44 +02:00
|
|
|
await this.access.requirePermission(authUser, Permission.PERSON_WRITE, id);
|
|
|
|
let person = await this.findOrFail(id);
|
2023-05-17 19:07:17 +02:00
|
|
|
|
2023-08-18 22:10:29 +02:00
|
|
|
if (dto.name !== undefined || dto.birthDate !== undefined || dto.isHidden !== undefined) {
|
|
|
|
person = await this.repository.update({ id, name: dto.name, birthDate: dto.birthDate, isHidden: dto.isHidden });
|
|
|
|
if (this.needsSearchIndexUpdate(dto)) {
|
2023-09-18 23:22:44 +02:00
|
|
|
const assets = await this.repository.getAssets(id);
|
2023-08-18 22:10:29 +02:00
|
|
|
const ids = assets.map((asset) => asset.id);
|
|
|
|
await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_ASSET, data: { ids } });
|
|
|
|
}
|
2023-07-03 00:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dto.featureFaceAssetId) {
|
2023-07-11 23:52:41 +02:00
|
|
|
const assetId = dto.featureFaceAssetId;
|
|
|
|
const face = await this.repository.getFaceById({ personId: id, assetId });
|
|
|
|
if (!face) {
|
|
|
|
throw new BadRequestException('Invalid assetId for feature face');
|
|
|
|
}
|
2023-07-03 00:46:20 +02:00
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
await this.jobRepository.queue({
|
|
|
|
name: JobName.GENERATE_FACE_THUMBNAIL,
|
|
|
|
data: {
|
|
|
|
personId: id,
|
|
|
|
assetId,
|
|
|
|
boundingBox: {
|
|
|
|
x1: face.boundingBoxX1,
|
|
|
|
x2: face.boundingBoxX2,
|
|
|
|
y1: face.boundingBoxY1,
|
|
|
|
y2: face.boundingBoxY2,
|
|
|
|
},
|
|
|
|
imageHeight: face.imageHeight,
|
|
|
|
imageWidth: face.imageWidth,
|
|
|
|
},
|
|
|
|
});
|
2023-07-03 00:46:20 +02:00
|
|
|
}
|
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
return mapPerson(person);
|
2023-05-17 19:07:17 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 05:00:43 +02:00
|
|
|
async updatePeople(authUser: AuthUserDto, dto: PeopleUpdateDto): Promise<BulkIdResponseDto[]> {
|
|
|
|
const results: BulkIdResponseDto[] = [];
|
|
|
|
for (const person of dto.people) {
|
|
|
|
try {
|
|
|
|
await this.update(authUser, person.id, {
|
|
|
|
isHidden: person.isHidden,
|
|
|
|
name: person.name,
|
2023-08-18 22:10:29 +02:00
|
|
|
birthDate: person.birthDate,
|
2023-07-23 05:00:43 +02:00
|
|
|
featureFaceAssetId: person.featureFaceAssetId,
|
|
|
|
}),
|
|
|
|
results.push({ id: person.id, success: true });
|
|
|
|
} catch (error: Error | any) {
|
|
|
|
this.logger.error(`Unable to update ${person.id} : ${error}`, error?.stack);
|
|
|
|
results.push({ id: person.id, success: false, error: BulkIdErrorReason.UNKNOWN });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:43:24 +02:00
|
|
|
async handlePersonCleanup() {
|
2023-05-17 19:07:17 +02:00
|
|
|
const people = await this.repository.getAllWithoutFaces();
|
|
|
|
for (const person of people) {
|
|
|
|
this.logger.debug(`Person ${person.name || person.id} no longer has any faces, deleting.`);
|
|
|
|
try {
|
|
|
|
await this.repository.delete(person);
|
|
|
|
await this.jobRepository.queue({ name: JobName.DELETE_FILES, data: { files: [person.thumbnailPath] } });
|
|
|
|
} catch (error: Error | any) {
|
|
|
|
this.logger.error(`Unable to delete person: ${error}`, error?.stack);
|
|
|
|
}
|
|
|
|
}
|
2023-05-26 21:43:24 +02:00
|
|
|
|
|
|
|
return true;
|
2023-05-17 19:07:17 +02:00
|
|
|
}
|
2023-07-11 23:52:41 +02:00
|
|
|
|
|
|
|
async mergePerson(authUser: AuthUserDto, id: string, dto: MergePersonDto): Promise<BulkIdResponseDto[]> {
|
|
|
|
const mergeIds = dto.ids;
|
2023-09-18 23:22:44 +02:00
|
|
|
await this.access.requirePermission(authUser, Permission.PERSON_WRITE, id);
|
|
|
|
const primaryPerson = await this.findOrFail(id);
|
2023-07-11 23:52:41 +02:00
|
|
|
const primaryName = primaryPerson.name || primaryPerson.id;
|
|
|
|
|
|
|
|
const results: BulkIdResponseDto[] = [];
|
|
|
|
|
|
|
|
for (const mergeId of mergeIds) {
|
2023-09-18 23:22:44 +02:00
|
|
|
const hasPermission = await this.access.hasPermission(authUser, Permission.PERSON_MERGE, mergeId);
|
|
|
|
|
|
|
|
if (!hasPermission) {
|
|
|
|
results.push({ id: mergeId, success: false, error: BulkIdErrorReason.NO_PERMISSION });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
try {
|
2023-09-18 23:22:44 +02:00
|
|
|
const mergePerson = await this.repository.getById(mergeId);
|
2023-07-11 23:52:41 +02:00
|
|
|
if (!mergePerson) {
|
|
|
|
results.push({ id: mergeId, success: false, error: BulkIdErrorReason.NOT_FOUND });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mergeName = mergePerson.name || mergePerson.id;
|
|
|
|
const mergeData: UpdateFacesData = { oldPersonId: mergeId, newPersonId: id };
|
|
|
|
this.logger.log(`Merging ${mergeName} into ${primaryName}`);
|
|
|
|
|
|
|
|
const assetIds = await this.repository.prepareReassignFaces(mergeData);
|
|
|
|
for (const assetId of assetIds) {
|
|
|
|
await this.jobRepository.queue({ name: JobName.SEARCH_REMOVE_FACE, data: { assetId, personId: mergeId } });
|
|
|
|
}
|
|
|
|
await this.repository.reassignFaces(mergeData);
|
|
|
|
await this.repository.delete(mergePerson);
|
|
|
|
|
|
|
|
this.logger.log(`Merged ${mergeName} into ${primaryName}`);
|
|
|
|
results.push({ id: mergeId, success: true });
|
|
|
|
} catch (error: Error | any) {
|
|
|
|
this.logger.error(`Unable to merge ${mergeId} into ${id}: ${error}`, error?.stack);
|
|
|
|
results.push({ id: mergeId, success: false, error: BulkIdErrorReason.UNKNOWN });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 21:42:12 +02:00
|
|
|
// Re-index all faces in typesense for up-to-date search results
|
|
|
|
await this.jobRepository.queue({ name: JobName.SEARCH_INDEX_FACES });
|
|
|
|
|
2023-07-11 23:52:41 +02:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2023-08-18 22:10:29 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the given person update is going to require an update of the search index.
|
|
|
|
* @param dto the Person going to be updated
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
private needsSearchIndexUpdate(dto: PersonUpdateDto): boolean {
|
|
|
|
return dto.name !== undefined || dto.isHidden !== undefined;
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:22:44 +02:00
|
|
|
private async findOrFail(id: string) {
|
|
|
|
const person = await this.repository.getById(id);
|
2023-07-11 23:52:41 +02:00
|
|
|
if (!person) {
|
|
|
|
throw new BadRequestException('Person not found');
|
|
|
|
}
|
|
|
|
return person;
|
|
|
|
}
|
2023-05-17 19:07:17 +02:00
|
|
|
}
|