File: expansions.R

package info (click to toggle)
r-cran-rtweet 1.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,224 kB
  • sloc: sh: 13; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,716 bytes parent folder | download
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
#' Expansions
#'
#' Twitter parameters to add more fields on the returned values.
#' Main ones:
#'  - Tweet
#'    - Referenced tweets
#'    - Attachments
#'  - User
#' @param attachments Add attachments values? Default yes.
#' @param referenced_tweets Add referenced_tweets values? Default yes.
#' @return A character with the characters of valid expanions.
#' @references <https://developer.twitter.com/en/docs/twitter-api/expansions>
#' @seealso [Fields]
#' @name Expansions
#' @aliases expansions
#' @examples
#' tweet_expansions()
#' user_expansions()
#' @export
tweet_expansions <- function(attachments = TRUE, referenced_tweets = TRUE) {
  expansions <- c("author_id", "in_reply_to_user_id", "geo.place_id",
                  "entities.mentions.username",  "edit_history_tweet_ids")
  if (attachments) {
    expansions <- c(expansions, "attachments.media_keys",
                    "attachments.poll_ids")
  }
  if (referenced_tweets) {
    expansions <- c(expansions, "referenced_tweets.id", "referenced_tweets.id.author_id")
  }
  expansions
}

#' @export
#' @name Expansions
user_expansions <- function() {
  "pinned_tweet_id"
}

check_expansions <- function(passed, allowed) {
  if (is.null(passed)) {
    return(list(expansions = allowed))
  }

  # Empty list or NA return NULL to disable the expansions
  empty_list <- is.list(passed) && length(passed) == 0
  na <- length(passed) == 1L && is.na(passed)
  if ( empty_list || na) {
    return(NULL)
  }

  within <- !passed %in% allowed
  if (length(passed) > 10 || any(within)) {
    extensions <- passed[within]
    stop("These extensions are not allowed: ",
         paste(extensions, collapse = ", "), call. = FALSE)
  }
  list(expansions = passed)
}