* get parent for drive item

* seems to be working

* cleanup

* add tests

* add business onedrive tests

* update docs

* rm stray debugging code

* test
This commit is contained in:
Hong Ooi 2021-08-24 12:27:34 +10:00 коммит произвёл GitHub
Родитель 1a78851157
Коммит 8cd57b701a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 71 добавлений и 6 удалений

10
NEWS.md
Просмотреть файл

@ -1,3 +1,13 @@
# Microsoft365R 2.3.0.9000
## OneDrive/SharePoint
- Add a `get_parent_folder()` method for drive items, which returns the parent folder as another drive item. The parent of the root is itself.
## Teams
- Fix a bug where attaching a file to a Teams chat/channel message would fail if the file was a type recognised by Microsoft 365 (#73).
# Microsoft365R 2.3.0
## Outlook

Просмотреть файл

@ -1,5 +1,12 @@
build_chatmessage_body <- function(channel, body, content_type, attachments, inline, mentions)
{
get_upload_location <- function(item)
{
path <- item$get_parent_folder()$properties$webUrl
name <- item$properties$name
file.path(path, name)
}
call_body <- list(body=list(content=paste(body, collapse="\n"), contentType=content_type))
if(!is_empty(attachments))
{
@ -10,7 +17,7 @@ build_chatmessage_body <- function(channel, body, content_type, attachments, inl
list(
id=regmatches(et, regexpr("[A-Za-z0-9\\-]{10,}", et)),
name=att$properties$name,
contentUrl=att$properties$webUrl,
contentUrl=get_upload_location(att),
contentType="reference"
)
})

Просмотреть файл

@ -21,6 +21,7 @@
#' - `upload(src, dest, blocksize)`: Upload a file. Only applicable for a folder item.
#' - `create_folder(path)`: Create a folder. Only applicable for a folder item.
#' - `get_item(path)`: Get a child item (file or folder) under this folder.
#' - `get_parent_folder()`: Get the parent folder for this item. Returns the root folder for the root.
#' - `is_folder()`: Information function, returns TRUE if this item is a folder.
#'
#' @section Initialization:
@ -195,7 +196,7 @@ public=list(
if(!is.null(filter))
opts$`filter` <- filter
op <- sub("::", "", paste0(private$make_absolute_path(path), ":/children"))
op <- sub("root:/children", "root/children", paste0(private$make_absolute_path(path), ":/children"))
children <- call_graph_endpoint(self$token, op, options=opts, simplify=TRUE)
# get file list as a data frame, or return the iterator immediately if n is NULL
@ -232,6 +233,12 @@ public=list(
ms_drive_item$new(self$token, self$tenant, call_graph_endpoint(self$token, op))
},
get_parent_folder=function()
{
op <- private$make_absolute_path("..")
ms_drive_item$new(self$token, self$tenant, call_graph_endpoint(self$token, op))
},
create_folder=function(path)
{
private$assert_is_folder()
@ -313,7 +320,10 @@ public=list(
private=list(
make_absolute_path=function(dest)
# dest = . or '' --> this item
# dest = .. --> parent folder
# dest = (childname) --> path to named child
make_absolute_path=function(dest=".")
{
if(dest == ".")
dest <- ""
@ -327,9 +337,13 @@ private=list(
# in this case, assume the parent is the root folder
if(is.null(parent$path))
parent$path <- sprintf("/drives/%s/root:", parent$driveId)
file.path(parent$path, name)
if(dest != "..")
file.path(parent$path, name)
else parent$path
}
utils::URLencode(enc2utf8(sub("/$", "", file.path(op, dest))))
if(dest != "..")
op <- file.path(op, dest)
utils::URLencode(enc2utf8(sub(":?/?$", "", op)))
},
assert_is_folder=function()

Просмотреть файл

@ -35,6 +35,7 @@ Class representing an item (file or folder) in a OneDrive or SharePoint document
\item \code{upload(src, dest, blocksize)}: Upload a file. Only applicable for a folder item.
\item \code{create_folder(path)}: Create a folder. Only applicable for a folder item.
\item \code{get_item(path)}: Get a child item (file or folder) under this folder.
\item \code{get_parent_folder()}: Get the parent folder for this item. Returns the root folder for the root.
\item \code{is_folder()}: Information function, returns TRUE if this item is a folder.
}
}

Просмотреть файл

@ -59,23 +59,37 @@ test_that("Drive item methods work",
root <- od$get_item("/")
expect_is(root, "ms_drive_item")
expect_equal(root$properties$name, "root")
rootp <- root$get_parent_folder()
expect_is(rootp, "ms_drive_item")
expect_equal(rootp$properties$name, "root")
tmpname1 <- make_name(10)
folder1 <- root$create_folder(tmpname1)
expect_is(folder1, "ms_drive_item")
expect_true(folder1$is_folder())
folder1p <- folder1$get_parent_folder()
expect_equal(rootp$properties$name, "root")
tmpname2 <- make_name(10)
folder2 <- folder1$create_folder(tmpname2)
expect_is(folder2, "ms_drive_item")
expect_true(folder2$is_folder())
folder2p <- folder2$get_parent_folder()
expect_equal(folder2p$properties$name, folder1$properties$name)
src <- write_file()
expect_silent(file1 <- root$upload(src))
expect_is(file1, "ms_drive_item")
expect_false(file1$is_folder())
expect_error(file1$create_folder("bad"))
file1p <- file1$get_parent_folder()
expect_equal(file1p$properties$name, "root")
file1_0 <- root$get_item(basename(src))
expect_is(file1_0, "ms_drive_item")
expect_false(file1_0$is_folder())
@ -88,6 +102,9 @@ test_that("Drive item methods work",
expect_silent(file2 <- folder1$upload(src))
expect_is(file2, "ms_drive_item")
file2p <- file2$get_parent_folder()
expect_equal(file2p$properties$name, folder1$properties$name)
dest2 <- tempfile()
expect_silent(file2$download(dest2))
expect_true(files_identical(src, dest2))

Просмотреть файл

@ -72,22 +72,35 @@ test_that("Drive item methods work",
root <- od$get_item("/")
expect_is(root, "ms_drive_item")
rootp <- root$get_parent_folder()
expect_is(rootp, "ms_drive_item")
expect_equal(rootp$properties$name, "root")
tmpname1 <- make_name(10)
folder1 <- root$create_folder(tmpname1)
expect_is(folder1, "ms_drive_item")
expect_true(folder1$is_folder())
folder1p <- folder1$get_parent_folder()
expect_equal(rootp$properties$name, "root")
tmpname2 <- make_name(10)
folder2 <- folder1$create_folder(tmpname2)
expect_is(folder2, "ms_drive_item")
expect_true(folder2$is_folder())
folder2p <- folder2$get_parent_folder()
expect_equal(folder2p$properties$name, folder1$properties$name)
src <- write_file()
expect_silent(file1 <- root$upload(src))
expect_is(file1, "ms_drive_item")
expect_false(file1$is_folder())
expect_error(file1$create_folder("bad"))
file1p <- file1$get_parent_folder()
expect_equal(file1p$properties$name, "root")
file1_0 <- root$get_item(basename(src))
expect_is(file1_0, "ms_drive_item")
expect_false(file1_0$is_folder())
@ -100,6 +113,9 @@ test_that("Drive item methods work",
expect_silent(file2 <- folder1$upload(src))
expect_is(file2, "ms_drive_item")
file2p <- file2$get_parent_folder()
expect_equal(file2p$properties$name, folder1$properties$name)
dest2 <- tempfile()
expect_silent(file2$download(dest2))
expect_true(files_identical(src, dest2))

Просмотреть файл

@ -56,7 +56,7 @@ test_that("Channel methods work",
expect_is(msg2, "ms_chat_message")
msg3_body <- sprintf("Test message with attachment: %s", make_name(5))
f0 <- write_file()
f0 <- write_file(fname=tempfile(tmpdir=tempdir(), fileext=".xlsx"))
msg3 <- chan$send_message(msg3_body, attachments=f0)
expect_is(msg3, "ms_chat_message")
expect_true(nzchar(msg3$properties$attachments[[1]]$contentUrl))