From 78f600ebce4afff3b6e9263d28bbfd2f6386c839 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Fri, 14 Jun 2024 18:29:32 -0400 Subject: [PATCH] refactor(server): partner ids (#10321) --- server/src/services/asset.service.ts | 17 +++++++------- server/src/services/map.service.ts | 11 ++++----- server/src/services/search.service.ts | 14 +++++------ server/src/services/sync.service.ts | 9 ++++--- server/src/services/timeline.service.ts | 10 +++----- server/src/utils/asset.util.ts | 31 +++++++++++++++++++++++++ 6 files changed, 57 insertions(+), 35 deletions(-) diff --git a/server/src/services/asset.service.ts b/server/src/services/asset.service.ts index da333a9449..1c3e81be17 100644 --- a/server/src/services/asset.service.ts +++ b/server/src/services/asset.service.ts @@ -39,6 +39,7 @@ import { ILoggerRepository } from 'src/interfaces/logger.interface'; import { IPartnerRepository } from 'src/interfaces/partner.interface'; import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface'; import { IUserRepository } from 'src/interfaces/user.interface'; +import { getMyPartnerIds } from 'src/utils/asset.util'; import { usePagination } from 'src/utils/pagination'; export class AssetService { @@ -62,18 +63,16 @@ export class AssetService { } async getMemoryLane(auth: AuthDto, dto: MemoryLaneDto): Promise { - const currentYear = new Date().getFullYear(); - - // get partners id - const userIds: string[] = [auth.user.id]; - const partners = await this.partnerRepository.getAll(auth.user.id); - const partnersIds = partners - .filter((partner) => partner.sharedBy && partner.inTimeline) - .map((partner) => partner.sharedById); - userIds.push(...partnersIds); + const partnerIds = await getMyPartnerIds({ + userId: auth.user.id, + repository: this.partnerRepository, + timelineEnabled: true, + }); + const userIds = [auth.user.id, ...partnerIds]; const assets = await this.assetRepository.getByDayOfYear(userIds, dto); const groups: Record = {}; + const currentYear = new Date().getFullYear(); for (const asset of assets) { const yearsAgo = currentYear - asset.localDateTime.getFullYear(); if (!groups[yearsAgo]) { diff --git a/server/src/services/map.service.ts b/server/src/services/map.service.ts index b4ade666e9..453b76691a 100644 --- a/server/src/services/map.service.ts +++ b/server/src/services/map.service.ts @@ -7,6 +7,7 @@ import { ILoggerRepository } from 'src/interfaces/logger.interface'; import { IMapRepository } from 'src/interfaces/map.interface'; import { IPartnerRepository } from 'src/interfaces/partner.interface'; import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface'; +import { getMyPartnerIds } from 'src/utils/asset.util'; export class MapService { private configCore: SystemConfigCore; @@ -23,14 +24,10 @@ export class MapService { } async getMapMarkers(auth: AuthDto, options: MapMarkerDto): Promise { - const userIds: string[] = [auth.user.id]; - // TODO convert to SQL join + const userIds = [auth.user.id]; if (options.withPartners) { - const partners = await this.partnerRepository.getAll(auth.user.id); - const partnersIds = partners - .filter((partner) => partner.sharedBy && partner.sharedWith && partner.sharedById != auth.user.id) - .map((partner) => partner.sharedById); - userIds.push(...partnersIds); + const partnerIds = await getMyPartnerIds({ userId: auth.user.id, repository: this.partnerRepository }); + userIds.push(...partnerIds); } // TODO convert to SQL join diff --git a/server/src/services/search.service.ts b/server/src/services/search.service.ts index 1588662ef1..5010067a3f 100644 --- a/server/src/services/search.service.ts +++ b/server/src/services/search.service.ts @@ -24,6 +24,7 @@ import { IPartnerRepository } from 'src/interfaces/partner.interface'; import { IPersonRepository } from 'src/interfaces/person.interface'; import { ISearchRepository, SearchExploreItem } from 'src/interfaces/search.interface'; import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface'; +import { getMyPartnerIds } from 'src/utils/asset.util'; import { isSmartSearchEnabled } from 'src/utils/misc'; @Injectable() @@ -140,13 +141,12 @@ export class SearchService { } private async getUserIdsToSearch(auth: AuthDto): Promise { - const userIds: string[] = [auth.user.id]; - const partners = await this.partnerRepository.getAll(auth.user.id); - const partnersIds = partners - .filter((partner) => partner.sharedBy && partner.inTimeline) - .map((partner) => partner.sharedById); - userIds.push(...partnersIds); - return userIds; + const partnerIds = await getMyPartnerIds({ + userId: auth.user.id, + repository: this.partnerRepository, + timelineEnabled: true, + }); + return [auth.user.id, ...partnerIds]; } private mapResponse(assets: AssetEntity[], nextPage: string | null): SearchResponseDto { diff --git a/server/src/services/sync.service.ts b/server/src/services/sync.service.ts index 228ad65603..1a7a74d699 100644 --- a/server/src/services/sync.service.ts +++ b/server/src/services/sync.service.ts @@ -10,6 +10,7 @@ import { IAccessRepository } from 'src/interfaces/access.interface'; import { IAssetRepository } from 'src/interfaces/asset.interface'; import { IAuditRepository } from 'src/interfaces/audit.interface'; import { IPartnerRepository } from 'src/interfaces/partner.interface'; +import { getMyPartnerIds } from 'src/utils/asset.util'; import { setIsEqual } from 'src/utils/set'; const FULL_SYNC = { needsFullSync: true, deleted: [], upserted: [] }; @@ -46,11 +47,9 @@ export class SyncService { return FULL_SYNC; } - const authUserId = auth.user.id; - // app does not have the correct partners synced - const partner = await this.partnerRepository.getAll(authUserId); - const userIds = [authUserId, ...partner.filter((p) => p.sharedWithId == auth.user.id).map((p) => p.sharedById)]; + const partnerIds = await getMyPartnerIds({ userId: auth.user.id, repository: this.partnerRepository }); + const userIds = [auth.user.id, ...partnerIds]; if (!setIsEqual(new Set(userIds), new Set(dto.userIds))) { return FULL_SYNC; } @@ -81,7 +80,7 @@ export class SyncService { auth, stripMetadata: false, // ignore stacks for non partner users - withStack: a.ownerId === authUserId, + withStack: a.ownerId === auth.user.id, }), ), deleted, diff --git a/server/src/services/timeline.service.ts b/server/src/services/timeline.service.ts index 95c4081e6a..fa00fdc58e 100644 --- a/server/src/services/timeline.service.ts +++ b/server/src/services/timeline.service.ts @@ -6,6 +6,7 @@ import { TimeBucketAssetDto, TimeBucketDto, TimeBucketResponseDto } from 'src/dt import { IAccessRepository } from 'src/interfaces/access.interface'; import { IAssetRepository, TimeBucketOptions } from 'src/interfaces/asset.interface'; import { IPartnerRepository } from 'src/interfaces/partner.interface'; +import { getMyPartnerIds } from 'src/utils/asset.util'; export class TimelineService { private accessCore: AccessCore; @@ -43,14 +44,9 @@ export class TimelineService { if (userId) { userIds = [userId]; - if (dto.withPartners) { - const partners = await this.partnerRepository.getAll(auth.user.id); - const partnersIds = partners - .filter((partner) => partner.sharedBy && partner.sharedWith && partner.inTimeline) - .map((partner) => partner.sharedById); - - userIds.push(...partnersIds); + const partnerIds = await getMyPartnerIds({ userId: auth.user.id, repository: this.partnerRepository }); + userIds.push(...partnerIds); } } diff --git a/server/src/utils/asset.util.ts b/server/src/utils/asset.util.ts index a55156e679..d556c905b1 100644 --- a/server/src/utils/asset.util.ts +++ b/server/src/utils/asset.util.ts @@ -2,6 +2,7 @@ import { AccessCore, Permission } from 'src/cores/access.core'; import { BulkIdErrorReason, BulkIdResponseDto } from 'src/dtos/asset-ids.response.dto'; import { AuthDto } from 'src/dtos/auth.dto'; import { IAccessRepository } from 'src/interfaces/access.interface'; +import { IPartnerRepository } from 'src/interfaces/partner.interface'; import { setDifference, setUnion } from 'src/utils/set'; export interface IBulkAsset { @@ -91,3 +92,33 @@ export const removeAssets = async ( return results; }; + +export type PartnerIdOptions = { + userId: string; + repository: IPartnerRepository; + /** only include partners with `inTimeline: true` */ + timelineEnabled?: boolean; +}; +export const getMyPartnerIds = async ({ userId, repository, timelineEnabled }: PartnerIdOptions) => { + const partnerIds = new Set(); + const partners = await repository.getAll(userId); + for (const partner of partners) { + // ignore deleted users + if (!partner.sharedBy || !partner.sharedWith) { + continue; + } + + // wrong direction + if (partner.sharedWithId !== userId) { + continue; + } + + if (timelineEnabled && !partner.inTimeline) { + continue; + } + + partnerIds.add(partner.sharedById); + } + + return [...partnerIds]; +};