Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

9.0 KB
Raw Blame
import { withRequestContext } from '../database/request-context';
import { UnitOfWork } from '../database/unit-of-work';
import { MapsService } from '../maps/maps.service';
import { PhotoCaptureBackfillService } from '../memories/photo-capture-backfill.service';
import { JourneyDomainService } from './journey-domain.service';
import { MikroORM } from '@mikro-orm/core';
import { Injectable } from '@nestjs/common';

/** The trek_photos ids of freshly added journey photo rows, whatever shape the add answered with. */
function trekPhotoIdsOf(photos: readonly unknown[]): number[] {
  return photos
    .map((p) => (p as { photo_id?: unknown } | null)?.photo_id)
    .filter((id): id is number => typeof id === 'number');
}

/** Where a batch of provider photos goes: one entry (and with it the journey gallery), or the gallery alone. */
export type ProviderPhotoTarget = { entryId: number } | { journeyId: number };

/** A batch of photos from one library, as every surface hands it over. */
export interface ProviderPhotoBatch {
  provider: string;
  assetIds: readonly string[];
  /** Parallel to assetIds: 'video' marks a clip, anything else is an image. */
  mediaTypes?: readonly unknown[];
  /** Kept on an entry's photos only; the gallery add has never recorded one. */
  caption?: string;
  /** For an album someone shared from Synology Photos. */
  passphrase?: string;
}

/**
 * Provider photos landing on a journey: the add itself, the event that tells
 * everyone on the journey, and the capture times and refresh that follow them
 * (#1587).
 *
 * The add answers before anyone has asked the provider when each photo was
 * taken (#1614 keeps that on the server rather than trusting the client). So
 * the reload a client does straight after the add still sees the new photos
 * without taken_at, and the gallery files them at the end by import time. Once
 * the backfill has written something, everyone on the journey is told, the
 * importer included: its own reload came too early, which is why the event
 * excludes no socket.
 *
 * One place for the REST routes, the MCP tool and the plugin RPC, so no surface
 * can add provider photos that stay out of order until somebody reloads.
 *
 * Uploads from a device fill their capture times here too, but tell nobody (see
 * scheduleUpload): one upload request carries one file, so a refresh per request
 * would reload every open client once per photo of a bulk upload.
 *
 * Every schedule* method runs its work after the add has answered, so each one
 * forks its own request context with `withRequestContext` instead of riding the
 * `EntityManager` fork of a request that is already over — the same shape the
 * import-job runner (`ImportJobsService#start`) and the webhook nudge timer
 * (`DocSyncWebhookController#schedule`) use for their detached chains.
 */
@Injectable()
export class JourneyPhotoCaptureService {
  constructor(
    private readonly backfill: PhotoCaptureBackfillService,
    private readonly journey: JourneyDomainService,
    private readonly maps: MapsService,
    private readonly orm: MikroORM,
    private readonly uow: UnitOfWork,
  ) {}

  /**
   * Adds photos from a connected library, the one way the REST routes and the
   * MCP tool do it. One add per asset, all in one transaction; an asset the
   * caller may not add, or one already there, answers null and is left out of
   * what comes back. Once they are in, everyone on the journey is told it
   * changed (REST leaves out the tab that asked, which reads its own answer),
   * and the provider is asked when and where each was taken, detached.
   */
  async addProviderPhotos(
    target: ProviderPhotoTarget,
    userId: number,
    batch: ProviderPhotoBatch,
    sid?: string,
  ): Promise<unknown[]> {
    const { provider, caption, passphrase } = batch;
    const added = await this.uow.transactional(async () => {
      const photos: unknown[] = [];
      for (const [i, assetId] of batch.assetIds.entries()) {
        const mediaType = batch.mediaTypes?.[i] === 'video' ? 'video' : 'image';
        const photo =
          'entryId' in target
            ? await this.journey.addProviderPhoto(
                target.entryId,
                userId,
                provider,
                assetId,
                caption,
                passphrase,
                mediaType,
              )
            : await this.journey.addProviderPhotoToGallery(
                target.journeyId,
                userId,
                provider,
                assetId,
                undefined,
                passphrase,
                mediaType,
              );
        if (photo) photos.push(photo);
      }
      return photos;
    });
    if (added.length === 0) return added;
    const journeyId = 'entryId' in target ? await this.journey.journeyIdOfEntry(target.entryId) : target.journeyId;
    // An entry whose journey has gone since still gets its capture times, with no one to tell.
    if (journeyId !== null) await this.journey.broadcastJourneyEvent(journeyId, 'journey:updated', {}, sid);
    this.scheduleForJourney(journeyId, added, userId);
    return added;
  }

