All files / lib/icloud/icloud-photos query-builder.ts

69.18% Statements 119/172
100% Branches 3/3
16.66% Functions 1/6
69.18% Lines 119/172

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 1721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 20x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x 1x 1x                                         1x 1x 1x 1x 1x 1x 1x            
/**
 * This file helps building queries for the iCloud Photos backend service
 */
 
import {iCloudAuth} from "../auth.js";
 
/**
 * All relevant record types for this application
 */
export const RECORD_TYPES = {
    "PHOTO_MASTER_RECORD": `CPLMaster`,
    "PHOTO_ASSET_RECORD": `CPLAsset`,
    "PHOTO_ALBUM_RECORD": `CPLAlbum`,
    "CONTAINER_RELATION": `CPLContainerRelation`, // Useless at the moment
    "PHOTO_RECORDS": `CPLContainerRelationLiveByPosition`, // Record CPLContainerRelationLiveByAssetDate
    "ALBUM_RECORDS": `CPLAlbumByPositionLive`,
    "INDEX_COUNT": `HyperionIndexCountLookup`,
    "ALL_PHOTOS": `CPLAssetAndMasterByAssetDateWithoutHiddenOrDeleted`, // CPLAssetAndMasterByAssetDateWithoutHiddenOrDeleted
};
 
/**
 * All relevant desired keys as provided to the backend
 */
const DESIRED_KEYS = {
    "RECORD_NAME": `recordName`,
    "IS_DELETED": `isDeleted`,
    "ORIGINAL_RESOURCE": `resOriginalRes`,
    "ORIGINAL_RESOURCE_FILE_TYPE": `resOriginalFileType`,
    "JPEG_RESOURCE": `resJPEGFullRes`,
    "JPEG_RESOURCE_FILE_TYPE": `resJPEGFullFileType`,
    "VIDEO_RESOURCE": `resVidFullRes`,
    "VIDEO_RESOURCE_FILE_TYPE": `resVidFullFileType`,
    "ENCODED_FILE_NAME": `filenameEnc`,
    "FAVORITE": `isFavorite`,
    "IS_HIDDEN": `isHidden`,
    "ADJUSTMENT_TYPE": `adjustmentType`,
    "MASTER_REF": `masterRef`,
};
 
/**
 * Desired keys, requested in queries in order for this application to be functioning
 */
export const QUERY_KEYS = [
    DESIRED_KEYS.RECORD_NAME,
    DESIRED_KEYS.ORIGINAL_RESOURCE,
    DESIRED_KEYS.ORIGINAL_RESOURCE_FILE_TYPE,
    DESIRED_KEYS.JPEG_RESOURCE,
    DESIRED_KEYS.JPEG_RESOURCE_FILE_TYPE,
    DESIRED_KEYS.VIDEO_RESOURCE,
    DESIRED_KEYS.VIDEO_RESOURCE_FILE_TYPE,
    DESIRED_KEYS.ENCODED_FILE_NAME,
    DESIRED_KEYS.IS_DELETED,
    DESIRED_KEYS.FAVORITE,
    DESIRED_KEYS.IS_HIDDEN,
    DESIRED_KEYS.MASTER_REF,
    DESIRED_KEYS.ADJUSTMENT_TYPE,
];
 
export enum Zones {
    Primary = `PrimaryLibrary`,
    Shared = `SharedLibrary`
}
 
/**
 * Returns the zoneID object needed as part of requests against the iCloudPhotos backend
 * @param zone - The zone details to get
 * @param auth - The auth object, holding zone information
 * @returns
 */
export function getZoneID(zone: Zones, auth: iCloudAuth) {
    if (zone === Zones.Shared) {
        return {
            "ownerRecordName": auth.iCloudPhotosAccount.shared.ownerName,
            "zoneName": auth.iCloudPhotosAccount.shared.zoneName,
            "zoneType": auth.iCloudPhotosAccount.shared.zoneType,
        };
    }
 
    return {
        "ownerRecordName": auth.iCloudPhotosAccount.primary.ownerName,
        "zoneName": auth.iCloudPhotosAccount.primary.zoneName,
        "zoneType": auth.iCloudPhotosAccount.primary.zoneType,
    };
}
 
/**
 * Builds a filter, that reduce the query based on the parentId
 * @param parentId - The parentId (record name) of the parent album
 * @returns The filter required to perform a query
 */
export function getParentFilterForParentId(parentId: string): any {
    return {
        "fieldName": `parentId`,
        "comparator": `EQUALS`,
        "fieldValue": {
            "value": parentId,
            "type": `STRING`,
        },
    };
}
 
/**
 * Builds a filter, that defines the sorting of the query
 * @param direction - Optionally the direction of the filter ('ASCENDING' by default)
 * @returns The filter required to perform a query
 */
export function getDirectionFilterForDirection(direction: string = `ASCENDING`): any {
    return {
        "fieldName": `direction`,
        "comparator": `EQUALS`,
        "fieldValue": {
            "value": direction,
            "type": `STRING`,
        },
    };
}
 
/**
 * Builds a filter, that will define the start rank of the query
 * @param startRank - The startRank/index of the query
 * @returns The filter object
 */
export function getStartRankFilterForStartRank(startRank: number): any {
    return {
        "fieldName": `startRank`,
        "comparator": `EQUALS`,
        "fieldValue": {
            "value": startRank,
            "type": `INT64`,
        },
    };
}
 
/**
 * Builds a filter, that will request the index count of a given folder, if parentId is undefined, all photos will be queried
 * @param parentId - The parentId of the folder
 * @returns The filter object
 */
export function getIndexCountFilter(parentId?: string): any {
    if (parentId) {
        return {
            "fieldName": `indexCountID`,
            "comparator": `IN`,
            "fieldValue": {
                "value": [`CPLContainerRelationNotDeletedByAssetDate:${parentId}`],
                "type": `STRING_LIST`,
            },
        };
    }

    return {
        "fieldName": `indexCountID`,
        "comparator": `IN`,
        "fieldValue": {
            "value": [`CPLAssetByAssetDateWithoutHiddenOrDeleted`],
            "type": `STRING_LIST`,
        },
    };
}
 
/**
 * Creates the operations field for isDeleted
 * @param _value - The value to set the field to - 1 by default
 * @returns The formatted field
 */
export function getIsDeletedField(value: number = 1): any {
    return {
        "isDeleted": {
            value,
        },
    };
}