From 61ee4ec15b7e09baba856251d08d428be2e3f43d Mon Sep 17 00:00:00 2001 From: abraunegg Date: Sun, 11 May 2025 07:51:33 +1000 Subject: [PATCH] Add 2 functions to check remoteItem JSON data is present (#3270) * Add 2 functions to check for the presence of required remoteItem elements to create a Shared Folder DB entries * Use 2 new functions to ensure data is available to create the required Shared Folder entries --- src/sync.d | 42 +++++++++++++++++++++++++++++++----------- src/util.d | 13 +++++++++++++ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/sync.d b/src/sync.d index 7489162c..1ce34612 100644 --- a/src/sync.d +++ b/src/sync.d @@ -3051,22 +3051,42 @@ class SyncEngine { // Using the onlineParentData JSON data make a DB record for this parent item so that it exists in the database Item sharedFolderDatabaseTie; - // What account type is this? This needs to be configured correctly so this can be queried correctly - if (appConfig.accountType == "personal") { - // OneDrive Personal JSON has this structure that we need to use - parentDriveId = onedriveJSONItem["remoteItem"]["parentReference"]["driveId"].str; - parentObjectId = onedriveJSONItem["remoteItem"]["id"].str; - } else { - // OneDrive Business|Sharepoint JSON has this structure that we need to use - parentDriveId = onedriveJSONItem["remoteItem"]["parentReference"]["driveId"].str; - parentObjectId = onedriveJSONItem["remoteItem"]["id"].str; + // A Shared Folder should have ["remoteItem"]["parentReference"] elements + bool remoteItemElementsExist = false; + + // Test that the required elements exist for Shared Folder DB entry creations to occur + if (isItemRemote(onedriveJSONItem)) { + // Required ["remoteItem"] element exists in the JSON data + if ((hasRemoteParentDriveId(onedriveJSONItem)) && (hasRemoteItemId(onedriveJSONItem))) { + // Required elements exist + remoteItemElementsExist = true; + // What account type is this? This needs to be configured correctly so this can be queried correctly + // - The setting of this is the 'same' for account types, but previously this was shown to need different data. Future code optimisation potentially here. + if (appConfig.accountType == "personal") { + // OneDrive Personal JSON has this structure that we need to use + parentDriveId = onedriveJSONItem["remoteItem"]["parentReference"]["driveId"].str; + parentObjectId = onedriveJSONItem["remoteItem"]["id"].str; + } else { + // OneDrive Business|Sharepoint JSON has this structure that we need to use + parentDriveId = onedriveJSONItem["remoteItem"]["parentReference"]["driveId"].str; + parentObjectId = onedriveJSONItem["remoteItem"]["id"].str; + } + } } - // Issue #3115 - Validate driveId length + // If the required elements do not exist, the Shared Folder DB elements cannot be created + if (!remoteItemElementsExist) { + // We cannot create the required entries in the database + if (debugLogging) {addLogEntry("Unable to create 'root' and 'Shared Folder' DB Tie Records in a consistent manner - required elements missing from provided JSON record" , ["debug"]);} + return; + } + + // Issue #3115 - Validate 'parentDriveId' length // What account type is this? if (appConfig.accountType == "personal") { - // Test driveId length and validation if the driveId we are testing is not equal to appConfig.defaultDriveId + // Test if the 'parentDriveId' is not equal to appConfig.defaultDriveId if (parentDriveId != appConfig.defaultDriveId) { + // Test 'parentDriveId' for length and validation - 15 character API bug parentDriveId = testProvidedDriveIdForLengthIssue(parentDriveId); } } diff --git a/src/util.d b/src/util.d index f4ab4662..358a9678 100644 --- a/src/util.d +++ b/src/util.d @@ -1128,6 +1128,19 @@ bool isItemRemote(const ref JSONValue item) { return ("remoteItem" in item) != null; } +// Check if ["remoteItem"]["parentReference"]["driveId"] exists +bool hasRemoteParentDriveId(const ref JSONValue item) { + return ("remoteItem" in item) && + ("parentReference" in item["remoteItem"]) && + ("driveId" in item["remoteItem"]["parentReference"]); +} + +// Check if ["remoteItem"]["id"] exists +bool hasRemoteItemId(const ref JSONValue item) { + return ("remoteItem" in item) && + ("id" in item["remoteItem"]); +} + bool isItemFile(const ref JSONValue item) { return ("file" in item) != null; }