From fc48e6040a5c02f33ba26625c818af1676e53d70 Mon Sep 17 00:00:00 2001 From: Hong Ooi Date: Thu, 30 Sep 2021 04:33:50 +1000 Subject: [PATCH] Shared folder fix (#90) * fix methods for shared drive items * update news * add explanatory comment * keep all properties consistent --- NEWS.md | 1 + R/ms_drive.R | 2 +- R/ms_drive_item.R | 25 ++++++++++++++++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 3d3d083..453b3fe 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # Microsoft365R 2.3.1.9000 +- Fix broken methods for accessing items in shared OneDrive/SharePoint folders (#89). - Fix a bug in sending file attachments in Teams chats (#87). # Microsoft365R 2.3.1 diff --git a/R/ms_drive.R b/R/ms_drive.R index b06d087..2ef7403 100644 --- a/R/ms_drive.R +++ b/R/ms_drive.R @@ -207,7 +207,7 @@ public=list( } df$remoteItem <- lapply(seq_len(nrow(df)), - function(i) ms_drive_item$new(self$token, self$tenant, df$remoteItem[i, ])) + function(i) ms_drive_item$new(self$token, self$tenant, df$remoteItem[i, ], remote=TRUE)) switch(info, partial=df[c("name", "size", "isdir", "remoteItem")], diff --git a/R/ms_drive_item.R b/R/ms_drive_item.R index 7bb0502..f76bf36 100644 --- a/R/ms_drive_item.R +++ b/R/ms_drive_item.R @@ -108,9 +108,10 @@ ms_drive_item <- R6::R6Class("ms_drive_item", inherit=ms_object, public=list( - initialize=function(token, tenant=NULL, properties=NULL) + initialize=function(token, tenant=NULL, properties=NULL, remote=FALSE) { self$type <- "drive item" + private$remote <- remote private$api_type <- file.path("drives", properties$parentReference$driveId, "items") super$initialize(token, tenant, properties) }, @@ -320,6 +321,9 @@ public=list( private=list( + # flag: whether this object is a shared file/folder on another drive + remote=NULL, + # dest = . or '' --> this item # dest = .. --> parent folder # dest = (childname) --> path to named child @@ -327,14 +331,18 @@ private=list( { if(dest == ".") dest <- "" + + # this is needed to find the correct parent folder for a shared item + if(private$remote) + private$normalise_remote_item() + parent <- self$properties$parentReference name <- self$properties$name op <- if(name == "root") file.path("drives", parent$driveId, "root:") else { - # have to infer the parent path if we got this item as a Teams channel folder - # in this case, assume the parent is the root folder + # null path means parent is the root folder if(is.null(parent$path)) parent$path <- sprintf("/drives/%s/root:", parent$driveId) if(dest != "..") @@ -356,6 +364,17 @@ private=list( { if(self$is_folder()) stop("This method is only applicable for a file item", call.=FALSE) + }, + + normalise_remote_item=function() + { + # if this was a shared item, replace properties with those obtained from original drive + if(!private$remote) + return(NULL) + parent <- self$properties$parentReference + op <- file.path("drives", parent$driveId, "items", self$properties$id) + self$properties <- call_graph_endpoint(self$token, op) + private$remote <- FALSE } ))