Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

Commit

Permalink
Add the auth_clean functions
Browse files Browse the repository at this point in the history
 [skip ci]
  • Loading branch information
llrs committed Nov 20, 2023
1 parent 8c6380b commit 8e89e7a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ S3method(rules,rules)
export(as_screenname)
export(as_userid)
export(auth_as)
export(auth_clean)
export(auth_get)
export(auth_has_default)
export(auth_list)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# rtweet (development version)

* Fix problem with `auth_sitrep()` not correctly handling old tokens.

# rtweet 1.2.1

* Fix `auth_sitrep()` to work well with OAuth2 tokens.
Expand Down
42 changes: 42 additions & 0 deletions R/auth_clean.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' Remove tokens
#'
#' If there is a file with saved tokens it will delete it.
#'
#' This functions helps to comply with CRAN policy to remove files created by
#' the package.
#' @param old A logical value if you want to remove old tokens.
#' @param new A logical value if you want to remove new tokens.
#'
#' @return An invisible logical value showing the success of the operation.
#' If no tokens need to be deleted it will return FALSE too.
#' `NULL` if there is nothing to do.
#' @export
#' @examples
#' auth_clean(FALSE, FALSE)
auth_clean <- function(old = TRUE, new = FALSE) {
stopifnot(is_logical(old))
stopifnot(is_logical(new))

if (isFALSE(old) && isFALSE(new)) {
inform(c("Nothing to do",
i = "Did you meant to set `old = TRUE`?"))
return(invisible(NULL))
}
old_tokens <- find_old_tokens()
tools_tokens <- find_tools_tokens()
all_tokens_files <- c(old_tokens, tools_tokens)
if (is.null(all_tokens_files)) {
inform("No tokens were found! Nothing to do.")
return(invisible(NULL))
}
ot <- FALSE
nt <- FALSE
if (length(old_tokens) != 0 && old) {
ot <- unlink(old_tokens)
}
if (length(tools_tokens) != 0 && new) {
nt <- unlink(tools_tokens)
}
out <- old && ot || new && nt
invisible(out)
}
21 changes: 16 additions & 5 deletions R/auth_sitrep.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ auth_sitrep <- function() {
old_tokens <- find_old_tokens()
tools_tokens <- find_tools_tokens()
all_tokens_files <- c(old_tokens, tools_tokens)
# FIXME: Deal with Oauth2 tokens
if (is.null(all_tokens_files)) {
inform("No tokens were found! See ?auth_as for more details.")
return(NULL)
Expand Down Expand Up @@ -88,7 +87,12 @@ token_auth <- function(tokens) {
key = character(n))
df <- as.data.frame(t(list2DF(tokens)))
rownames(df) <- names(tokens)
colnames(df) <- c("app", "user_id", "key")
if (ncol(df) < 3L) {
x <- seq_len(ncol(df))
} else {
x <- 1L:3L
}
colnames(df) <- c("app", "user_id", "key")[x]
uk <- unique(df$key)
length_levels <- length(uk) - sum(any(uk == ""))
df$key <- factor(df$key, labels = LETTERS[seq_len(length_levels)], exclude = "")
Expand All @@ -97,8 +101,16 @@ token_auth <- function(tokens) {

#' @importFrom methods is
type_auth <- function(tokens) {
class_tokens <- vapply(tokens, is, character(1L))
class_tokens <- ifelse(endsWith(class_tokens, "Token1.0"), "token", "bearer")

class_tokens <- vapply(tokens, function(x){is(x)[1]}, character(1L))
class_tokens2 <- character(length(class_tokens))
class_tokens2[class_tokens %in% c("Token1.0", "TwitterToken1.0")] <- "token"
class_tokens2[class_tokens == "rtweet_bearer"] <- "bearer"
class_tokens2[class_tokens == "httr2_token"] <- "httr2_token"
if (any(!nzchar(class_tokens2))) {
warn("Detected some file which doesn't seems created by rtweet.", parent = current_call())
}
class_tokens2
}

move_tokens <- function(tokens, folder) {
Expand Down Expand Up @@ -179,7 +191,6 @@ handle_token <- function(tokens) {
action_tokens
}


auth_check <- function(tokens) {
type_auth <- type_auth(tokens)

Expand Down
6 changes: 4 additions & 2 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Update to add some functions, fix a bug and remove some links to removed documentation.
There might be some errors following links, sometimes Twitter.com or rOpenSci block the CRAN checks of urls.

Checks passed locally, in R-Hub there is a NOTE about lastMiKTeXException which apparently could be ignored.
This submission so close to the previous one is because there is an ERROR in one of the CRAN checks after acceptance.
This is probably caused by a previous version not following CRAN's policy of not writing to user directory.
The example detected a file with the pattern .rtweet_token.*rds in the user directory.
27 changes: 27 additions & 0 deletions man/auth_clean.Rd

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

0 comments on commit 8e89e7a

Please sign in to comment.