Skip to content

Commit

Permalink
switched to dev version and linted post.R
Browse files Browse the repository at this point in the history
  • Loading branch information
schochastics committed Nov 6, 2024
1 parent ff079a2 commit e051f13
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 89 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-2952-4812")),
person("Chung-hong", "Chan", ,"[email protected]", role = c("aut"), comment = c(ORCID = "0000-0002-6232-7530")),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
176 changes: 88 additions & 88 deletions R/post.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}


Expand All @@ -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)
}

0 comments on commit e051f13

Please sign in to comment.