From 3124b2dcf211da226355071ceef8d3ca9c3edb33 Mon Sep 17 00:00:00 2001 From: abraunegg Date: Thu, 10 Mar 2022 07:00:07 +1100 Subject: [PATCH] Implement feature request --modified-by (#1869) * Implement --modified-by to display who last modified a file and when the modification was done --- src/config.d | 5 +++++ src/main.d | 15 ++++++++++++--- src/onedrive.d | 2 +- src/sync.d | 38 +++++++++++++++++++++++++++++--------- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/config.d b/src/config.d index 21534ce2..f7149918 100644 --- a/src/config.d +++ b/src/config.d @@ -286,6 +286,7 @@ final class Config stringValues["create_share_link"] = ""; stringValues["destination_directory"] = ""; stringValues["get_file_link"] = ""; + stringValues["modified_by"] = ""; stringValues["get_o365_drive_id"] = ""; stringValues["remove_directory"] = ""; stringValues["single_directory"] = ""; @@ -388,6 +389,9 @@ final class Config "min-notify-changes", "Minimum number of pending incoming changes necessary to trigger a desktop notification", &longValues["min_notify_changes"], + "modified-by", + "Display the last modified by details of a given path", + &stringValues["modified_by"], "monitor|m", "Keep monitoring for local and remote changes", &boolValues["monitor"], @@ -721,6 +725,7 @@ void outputLongHelp(Option[] opt) "--get-O365-drive-id", "--log-dir", "--min-notify-changes", + "--modified-by", "--monitor-interval", "--monitor-log-frequency", "--monitor-fullscan-frequency", diff --git a/src/main.d b/src/main.d index 49b2375f..90e64e28 100644 --- a/src/main.d +++ b/src/main.d @@ -726,7 +726,7 @@ int main(string[] args) // create-directory, remove-directory, source-directory, destination-directory // these are activities that dont perform a sync, so to not generate an error message for these items either - if (((cfg.getValueString("create_directory") != "") || (cfg.getValueString("remove_directory") != "")) || ((cfg.getValueString("source_directory") != "") && (cfg.getValueString("destination_directory") != "")) || (cfg.getValueString("get_file_link") != "") || (cfg.getValueString("create_share_link") != "") || (cfg.getValueString("get_o365_drive_id") != "") || cfg.getValueBool("display_sync_status") || cfg.getValueBool("list_business_shared_folders")) { + if (((cfg.getValueString("create_directory") != "") || (cfg.getValueString("remove_directory") != "")) || ((cfg.getValueString("source_directory") != "") && (cfg.getValueString("destination_directory") != "")) || (cfg.getValueString("get_file_link") != "") || (cfg.getValueString("modified_by") != "") || (cfg.getValueString("create_share_link") != "") || (cfg.getValueString("get_o365_drive_id") != "") || cfg.getValueBool("display_sync_status") || cfg.getValueBool("list_business_shared_folders")) { performSyncOK = true; } @@ -996,10 +996,19 @@ int main(string[] args) return EXIT_SUCCESS; } - // Are we obtaining the URL path for a synced file? + // --get-file-link - Are we obtaining the URL path for a synced file? if (cfg.getValueString("get_file_link") != "") { // Query OneDrive for the file link - sync.queryOneDriveForFileURL(cfg.getValueString("get_file_link"), syncDir); + sync.queryOneDriveForFileDetails(cfg.getValueString("get_file_link"), syncDir, "URL"); + // Exit application + // Use exit scopes to shutdown API + return EXIT_SUCCESS; + } + + // --modified-by - Are we listing the modified-by details of a provided path? + if (cfg.getValueString("modified_by") != "") { + // Query OneDrive for the file link + sync.queryOneDriveForFileDetails(cfg.getValueString("modified_by"), syncDir, "ModifiedBy"); // Exit application // Use exit scopes to shutdown API return EXIT_SUCCESS; diff --git a/src/onedrive.d b/src/onedrive.d index df9f5448..0fc82ab0 100644 --- a/src/onedrive.d +++ b/src/onedrive.d @@ -853,7 +853,7 @@ final class OneDriveApi checkAccessTokenExpired(); const(char)[] url; url = driveByIdUrl ~ driveId ~ "/items/" ~ id; - url ~= "?select=size,malware,file,webUrl"; + url ~= "?select=size,malware,file,webUrl,lastModifiedBy,lastModifiedDateTime"; return get(url); } diff --git a/src/sync.d b/src/sync.d index 2f714ca1..d9e4c65c 100644 --- a/src/sync.d +++ b/src/sync.d @@ -6104,8 +6104,8 @@ final class SyncEngine } } - // Query OneDrive for a URL path of a file - void queryOneDriveForFileURL(string localFilePath, string syncDir) + // Query OneDrive for file details of a given path + void queryOneDriveForFileDetails(string localFilePath, string syncDir, string outputType) { // Query if file is valid locally if (exists(localFilePath)) { @@ -6127,21 +6127,41 @@ final class SyncEngine displayOneDriveErrorMessage(e.msg, getFunctionName!({})); return; } - - if ((fileDetails.type() == JSONType.object) && ("webUrl" in fileDetails)) { - // Valid JSON object - writeln(fileDetails["webUrl"].str); + + // debug output of response + log.vdebug("API Response: ", fileDetails); + + // What sort of response to we generate + // --get-file-link response + if (outputType == "URL") { + if ((fileDetails.type() == JSONType.object) && ("webUrl" in fileDetails)) { + // Valid JSON object + writeln(fileDetails["webUrl"].str); + } } + + // --modified-by response + if (outputType == "ModifiedBy") { + if ((fileDetails.type() == JSONType.object) && ("lastModifiedBy" in fileDetails)) { + // Valid JSON object + writeln("Last modified: ", fileDetails["lastModifiedDateTime"].str); + writeln("Last modified by: ", fileDetails["lastModifiedBy"]["user"]["displayName"].str); + // if 'email' provided, add this to the output + if ("email" in fileDetails["lastModifiedBy"]["user"]) { + writeln("Email Address: ", fileDetails["lastModifiedBy"]["user"]["email"].str); + } + } + } } } - // was file found? + // was path found? if (!fileInDB) { // File has not been synced with OneDrive - log.error("File has not been synced with OneDrive: ", localFilePath); + log.error("Path has not been synced with OneDrive: ", localFilePath); } } else { // File does not exist locally - log.error("File not found on local system: ", localFilePath); + log.error("Path not found on local system: ", localFilePath); } }