Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

1.3 KB
Raw Blame
import { FILE_REPOSITORY } from '../constants/operations.js'
import { CACHE_INDEXING_EVENT_PREFIX } from '../constants/indexing.js'
import { SpaceEnv } from '../../spaces/models/space-env.model.js'
import { SpaceToFileRepository } from '../events/files-events.utils.js'

export function genIndexingKey(id: number, type: FILE_REPOSITORY, sep = '_'): string {
  return `${type}${sep}${id}`
}

export function genRunId(): string {
  return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`
}

export function indexingUpdateCacheKeysFromSpace(userId: number, space: SpaceEnv): string[] {
  const cacheKeys: string[] = []
  if (space.inSharesRepository && !space.root!.externalPath) {
    // shares that are linked to a space must also be indexed
    cacheKeys.push(`${CACHE_INDEXING_EVENT_PREFIX}-${genIndexingKey(space.id, FILE_REPOSITORY.SHARE, '-')}`)
  } else if (space.inFilesRepository && space.root?.owner?.login) {
    // space where the user’s root file is anchored
    cacheKeys.push(`${CACHE_INDEXING_EVENT_PREFIX}-${genIndexingKey(space.id, FILE_REPOSITORY.SPACE, '-')}`)
  }
  const repository = SpaceToFileRepository(userId, space)
  if (repository !== null) {
    cacheKeys.push(`${CACHE_INDEXING_EVENT_PREFIX}-${genIndexingKey(repository.id, repository.type, '-')}`)
  }
  return cacheKeys
}