Skip to content

Commit

Permalink
Merge pull request GregSutcliffe#9 from GregSutcliffe/updates
Browse files Browse the repository at this point in the history
Make room_history handle a list of room_ids
  • Loading branch information
GregSutcliffe authored Dec 16, 2021
2 parents 7d48e47 + c794978 commit bb87c66
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Imports:
glue,
httr,
lubridate,
purrr,
rlog,
tibble,
tidyr
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(get_messages)
export(room_history)
export(get_rooms)
export(sync)
60 changes: 42 additions & 18 deletions R/room_history.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
#' Get the room events for a given room ID.
#'
#' @param room_id The room to get data for.
#' @param since Stop paginating when reaching this time.
#' @param room_id The room to get data for.
#' @param since Stop paginating when reaching this time.
#' @param initial_sync Result of a prior call to [sync()].
#'
#' @return A tibble containing event information.
#'
#' @export
room_history <- function(room_id, since) {
# We expect since to be a POSIX datetime, but could be a string.
since <- lubridate::as_datetime(since)
#' @noRd
room_history <- function(room_id, since, initial_sync) {
# TODO: Add better configuration.
token <- Sys.getenv("token")
timeline <- initial_sync$rooms$join[[room_id]]$timeline
from <- timeline$prev_batch

events <- process_events(timeline$events)
rlog::log_debug(glue::glue("Initial sync yielded {nrow(events)} events."))

rlog::log_info(
glue::glue("Fetching events for room {room_id} since {since}.")
)

# TODO: Add better configuration.
token <- Sys.getenv("token")

# Perform an initial sync and get events for the room.
initial_sync <- sync()
timeline <- initial_sync$rooms$join[[room_id]]$timeline
from <- timeline$prev_batch

events <- process_events(timeline$events)

rlog::log_debug(glue::glue("Initial sync yielded {nrow(events)} events."))

while (TRUE) {
oldest_time <- events |>
dplyr::slice_min(time) |>
Expand Down Expand Up @@ -55,3 +49,33 @@ room_history <- function(room_id, since) {

events |> dplyr::filter(time >= since)
}

#' Get the room events for a given room ID.
#'
#' @param room_ids Vector of room IDs to iterate over.
#' @param since Stop paginating when reaching this time.
#' @param sync (Optional) Result of a prior call to [sync()] to save
#' duplication.
#'
#' @return A tibble containing event information.
#'
#' @export
get_rooms <- function(room_ids, since, sync = NULL) {
# We expect since to be a POSIX datetime, but could be a string.
since <- lubridate::as_datetime(since)

rlog::log_debug(glue::glue("Getting history for {length(room_ids)} rooms."))

# Perform an initial sync and get events for the room.
initial_sync <- if (is.null(sync)) {
rlog::log_debug("Initial sync not provided - running sync() now.")
sync()
} else {
rlog::log_debug("Initial sync provided - not running sync().")
sync
}

tidyr::tibble(room = room_ids) |>
dplyr::group_by(room) |>
dplyr::mutate(events = purrr::map(room, room_history, since, initial_sync))
}
1 change: 1 addition & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ api_url <- function(url, host = NULL, port = NULL) {
process_events <- function(events) {
tibble::tibble(event = events) |>
tidyr::hoist(event,
id = "event_id",
time = "origin_server_ts",
type = "type",
sender = "sender",
Expand Down
11 changes: 7 additions & 4 deletions man/room_history.Rd → man/get_rooms.Rd

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

0 comments on commit bb87c66

Please sign in to comment.