Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions to manage comments #15

Merged
merged 7 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: objr
Title: Wrapper for Objective APIs
Version: 0.0.0.9001
Version: 0.0.0.9002
Authors@R: c(
person("Scottish Government", , , "[email protected]", role = "cph"),
person("Alice", "Hannah", , "[email protected]", c("aut", "cre"))
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

export(allow_bypass_2fa)
export(asset_info)
export(comments)
export(create_folder)
export(download_file)
export(my_user_id)
export(my_workspaces)
export(new_reply)
export(new_thread)
export(new_version)
export(objr)
export(objr_auth)
Expand Down
3 changes: 2 additions & 1 deletion R/assets.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ workspace_assets <- function(workspace_uuid,
url_query = list(workspaceUuid = workspace_uuid,
type = type,
page = page,
size = size)
size = size),
use_proxy = use_proxy
)

content <-
Expand Down
177 changes: 177 additions & 0 deletions R/comments.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#' Get comments for workspaces of current user
#'
#' @param created_after Date (and optionally time) to filter comments created
#' since this date/time. If a time is not supplied, all comments made on this
#' day will be included.
#' @param thread_uuid UUID of thread to filter by
#' @param mention_uuid UUID of user to filter comments where mentioned
#' @inheritParams objr
#' @inheritParams my_workspaces
#'
#' @return Data frame
#'
#' @export

comments <- function(created_after = NULL,
thread_uuid = NULL,
mention_uuid = NULL,
workgroup_uuid = NULL,
page = NULL,
size = NULL,
use_proxy = FALSE) {

response <- objr(
endpoint = "comments",
url_query = list(createdAfter = convert_to_epoch(created_after),
threadUuid = thread_uuid,
mentionUuid = mention_uuid,
workgroupUuid = workgroup_uuid,
page = page,
size = size),
use_proxy = use_proxy
)

content <-
httr2::resp_body_json(response)$content |>
lapply(
\(content) {
data.frame(
type = content$commentType,
text = content$commentText,
author = paste(content$creator$givenName,
content$creator$familyName),
created_time = as.POSIXct(content$createdTime / 1000,
origin = "1970-01-01"),
thread_uuid = content$thread$uuid,
workspace_name = content$workspace$name,
workspace_uuid = content$workspace$uuid
)
}
)

Reduce(dplyr::bind_rows, content)

}


#' Create a new thread
#'
#' @param workspace_uuid UUID of workspace
#' @param text Character string to include in body of thread
#' @param mentioned_assets UUID(s) of asset(s) to mention
#' @param mentioned_users UUID(s) of user(s) to mention
#' @inheritParams objr
#'
#' @return API response (invisibly)
#'
#' @export

new_thread <- function(workspace_uuid,
text,
mentioned_assets = NULL,
mentioned_users = NULL,
use_proxy = FALSE) {

response <- objr(
endpoint = "threads",
method = "POST",
body = list(
workspaceUuid = workspace_uuid,
text = text,
mentionedAssets = list(mentioned_assets),
mentionedUsers = list(mentioned_users)
),
use_proxy = use_proxy
)

if(httr2::resp_status(response) == 200) {
cli::cli_alert_success(
"New thread created."
)
}

invisible(response)

}


#' Create a new reply to a thread
#'
#' @param thread_uuid UUID of thread to reply to
#' @inheritParams new_thread
#' @inheritParams objr
#'
#' @return API response (invisibly)
#'
#' @export

new_reply <- function(thread_uuid,
text,
mentioned_assets = NULL,
mentioned_users = NULL,
use_proxy = FALSE) {

response <- objr(
endpoint = "replies",
method = "POST",
body = list(
threadUuid = thread_uuid,
text = text,
mentionedAssets = list(mentioned_assets),
mentionedUsers = list(mentioned_users)
),
use_proxy = use_proxy
)

if(httr2::resp_status(response) == 200) {
cli::cli_alert_success(
"New reply created."
)
}

invisible(response)

}


#' Convert date or datetime object to number of milliseconds from epoch
#'
#' @param date_time Date or datetime.
#'
#' @details
#' * If only a date is supplied, a time of 00:00:01 will be added.
#' * If NULL is supplied, NULL is returned.
#' * If an invalid value is supplied (not date, datetime or NULL), an error will
#' be produced.
#'
#' @return Integer
#'
#' @examples
#' convert_to_epoch(as.POSIXct("2024-01-01 09:00:00"))
#'
#' @noRd

convert_to_epoch <- function(date_time) {

# Check correct class if supplied
stopifnot(
"`created_after` must be date or datetime class" =
is.null(date_time) |
any(class(date_time) %in% c("Date", "POSIXct", "POSIXt"))
)

if(!is.null(date_time)) {

# Add time if only date supplied
if(any(class(date_time) == "Date")) {
date_time <- as.POSIXct(paste(date_time, "00:00:01"))
}

# Convert to epoch
as.integer(date_time) * 1000

} else {
NULL
}

}
3 changes: 2 additions & 1 deletion R/my_workspaces.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ my_workspaces <- function(workgroup_uuid = NULL,
endpoint = "myworkspaces",
url_query = list(workgroupUuid = workgroup_uuid,
page = page,
size = size)
size = size),
use_proxy = use_proxy
)

