Implement remote library metadata sync for iPhone

This commit is contained in:
diyaa
2026-05-29 08:54:09 +02:00
parent ebc187f4a1
commit 6e73c1878e
20 changed files with 1379 additions and 48 deletions
+92
View File
@@ -238,6 +238,37 @@
]
}
},
"/api/v1/library/tracks": {
"get": {
"operationId": "LibraryController_getTracks_v1",
"parameters": [
{
"name": "deviceId",
"required": true,
"in": "query",
"schema": {
"format": "uuid",
"type": "string"
}
}
],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/LibraryTracksResponseDto"
}
}
}
}
},
"tags": [
"library"
]
}
},
"/api/v1/sync/bootstrap": {
"get": {
"operationId": "SyncController_bootstrap_v1",
@@ -582,6 +613,67 @@
"assetId"
]
},
"RemoteLibraryTrackDto": {
"type": "object",
"properties": {
"trackId": {
"type": "string",
"format": "uuid"
},
"title": {
"type": "string",
"example": "Track Title"
},
"artist": {
"type": "string",
"example": "Track Artist"
},
"durationSeconds": {
"type": "number",
"example": 245
},
"sha256": {
"type": "string",
"example": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
"assetId": {
"type": "string",
"format": "uuid"
},
"createdAt": {
"type": "string",
"example": "2026-05-29T08:00:00.000Z"
},
"updatedAt": {
"type": "string",
"example": "2026-05-29T08:05:00.000Z"
}
},
"required": [
"trackId",
"title",
"artist",
"durationSeconds",
"sha256",
"assetId",
"createdAt",
"updatedAt"
]
},
"LibraryTracksResponseDto": {
"type": "object",
"properties": {
"tracks": {
"type": "array",
"items": {
"$ref": "#/components/schemas/RemoteLibraryTrackDto"
}
}
},
"required": [
"tracks"
]
},
"LibraryTrackDto": {
"type": "object",
"properties": {
@@ -0,0 +1,26 @@
import { Controller, Get, Query } from '@nestjs/common';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import {
LibraryTracksQueryDto,
LibraryTracksResponseDto,
} from './library.dto';
import { LibraryService } from './library.service';
@ApiTags('library')
@Controller({
path: 'library',
version: '1',
})
export class LibraryController {
constructor(private readonly libraryService: LibraryService) {}
@Get('tracks')
@ApiOkResponse({ type: LibraryTracksResponseDto })
async getTracks(
@Query() query: LibraryTracksQueryDto,
): Promise<LibraryTracksResponseDto> {
return {
tracks: await this.libraryService.getRemoteLibraryTracks(query.deviceId),
};
}
}
@@ -0,0 +1,42 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsUUID } from 'class-validator';
export class LibraryTracksQueryDto {
@ApiProperty({ format: 'uuid' })
@IsUUID()
deviceId!: string;
}
export class RemoteLibraryTrackDto {
@ApiProperty({ format: 'uuid' })
trackId!: string;
@ApiProperty({ example: 'Track Title' })
title!: string;
@ApiProperty({ example: 'Track Artist' })
artist!: string;
@ApiProperty({ example: 245 })
durationSeconds!: number;
@ApiProperty({
example:
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
})
sha256!: string;
@ApiProperty({ format: 'uuid' })
assetId!: string;
@ApiProperty({ example: '2026-05-29T08:00:00.000Z' })
createdAt!: string;
@ApiProperty({ example: '2026-05-29T08:05:00.000Z' })
updatedAt!: string;
}
export class LibraryTracksResponseDto {
@ApiProperty({ type: [RemoteLibraryTrackDto] })
tracks!: RemoteLibraryTrackDto[];
}
@@ -1,10 +1,12 @@
import { Module } from '@nestjs/common';
import { PrismaModule } from '../../infrastructure/database/prisma.module';
import { UsersModule } from '../users/users.module';
import { LibraryController } from './library.controller';
import { LibraryService } from './library.service';
@Module({
imports: [PrismaModule, UsersModule],
controllers: [LibraryController],
providers: [LibraryService],
exports: [LibraryService],
})
@@ -0,0 +1,201 @@
import { randomUUID } from 'node:crypto';
import { NotFoundException } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { PrismaService } from '../../infrastructure/database/prisma.service';
import { DefaultUserService } from '../users/default-user.service';
import { LibraryService } from './library.service';
function createPrismaMock() {
const devices = new Map<string, any>();
const tracks = new Map<string, any>();
const audioAssets = new Map<string, any>();
return {
prismaMock: {
device: {
findUnique: jest.fn().mockImplementation(async ({ where }) => {
const device = devices.get(where.id) ?? null;
return device ? { userId: device.userId } : null;
}),
},
track: {
findMany: jest.fn().mockImplementation(async ({ where }) => {
return [...tracks.values()]
.filter((track) => {
const userMatches = track.userId === where.userId;
const statusMatches = track.status === where.status;
const assetMatches = where.primaryAudioAssetId?.not
? track.primaryAudioAssetId != null
: true;
return userMatches && statusMatches && assetMatches;
})
.sort((lhs, rhs) => lhs.createdAt.getTime() - rhs.createdAt.getTime())
.map((track) => ({
...track,
primaryAudioAsset: track.primaryAudioAssetId
? audioAssets.get(track.primaryAudioAssetId) ?? null
: null,
}));
}),
},
},
state: {
devices,
tracks,
audioAssets,
},
};
}
describe('LibraryService', () => {
let libraryService: LibraryService;
let state: ReturnType<typeof createPrismaMock>['state'];
beforeEach(async () => {
const { prismaMock, state: nextState } = createPrismaMock();
state = nextState;
const moduleRef = await Test.createTestingModule({
providers: [
LibraryService,
{
provide: PrismaService,
useValue: prismaMock,
},
{
provide: DefaultUserService,
useValue: {},
},
],
}).compile();
libraryService = moduleRef.get(LibraryService);
});
it('returns only tracks owned by the requesting device user in created order', async () => {
const ownerId = randomUUID();
const otherUserId = randomUUID();
const ownerDeviceId = randomUUID();
const ownerTrackId = randomUUID();
const ownerAssetId = randomUUID();
const secondOwnerTrackId = randomUUID();
const secondOwnerAssetId = randomUUID();
const otherTrackId = randomUUID();
const otherAssetId = randomUUID();
state.devices.set(ownerDeviceId, { id: ownerDeviceId, userId: ownerId });
state.audioAssets.set(ownerAssetId, {
id: ownerAssetId,
sha256: 'sha-owner-a',
durationMs: 181000,
});
state.audioAssets.set(secondOwnerAssetId, {
id: secondOwnerAssetId,
sha256: 'sha-owner-b',
durationMs: 182000,
});
state.audioAssets.set(otherAssetId, {
id: otherAssetId,
sha256: 'sha-other',
durationMs: 183000,
});
state.tracks.set(secondOwnerTrackId, {
id: secondOwnerTrackId,
userId: ownerId,
title: 'Second',
artist: 'Owner',
durationMs: 182000,
status: 'ACTIVE',
primaryAudioAssetId: secondOwnerAssetId,
createdAt: new Date('2026-05-29T08:05:00.000Z'),
updatedAt: new Date('2026-05-29T08:06:00.000Z'),
});
state.tracks.set(ownerTrackId, {
id: ownerTrackId,
userId: ownerId,
title: 'First',
artist: 'Owner',
durationMs: 181000,
status: 'ACTIVE',
primaryAudioAssetId: ownerAssetId,
createdAt: new Date('2026-05-29T08:00:00.000Z'),
updatedAt: new Date('2026-05-29T08:01:00.000Z'),
});
state.tracks.set(otherTrackId, {
id: otherTrackId,
userId: otherUserId,
title: 'Other',
artist: 'Elsewhere',
durationMs: 183000,
status: 'ACTIVE',
primaryAudioAssetId: otherAssetId,
createdAt: new Date('2026-05-29T07:59:00.000Z'),
updatedAt: new Date('2026-05-29T08:02:00.000Z'),
});
const tracks = await libraryService.getRemoteLibraryTracks(ownerDeviceId);
expect(tracks).toEqual([
{
trackId: ownerTrackId,
title: 'First',
artist: 'Owner',
durationSeconds: 181,
sha256: 'sha-owner-a',
assetId: ownerAssetId,
createdAt: '2026-05-29T08:00:00.000Z',
updatedAt: '2026-05-29T08:01:00.000Z',
},
{
trackId: secondOwnerTrackId,
title: 'Second',
artist: 'Owner',
durationSeconds: 182,
sha256: 'sha-owner-b',
assetId: secondOwnerAssetId,
createdAt: '2026-05-29T08:05:00.000Z',
updatedAt: '2026-05-29T08:06:00.000Z',
},
]);
});
it('returns an empty library when the user has no remote tracks', async () => {
const ownerId = randomUUID();
const ownerDeviceId = randomUUID();
state.devices.set(ownerDeviceId, { id: ownerDeviceId, userId: ownerId });
await expect(
libraryService.getRemoteLibraryTracks(ownerDeviceId),
).resolves.toEqual([]);
});
it('throws for an unknown device', async () => {
await expect(
libraryService.getRemoteLibraryTracks(randomUUID()),
).rejects.toBeInstanceOf(NotFoundException);
});
it('skips tracks without a primary audio asset', async () => {
const ownerId = randomUUID();
const ownerDeviceId = randomUUID();
state.devices.set(ownerDeviceId, { id: ownerDeviceId, userId: ownerId });
state.tracks.set(randomUUID(), {
id: randomUUID(),
userId: ownerId,
title: 'Incomplete',
artist: 'Owner',
durationMs: 181000,
status: 'ACTIVE',
primaryAudioAssetId: null,
createdAt: new Date('2026-05-29T08:00:00.000Z'),
updatedAt: new Date('2026-05-29T08:01:00.000Z'),
});
await expect(
libraryService.getRemoteLibraryTracks(ownerDeviceId),
).resolves.toEqual([]);
});
});
+67 -1
View File
@@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../infrastructure/database/prisma.service';
import { LibraryTrackDto } from '../sync/sync.dto';
import { DefaultUserService } from '../users/default-user.service';
import { RemoteLibraryTrackDto } from './library.dto';
@Injectable()
export class LibraryService {
@@ -33,4 +34,69 @@ export class LibraryService {
artist: track.artist,
}));
}
async getRemoteLibraryTracks(
deviceId: string,
): Promise<RemoteLibraryTrackDto[]> {
const device = await this.prismaService.device.findUnique({
where: { id: deviceId },
select: {
userId: true,
},
});
if (!device) {
throw new NotFoundException('Device not found');
}
const tracks = await this.prismaService.track.findMany({
where: {
userId: device.userId,
status: 'ACTIVE',
primaryAudioAssetId: {
not: null,
},
},
orderBy: {
createdAt: 'asc',
},
select: {
id: true,
title: true,
artist: true,
durationMs: true,
createdAt: true,
updatedAt: true,
primaryAudioAsset: {
select: {
id: true,
sha256: true,
durationMs: true,
},
},
},
});
return tracks.flatMap((track) => {
if (!track.primaryAudioAsset) {
return [];
}
const durationMs =
track.durationMs ?? track.primaryAudioAsset.durationMs ?? 0;
return [
{
trackId: track.id,
title: track.title,
artist: track.artist,
durationSeconds: Math.max(0, Math.round(durationMs / 1000)),
sha256: track.primaryAudioAsset.sha256,
assetId: track.primaryAudioAsset.id,
createdAt: track.createdAt.toISOString(),
updatedAt: track.updatedAt.toISOString(),
},
];
});
}
}
+161 -3
View File
@@ -9,6 +9,8 @@ import { AppModule } from '../../src/app.module';
import { AppConfigService } from '../../src/modules/config/config.service';
import { DevicesController } from '../../src/modules/devices/devices.controller';
import { HealthController } from '../../src/modules/health/health.controller';
import { LibraryController } from '../../src/modules/library/library.controller';
import { LibraryTracksQueryDto } from '../../src/modules/library/library.dto';
import { SyncController } from '../../src/modules/sync/sync.controller';
import { UploadsController } from '../../src/modules/uploads/uploads.controller';
import { UploadsService } from '../../src/modules/uploads/uploads.service';
@@ -72,7 +74,16 @@ function createPrismaMock() {
return record;
}),
findUnique: jest.fn().mockImplementation(async ({ where }) => {
return devices.get(where.id) ?? null;
const device = devices.get(where.id) ?? null;
if (!device) {
return null;
}
if (where.id && typeof where.id === 'string') {
return device;
}
return device;
}),
update: jest.fn().mockImplementation(async ({ where, data }) => {
const current = devices.get(where.id);
@@ -91,9 +102,18 @@ function createPrismaMock() {
.filter((track) => {
const userMatches = where?.userId ? track.userId === where.userId : true;
const statusMatches = where?.status ? track.status === where.status : true;
return userMatches && statusMatches;
const assetMatches = where?.primaryAudioAssetId?.not
? track.primaryAudioAssetId != null
: true;
return userMatches && statusMatches && assetMatches;
})
.sort((lhs, rhs) => lhs.createdAt.getTime() - rhs.createdAt.getTime());
.sort((lhs, rhs) => lhs.createdAt.getTime() - rhs.createdAt.getTime())
.map((track) => ({
...track,
primaryAudioAsset: track.primaryAudioAssetId
? audioAssets.get(track.primaryAudioAssetId) ?? null
: null,
}));
}),
findUnique: jest.fn().mockImplementation(async ({ where }) => {
return tracks.get(where.id) ?? null;
@@ -236,6 +256,7 @@ describe('Velody API wiring (e2e)', () => {
let app: INestApplication;
let healthController: HealthController;
let devicesController: DevicesController;
let libraryController: LibraryController;
let syncController: SyncController;
let uploadsController: UploadsController;
let uploadsService: UploadsService;
@@ -274,6 +295,7 @@ describe('Velody API wiring (e2e)', () => {
healthController = moduleRef.get(HealthController);
devicesController = moduleRef.get(DevicesController);
libraryController = moduleRef.get(LibraryController);
syncController = moduleRef.get(SyncController);
uploadsController = moduleRef.get(UploadsController);
uploadsService = moduleRef.get(UploadsService);
@@ -322,6 +344,142 @@ describe('Velody API wiring (e2e)', () => {
expect(changesResponse.nextCursor).toBe('0');
});
it('returns remote library metadata for the requesting device owner', async () => {
const primaryDevice = await devicesController.register({
platform: 'IPHONE',
deviceName: 'iPhone',
appVersion: '0.1.0',
});
const secondUserId = randomUUID();
const secondaryDeviceId = randomUUID();
const primaryTrackId = randomUUID();
const primaryAssetId = randomUUID();
const secondaryTrackId = randomUUID();
const secondaryAssetId = randomUUID();
prismaState.devices.set(secondaryDeviceId, {
id: secondaryDeviceId,
userId: secondUserId,
platform: 'IPHONE',
deviceName: 'Other iPhone',
appVersion: '0.1.0',
installTokenHash: 'other-device-hash',
lastSeenAt: new Date(),
createdAt: new Date(),
updatedAt: new Date(),
});
prismaState.audioAssets.set(primaryAssetId, {
id: primaryAssetId,
userId: prismaState.defaultUser.id,
trackId: primaryTrackId,
sha256: 'sha-default',
storageKey: 'users/default/audio/sha-default.mp3',
originalFilename: 'default.mp3',
mimeType: 'audio/mpeg',
fileExtension: 'mp3',
fileSizeBytes: BigInt(42),
durationMs: 245000,
sourceDeviceId: primaryDevice.deviceId,
createdAt: new Date('2026-05-29T08:00:00.000Z'),
});
prismaState.audioAssets.set(secondaryAssetId, {
id: secondaryAssetId,
userId: secondUserId,
trackId: secondaryTrackId,
sha256: 'sha-other-user',
storageKey: 'users/other/audio/sha-other-user.mp3',
originalFilename: 'other.mp3',
mimeType: 'audio/mpeg',
fileExtension: 'mp3',
fileSizeBytes: BigInt(24),
durationMs: 180000,
sourceDeviceId: secondaryDeviceId,
createdAt: new Date('2026-05-29T08:01:00.000Z'),
});
prismaState.tracks.set(primaryTrackId, {
id: primaryTrackId,
userId: prismaState.defaultUser.id,
primaryAudioAssetId: primaryAssetId,
artworkAssetId: null,
title: 'Default User Track',
artist: 'Velody',
album: null,
albumArtist: null,
genre: null,
discNumber: null,
trackNumber: null,
year: null,
durationMs: 245000,
status: 'ACTIVE',
deletedAt: null,
createdAt: new Date('2026-05-29T08:00:00.000Z'),
updatedAt: new Date('2026-05-29T08:02:00.000Z'),
});
prismaState.tracks.set(secondaryTrackId, {
id: secondaryTrackId,
userId: secondUserId,
primaryAudioAssetId: secondaryAssetId,
artworkAssetId: null,
title: 'Other User Track',
artist: 'Elsewhere',
album: null,
albumArtist: null,
genre: null,
discNumber: null,
trackNumber: null,
year: null,
durationMs: 180000,
status: 'ACTIVE',
deletedAt: null,
createdAt: new Date('2026-05-29T08:01:00.000Z'),
updatedAt: new Date('2026-05-29T08:03:00.000Z'),
});
const response = await libraryController.getTracks({
deviceId: primaryDevice.deviceId,
});
expect(response).toEqual({
tracks: [
{
trackId: primaryTrackId,
title: 'Default User Track',
artist: 'Velody',
durationSeconds: 245,
sha256: 'sha-default',
assetId: primaryAssetId,
createdAt: '2026-05-29T08:00:00.000Z',
updatedAt: '2026-05-29T08:02:00.000Z',
},
],
});
});
it('rejects an invalid remote library device id query', async () => {
const validationPipe = new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
});
await expect(
validationPipe.transform(
{ deviceId: 'not-a-uuid' },
{
type: 'query',
metatype: LibraryTracksQueryDto,
data: '',
},
),
).rejects.toMatchObject({
response: {
message: expect.arrayContaining(['deviceId must be a UUID']),
},
});
});
it('supports the MP3 upload pipeline through the Nest app wiring', async () => {
const registerResponse = await devicesController.register({
platform: 'MACOS',