diff --git a/DESCRIPTION b/DESCRIPTION index ebda399..50bf5a7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rtoot Title: Collecting and Analyzing Mastodon Data -Version: 0.3.5 +Version: 0.3.5.9000 Authors@R: c(person("David", "Schoch", , "david@schochastics.net", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-2952-4812")), person("Chung-hong", "Chan", ,"chainsawtiney@gmail.com", role = c("aut"), comment = c(ORCID = "0000-0002-6232-7530")), diff --git a/NEWS.md b/NEWS.md index a73fe38..63ac2dc 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,5 @@ +# rtoot 0.3.5.9000 + # rtoot 0.3.5 * fixed #153 by allowing POSIXct as `max_id`, `min_id` and `since_id` (#154) by @Kudusch diff --git a/R/post.R b/R/post.R index 66491c4..cf8ea9b 100644 --- a/R/post.R +++ b/R/post.R @@ -32,72 +32,72 @@ post_toot <- function( scheduled_at = NULL, language = NULL, verbose = TRUE) { - token <- check_token_rtoot(token) + token <- check_token_rtoot(token) - stopifnot(is.character(status), length(status) == 1) - if (!is.null(media)) { - check_media(media, alt_text) - media_id_string <- character(length(media)) - for (i in seq_along(media)) { - media_id_string[[i]] <- upload_media_to_mastodon(media[[i]], alt_text[[i]], token) + stopifnot(is.character(status), length(status) == 1) + if (!is.null(media)) { + check_media(media, alt_text) + media_id_string <- character(length(media)) + for (i in seq_along(media)) { + media_id_string[[i]] <- upload_media_to_mastodon(media[[i]], alt_text[[i]], token) + } + media_id_string <- lapply(media_id_string, identity) # paste(media_id_string, collapse = ",") + names(media_id_string) <- rep("media_ids[]", length(media_id_string)) + params <- c( + status = status, + media_id_string + ) + } else { + params <- list(status = status) } - media_id_string <- lapply(media_id_string, identity) # paste(media_id_string, collapse = ",") - names(media_id_string) <- rep("media_ids[]", length(media_id_string)) - params <- c( - status = status, - media_id_string + if (!is.null(in_reply_to_id)) { + params[["in_reply_to_id"]] <- in_reply_to_id + } + params[["sensitive"]] <- tolower(as.logical(sensitive)) + if (!is.null(spoiler_text)) { + params[["spoiler_text"]] <- spoiler_text + } + visibility <- match.arg(visibility, c("public", "unlisted", "private", "direct")) + params[["visibility"]] <- visibility + if (!is.null(scheduled_at)) { + params[["scheduled_at"]] <- scheduled_at + } + if (!is.null(language)) { + params[["language"]] <- language + } + url <- prepare_url(token$instance) + r <- httr::POST(httr::modify_url(url = url, path = "api/v1/statuses"), + body = params, + httr::add_headers(Authorization = paste0("Bearer ", token$bearer)) ) - } else { - params <- list(status = status) - } - if (!is.null(in_reply_to_id)) { - params[["in_reply_to_id"]] <- in_reply_to_id - } - params[["sensitive"]] <- tolower(as.logical(sensitive)) - if (!is.null(spoiler_text)) { - params[["spoiler_text"]] <- spoiler_text - } - visibility <- match.arg(visibility, c("public", "unlisted", "private", "direct")) - params[["visibility"]] <- visibility - if (!is.null(scheduled_at)) { - params[["scheduled_at"]] <- scheduled_at - } - if (!is.null(language)) { - params[["language"]] <- language - } - url <- prepare_url(token$instance) - r <- httr::POST(httr::modify_url(url = url, path = "api/v1/statuses"), - body = params, - httr::add_headers(Authorization = paste0("Bearer ", token$bearer)) - ) - if (httr::status_code(r) == 200L) { - sayif(verbose, "Your toot has been posted!") - } - invisible(r) + if (httr::status_code(r) == 200L) { + sayif(verbose, "Your toot has been posted!") + } + invisible(r) } upload_media_to_mastodon <- function(media, alt_text, token) { - url <- prepare_url(token$instance) - params <- list(file = httr::upload_file(media), description = alt_text) - r <- httr::POST(httr::modify_url(url = url, path = "api/v1/media"), - body = params, - httr::add_headers(Authorization = paste0("Bearer ", token$bearer)) - ) - httr::content(r)$id + url <- prepare_url(token$instance) + params <- list(file = httr::upload_file(media), description = alt_text) + r <- httr::POST(httr::modify_url(url = url, path = "api/v1/media"), + body = params, + httr::add_headers(Authorization = paste0("Bearer ", token$bearer)) + ) + httr::content(r)$id } check_media <- function(media, alt_text) { - if (!is.character(media) | !is.character(alt_text)) { - stop("Media and alt_text must be character vectors.", call. = FALSE) - } + if (!is.character(media) || !is.character(alt_text)) { + stop("Media and alt_text must be character vectors.", call. = FALSE) + } - if (!is.null(alt_text) && length(alt_text) != length(media)) { - stop("Alt text for media isn't provided for each image.", call. = TRUE) - } + if (!is.null(alt_text) && length(alt_text) != length(media)) { + stop("Alt text for media isn't provided for each image.", call. = TRUE) + } - if (any(nchar(alt_text) > 1000)) { - stop("Alt text cannot be longer than 1000 characters.", call. = TRUE) - } + if (any(nchar(alt_text) > 1000)) { + stop("Alt text cannot be longer than 1000 characters.", call. = TRUE) + } } #' Perform actions on an account @@ -116,27 +116,27 @@ check_media <- function(media, alt_text) { #' } #' @export post_user <- function(id, action = "follow", comment = "", token = NULL, verbose = TRUE) { - token <- check_token_rtoot(token) - action <- match.arg(action, c( - "follow", "unfollow", "block", "unblock", - "mute", "unmute", "pin", "unpin", "note" - )) - path <- paste0("/api/v1/accounts/", id, "/", action) - if (action == "note") { - params <- list(comment = comment) - } else { - params <- list() - } + token <- check_token_rtoot(token) + action <- match.arg(action, c( + "follow", "unfollow", "block", "unblock", + "mute", "unmute", "pin", "unpin", "note" + )) + path <- paste0("/api/v1/accounts/", id, "/", action) + if (action == "note") { + params <- list(comment = comment) + } else { + params <- list() + } - url <- prepare_url(token$instance) - r <- httr::POST(httr::modify_url(url = url, path = path), - body = params, - httr::add_headers(Authorization = paste0("Bearer ", token$bearer)) - ) - if (httr::status_code(r) == 200L) { - sayif(verbose, "successfully performed action on user") - } - invisible(r) + url <- prepare_url(token$instance) + r <- httr::POST(httr::modify_url(url = url, path = path), + body = params, + httr::add_headers(Authorization = paste0("Bearer ", token$bearer)) + ) + if (httr::status_code(r) == 200L) { + sayif(verbose, "successfully performed action on user") + } + invisible(r) } @@ -155,18 +155,18 @@ post_user <- function(id, action = "follow", comment = "", token = NULL, verbose #' } #' @export post_status <- function(id, action = "favourite", token = NULL, verbose = TRUE) { - token <- check_token_rtoot(token) - action <- match.arg(action, c("unfavourite", "favourite", "reblog", "unreblog", "bookmark", "unbookmark")) - path <- paste0("/api/v1/statuses/", id, "/", action) - params <- list() + token <- check_token_rtoot(token) + action <- match.arg(action, c("unfavourite", "favourite", "reblog", "unreblog", "bookmark", "unbookmark")) + path <- paste0("/api/v1/statuses/", id, "/", action) + params <- list() - url <- prepare_url(token$instance) - r <- httr::POST(httr::modify_url(url = url, path = path), - body = params, - httr::add_headers(Authorization = paste0("Bearer ", token$bearer)) - ) - if (httr::status_code(r) == 200L) { - sayif(verbose, "successfully performed action on status") - } - invisible(r) + url <- prepare_url(token$instance) + r <- httr::POST(httr::modify_url(url = url, path = path), + body = params, + httr::add_headers(Authorization = paste0("Bearer ", token$bearer)) + ) + if (httr::status_code(r) == 200L) { + sayif(verbose, "successfully performed action on status") + } + invisible(r) }