  /**
   * Provider photos just added to a journey, to its gallery or to one of its entries. Detached.
   * @txStandalone a detached refresh after the add has committed.
   */
  scheduleForJourney(journeyId: number | null, photos: readonly unknown[], userId: number): void {
    const ids = trekPhotoIdsOf(photos);
    if (!ids.length) return;
    void withRequestContext(this.orm, () => this.fill(journeyId, ids, userId));
  }

  /**
   * Photos uploaded from a device: read their capture times, and nothing more.
   * Detached.
   *
   * No refresh on purpose. The client sends one file per upload request and
   * reloads the journey itself once the last one is in, so an event per request
   * would only make every other open client reload once per uploaded photo. The
   * refresh belongs to the provider adds, where one request carries the batch.
   *
   * @txStandalone a detached refresh after the upload has committed.
   */
  scheduleUpload(photos: readonly unknown[], userId: number): void {
    const ids = trekPhotoIdsOf(photos);
    if (!ids.length) return;
    // The placing is chained inside the fork, not after it, so it runs in the
    // same context as the backfill rather than in whatever the caller had.
    void withRequestContext(this.orm, () => this.backfill.run(ids, userId).then(() => this.placeEntries(ids))).catch(
      (err) => console.error('[Journey] capture for uploads failed:', err instanceof Error ? err.message : err),
    );
  }

  /**
   * Entries waiting for a place get the one their photo was taken at (#1003),
   * then a name for it, then everyone on the journey hears about it. The name is
   * best effort: a geocoder that does not answer leaves a pin without a label,
   * which the owner can still name by hand.
   *
   * @txIndependent the placing commits before the geocoder is asked, and each
   * name is written on its own when its answer arrives.
   */
  async placeEntries(trekPhotoIds: number[]): Promise<number> {
    let placed: Awaited<ReturnType<JourneyDomainService['placeEntriesFromPhotos']>>;
    try {
      placed = await this.journey.placeEntriesFromPhotos(trekPhotoIds);
    } catch (err) {
      // An extra on top of the capture times: it must never cost the refresh below it.
      console.error('[Journey] placing entries from photos failed:', err instanceof Error ? err.message : err);
      return 0;
    }
    if (!placed.length) return 0;
    await Promise.all(
      placed.map(async (p) => {
        try {
          const where = await this.maps.reverseGeocode(String(p.lat), String(p.lng), undefined, {
            timeoutMs: 8000,
            locality: true,
          });
          const name = where.name || where.address;
          if (name) await this.journey.nameEntryLocation(p.entryId, name);
        } catch {
          /* the pin stands without a name */
        }
      }),
    );
    for (const journeyId of new Set(placed.map((p) => p.journeyId))) {
      await this.journey.broadcastJourneyEvent(journeyId, 'journey:photos:updated', {});
    }
    return placed.length;
  }

  /**
   * Awaitable form, so tests do not have to chase a floating promise. Answers
   * whether the batch changed anything. Never rejects: it runs detached, and a
   * refresh that could not be sent is a gallery that sorts right on the next
   * reload, not a failed add.
   */
  async fill(journeyId: number | null, trekPhotoIds: number[], userId: number): Promise<boolean> {
    let changed = false;
    try {
      changed = await this.backfill.run(trekPhotoIds, userId);
      // Its own broadcast when it places something; the one below is about order.
      await this.placeEntries(trekPhotoIds);
      if (changed && journeyId != null) {
        await this.journey.broadcastJourneyEvent(journeyId, 'journey:photos:updated', {});
      }
    } catch (err) {
      console.error(
        `[Journey] capture refresh failed for journey ${journeyId}:`,
        err instanceof Error ? err.message : err,
      );
    }
    return changed;
  }
}