-
Notifications
You must be signed in to change notification settings - Fork 1
/
record_post.R
162 lines (142 loc) · 4.55 KB
/
record_post.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#' Make a post on Bluesky Social
#'
#' @param text text of post
#' @param images character vector of paths to images to attach to post
#' @param images_alt character vector of alt text for images. Must be same length as `images` if used.
#' @param langs character vector of languages in BCP-47 format
#' @param reply character vector with link to the parent post to reply to
#' @param quote character vector with link to a post to quote
#' @param user `r template_var_user()`
#' @param pass `r template_var_pass()`
#' @param auth `r template_var_auth()`
#' @param clean `r template_var_clean()`
#'
#' @concept record
#'
#' @section Lexicon references:
#' [feed/post.json (2023-10-02)](https://github.com/bluesky-social/atproto/blob/main/lexicons/app/bsky/feed/post.json)
#' [repo/createRecord.json (2023-10-02)](https://github.com/bluesky-social/atproto/blob/main/lexicons/com/atproto/repo/createRecord.json)
#'
#' @section Function introduced:
#' `v0.0.1` (2023-10-02)
#'
#' @return a [tibble::tibble] of post information
#' @export
#'
#' @examplesIf has_bluesky_pass() & has_bluesky_user()
#' bs_post('Test post from R CMD Check for r package `bskyr`
#' via @bskyr.bsky.social (https://christophertkenny.com/bskyr/)')
#' bs_post('Test self-reply from r package `bskyr`
#' via @bskyr.bsky.social (https://christophertkenny.com/bskyr/)',
#' reply = 'https://bsky.app/profile/bskyr.bsky.social/post/3kexwuoyqj32g'
#' )
#' bs_post('Test quoting from r package `bskyr`
#' via @bskyr.bsky.social (https://christophertkenny.com/bskyr/)',
#' quote = 'https://bsky.app/profile/bskyr.bsky.social/post/3kf24wd6cmb2a'
#' )
#' bs_post('Test quote and reply from r package `bskyr`
#' via @bskyr.bsky.social (https://christophertkenny.com/bskyr/)',
#' reply = 'https://bsky.app/profile/bskyr.bsky.social/post/3kexwuoyqj32g',
#' quote = 'https://bsky.app/profile/bskyr.bsky.social/post/3kf24wd6cmb2a'
#' )
bs_post <- function(text, images, images_alt, langs, reply, quote,
user = get_bluesky_user(), pass = get_bluesky_pass(),
auth = bs_auth(user, pass), clean = TRUE) {
if (missing(text)) {
cli::cli_abort('{.arg text} must not be missing.')
}
if (!missing(images)) {
if (length(images) > 4) {
cli::cli_abort('You can only attach up to 4 images to a post.')
}
}
if (!missing(images)) {
if (missing(images_alt)) {
cli::cli_abort('If {.arg images} is provided, {.arg images_alt} must also be provided.')
}
}
facets_l <- parse_facets(txt = text, auth = auth)
if (!missing(images)) {
if (is.list(images)) { # any(fs::path_ext(images) == '')
# then we assume it's a blob
blob <- images
} else {
# otherwise it's a set of paths
blob <- bs_upload_blob(images, auth = auth, clean = FALSE)
}
}
if (!missing(images_alt)) {
if (length(blob) != length(images_alt)) {
cli::cli_abort('{.arg images_alt} must be the same length as {.arg images}.')
}
}
post <- list(
`$type` = 'app.bsky.feed.post',
text = text,
createdAt = bs_created_at()
)
if (!missing(langs)) {
post$langs <- as.list(langs)
}
if (!purrr::is_empty(facets_l)) {
post$facets <- facets_l[[1]]
}
if (!missing(images)) {
if (!missing(images_alt)) {
img_incl <- lapply(seq_along(images), function(i) {
list(
image = blob[[i]]$blob,
alt = images_alt[[i]]
)
})
} else {
img_incl <- lapply(blob, function(x) {
list(
image = x$blob
)
})
}
post$embed <- list(
'$type' = 'app.bsky.embed.images',
images = img_incl
)
}
if (!missing(reply)) {
post$reply <- get_reply_refs(reply, auth = auth)
}
if (!missing(quote)) {
quote_rcd <- bs_get_record(quote, auth = auth, clean = FALSE)
quote_inc <- list(
'$type' = 'app.bsky.embed.record',
record = list(
uri = quote_rcd$uri,
cid = quote_rcd$cid
)
)
if (!is.null(post$embed)) {
post$embed <- append(post$embed, quote_inc)
} else {
post$embed <- quote_inc
}
}
req <- httr2::request('https://bsky.social/xrpc/com.atproto.repo.createRecord') |>
httr2::req_auth_bearer_token(token = auth$accessJwt) |>
httr2::req_body_json(
data = list(
repo = auth$did,
collection = 'app.bsky.feed.post',
record = post
)
)
# return(httr2::req_dry_run(req))
resp <- req |>
httr2::req_perform() |>
httr2::resp_body_json()
if (!clean) {
return(resp)
}
resp |>
dplyr::bind_rows() |>
clean_names() |>
add_req_url(req)
}