content <-
Expand Down
3 changes: 2 additions & 1 deletion R/participants.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ participants <- function(workspace_uuid, use_proxy = FALSE) {

response <- objr(
endpoint = "participants",
url_query = list(workspaceUuid = workspace_uuid)
url_query = list(workspaceUuid = workspace_uuid),
use_proxy = use_proxy
)

content <-
Expand Down
7 changes: 7 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ reference:
- upload_file
- new_version

- subtitle: Comments
desc: View comments and create new threads and replies in workspaces
contents:
- comments
- new_thread
- new_reply

- title: API authentication
contents:
- objr_auth
Expand Down
39 changes: 39 additions & 0 deletions man/comments.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions man/new_reply.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions man/new_thread.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/testthat/api/comments.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
structure(list(method = "GET", url = "https://api/comments",
status_code = 200L, headers = structure(list(Date = "Mon, 12 Aug 2024 13:01:51 GMT",
`Content-Type` = "application/json", `Transfer-Encoding` = "chunked",
Connection = "keep-alive", `X-CONNECT-MDC` = "apimQTuvVAA",
`X-Frame-Options` = "deny", `X-XSS-Protection` = "1; mode=block",
`Cache-Control` = "no-cache, no-store", Expires = "0",
Pragma = "no-cache", `Strict-Transport-Security` = "max-age=31536000 ; includeSubDomains",
`Content-Security-Policy` = "script-src 'self' 'unsafe-inline'",
`X-Content-Type-Options` = "nosniff", `Referrer-Policy` = "strict-origin-when-cross-origin",
`Feature-Policy` = "vibrate 'none'; geolocation 'none'",
Authorization = "REDACTED", `Set-Cookie` = "REDACTED"), class = "httr2_headers"),
body = charToRaw("{\"content\":[{\"model\":\"Comment\",\"uuid\":\"test_uuid\",\"threadUuid\":\"test_thread_uuid\",\"workspaceUuid\":\"test_workspace_uuid\",\"commentType\":\"REPLY\",\"commentText\":\"test_text\",\"createdTime\":1723457395000,\"thread\":{\"model\":\"Concise Thread\",\"uuid\":\"test_uuid\",\"replyCount\":1,\"workspaceUuid\":\"test_workspace_uuid\",\"createdTime\":1723457374000,\"updatedTime\":1723457395000},\"creator\":{\"model\":\"Concise User\",\"uuid\":\"test_creator_uuid\",\"givenName\":\"test_creator1\",\"familyName\":\"test_creator2\"},\"workspace\":{\"model\":\"Concise Workspace\",\"uuid\":\"test_workspace_uuid\",\"name\":\"test_workspace_name\",\"workgroupUuid\":\"test_workgroup_uuid\"},\"mentionedUsers\":[],\"mentionedAssets\":[]}],\"metadata\":{\"totalElements\":1,\"totalPages\":1,\"page\":0,\"offset\":0}}"),
cache = new.env(parent = emptyenv())), class = "httr2_response")
2 changes: 1 addition & 1 deletion tests/testthat/api/myworkspaces-0ccf8b.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
structure(list(method = "GET", url = "https://api/myworkspaces?workgroupUuid=df05-8377-f703-4ce8-a163-9104-2bc6-64e5",
structure(list(method = "GET", url = "https://api/myworkspaces?workgroupUuid=test_workgroup_uuid",
status_code = 200L, headers = structure(list(Date = "Thu, 09 May 2024 15:49:51 GMT",
`Content-Type` = "application/json", `Transfer-Encoding` = "chunked",
Connection = "keep-alive", `X-CONNECT-MDC` = "apipGcxcEvR",
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/api/replies-9667ef-POST.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
structure(list(method = "POST", url = "https://api/replies",
status_code = 200L, headers = structure(list(Date = "Mon, 12 Aug 2024 13:17:30 GMT",
`Content-Type` = "application/json", `Transfer-Encoding` = "chunked",
Connection = "keep-alive", `X-CONNECT-MDC` = "apisnEiGPzU",
`X-Frame-Options` = "deny", `X-XSS-Protection` = "1; mode=block",
`Cache-Control` = "no-cache, no-store", Expires = "0",
Pragma = "no-cache", `Strict-Transport-Security` = "max-age=31536000 ; includeSubDomains",
`Content-Security-Policy` = "script-src 'self' 'unsafe-inline'",
`X-Content-Type-Options` = "nosniff", `Referrer-Policy` = "strict-origin-when-cross-origin",
`Feature-Policy` = "vibrate 'none'; geolocation 'none'",
Authorization = "REDACTED", `Set-Cookie` = "REDACTED"), class = "httr2_headers"),
body = charToRaw("{\"model\":\"Comment\",\"uuid\":\"test_uuid\"}"),
cache = new.env(parent = emptyenv())), class = "httr2_response")
13 changes: 13 additions & 0 deletions tests/testthat/api/threads-4aa328-POST.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
structure(list(method = "POST", url = "https://api/threads",
status_code = 200L, headers = structure(list(Date = "Mon, 12 Aug 2024 13:11:58 GMT",
`Content-Type` = "application/json", `Transfer-Encoding` = "chunked",
Connection = "keep-alive", `X-CONNECT-MDC` = "apiPOKLkfNY",
`X-Frame-Options` = "deny", `X-XSS-Protection` = "1; mode=block",
`Cache-Control` = "no-cache, no-store", Expires = "0",
Pragma = "no-cache", `Strict-Transport-Security` = "max-age=31536000 ; includeSubDomains",
`Content-Security-Policy` = "script-src 'self' 'unsafe-inline'",
`X-Content-Type-Options` = "nosniff", `Referrer-Policy` = "strict-origin-when-cross-origin",
`Feature-Policy` = "vibrate 'none'; geolocation 'none'",
Authorization = "REDACTED", `Set-Cookie` = "REDACTED"), class = "httr2_headers"),
body = charToRaw("{\"model\":\"Thread\",\"uuid\":\"test_uuid\"}"),
cache = new.env(parent = emptyenv())), class = "httr2_response")
Loading
Loading