Skip to content

Commit

Permalink
Merge pull request #77 from chainsawriot/verbose
Browse files Browse the repository at this point in the history
fix #75
  • Loading branch information
schochastics committed Nov 15, 2022
2 parents cc66788 + e751100 commit ffcf1ab
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 21 deletions.
11 changes: 6 additions & 5 deletions R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,16 @@ create_token <- function(client, type = "public"){
#' Verify mastodon credentials
#'
#' @param token bearer token, either public or user level
#' @return success or failure message of the verification process
#' @return Raise an error if the token is not valid. Return the response from the verification API invisibly otherwise.
#' @details If you have created your token as an environment variable, use `verify_envvar` to verify your token.
#' @inheritParams auth_setup
#' @examples
#' \dontrun{
#' #read a token from a file
#' verify_credentials(token)
#' }
#' @export
verify_credentials <- function(token) {
verify_credentials <- function(token, verbose = TRUE) {
if(!is_auth_rtoot(token)){
stop("token is not an object of type rtoot_bearer")
}
Expand All @@ -149,7 +150,7 @@ verify_credentials <- function(token) {
}
success <- isTRUE(acc[["status_code"]] == 200L)
if (success) {
message("Token of type \"", token$type, "\" for instance ", token$instance, " is valid")
sayif(verbose, "Token of type \"", token$type, "\" for instance ", token$instance, " is valid")
} else {
stop("Token not valid. Use auth_setup() to create a token")
}
Expand All @@ -158,9 +159,9 @@ verify_credentials <- function(token) {

#' @export
#' @rdname verify_credentials
verify_envvar <- function() {
verify_envvar <- function(verbose = TRUE) {
token <- get_token_from_envvar()
verify_credentials(token)
verify_credentials(token, verbose = verbose)
}

#' save a bearer token to file
Expand Down
11 changes: 7 additions & 4 deletions R/post.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' @param scheduled_at ISO 8601 Datetime at which to schedule a status. Must be at least 5 minutes in the future.
#' @param language ISO 639 language code for this status.
#' @return no return value, called for site effects
#' @inheritParams auth_setup
#' @examples
#' \dontrun{
#' # post a simple status
Expand All @@ -29,7 +30,8 @@ post_toot <- function(
spoiler_text = NULL,
visibility = "public",
scheduled_at = NULL,
language = NULL){
language = NULL,
verbose = TRUE){

token <- check_token_rtoot(token)

Expand Down Expand Up @@ -69,7 +71,7 @@ post_toot <- function(
body=params,
httr::add_headers(Authorization = paste0("Bearer ",token$bearer)))
if(httr::status_code(r)==200L){
message("Your toot has been posted!")
sayif(verbose, "Your toot has been posted!")
}
invisible(r)
}
Expand Down Expand Up @@ -103,6 +105,7 @@ check_media <- function(media, alt_text) {
#' @param action character, one of "(un)follow","(un)block", "(un)mute", "(un)pin","note"
#' @param comment character (if action="note"), The comment to be set on that user. Provide an empty string or leave out this parameter to clear the currently set note.
#' @return no return value, called for site effects
#' @inheritParams auth_setup
#' @examples
#' \dontrun{
#' #follow a user
Expand All @@ -111,7 +114,7 @@ check_media <- function(media, alt_text) {
#' post_user("xxxxxx",action = "unfollow")
#' }
#' @export
post_user <- function(id,action = "follow",comment = "",token = NULL){
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"))
Expand All @@ -127,7 +130,7 @@ post_user <- function(id,action = "follow",comment = "",token = NULL){
body = params,
httr::add_headers(Authorization = paste0("Bearer ",token$bearer)))
if(httr::status_code(r)==200L){
message("successfully performed action on user")
sayif(verbose, "successfully performed action on user")
}
invisible(r)
}
5 changes: 4 additions & 1 deletion man/post_toot.Rd

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

4 changes: 3 additions & 1 deletion man/post_user.Rd

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

8 changes: 5 additions & 3 deletions man/verify_credentials.Rd

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

57 changes: 57 additions & 0 deletions tests/fixtures/envvar_silent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
http_interactions:
- request:
method: get
uri: https://emacs.ch/api/v1/accounts/verify_credentials
body:
encoding: ''
string: ''
headers:
Accept: application/json, text/xml, application/xml, */*
Authorization: Bearer <<<MASTODON TOKEN>>>
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
cache-control: no-store
content-encoding: gzip
content-security-policy: 'base-uri ''none''; default-src ''none''; frame-ancestors
''none''; font-src ''self'' https://emacs.ch; img-src ''self'' https: data:
blob: https://emacs.ch; style-src ''self'' https://emacs.ch ''nonce-9/p9d92Slffz1YqNYKPHVw=='';
media-src ''self'' https: data: https://emacs.ch; frame-src ''self'' https:;
manifest-src ''self'' https://emacs.ch; connect-src ''self'' data: blob: https://emacs.ch
https://emacs.ch wss://emacs.ch; script-src ''self'' https://emacs.ch ''wasm-unsafe-eval'';
child-src ''self'' blob: https://emacs.ch; worker-src ''self'' blob: https://emacs.ch'
content-type: application/json; charset=utf-8
date: Tue, 15 Nov 2022 17:59:55 GMT
etag: W/"a682db363392a2c8dca0e904ac90c6a1"
permissions-policy: interest-cohort=()
server: Mastodon
strict-transport-security: max-age=63072000; includeSubDomains
vary:
- Accept-Encoding
- Origin
x-cached: MISS
x-content-type-options: nosniff
x-frame-options: DENY
x-ratelimit-limit: '300'
x-ratelimit-remaining: '296'
x-ratelimit-reset: '2022-11-15T18:00:00.039696Z'
x-request-id: bc0ad1a2-1cdd-40f6-8bc9-3616e2e093f7
x-runtime: '0.018868'
x-xss-protection: '0'
body:
encoding: ''
file: no
string: '{"id":"109337011845249544","username":"chainsawriot","acct":"chainsawriot","display_name":"chainsawriot","locked":false,"bot":false,"discoverable":false,"group":false,"created_at":"2022-11-13T00:00:00.000Z","note":"","url":"https://emacs.ch/@chainsawriot","avatar":"https://emacs.ch/system/accounts/avatars/109/337/011/845/249/544/original/00fc3aabc87d3564.jpg","avatar_static":"https://emacs.ch/system/accounts/avatars/109/337/011/845/249/544/original/00fc3aabc87d3564.jpg","header":"https://emacs.ch/headers/original/missing.png","header_static":"https://emacs.ch/headers/original/missing.png","followers_count":126,"following_count":63,"statuses_count":24,"last_status_at":"2022-11-15","noindex":true,"source":{"privacy":"public","sensitive":false,"language":"","note":"","fields":[{"name":"Stack","value":":emacs:,
R, stumpwm, linux, firefox","verified_at":null},{"name":"Research Interests","value":"Keine
Ahnung","verified_at":null},{"name":"Web","value":"https://chainsawriot.com/about/","verified_at":"2022-11-14T12:22:07.327+00:00"}],"follow_requests_count":0},"emojis":[{"shortcode":"emacs","url":"https://emacs.ch/system/custom_emojis/images/000/000/358/original/9f320443168e793f.png","static_url":"https://emacs.ch/system/custom_emojis/images/000/000/358/static/9f320443168e793f.png","visible_in_picker":true}],"fields":[{"name":"Stack","value":":emacs:,
R, stumpwm, linux, firefox","verified_at":null},{"name":"Research Interests","value":"Keine
Ahnung","verified_at":null},{"name":"Web","value":"\u003ca href=\"https://chainsawriot.com/about/\"
target=\"_blank\" rel=\"nofollow noopener noreferrer me\"\u003e\u003cspan
class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003echainsawriot.com/about/\u003c/span\u003e\u003cspan
class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e","verified_at":"2022-11-14T12:22:07.327+00:00"}],"role":{"id":"-99","name":"","permissions":"65536","color":"","highlighted":false}}'
recorded_at: 2022-11-15 17:59:55 GMT
recorded_with: vcr/1.1.0, webmockr/0.8.2
56 changes: 56 additions & 0 deletions tests/fixtures/post_toot_silent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
http_interactions:
- request:
method: post
uri: https://emacs.ch/api/v1/statuses
body:
encoding: ''
string: status=testing in progress, please ignore,sensitive=false,visibility=public
headers:
Accept: application/json, text/xml, application/xml, */*
Authorization: Bearer <<<MASTODON TOKEN>>>
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
cache-control: no-store
content-encoding: gzip
content-security-policy: 'base-uri ''none''; default-src ''none''; frame-ancestors
''none''; font-src ''self'' https://emacs.ch; img-src ''self'' https: data:
blob: https://emacs.ch; style-src ''self'' https://emacs.ch ''nonce-YxaYMg3GKP5qZdKUUau3Jw=='';
media-src ''self'' https: data: https://emacs.ch; frame-src ''self'' https:;
manifest-src ''self'' https://emacs.ch; connect-src ''self'' data: blob: https://emacs.ch
https://emacs.ch wss://emacs.ch; script-src ''self'' https://emacs.ch ''wasm-unsafe-eval'';
child-src ''self'' blob: https://emacs.ch; worker-src ''self'' blob: https://emacs.ch'
content-type: application/json; charset=utf-8
date: Tue, 15 Nov 2022 18:04:22 GMT
etag: W/"c552b327bdc75e6e987171b7aadfc790"
permissions-policy: interest-cohort=()
server: Mastodon
strict-transport-security: max-age=63072000; includeSubDomains
vary:
- Accept-Encoding
- Origin
x-content-type-options: nosniff
x-frame-options: DENY
x-ratelimit-limit: '300'
x-ratelimit-remaining: '296'
x-ratelimit-reset: '2022-11-15T21:00:00.373002Z'
x-request-id: 554397da-9378-42a2-b292-f86ca95778b7
x-runtime: '0.046660'
x-xss-protection: '0'
body:
encoding: ''
file: no
string: '{"id":"109349140059981889","created_at":"2022-11-15T18:04:22.342Z","in_reply_to_id":null,"in_reply_to_account_id":null,"sensitive":false,"spoiler_text":"","visibility":"public","language":"en","uri":"https://emacs.ch/users/chainsawriot/statuses/109349140059981889","url":"https://emacs.ch/@chainsawriot/109349140059981889","replies_count":0,"reblogs_count":0,"favourites_count":0,"edited_at":null,"favourited":false,"reblogged":false,"muted":false,"bookmarked":false,"pinned":false,"content":"\u003cp\u003etesting
in progress, please ignore\u003c/p\u003e","filtered":[],"reblog":null,"application":{"name":"rtoot
package","website":null},"account":{"id":"109337011845249544","username":"chainsawriot","acct":"chainsawriot","display_name":"chainsawriot","locked":false,"bot":false,"discoverable":false,"group":false,"created_at":"2022-11-13T00:00:00.000Z","note":"","url":"https://emacs.ch/@chainsawriot","avatar":"https://emacs.ch/system/accounts/avatars/109/337/011/845/249/544/original/00fc3aabc87d3564.jpg","avatar_static":"https://emacs.ch/system/accounts/avatars/109/337/011/845/249/544/original/00fc3aabc87d3564.jpg","header":"https://emacs.ch/headers/original/missing.png","header_static":"https://emacs.ch/headers/original/missing.png","followers_count":126,"following_count":63,"statuses_count":28,"last_status_at":"2022-11-15","noindex":true,"emojis":[{"shortcode":"emacs","url":"https://emacs.ch/system/custom_emojis/images/000/000/358/original/9f320443168e793f.png","static_url":"https://emacs.ch/system/custom_emojis/images/000/000/358/static/9f320443168e793f.png","visible_in_picker":true}],"fields":[{"name":"Stack","value":":emacs:,
R, stumpwm, linux, firefox","verified_at":null},{"name":"Research Interests","value":"Keine
Ahnung","verified_at":null},{"name":"Web","value":"\u003ca href=\"https://chainsawriot.com/about/\"
target=\"_blank\" rel=\"nofollow noopener noreferrer me\"\u003e\u003cspan
class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003echainsawriot.com/about/\u003c/span\u003e\u003cspan
class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e","verified_at":"2022-11-14T12:22:07.327+00:00"}]},"media_attachments":[],"mentions":[],"tags":[],"emojis":[],"card":null,"poll":null}'
recorded_at: 2022-11-15 18:04:22 GMT
recorded_with: vcr/1.1.0, webmockr/0.8.2
56 changes: 56 additions & 0 deletions tests/fixtures/post_toot_verbose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
http_interactions:
- request:
method: post
uri: https://emacs.ch/api/v1/statuses
body:
encoding: ''
string: status=testing in progress, please ignore,sensitive=false,visibility=public
headers:
Accept: application/json, text/xml, application/xml, */*
Authorization: Bearer <<<MASTODON TOKEN>>>
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
cache-control: no-store
content-encoding: gzip
content-security-policy: 'base-uri ''none''; default-src ''none''; frame-ancestors
''none''; font-src ''self'' https://emacs.ch; img-src ''self'' https: data:
blob: https://emacs.ch; style-src ''self'' https://emacs.ch ''nonce-WhkAK1UcAYaMaoCdwdqlsg=='';
media-src ''self'' https: data: https://emacs.ch; frame-src ''self'' https:;
manifest-src ''self'' https://emacs.ch; connect-src ''self'' data: blob: https://emacs.ch
https://emacs.ch wss://emacs.ch; script-src ''self'' https://emacs.ch ''wasm-unsafe-eval'';
child-src ''self'' blob: https://emacs.ch; worker-src ''self'' blob: https://emacs.ch'
content-type: application/json; charset=utf-8
date: Tue, 15 Nov 2022 18:04:22 GMT
etag: W/"4819c29985fd22b96038a0483f5c7993"
permissions-policy: interest-cohort=()
server: Mastodon
strict-transport-security: max-age=63072000; includeSubDomains
vary:
- Accept-Encoding
- Origin
x-content-type-options: nosniff
x-frame-options: DENY
x-ratelimit-limit: '300'
x-ratelimit-remaining: '297'
x-ratelimit-reset: '2022-11-15T21:00:00.260950Z'
x-request-id: d6d3fe2b-57e2-47a0-99ca-01a96daa343c
x-runtime: '0.048207'
x-xss-protection: '0'
body:
encoding: ''
file: no
string: '{"id":"109349140052391379","created_at":"2022-11-15T18:04:22.230Z","in_reply_to_id":null,"in_reply_to_account_id":null,"sensitive":false,"spoiler_text":"","visibility":"public","language":"en","uri":"https://emacs.ch/users/chainsawriot/statuses/109349140052391379","url":"https://emacs.ch/@chainsawriot/109349140052391379","replies_count":0,"reblogs_count":0,"favourites_count":0,"edited_at":null,"favourited":false,"reblogged":false,"muted":false,"bookmarked":false,"pinned":false,"content":"\u003cp\u003etesting
in progress, please ignore\u003c/p\u003e","filtered":[],"reblog":null,"application":{"name":"rtoot
package","website":null},"account":{"id":"109337011845249544","username":"chainsawriot","acct":"chainsawriot","display_name":"chainsawriot","locked":false,"bot":false,"discoverable":false,"group":false,"created_at":"2022-11-13T00:00:00.000Z","note":"","url":"https://emacs.ch/@chainsawriot","avatar":"https://emacs.ch/system/accounts/avatars/109/337/011/845/249/544/original/00fc3aabc87d3564.jpg","avatar_static":"https://emacs.ch/system/accounts/avatars/109/337/011/845/249/544/original/00fc3aabc87d3564.jpg","header":"https://emacs.ch/headers/original/missing.png","header_static":"https://emacs.ch/headers/original/missing.png","followers_count":126,"following_count":63,"statuses_count":27,"last_status_at":"2022-11-15","noindex":true,"emojis":[{"shortcode":"emacs","url":"https://emacs.ch/system/custom_emojis/images/000/000/358/original/9f320443168e793f.png","static_url":"https://emacs.ch/system/custom_emojis/images/000/000/358/static/9f320443168e793f.png","visible_in_picker":true}],"fields":[{"name":"Stack","value":":emacs:,
R, stumpwm, linux, firefox","verified_at":null},{"name":"Research Interests","value":"Keine
Ahnung","verified_at":null},{"name":"Web","value":"\u003ca href=\"https://chainsawriot.com/about/\"
target=\"_blank\" rel=\"nofollow noopener noreferrer me\"\u003e\u003cspan
class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003echainsawriot.com/about/\u003c/span\u003e\u003cspan
class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e","verified_at":"2022-11-14T12:22:07.327+00:00"}]},"media_attachments":[],"mentions":[],"tags":[],"emojis":[],"card":null,"poll":null}'
recorded_at: 2022-11-15 18:04:22 GMT
recorded_with: vcr/1.1.0, webmockr/0.8.2
49 changes: 49 additions & 0 deletions tests/fixtures/post_user_pin_silent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
http_interactions:
- request:
method: post
uri: https://emacs.ch/api/v1/accounts/109337044410360200/pin
body:
encoding: ''
string: ''
headers:
Accept: application/json, text/xml, application/xml, */*
Authorization: Bearer <<<MASTODON TOKEN>>>
response:
status:
status_code: 200
category: Success
reason: OK
message: 'Success: (200) OK'
headers:
cache-control: no-store
content-encoding: gzip
content-security-policy: 'base-uri ''none''; default-src ''none''; frame-ancestors
''none''; font-src ''self'' https://emacs.ch; img-src ''self'' https: data:
blob: https://emacs.ch; style-src ''self'' https://emacs.ch ''nonce-s5sxVSnJXDMNUPSFF0xx6g=='';
media-src ''self'' https: data: https://emacs.ch; frame-src ''self'' https:;
manifest-src ''self'' https://emacs.ch; connect-src ''self'' data: blob: https://emacs.ch
https://emacs.ch wss://emacs.ch; script-src ''self'' https://emacs.ch ''wasm-unsafe-eval'';
child-src ''self'' blob: https://emacs.ch; worker-src ''self'' blob: https://emacs.ch'
content-type: application/json; charset=utf-8
date: Tue, 15 Nov 2022 18:12:24 GMT
etag: W/"71fc28ed1faf9b0390808ead5c4527f1"
permissions-policy: interest-cohort=()
server: Mastodon
strict-transport-security: max-age=63072000; includeSubDomains
vary:
- Accept-Encoding
- Origin
x-content-type-options: nosniff
x-frame-options: DENY
x-ratelimit-limit: '300'
x-ratelimit-remaining: '276'
x-ratelimit-reset: '2022-11-15T18:15:00.511173Z'
x-request-id: 441d3e11-2a78-44f3-91e8-76de6e743a72
x-runtime: '0.033891'
x-xss-protection: '0'
body:
encoding: ''
file: no
string: '{"id":"109337044410360200","following":true,"showing_reblogs":true,"notifying":false,"languages":null,"followed_by":true,"blocking":false,"blocked_by":false,"muting":false,"muting_notifications":false,"requested":false,"domain_blocking":false,"endorsed":true,"note":""}'
recorded_at: 2022-11-15 18:12:24 GMT
recorded_with: vcr/1.1.0, webmockr/0.8.2
Loading

0 comments on commit ffcf1ab

Please sign in to comment.