Skip to content

Commit

Permalink
Merge pull request #202 from mrkaye97/3.4.0
Browse files Browse the repository at this point in the history
slackr 3.4.0
  • Loading branch information
mrkaye97 authored Sep 27, 2024
2 parents e583ec5 + 90e6489 commit 266f493
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 209 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Package
Package: slackr
Title: Send Messages, Images, R Objects and Files to 'Slack'
Channels/Users
Version: 3.3.2
Version: 3.4.0
Authors@R: c(
person("Matt", "Kaye", role = c("aut", "cre"), email = "[email protected]", comment = c(website = "matthewrkaye.com")),
person("Bob", "Rudis",role = c("aut")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# slackr 3.4.0

* Fixes the internals of `files_upload` to use new Slack API and migrate off of [deprecated `files.upload` method](files.upload method)

# slackr 3.3.1
* Fixes a bug in `slackr_history` where the function fails to infer `posted_from_time` if not provided
* Adds default `message_count = 100` to `slackr_history`
Expand Down
45 changes: 39 additions & 6 deletions R/call_slack_internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,52 @@ files_upload <- function(
token,
...
) {
r <- call_slack_api(
"/api/files.upload",
initial_upload_response <- call_slack_api(
"/api/files.getUploadURLExternal",
.method = POST,
token = token,
body = list(
file = upload_file(file),
initial_comment = initial_comment,
channels = paste(channels, collapse = ","),
filename=file,
length=file.size(file)
)
)

initial_upload_content <- content(initial_upload_response)

upload_url <- initial_upload_content$upload_url
file_id <- initial_upload_content$file_id
channel <- slackr_chtrans(channels, token)

httr::POST(
url=upload_url,
body=upload_file(file)
)

kwargs <- list(...)
title <- kwargs$title

if (is.null(title)) {
inner <- list(id = file_id)
} else {
inner <- list(id = file_id, title = title)
}

response <- httr::POST(
"https://slack.com/api/files.completeUploadExternal",
add_headers(
.headers = c(Authorization = paste("Bearer", token))
),
httr::content_type_json(),
encode="json",
body = list(
files = list(inner),
channel_id=channel,
initial_comment=initial_comment,
...
)
)

invisible(content(r))
invisible(content(response))
}


Expand Down
Binary file removed logo.png
Binary file not shown.
35 changes: 0 additions & 35 deletions tests/testthat/test-dev-tex.R

This file was deleted.

166 changes: 0 additions & 166 deletions tests/testthat/test-history.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,169 +19,3 @@ test_that("slackr_history correctly retrieves post", {
)
})

test_that("slackr_history works when posted_from and posted_to are specified", {
skip_on_cran()

lapply(
c(TRUE, FALSE),
function(paginate) {
post <- slackr_msg("History posted_from posted_to test")

history <- slackr_history(
message_count = 1,
posted_from_time = post$ts,
posted_to_time = post$ts,
paginate = paginate
)

expect_identical(post$message$text, history$text)
expect_identical(post$message$subtype, history$subtype)
expect_identical(post$message$ts, history$ts)
expect_identical(post$message$bot_id, history$bot_id)
expect_identical(post$message$app_id, history$app_id)
}
)
})

test_that("slackr_history works with duration specified", {
skip_on_ci()
skip_on_cran()

post1 <- slackr_msg("History test post 1.")

Sys.sleep(5)

post2 <- slackr_msg("History test post 2.")

Sys.sleep(1.50)
to_ts <- as.numeric(Sys.time())

## Check the past seven seconds
dur_s <- 7
dur_ms <- dur_s * 1000
dur_hours <- dur_s / 3600

history <- slackr_history(
message_count = 1000,
posted_to_time = to_ts,
duration = dur_hours
)

expect_gte(nrow(history), 2)
expect_gte(min(as.numeric(history$ts)), to_ts - dur_ms)

history_limited <- slackr_history(
message_count = 1000,
posted_to_time = to_ts,
duration = dur_hours / 2
)

expect_gte(as.numeric(post2$ts), max(as.numeric(history_limited$ts)))
expect_gte(nrow(history_limited), 1)
})

test_that("slackr_history works when posted_from and posted_to are specified for multiple posts", {
skip_on_cran()

lapply(
c(TRUE, FALSE),
function(paginate) {
post1 <- slackr_msg("History test post 1.")

Sys.sleep(1)

post2 <- slackr_msg("History test post 2.")
post3 <- slackr_csv(iris, initial_comment = "History test post 3.")
post4 <- slackr_msg("History test post 4.")

all_history <- slackr_history(
message_count = 1000,
posted_from_time = post1$ts,
posted_to_time = post4$ts,
paginate = paginate
)

expect_gte(nrow(all_history), 4)
expect_gte(length(Filter(function(.x) !is.null(.x), all_history$files)), 1)
expect_equal(min(all_history$ts), post1$ts)
expect_equal(max(all_history$ts), post4$ts)

all_history <- slackr_history(
message_count = 1000,
posted_from_time = post1$ts,
posted_to_time = post2$ts,
paginate = paginate
)

expect_gte(nrow(all_history), 2)
expect_lte(min(as.numeric(all_history$ts)), as.numeric(post1$ts))
expect_gte(max(as.numeric(all_history$ts)), as.numeric(post2$ts))
}
)
})

test_that("Specifying post times in slackr_history correctly limits time window", {
skip_on_cran()

lapply(
c(TRUE, FALSE),
function(paginate) {
post1 <- slackr_msg("History test post 1.")

Sys.sleep(1)

post2 <- slackr_msg("History test post 2.")

Sys.sleep(1)
post3 <- slackr_msg("History test post 4.")

all_history <- slackr_history(
message_count = 1000,
posted_from_time = post1$ts,
posted_to_time = post2$ts,
paginate = paginate
)

expect_lte(min(as.numeric(all_history$ts)), as.numeric(post1$ts))
expect_gte(max(as.numeric(all_history$ts)), as.numeric(post2$ts))
expect_lt(as.numeric(max(all_history$ts)), as.numeric(post3$ts))
}
)
})

test_that("Expect warning for too many params", {
skip_on_cran()

from_time <- slackr_msg("History warning test start")$ts
Sys.sleep(1)
slackr_msg("History warning test")
Sys.sleep(1)
to_time <- slackr_msg("History warning test end")$ts

expect_silent(
slackr_history(
posted_to_time = to_time,
duration = 2 * (as.numeric(to_time) - as.numeric(from_time)) / (60 * 60)
)
)

expect_warning(
slackr_history(
posted_from_time = from_time,
posted_to_time = to_time,
duration = 2 * (as.numeric(to_time) - as.numeric(from_time)) / (60 * 60)
),
"You specified all three of"
)
})

test_that("Error if trying to paginate with no start time", {
skip_on_cran()

expect_error(
slackr_history(
paginate = TRUE
),
"To use pagination with `slackr_history`, you must specify a value for `posted_from_time`"
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test_that("Channels works", {

expect_equal(
ncol(channels),
28
29
)

expect_gte(
Expand Down

0 comments on commit 266f493

Please sign in to comment.