From 124bf198cc5982f0ffc4244b0d867a6e3497e9f8 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 18 Nov 2026 11:52:29 -0600 Subject: [PATCH 1/5] Carry a caller's own content keys on send, edit and receive chat_send() and chat_edit() take content, a named list of keys for the message's own content, such as a namespaced "ai.cornball.agent" that one client reads and the rest ignore. Matrix adds them beside body and msgtype, in the cleartext path through mx_send() (as rich goes, since mx_send_text() takes no extra content, with the mentions, thread and markdown it would have built) and in the encrypted path as extra content inside the ciphertext. An edit puts them in m.new_content. A key chat.api writes itself is refused. Received, a cleartext message's raw now carries the event's whole content as raw$content, looked up from the sync by event id as the times are: mx_extract_text_events() keeps 6 fields and drops the rest. A decrypted record has content already, and history's raw is the event, so raw$content reads the same on all 3. The loopback adapter records the keys there too; the other adapters take the argument and ignore it, as they ignore rich. --- R/contract.R | 60 ++++++- R/irc.R | 2 +- R/loopback.R | 23 ++- R/matrix.R | 120 ++++++++++++-- R/slack.R | 6 +- R/telegram.R | 5 +- inst/tinytest/test_contract.R | 49 ++++++ inst/tinytest/test_matrix.R | 225 +++++++++++++++++++++++++++ inst/tinytest/test_matrix_mxclient.R | 5 +- man/chat_edit.Rd | 7 + man/chat_matrix.Rd | 3 +- man/chat_message.Rd | 7 +- man/chat_send.Rd | 18 +++ 13 files changed, 499 insertions(+), 31 deletions(-) diff --git a/R/contract.R b/R/contract.R index da8c1d2..e0f85ee 100644 --- a/R/contract.R +++ b/R/contract.R @@ -61,6 +61,22 @@ chat_poll <- function(client, since = NULL, timeout = NULL, ...) { #' nothing else. #' @param notify Logical; FALSE requests a silent delivery where #' supported. +#' @param content Keys of the caller's own for the message's content, as +#' a named list, or NULL. Matrix adds them to the \code{m.room.message} +#' content beside \code{body} and \code{msgtype}, inside the ciphertext +#' in an encrypted room, so a consumer can carry data its own client +#' reads and every other client ignores, under a namespaced key such +#' as \code{"ai.cornball.agent"}. A received message carries its +#' content in \code{raw$content}, and the loopback adapter records +#' these keys there too; the other adapters ignore them, as they +#' ignore \code{rich}, so \code{text} has to stand on its own. +#' +#' A key chat.api writes itself is refused: \code{body}, +#' \code{msgtype}, \code{format}, \code{formatted_body}, +#' \code{m.relates_to}, \code{m.mentions} and \code{m.new_content}. +#' \code{text}, \code{kind}, \code{rich}, \code{thread} and the +#' \code{mentions} option set those. With \code{files}, the keys go on +#' the text's event, not on the attachments'. #' @param ... Adapter-specific options. #' @return Character vector of the message ids this call created, in the #' order they were sent, invisibly. Usually length one. A platform that @@ -78,10 +94,38 @@ chat_poll <- function(client, since = NULL, timeout = NULL, ...) { chat_send <- function(client, channel, text, markup = c("plain", "markdown"), thread = NULL, reply_to = NULL, identity = NULL, files = NULL, kind = "message", notify = TRUE, - rich = NULL, ...) { + rich = NULL, content = NULL, ...) { UseMethod("chat_send") } +# The keys chat_send() and chat_edit() write themselves, which a +# caller's content may not name. Each has an argument of its own, and a +# second way to set it would leave the two disagreeing on the wire. +CHAT_CONTENT_RESERVED <- c("body", "msgtype", "format", "formatted_body", + "m.relates_to", "m.mentions", "m.new_content") + +# A caller's content keys, checked: NULL for none (an empty list is +# none too), or a named list whose names are distinct, non-empty and +# not chat.api's own. +chat_content_keys <- function(content) { + if (is.null(content) || (is.list(content) && !length(content))) { + return(NULL) + } + nms <- names(content) + if (!is.list(content) || is.null(nms) || anyNA(nms) || + !all(nzchar(nms)) || anyDuplicated(nms)) { + stop("content must be NULL or a list with distinct names.", + call. = FALSE) + } + taken <- intersect(nms, CHAT_CONTENT_RESERVED) + if (length(taken)) { + stop("content may not set ", paste(taken, collapse = ", "), + ", which chat.api writes itself. Use text, kind, rich, thread ", + "or mentions instead.", call. = FALSE) + } + content +} + #' Signal typing state in a channel #' #' Capability-gated: the default method is a no-op so adapters without @@ -555,6 +599,11 @@ chat_disconnect.default <- function(client, ...) { #' adapter-specific and may already be normalized by the transport #' package (Matrix hands over an extracted record, not the timeline #' event), so it is an escape hatch, not a guarantee of completeness. +#' One part is promised: a Matrix message, cleartext or decrypted, +#' carries its event's whole content in \code{raw$content}, with any +#' keys a sender added of its own (\code{\link{chat_send}}'s +#' \code{content}), and the loopback adapter records those keys +#' there. #' @param attachments List of \code{\link{chat_attachment}} records, #' or NULL. Inbound media: what a sent \code{files =} looks like #' from the receiving side, on adapters whose @@ -1186,6 +1235,11 @@ chat_relogin.default <- function(client, ...) { #' the message was sent as: Matrix carries the msgtype inside the #' replacement content, so editing an \code{m.notice} without saying so #' turns it into an ordinary message. +#' @param content Keys of the caller's own for the replacement, as +#' \code{\link{chat_send}}. Matrix puts them in \code{m.new_content}, +#' beside the replacement text. Like the text, they replace the +#' original's: a key the edit does not repeat is gone from the edited +#' message. #' @param ... Adapter-specific options. #' @return The identifier of the event the edit created where the #' platform makes one (Matrix), or of the edited message where it does @@ -1205,14 +1259,14 @@ chat_relogin.default <- function(client, ...) { #' @export chat_edit <- function(client, channel, message_id, text, markup = c("plain", "markdown"), rich = NULL, - kind = "message", ...) { + kind = "message", content = NULL, ...) { UseMethod("chat_edit") } #' @export chat_edit.default <- function(client, channel, message_id, text, markup = c("plain", "markdown"), rich = NULL, - kind = "message", ...) { + kind = "message", content = NULL, ...) { stop("chat_edit() is not supported by this adapter (", paste(class(client), collapse = "/"), "). Check chat_capabilities()$edits.", call. = FALSE) diff --git a/R/irc.R b/R/irc.R index 37fded8..c525a60 100644 --- a/R/irc.R +++ b/R/irc.R @@ -98,7 +98,7 @@ chat_send.chat_irc <- function(client, channel, text, thread = NULL, reply_to = NULL, identity = NULL, files = NULL, kind = "message", notify = TRUE, rich = NULL, - ...) { + content = NULL, ...) { markup <- match.arg(markup) verb <- if (identical(kind, "notice")) { "NOTICE" diff --git a/R/loopback.R b/R/loopback.R index c29cae9..70e29e0 100644 --- a/R/loopback.R +++ b/R/loopback.R @@ -84,8 +84,9 @@ chat_send.chat_loopback <- function(client, channel, text, thread = NULL, reply_to = NULL, identity = NULL, files = NULL, kind = "message", notify = TRUE, - rich = NULL, ...) { + rich = NULL, content = NULL, ...) { markup <- match.arg(markup) + content <- chat_content_keys(content) attachments <- NULL if (!is.null(files)) { # A missing file errors rather than sending a message that @@ -111,11 +112,22 @@ chat_send.chat_loopback <- function(client, channel, text, else identity$name, body = text, ts = Sys.time(), thread = thread, markup = markup, kind = kind, - attachments = attachments) + attachments = attachments, + raw = loopback_raw(content)) client$env$log[[length(client$env$log) + 1L]] <- msg invisible(id) } +# A caller's content keys where a Matrix message carries its content, +# raw$content, so a consumer reads them the same way against the +# reference adapter and a homeserver. NULL when there are none. +loopback_raw <- function(content) { + if (is.null(content)) { + return(NULL) + } + list(content = content) +} + #' @export chat_download.chat_loopback <- function(client, attachment, dest = NULL, ...) { dest <- attachment_dest(attachment, dest) @@ -224,8 +236,10 @@ chat_history.chat_loopback <- function(client, channel, limit = 50L, #' @export chat_edit.chat_loopback <- function(client, channel, message_id, text, markup = c("plain", "markdown"), - rich = NULL, kind = "message", ...) { + rich = NULL, kind = "message", + content = NULL, ...) { markup <- match.arg(markup) + content <- chat_content_keys(content) pos <- which(vapply(client$env$log, function(m) identical(m$id, message_id), logical(1))) if (!length(pos)) { @@ -238,6 +252,9 @@ chat_edit.chat_loopback <- function(client, channel, message_id, text, msg <- client$env$log[[pos[[1L]]]] msg$body <- text msg$markup <- markup + # The edit's keys replace the original's, as on Matrix, where the + # edited message is m.new_content and nothing else. + msg["raw"] <- list(loopback_raw(content)) client$env$log[[pos[[1L]]]] <- msg invisible(message_id) } diff --git a/R/matrix.R b/R/matrix.R index cc84e4a..83a618c 100644 --- a/R/matrix.R +++ b/R/matrix.R @@ -157,7 +157,8 @@ #' (\code{mx.api::mx_upload} and \code{mx_set_avatar_url}). Leave #' NULL in production. #' @param .rich Testing seam: replacement for \code{mx.api::mx_send} on -#' the rich-send path. Leave NULL in production. +#' the rich-send path, and on the path of a send with \code{content} +#' outside an encrypted room. Leave NULL in production. #' @param .edit Testing seam: replacement for \code{mx.api::mx_send} on #' the edit path. Leave NULL in production. #' @return A \code{chat_client} of class \code{chat_matrix}. @@ -277,6 +278,33 @@ matrix_event_times <- function(sync) { out } +# Every joined-room timeline event's content, keyed by event id, the +# same keyed lookup: mx_extract_text_events() keeps 6 fields of a +# message's content, and keys a sender added of its own +# (chat_send(content =)) are none of them. +matrix_event_contents <- function(sync) { + out <- list() + for (room in sync$rooms$join %||% list()) { + for (ev in room$timeline$events %||% list()) { + if (!is.null(ev$event_id) && is.list(ev$content)) { + out[[as.character(ev$event_id)]] <- ev$content + } + } + } + out +} + +# A cleartext record as a message's raw, with its event's whole content +# in raw$content. A decrypted record has content already (mx.client +# keeps the decrypted payload's), and history's raw is the event itself, +# so raw$content reads the same for all three. +matrix_raw_with_content <- function(record, contents) { + if (is.null(record$content)) { + record$content <- contents[[as.character(record$event_id)]] + } + record +} + # Each timeline event's position in the sync, by event id. Walks the same # structure matrix_event_times() does, and for the same reason: it is the # only place the homeserver's own ordering survives, since the cleartext @@ -547,6 +575,7 @@ chat_poll.chat_matrix <- function(client, since = NULL, timeout = NULL, ...) { event_ts <- matrix_event_times(res$sync) event_pos <- matrix_event_order(res$sync) + event_content <- matrix_event_contents(res$sync) recs <- client$extract_fn(res$sync, self_id = res$client$user_id) messages <- lapply(recs, function(r) { # ts is the event's own origin_server_ts, from the record when @@ -568,7 +597,7 @@ chat_poll.chat_matrix <- function(client, since = NULL, timeout = NULL, ...) { markup = "plain", kind = matrix_kind(r$msgtype), self = isTRUE(r$is_self), mentions = unlist(r$mentions, use.names = FALSE), - raw = r) + raw = matrix_raw_with_content(r, event_content)) }) # Decrypted traffic folds in alongside the cleartext, in the same # chat_message shape, so a consumer cannot tell which arrived @@ -686,8 +715,11 @@ chat_send.chat_matrix <- function(client, channel, text, thread = NULL, reply_to = NULL, identity = NULL, files = NULL, kind = "message", notify = TRUE, - rich = NULL, ...) { + rich = NULL, content = NULL, ...) { markup <- match.arg(markup) + # Checked before anything is uploaded, like the encryption question + # below: a refused key must not leave the files posted already. + content <- chat_content_keys(content) # Resolved before anything is uploaded or posted. The encryption # question used to be asked after the attachment loop had already run, # which put the files on the homeserver in the clear before the text @@ -750,10 +782,25 @@ chat_send.chat_matrix <- function(client, channel, text, # shape for a caller's fragment. The text still arrives, and # chat_capabilities()$rich_markup is empty on an e2ee client # so a consumer can know beforehand. - event <- client$crypto_ops$send(crypto, client$env$mx, channel, - text, msgtype = msgtype, + args <- list(crypto, client$env$mx, channel, text, + msgtype = msgtype, + markdown = identical(markup, "markdown"), + mentions = list(...)$mentions) + # A caller's keys go in as extra content, which the Megolm + # path encrypts with the rest. Named only when there are + # some, so a crypto seam without the argument still sends. + if (!is.null(content)) { + args$extra <- content + } + event <- do.call(client$crypto_ops$send, args) + return(invisible(c(media_ids, as.character(event)))) + } + if (!is.null(content)) { + event <- matrix_send_content(client, channel, text, msgtype, + content, rich = rich, markdown = identical(markup, "markdown"), - mentions = list(...)$mentions) + mentions = list(...)$mentions, + thread = thread) return(invisible(c(media_ids, as.character(event)))) } if (!is.null(rich)) { @@ -1187,8 +1234,10 @@ chat_mark_read.chat_matrix <- function(client, channel, message_id, ...) { #' @export chat_edit.chat_matrix <- function(client, channel, message_id, text, markup = c("plain", "markdown"), - rich = NULL, kind = "message", ...) { + rich = NULL, kind = "message", + content = NULL, ...) { markup <- match.arg(markup) + content <- chat_content_keys(content) msgtype <- matrix_msgtype(kind) html <- if (identical(markup, "markdown")) { mx.client::mx_markdown_to_html(text) @@ -1206,6 +1255,10 @@ chat_edit.chat_matrix <- function(client, channel, message_id, text, new_content$format <- "org.matrix.custom.html" new_content$formatted_body <- html } + # A caller's keys belong to the replacement, which is what a client + # that understands edits shows; the fallback copy around it does not + # repeat them. Encrypted, they ride inside the ciphertext with it. + new_content <- c(new_content, content) extra <- list(`m.new_content` = new_content, `m.relates_to` = list(rel_type = "m.replace", event_id = message_id)) if (!is.null(html)) { @@ -1251,19 +1304,54 @@ matrix_send_rich <- function(client, channel, text, rich, msgtype, fn <- client$rich_fn %||% mx.api::mx_send extra <- list(format = "org.matrix.custom.html", formatted_body = rich) if (!is.null(thread)) { - # Built here rather than borrowed from mx_send_text(), which - # this path exists to bypass: it renders its own HTML and has - # nowhere to put a supplied fragment. The relation is the same - # one, fallback included, so a rich threaded reply is threaded - # in exactly the clients a plain one is. - extra[["m.relates_to"]] <- list( - rel_type = "m.thread", event_id = thread, - is_falling_back = TRUE, - "m.in_reply_to" = list(event_id = thread)) + extra[["m.relates_to"]] <- matrix_thread_relation(thread) } fn(sess, channel, text, msgtype = msgtype, extra = extra) } +# A threaded reply's relation, for the sends that bypass mx_send_text(): +# it renders its own HTML and has nowhere to put a supplied fragment or +# a caller's keys. The relation is the one it builds, fallback included, +# so such a reply is threaded in exactly the clients a plain one is. +matrix_thread_relation <- function(thread) { + list(rel_type = "m.thread", event_id = thread, is_falling_back = TRUE, + "m.in_reply_to" = list(event_id = thread)) +} + +# A send carrying a caller's own content keys (chat_send(content =)) in +# a room that is not encrypted. mx_send_text() takes no extra content, +# so this builds the event it would have built -- HTML rendered from +# markdown with the mentions as pills, m.mentions, the thread relation +# -- or carries the caller's fragment as matrix_send_rich() does, and +# adds the keys last. m.mentions goes in with a fragment too, since the +# notification depends on it; only the pills need HTML of chat.api's +# own. Through the rich seam: both are an mx_send() with extra content. +matrix_send_content <- function(client, channel, text, msgtype, content, + rich = NULL, markdown = FALSE, + mentions = NULL, thread = NULL) { + html <- rich + if (is.null(html) && (isTRUE(markdown) || length(mentions))) { + html <- mx.client::mx_markdown_to_html(text) + if (length(mentions)) { + html <- mx.client::mx_pill_mentions(html, mentions) + } + } + extra <- list() + if (!is.null(html)) { + extra$format <- "org.matrix.custom.html" + extra$formatted_body <- html + } + if (length(mentions)) { + extra[["m.mentions"]] <- list(user_ids = as.list(mentions)) + } + if (!is.null(thread)) { + extra[["m.relates_to"]] <- matrix_thread_relation(thread) + } + sess <- mx.client::mx_client_session(client$env$mx) + fn <- client$rich_fn %||% mx.api::mx_send + fn(sess, channel, text, msgtype = msgtype, extra = c(extra, content)) +} + # The contract's kind vocabulary as a Matrix msgtype, plus a documented # way past it: a kind already spelled as a msgtype goes out as itself. # The contract has no word for an image or a file, and mapping those to diff --git a/R/slack.R b/R/slack.R index 0e055c3..138140f 100644 --- a/R/slack.R +++ b/R/slack.R @@ -205,7 +205,8 @@ chat_send.chat_slack <- function(client, channel, text, thread = NULL, reply_to = NULL, identity = NULL, files = NULL, kind = "message", notify = TRUE, - rich = NULL, as_user = FALSE, ...) { + rich = NULL, content = NULL, + as_user = FALSE, ...) { markup <- match.arg(markup) if (isTRUE(as_user) && !nzchar(client$user_token %||% "")) { stop("chat_send(as_user = TRUE) needs a user token ", @@ -645,7 +646,8 @@ chat_set_identity.chat_slack <- function(client, display = NULL, #' @export chat_edit.chat_slack <- function(client, channel, message_id, text, markup = c("plain", "markdown"), - rich = NULL, kind = "message", ...) { + rich = NULL, kind = "message", + content = NULL, ...) { markup <- match.arg(markup) api <- client$api_fn %||% slackr::call_slack_api resp <- api("/api/chat.update", .method = "POST", token = client$token, diff --git a/R/telegram.R b/R/telegram.R index 91105fb..df0ca4d 100644 --- a/R/telegram.R +++ b/R/telegram.R @@ -500,7 +500,7 @@ chat_send.chat_telegram <- function(client, channel, text, thread = NULL, reply_to = NULL, identity = NULL, files = NULL, kind = "message", notify = TRUE, - rich = NULL, ...) { + rich = NULL, content = NULL, ...) { markup <- match.arg(markup) # identity is ignored: a bot posts as itself and Telegram has no # per-message override, which identity_override = FALSE says. kind @@ -538,7 +538,8 @@ chat_send.chat_telegram <- function(client, channel, text, #' @export chat_edit.chat_telegram <- function(client, channel, message_id, text, markup = c("plain", "markdown"), - rich = NULL, kind = "message", ...) { + rich = NULL, kind = "message", + content = NULL, ...) { markup <- match.arg(markup) r <- telegram_render(text, markup, rich) telegram_call(client, "editMessageText", diff --git a/inst/tinytest/test_contract.R b/inst/tinytest/test_contract.R index d7c38bb..dcbeffe 100644 --- a/inst/tinytest/test_contract.R +++ b/inst/tinytest/test_contract.R @@ -315,6 +315,55 @@ local({ "not supported by this adapter") }) +# ---- A caller's content keys on the reference adapter ---- +# Recorded where a Matrix message carries its content, raw$content, so a +# consumer's test reads them as its code does against a homeserver. +local({ + cl <- chat_loopback() + tool <- list(v = 1L, kind = "tool", tool_call_id = "t1", + status = "pending") + id <- chat_send(cl, "general", "[tool] ls (pending)", kind = "notice", + content = list(ai.cornball.agent = tool)) + m <- chat_poll(cl)$messages[[1L]] + expect_identical(m$raw$content, list(ai.cornball.agent = tool)) + expect_identical(m$kind, "notice") + # An edit's keys replace the original's, as the edited Matrix message + # is m.new_content alone; an edit without keys leaves none. + tool$status <- "completed" + chat_edit(cl, "general", id, "[tool] ls (completed)", kind = "notice", + content = list(ai.cornball.agent = tool)) + h <- chat_history(cl, "general")$messages[[1L]] + expect_identical(h$raw$content[["ai.cornball.agent"]]$status, "completed") + chat_edit(cl, "general", id, "plain now") + h <- chat_history(cl, "general")$messages[[1L]] + expect_null(h$raw) + expect_true("raw" %in% names(h)) + # Without keys, a message has no raw, as before; an empty list is + # no keys. + chat_send(cl, "general", "hello", content = list()) + expect_null(chat_poll(cl, since = 1L)$messages[[1L]]$raw) + # The same refusals as Matrix: the reference adapter is where a + # consumer finds out. + expect_error(chat_send(cl, "general", "x", content = list(body = "y")), + "content may not set body") + expect_error(chat_edit(cl, "general", id, "x", + content = list(format = "html")), + "content may not set format") +}) + +# The other adapters take the argument and ignore it, as they ignore +# rich: text is the message there. +local({ + for (fn in list(chat.api:::chat_send.chat_slack, + chat.api:::chat_send.chat_telegram, + chat.api:::chat_send.chat_irc, + chat.api:::chat_edit.chat_slack, + chat.api:::chat_edit.chat_telegram, + chat.api:::chat_edit.default)) { + expect_true("content" %in% names(formals(fn))) + } +}) + # ---- Channel lifecycle on the reference adapter ---- # A created channel exists before any traffic: chat_channels() must # report it, or a consumer that creates and then lists sees its own diff --git a/inst/tinytest/test_matrix.R b/inst/tinytest/test_matrix.R index 3853bc0..63fe690 100644 --- a/inst/tinytest/test_matrix.R +++ b/inst/tinytest/test_matrix.R @@ -2382,3 +2382,228 @@ expect_identical(chat.api:::matrix_msgtype("emote"), "m.emote") expect_identical(chat.api:::matrix_msgtype("message"), "m.text") expect_identical(chat.api:::matrix_msgtype("m.image"), "m.image") expect_identical(chat.api:::matrix_msgtype(NULL), "m.text") + +# ---- A caller's content keys ---- +# A consumer's own data rides in the event's content under a namespaced +# key, which its client reads while every other client shows the body. +# Both ways, cleartext and encrypted. + +agent <- list(v = 1L, kind = "tool", tool_call_id = "t1", status = "pending") +done <- list(v = 1L, kind = "tool", tool_call_id = "t1", status = "completed") + +# A cleartext send puts the keys beside body and msgtype. mx_send_text() +# takes no extra content, so the send goes through mx_send(), and still +# carries what mx_send_text() would have: mentions, thread, markdown. +if (requireNamespace("mx.client", quietly = TRUE)) +local({ + seen <- NULL + plain <- 0L + cl <- seam_client(send = function(...) { + plain <<- plain + 1L + "$plain" + }, + .rich = function(session, channel, text, msgtype = "m.text", + extra = NULL) { + seen <<- list(channel = channel, text = text, msgtype = msgtype, + extra = extra) + "$c1" + }) + expect_identical(chat_send(cl, "!a:ex", "[tool] ls (pending)", + kind = "notice", + content = list(ai.cornball.agent = agent)), + "$c1") + expect_identical(seen$channel, "!a:ex") + expect_identical(seen$text, "[tool] ls (pending)") + expect_identical(seen$msgtype, "m.notice") + expect_identical(seen$extra[["ai.cornball.agent"]], agent) + # Plain, with nobody mentioned: no HTML and no m.mentions to add. + expect_null(seen$extra$format) + expect_null(seen$extra[["m.mentions"]]) + expect_null(seen$extra[["m.relates_to"]]) + + chat_send(cl, "!a:ex", "Turn done, @owner", kind = "notice", + content = list(ai.cornball.agent = done), + mentions = "@owner:ex", thread = "$root") + expect_identical(seen$extra[["m.mentions"]], + list(user_ids = list("@owner:ex"))) + expect_identical(seen$extra$format, "org.matrix.custom.html") + expect_identical(seen$extra[["m.relates_to"]]$rel_type, "m.thread") + expect_identical(seen$extra[["m.relates_to"]]$event_id, "$root") + expect_identical(seen$extra[["ai.cornball.agent"]], done) + + chat_send(cl, "!a:ex", "**ok**", markup = "markdown", + content = list(ai.cornball.agent = agent)) + expect_true(grepl("ok", seen$extra$formatted_body, + fixed = TRUE)) + # A caller's own fragment is sent as given, and its mentions still + # notify. + chat_send(cl, "!a:ex", "ok", rich = "ok", + content = list(ai.cornball.agent = agent), + mentions = "@owner:ex") + expect_identical(seen$extra$formatted_body, "ok") + expect_identical(seen$extra[["m.mentions"]], + list(user_ids = list("@owner:ex"))) + + # Without keys, nothing changes: the ordinary path, and an empty + # list is no keys. + expect_identical(plain, 0L) + chat_send(cl, "!a:ex", "hello") + chat_send(cl, "!a:ex", "hello", content = list()) + expect_identical(plain, 2L) +}) + +# A key chat.api writes itself is refused, before anything is sent: a +# second way to set it would leave the two disagreeing on the wire. +local({ + cl <- seam_client(send = function(...) stop("sent"), + .rich = function(...) stop("sent"), + media = function(...) stop("uploaded")) + expect_error(chat_send(cl, "!a:ex", "x", content = list(body = "y")), + "content may not set body") + expect_error(chat_send(cl, "!a:ex", "x", + content = list(`m.relates_to` = list())), + "m.relates_to") + expect_error(chat_send(cl, "!a:ex", "x", + files = tempfile(), content = list(msgtype = "m.text")), + "msgtype") + expect_error(chat_send(cl, "!a:ex", "x", content = list(1)), + "distinct names") + expect_error(chat_send(cl, "!a:ex", "x", content = "ai.cornball.agent"), + "distinct names") + expect_error(chat_send(cl, "!a:ex", "x", + content = list(a = 1, a = 2)), + "distinct names") + expect_error(chat_edit(cl, "!a:ex", "$o", "x", + content = list(`m.new_content` = list())), + "m.new_content") +}) + +# Encrypted, the keys go in as extra content, which the Megolm path +# encrypts with the rest. No cleartext transport is touched. Without +# keys no extra is named, so a crypto seam without the argument sends. +local({ + ctx <- new.env(parent = emptyenv()) + seen <- list() + ops <- list(init = function(...) ctx, + encrypted = function(crypto, mx, room_id) TRUE, + send = function(crypto, mx, room_id, text, ...) { + seen[[length(seen) + 1L]] <<- c(list(text = text), list(...)) + "$enc" + }, + decrypt = function(...) list()) + cl <- seam_client(mx = fake_mx(user_id = "@e2ee-content:ex"), + .crypto = ops, e2ee = TRUE, + send = function(...) stop("cleartext send"), + .rich = function(...) stop("cleartext send")) + expect_identical(chat_send(cl, "!secret:ex", "[tool] ls (pending)", + kind = "notice", + content = list(ai.cornball.agent = agent)), + "$enc") + expect_identical(seen[[1L]]$text, "[tool] ls (pending)") + expect_identical(seen[[1L]]$msgtype, "m.notice") + expect_identical(seen[[1L]]$extra, list(ai.cornball.agent = agent)) + chat_send(cl, "!secret:ex", "plain") + expect_false("extra" %in% names(seen[[2L]])) +}) + +# An edit's keys go in m.new_content, which is the edited message, and +# the fallback copy around it does not repeat them. Cleartext ... +if (requireNamespace("mx.client", quietly = TRUE)) +local({ + seen <- NULL + cl <- seam_client(.edit = function(session, room_id, body, msgtype = "m.text", + extra = NULL) { + seen <<- list(body = body, msgtype = msgtype, extra = extra) + "$e" + }) + chat_edit(cl, "!a:ex", "$o", "[tool] ls (completed)", kind = "notice", + content = list(ai.cornball.agent = done)) + expect_identical(seen$extra[["m.new_content"]][["ai.cornball.agent"]], done) + expect_identical(seen$extra[["m.new_content"]]$body, "[tool] ls (completed)") + expect_identical(seen$extra[["m.new_content"]]$msgtype, "m.notice") + expect_null(seen$extra[["ai.cornball.agent"]]) + expect_identical(seen$body, "* [tool] ls (completed)") + # Without keys the replacement has none, as before. + chat_edit(cl, "!a:ex", "$o", "x", kind = "notice") + expect_identical(names(seen$extra[["m.new_content"]]), c("msgtype", "body")) +}) +# ... and encrypted, inside the ciphertext. +local({ + ctx <- new.env(parent = emptyenv()) + seen <- NULL + ops <- list(init = function(...) ctx, + encrypted = function(crypto, mx, room_id) TRUE, + send = function(crypto, mx, room_id, text, ...) { + seen <<- c(list(text = text), list(...)) + "$enc" + }, + decrypt = function(...) list()) + cl <- seam_client(mx = fake_mx(user_id = "@e2ee-content-edit:ex"), + .crypto = ops, e2ee = TRUE, + .edit = function(...) stop("must not be reached")) + chat_edit(cl, "!secret:ex", "$orig", "[tool] ls (completed)", + kind = "notice", content = list(ai.cornball.agent = done)) + expect_identical(seen$text, "* [tool] ls (completed)") + expect_identical(seen$extra[["m.new_content"]][["ai.cornball.agent"]], done) + expect_null(seen$extra[["ai.cornball.agent"]]) + expect_identical(seen$extra[["m.relates_to"]], + list(rel_type = "m.replace", event_id = "$orig")) +}) + +# The relation of an encrypted edit has to be readable outside the +# ciphertext, or the server cannot aggregate the edit and a client that +# reads relations off the envelope shows a new message. chat.api hands +# it to mx.client inside the content, and mx.client's envelope copies +# it out; pinned here, since chat.api's encrypted edits rely on that. +if (requireNamespace("mx.client", quietly = TRUE) && + requireNamespace("mx.crypto", quietly = TRUE) && + utils::packageVersion("mx.client") >= "0.3.0") +local({ + account <- mx.crypto::mxc_account_new() + curve <- mx.crypto::mxc_account_identity_keys(account)$curve25519 + rel <- list(rel_type = "m.replace", event_id = "$orig") + env <- mx.client::mx_crypto_encrypt_event( + mx.crypto::mxc_megolm_outbound_new(), + list(msgtype = "m.notice", body = "* x", `m.relates_to` = rel, + `m.new_content` = list(msgtype = "m.notice", body = "x", + ai.cornball.agent = done)), + "!secret:ex", curve, "DEV1") + expect_identical(env[["m.relates_to"]], rel) + # The replacement and its keys are in the ciphertext only. + expect_null(env[["m.new_content"]]) +}) + +# Received, a cleartext message carries its event's whole content in +# raw$content: the extractor's record keeps 6 fields of it, and a +# sender's own keys are none of them. +local({ + answer <- list(v = 1L, kind = "approval_response", approval_id = "a1", + option_id = "allow") + cl <- seam_client(recs = list(rec("$1", body = "/approve a1")), + sync = wrap_sync(list(ev("$1", body = "/approve a1", + content = list(ai.cornball.agent = answer))))) + m <- chat_poll(cl)$messages[[1L]] + expect_identical(m$raw$content[["ai.cornball.agent"]], answer) + expect_identical(m$raw$content$body, "/approve a1") + # The record's own fields stay. + expect_identical(m$raw$event_id, "$1") + expect_identical(m$raw$msgtype, "m.text") + # An event the sync does not carry gets no content, not an error. + cl <- seam_client(recs = list(rec("$2")), sync = wrap_sync(list())) + expect_null(chat_poll(cl)$messages[[1L]]$raw$content) +}) +# A decrypted message has the decrypted payload's content, which +# mx.client keeps, so raw$content reads the same way. +local({ + answer <- list(v = 1L, kind = "approval_response", approval_id = "a1", + option_id = "allow") + d <- dec_rec("$e1", body = "/approve a1") + d$content <- list(msgtype = "m.text", body = "/approve a1", + ai.cornball.agent = answer) + f <- fake_crypto(decrypted = list(d)) + cl <- seam_client(mx = fake_mx(user_id = "@e2ee-recv:ex"), e2ee = TRUE, + .crypto = f$ops) + m <- chat_poll(cl)$messages[[1L]] + expect_true(m$encrypted) + expect_identical(m$raw$content[["ai.cornball.agent"]], answer) +}) diff --git a/inst/tinytest/test_matrix_mxclient.R b/inst/tinytest/test_matrix_mxclient.R index af67a97..6b38441 100644 --- a/inst/tinytest/test_matrix_mxclient.R +++ b/inst/tinytest/test_matrix_mxclient.R @@ -202,10 +202,11 @@ expect_false("a notice" %in% vapply(got$messages, `[[`, "", "body")) # The record shape the adapter reads. The wire's content$m.relates_to # arrives as the record's `relates_to`, so nothing here looks for the -# wire name. +# wire name. The event's whole content rides beside the record, as +# raw$content, which the extractor's fields do not include. expect_true(all(c("room_id", "event_id", "sender", "is_self", "body", "msgtype", "mentions") %in% names(m$raw))) -expect_false("content" %in% names(m$raw)) +expect_identical(m$raw$content$body, m$body) expect_null(m$raw[["m.relates_to"]]) # An unthreaded message has no thread, and NULL is the answer. expect_null(m$thread) diff --git a/man/chat_edit.Rd b/man/chat_edit.Rd index ad35657..a69adf5 100644 --- a/man/chat_edit.Rd +++ b/man/chat_edit.Rd @@ -11,6 +11,7 @@ chat_edit( markup = c("plain", "markdown"), rich = NULL, kind = "message", + content = NULL, ... ) } @@ -41,6 +42,12 @@ the message was sent as: Matrix carries the msgtype inside the replacement content, so editing an \code{m.notice} without saying so turns it into an ordinary message.} +\item{content}{Keys of the caller's own for the replacement, as +\code{\link{chat_send}}. Matrix puts them in \code{m.new_content}, +beside the replacement text. Like the text, they replace the +original's: a key the edit does not repeat is gone from the edited +message.} + \item{...}{Adapter-specific options.} } \value{ diff --git a/man/chat_matrix.Rd b/man/chat_matrix.Rd index 4ecf1da..4d64ac2 100644 --- a/man/chat_matrix.Rd +++ b/man/chat_matrix.Rd @@ -217,7 +217,8 @@ Leave NULL in production.} the edit path. Leave NULL in production.} \item{.rich}{Testing seam: replacement for \code{mx.api::mx_send} on -the rich-send path. Leave NULL in production.} +the rich-send path, and on the path of a send with \code{content} +outside an encrypted room. Leave NULL in production.} \item{.avatar}{Testing seam: replacement for the upload-then-set pair behind \code{chat_set_identity(avatar =)} diff --git a/man/chat_message.Rd b/man/chat_message.Rd index 4c0d334..36d19b4 100644 --- a/man/chat_message.Rd +++ b/man/chat_message.Rd @@ -59,7 +59,12 @@ substring matching misses rich mentions that carry no plain text.} exactly as the transport layer handed it over. Shape is adapter-specific and may already be normalized by the transport package (Matrix hands over an extracted record, not the timeline -event), so it is an escape hatch, not a guarantee of completeness.} +event), so it is an escape hatch, not a guarantee of completeness. +One part is promised: a Matrix message, cleartext or decrypted, +carries its event's whole content in \code{raw$content}, with any +keys a sender added of its own (\code{\link{chat_send}}'s +\code{content}), and the loopback adapter records those keys +there.} \item{encrypted}{Logical: did this message arrive end-to-end encrypted? FALSE on transports without E2EE and on cleartext diff --git a/man/chat_send.Rd b/man/chat_send.Rd index 39cf511..f8eb52b 100644 --- a/man/chat_send.Rd +++ b/man/chat_send.Rd @@ -15,6 +15,7 @@ chat_send( kind = "message", notify = TRUE, rich = NULL, + content = NULL, ... ) } @@ -62,6 +63,23 @@ Ignored rather than refused where unsupported, on the markup is decoration, so losing it costs presentation and nothing else.} +\item{content}{Keys of the caller's own for the message's content, as +a named list, or NULL. Matrix adds them to the \code{m.room.message} +content beside \code{body} and \code{msgtype}, inside the ciphertext +in an encrypted room, so a consumer can carry data its own client +reads and every other client ignores, under a namespaced key such +as \code{"ai.cornball.agent"}. A received message carries its +content in \code{raw$content}, and the loopback adapter records +these keys there too; the other adapters ignore them, as they +ignore \code{rich}, so \code{text} has to stand on its own. + +A key chat.api writes itself is refused: \code{body}, +\code{msgtype}, \code{format}, \code{formatted_body}, +\code{m.relates_to}, \code{m.mentions} and \code{m.new_content}. +\code{text}, \code{kind}, \code{rich}, \code{thread} and the +\code{mentions} option set those. With \code{files}, the keys go on +the text's event, not on the attachments'.} + \item{...}{Adapter-specific options.} } \value{ From 126801620488c12ee73fbb0244b3ed40ce770c92 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 18 Nov 2026 12:13:12 -0600 Subject: [PATCH 2/5] Put an edit's content keys in its fallback copy too The keys went in m.new_content only. A client showing the edit event itself, its original out of view, saw the fallback without them. They now go in both, as chat_edit() already does with formatted text. Asked for by the app that reads the keys. --- R/contract.R | 3 ++- R/matrix.R | 9 ++++++--- inst/tinytest/test_matrix.R | 11 +++++++---- man/chat_edit.Rd | 3 ++- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/R/contract.R b/R/contract.R index e0f85ee..bb23f35 100644 --- a/R/contract.R +++ b/R/contract.R @@ -1237,7 +1237,8 @@ chat_relogin.default <- function(client, ...) { #' turns it into an ordinary message. #' @param content Keys of the caller's own for the replacement, as #' \code{\link{chat_send}}. Matrix puts them in \code{m.new_content}, -#' beside the replacement text. Like the text, they replace the +#' beside the replacement text, and in the fallback copy around it, as +#' it does the formatted text. Like the text, they replace the #' original's: a key the edit does not repeat is gone from the edited #' message. #' @param ... Adapter-specific options. diff --git a/R/matrix.R b/R/matrix.R index 83a618c..b64a8fa 100644 --- a/R/matrix.R +++ b/R/matrix.R @@ -1255,9 +1255,11 @@ chat_edit.chat_matrix <- function(client, channel, message_id, text, new_content$format <- "org.matrix.custom.html" new_content$formatted_body <- html } - # A caller's keys belong to the replacement, which is what a client - # that understands edits shows; the fallback copy around it does not - # repeat them. Encrypted, they ride inside the ciphertext with it. + # A caller's keys go in the replacement, which is what a client that + # understands edits shows, and in the fallback copy around it, as the + # formatted text does: a client showing the edit event itself, its + # original out of view, still has them. Encrypted, they ride inside + # the ciphertext with the rest. new_content <- c(new_content, content) extra <- list(`m.new_content` = new_content, `m.relates_to` = list(rel_type = "m.replace", event_id = message_id)) @@ -1269,6 +1271,7 @@ chat_edit.chat_matrix <- function(client, channel, message_id, text, # it is a correction rather than the bot repeating itself. extra$formatted_body <- paste0("* ", html) } + extra <- c(extra, content) # In an encrypted room the whole event -- the "* " fallback body, the # m.new_content and the relation -- is encrypted with Megolm, so the # replacement text never reaches the homeserver in the clear. The diff --git a/inst/tinytest/test_matrix.R b/inst/tinytest/test_matrix.R index 63fe690..48d7a17 100644 --- a/inst/tinytest/test_matrix.R +++ b/inst/tinytest/test_matrix.R @@ -2507,7 +2507,8 @@ local({ }) # An edit's keys go in m.new_content, which is the edited message, and -# the fallback copy around it does not repeat them. Cleartext ... +# in the fallback copy around it, as the formatted text does: a client +# showing the edit event itself still has them. Cleartext ... if (requireNamespace("mx.client", quietly = TRUE)) local({ seen <- NULL @@ -2521,11 +2522,13 @@ local({ expect_identical(seen$extra[["m.new_content"]][["ai.cornball.agent"]], done) expect_identical(seen$extra[["m.new_content"]]$body, "[tool] ls (completed)") expect_identical(seen$extra[["m.new_content"]]$msgtype, "m.notice") - expect_null(seen$extra[["ai.cornball.agent"]]) + expect_identical(seen$extra[["ai.cornball.agent"]], done) expect_identical(seen$body, "* [tool] ls (completed)") - # Without keys the replacement has none, as before. + # Without keys the replacement has none, and the fallback none, as + # before. chat_edit(cl, "!a:ex", "$o", "x", kind = "notice") expect_identical(names(seen$extra[["m.new_content"]]), c("msgtype", "body")) + expect_identical(names(seen$extra), c("m.new_content", "m.relates_to")) }) # ... and encrypted, inside the ciphertext. local({ @@ -2545,7 +2548,7 @@ local({ kind = "notice", content = list(ai.cornball.agent = done)) expect_identical(seen$text, "* [tool] ls (completed)") expect_identical(seen$extra[["m.new_content"]][["ai.cornball.agent"]], done) - expect_null(seen$extra[["ai.cornball.agent"]]) + expect_identical(seen$extra[["ai.cornball.agent"]], done) expect_identical(seen$extra[["m.relates_to"]], list(rel_type = "m.replace", event_id = "$orig")) }) diff --git a/man/chat_edit.Rd b/man/chat_edit.Rd index a69adf5..1ca59bd 100644 --- a/man/chat_edit.Rd +++ b/man/chat_edit.Rd @@ -44,7 +44,8 @@ turns it into an ordinary message.} \item{content}{Keys of the caller's own for the replacement, as \code{\link{chat_send}}. Matrix puts them in \code{m.new_content}, -beside the replacement text. Like the text, they replace the +beside the replacement text, and in the fallback copy around it, as +it does the formatted text. Like the text, they replace the original's: a key the edit does not repeat is gone from the edited message.} From cf976954381cd4bff8357dfc1aef34b33ae0f119 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 18 Nov 2026 15:10:32 -0600 Subject: [PATCH 3/5] Carry a picture's event content in its raw too chat_message()'s raw now promises the event's whole content in raw$content for a Matrix message. A picture reached chat_poll() with the media record alone as its raw. --- R/matrix.R | 10 +++++----- inst/tinytest/test_matrix.R | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/R/matrix.R b/R/matrix.R index b64a8fa..80955e0 100644 --- a/R/matrix.R +++ b/R/matrix.R @@ -294,10 +294,10 @@ matrix_event_contents <- function(sync) { out } -# A cleartext record as a message's raw, with its event's whole content -# in raw$content. A decrypted record has content already (mx.client -# keeps the decrypted payload's), and history's raw is the event itself, -# so raw$content reads the same for all three. +# A cleartext record, text or media, as a message's raw, with its event's +# whole content in raw$content. A decrypted record has content already +# (mx.client keeps the decrypted payload's), and history's raw is the +# event itself, so raw$content reads the same for all of them. matrix_raw_with_content <- function(record, contents) { if (is.null(record$content)) { record$content <- contents[[as.character(record$event_id)]] @@ -666,7 +666,7 @@ chat_poll.chat_matrix <- function(client, since = NULL, timeout = NULL, ...) { url = as.character(a$url), sha256 = a$sha256 %||% NA_character_, raw = a)), - raw = a) + raw = matrix_raw_with_content(a, event_content)) } # Back into the order the homeserver sent them. Appending the # decrypted events put every one of them after every cleartext one, diff --git a/inst/tinytest/test_matrix.R b/inst/tinytest/test_matrix.R index 48d7a17..e79b2be 100644 --- a/inst/tinytest/test_matrix.R +++ b/inst/tinytest/test_matrix.R @@ -2594,6 +2594,20 @@ local({ # An event the sync does not carry gets no content, not an error. cl <- seam_client(recs = list(rec("$2")), sync = wrap_sync(list())) expect_null(chat_poll(cl)$messages[[1L]]$raw$content) + # A picture is a message too: its raw has the event's content, and + # the media record's own fields stay. + cl <- seam_client(sync = wrap_sync(list(ev("$img", body = "cat.png", + msgtype = "m.image", + content = list(url = "mxc://ex/abc", + ai.cornball.agent = answer)))), + .extract_media = function(sync, self_id) { + list(media_rec("$img", body = "cat.png")) + }) + m <- chat_poll(cl)$messages[[1L]] + expect_identical(m$raw$content[["ai.cornball.agent"]], answer) + expect_identical(m$raw$content$msgtype, "m.image") + expect_identical(m$raw$url, "mxc://ex/abc") + expect_null(m$attachments[[1L]]$raw$content) }) # A decrypted message has the decrypted payload's content, which # mx.client keeps, so raw$content reads the same way. From 77b34c5661e20689e361e86c06c98e4d45530e64 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 18 Nov 2026 15:13:20 -0600 Subject: [PATCH 4/5] Read pictures and encrypted messages in history chat_history() on Matrix skipped every picture and every encrypted event. A restart's backfill reads history, so it never saw a picture sent while a bot was down, and saw nothing of an encrypted room. History now reads its page through the poll's own media and decrypt steps and builds the same messages, through helpers both share. --- R/matrix.R | 196 ++++++++++++++++++++++++------------ inst/tinytest/test_matrix.R | 115 ++++++++++++++++++++- 2 files changed, 245 insertions(+), 66 deletions(-) diff --git a/R/matrix.R b/R/matrix.R index 80955e0..2e96b3a 100644 --- a/R/matrix.R +++ b/R/matrix.R @@ -305,6 +305,71 @@ matrix_raw_with_content <- function(record, contents) { record } +# An event's time, from its origin_server_ts in milliseconds; NA when +# unknown. +matrix_ts <- function(ms) { + if (is.null(ms)) { + return(as.POSIXct(NA)) + } + as.POSIXct(ms / 1000, origin = "1970-01-01") +} + +# A decrypted record as a chat_message, the same from chat_poll() and +# chat_history(). NULL for one that is not a message: mx.client hands +# back every decrypted room event (a key, a ring notification, a +# reaction), and those have no body; a chat_message with an empty body +# is what a consumer trips over. +matrix_decrypted_message <- function(d, ms, self_id) { + if (!is.character(d$body) || length(d$body) != 1L) { + return(NULL) + } + chat_message(id = as.character(d$event_id), + channel = as.character(d$room_id), + sender = as.character(d$sender), + body = as.character(d$body), ts = matrix_ts(ms), + thread = matrix_thread_root(d$relates_to), + markup = "plain", kind = matrix_kind(d$msgtype), + self = isTRUE(d$is_self), + mentions = unlist(d$mentions, use.names = FALSE), + encrypted = TRUE, + sender_verified = isTRUE(d$sender_verified), + attachments = matrix_decrypted_attachments(d, self_id), + raw = d) +} + +# A cleartext media record as a chat_message, the same from chat_poll() +# and chat_history(). A picture is its own m.room.message event on +# Matrix, so it becomes its own chat_message carrying one attachment -- +# the same shape chat_send() produces outbound, where each file is sent +# as a separate event. `contents` holds the events' content by id. +matrix_media_message <- function(a, ms, contents) { + name <- a$filename %||% a$body + chat_message(id = as.character(a$event_id), + channel = as.character(a$room_id), + sender = as.character(a$sender), + # The body is the filename Matrix ships as the event's + # text fallback. Kept, because it is what a client that + # cannot render the picture shows, and what a consumer + # writing a transcript has to write. + body = as.character(a$body %||% ""), ts = matrix_ts(ms), + thread = matrix_thread_root(a$relates_to), + markup = "plain", kind = "message", + self = isTRUE(a$is_self), + mentions = unlist(a$mentions, use.names = FALSE), + encrypted = isTRUE(a$encrypted), + attachments = list(chat_attachment( + id = as.character(a$url), + name = if (is.null(name)) NA_character_ else + as.character(name), + mime = a$mime %||% NA_character_, + bytes = if (is.null(a$size)) NA_integer_ else + as.integer(a$size), + url = as.character(a$url), + sha256 = a$sha256 %||% NA_character_, + raw = a)), + raw = matrix_raw_with_content(a, contents)) +} + # Each timeline event's position in the sync, by event id. Walks the same # structure matrix_event_times() does, and for the same reason: it is the # only place the homeserver's own ordering survives, since the cleartext @@ -605,68 +670,20 @@ chat_poll.chat_matrix <- function(client, since = NULL, timeout = NULL, ...) { # owning crypto here: corteza used to run its own decrypt off $raw and # concatenate the results itself. for (d in dec) { - # Only messages. mx.client hands back every decrypted room event - # (a key, a ring notification, a reaction), and those have - # no body; a chat_message with an empty body is what a consumer - # trips over. - if (!is.character(d$body) || length(d$body) != 1L) { - next - } ms <- d$ts %||% event_ts[[as.character(d$event_id)]] - messages[[length(messages) + 1L]] <- chat_message( - id = as.character(d$event_id), - channel = as.character(d$room_id), - sender = as.character(d$sender), - body = as.character(d$body), - ts = if (is.null(ms)) as.POSIXct(NA) else - as.POSIXct(ms / 1000, origin = "1970-01-01"), - thread = matrix_thread_root(d$relates_to), - markup = "plain", kind = matrix_kind(d$msgtype), - self = isTRUE(d$is_self), - mentions = unlist(d$mentions, use.names = FALSE), - encrypted = TRUE, - sender_verified = isTRUE(d$sender_verified), - attachments = matrix_decrypted_attachments(d, res$client$user_id), - raw = d) + m <- matrix_decrypted_message(d, ms, res$client$user_id) + if (!is.null(m)) { + messages[[length(messages) + 1L]] <- m + } } - # Inbound media. A picture is its own m.room.message event on - # Matrix, so it becomes its own chat_message carrying one - # attachment -- the same shape chat_send() produces outbound, where - # each file is sent as a separate event. - # - # The text extractor filters to text msgtypes, so without this pass - # an image is not merely unreadable, it is invisible: nothing in the - # room's traffic says a picture was ever sent. + # Inbound media. The text extractor filters to text msgtypes, so + # without this pass an image is not merely unreadable, it is + # invisible: nothing in the room's traffic says a picture was ever + # sent. for (a in matrix_media_records(client, res$sync, res$client$user_id)) { ms <- a$ts %||% event_ts[[as.character(a$event_id)]] - name <- a$filename %||% a$body - messages[[length(messages) + 1L]] <- chat_message( - id = as.character(a$event_id), - channel = as.character(a$room_id), - sender = as.character(a$sender), - # The body is the filename Matrix ships as the event's - # text fallback. Kept, because it is what a client that - # cannot render the picture shows, and what a consumer - # writing a transcript has to write. - body = as.character(a$body %||% ""), - ts = if (is.null(ms)) as.POSIXct(NA) else - as.POSIXct(ms / 1000, origin = "1970-01-01"), - thread = matrix_thread_root(a$relates_to), - markup = "plain", kind = "message", - self = isTRUE(a$is_self), - mentions = unlist(a$mentions, use.names = FALSE), - encrypted = isTRUE(a$encrypted), - attachments = list(chat_attachment( - id = as.character(a$url), - name = if (is.null(name)) NA_character_ else - as.character(name), - mime = a$mime %||% NA_character_, - bytes = if (is.null(a$size)) NA_integer_ else - as.integer(a$size), - url = as.character(a$url), - sha256 = a$sha256 %||% NA_character_, - raw = a)), - raw = matrix_raw_with_content(a, event_content)) + messages[[length(messages) + 1L]] <- matrix_media_message(a, ms, + event_content) } # Back into the order the homeserver sent them. Appending the # decrypted events put every one of them after every cleartext one, @@ -1145,6 +1162,30 @@ chat_channels.chat_matrix <- function(client, ...) { as.character(fn(sess)) } +# The page's encrypted messages, decrypted, by event id. Empty on a +# client without E2EE. A page that cannot be decrypted is reported and +# read for its cleartext: history is context, and an encrypted event this +# device holds no key for stays unread either way, as it does in a poll. +matrix_history_decrypt <- function(client, page) { + if (!isTRUE(client$e2ee)) { + return(list()) + } + dec <- tryCatch({ + crypto <- matrix_crypto_require(client) + client$crypto_ops$decrypt(crypto, page, client$env$mx) + }, error = function(e) { + message("chat.api: could not decrypt history: ", conditionMessage(e)) + list() + }) + out <- list() + for (d in dec) { + if (!is.null(d$event_id)) { + out[[as.character(d$event_id)]] <- d + } + } + out +} + #' @export chat_history.chat_matrix <- function(client, channel, limit = 50L, cursor = NULL, ...) { @@ -1164,18 +1205,49 @@ chat_history.chat_matrix <- function(client, channel, limit = 50L, # contract promises oldest-first, and this is the one place that # knows which direction it asked for. chunk <- rev(chunk) + # The page in the shape a sync has, for the 2 readers chat_poll() + # uses, so a picture or an encrypted message reads the same from + # either: what a restart's backfill sees is what the poll would + # have. + self_id <- client$env$mx$user_id + page <- stats::setNames(list(list(timeline = list(events = chunk))), + channel) + page <- list(rooms = list(join = page)) + decrypted <- matrix_history_decrypt(client, page) + media <- list() + for (a in matrix_media_records(client, page, self_id)) { + media[[as.character(a$event_id)]] <- a + } + contents <- matrix_event_contents(page) out <- list() for (ev in chunk) { + id <- as.character(ev$event_id %||% "") + if (nzchar(id) && !is.null(decrypted[[id]])) { + d <- decrypted[[id]] + m <- matrix_decrypted_message(d, d$ts %||% ev$origin_server_ts, + self_id) + if (!is.null(m)) { + out[[length(out) + 1L]] <- m + } + next + } if (!isTRUE(ev$type == "m.room.message")) { next } + if (nzchar(id) && !is.null(media[[id]])) { + a <- media[[id]] + out[[length(out) + 1L]] <- matrix_media_message(a, + a$ts %||% ev$origin_server_ts, contents) + next + } msgtype <- ev$content$msgtype %||% "" kind <- matrix_kind_strict(msgtype) if (is.na(kind)) { - # A msgtype the contract has no word for -- an image, a - # file. Skipping beats inventing a kind: a consumer - # replaying history into a transcript would otherwise get an - # empty "message" where a picture was. + # A msgtype the contract has no word for, with nothing to + # fetch: a location, a picture with no address. Skipping + # beats inventing a kind: a consumer replaying history into + # a transcript would otherwise get an empty "message" where + # something else was. next } ms <- ev$origin_server_ts diff --git a/inst/tinytest/test_matrix.R b/inst/tinytest/test_matrix.R index e79b2be..90e4f4c 100644 --- a/inst/tinytest/test_matrix.R +++ b/inst/tinytest/test_matrix.R @@ -2009,11 +2009,13 @@ local({ if (requireNamespace("mx.client", quietly = TRUE)) local({ - # A msgtype the contract has no word for is dropped, not renamed. - # matrix_kind() answers "message" for anything, so an m.image would - # otherwise arrive as a text message whose body is a filename. + # A msgtype the contract has no word for, with nothing to fetch, is + # dropped, not renamed. matrix_kind() answers "message" for + # anything, so a picture with no address would otherwise arrive as a + # text message whose body is a filename. cl <- seam_client(.history = function(...) { - list(chunk = list(hev("$2", "cat.png", msgtype = "m.image"), + list(chunk = list(hev("$3", "here", msgtype = "m.location"), + hev("$2", "cat.png", msgtype = "m.image"), hev("$1", "look"))) }) h <- chat_history(cl, "!a:ex")$messages @@ -2047,6 +2049,111 @@ local({ expect_identical(h[[1L]]$mentions, "@ann:ex") }) +# A picture is history too: it arrives where it was sent, as the same +# message the poll would have delivered, attachment included. A restart's +# backfill reads history, so a picture sent while a bot was down is +# otherwise never seen. +img_ev <- function(id, ts = 1700000001000) { + list(type = "m.room.message", event_id = id, sender = "@ann:ex", + origin_server_ts = ts, + content = list(msgtype = "m.image", body = "cat.png", + url = "mxc://ex/cat", + info = list(mimetype = "image/png", size = 2048L))) +} +if (chat.api:::matrix_media_available()) +local({ + cl <- seam_client(.history = function(...) { + list(chunk = list(hev("$2", "nice", ts = 1700000002000), + img_ev("$img"), + hev("$1", "look", ts = 1700000000000))) + }) + h <- chat_history(cl, "!a:ex")$messages + expect_identical(vapply(h, function(m) m$id, ""), c("$1", "$img", "$2")) + pic <- h[[2L]] + expect_identical(pic$body, "cat.png") + expect_identical(pic$kind, "message") + expect_identical(pic$attachments[[1L]]$url, "mxc://ex/cat") + expect_identical(pic$attachments[[1L]]$mime, "image/png") + expect_identical(pic$attachments[[1L]]$bytes, 2048L) + expect_identical(pic$raw$content$url, "mxc://ex/cat") + expect_null(h[[1L]]$attachments) + # The poll's message for the same event, field for field. + polled <- seam_client(sync = wrap_sync(list(img_ev("$img")), room = "!a:ex")) + expect_identical(chat_poll(polled)$messages[[1L]], pic) +}) + +# An encrypted room's history is decrypted with the keys this device +# holds, through the same step the poll takes, and the messages keep +# their places among the cleartext ones. +enc_ev <- function(id, ts = 1700000000500) { + list(type = "m.room.encrypted", event_id = id, sender = "@alice:ex", + origin_server_ts = ts, + content = list(algorithm = "m.megolm.v1.aes-sha2", + ciphertext = "opaque", session_id = "s1")) +} +history_chunk <- function(...) { + list(chunk = list(hev("$c2", "after", ts = 1700000002000), enc_ev("$e1"), + hev("$c1", "before", ts = 1700000000000))) +} +local({ + f <- fake_crypto(decrypted = list(dec_rec("$e1", room_id = "!a:ex"))) + ops <- f$ops + seen <- NULL + inner <- ops$decrypt + ops$decrypt <- function(crypto, sync, mx) { + seen <<- sync + inner(crypto, sync, mx) + } + cl <- seam_client(mx = fake_mx(user_id = "@e2ee-recv:ex"), e2ee = TRUE, + .crypto = ops, .history = history_chunk, + .extract_media = function(...) list()) + h <- chat_history(cl, "!a:ex")$messages + expect_identical(vapply(h, function(m) m$id, ""), c("$c1", "$e1", "$c2")) + expect_true(h[[2L]]$encrypted) + expect_identical(h[[2L]]$body, "psst") + expect_true(h[[2L]]$sender_verified) + # The record had no time of its own; the event's is used. + expect_identical(as.numeric(h[[2L]]$ts), 1700000000.5) + expect_false(isTRUE(h[[1L]]$encrypted)) + # The page went to the decrypt step as the sync it expects, under + # the room it came from. + expect_identical(names(seen$rooms$join), "!a:ex") + expect_identical(length(seen$rooms$join[["!a:ex"]]$timeline$events), 3L) + expect_identical(f$log$decrypted, 1L) +}) + +# A page that cannot be decrypted is reported, and its cleartext still +# arrives. +local({ + f <- fake_crypto(decrypt_error = TRUE) + cl <- seam_client(mx = fake_mx(user_id = "@e2ee-recv:ex"), e2ee = TRUE, + .crypto = f$ops, .history = history_chunk, + .extract_media = function(...) list()) + expect_message(h <- chat_history(cl, "!a:ex")$messages, + "could not decrypt history") + expect_identical(vapply(h, function(m) m$id, ""), c("$c1", "$c2")) +}) + +# A decrypted event that is not a message (a key, a reaction) has no +# body and is no message here either. A client without E2EE asks +# nothing of the crypto layer and reads the cleartext alone. +local({ + f <- fake_crypto(decrypted = list(list(event_id = "$e1", room_id = "!a:ex", + type = "m.reaction", + content = list()))) + cl <- seam_client(mx = fake_mx(user_id = "@e2ee-recv:ex"), e2ee = TRUE, + .crypto = f$ops, .history = history_chunk, + .extract_media = function(...) list()) + h <- chat_history(cl, "!a:ex")$messages + expect_identical(vapply(h, function(m) m$id, ""), c("$c1", "$c2")) + plain <- fake_crypto(decrypted = list(dec_rec("$e1", room_id = "!a:ex"))) + cl <- seam_client(.crypto = plain$ops, .history = history_chunk, + .extract_media = function(...) list()) + h <- chat_history(cl, "!a:ex")$messages + expect_identical(vapply(h, function(m) m$id, ""), c("$c1", "$c2")) + expect_identical(plain$log$decrypted, 0L) +}) + # ---- State: pending ---- if (requireNamespace("mx.client", quietly = TRUE)) local({ From 84e1cf773294a47054444a45baa03031613aa554 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 18 Nov 2026 17:29:14 -0600 Subject: [PATCH 5/5] Bump version to 0.2.0.2 --- DESCRIPTION | 2 +- NEWS.md | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 2d2d66a..af8d5d5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: chat.api Type: Package Title: Transport-Agnostic Chat Contract -Version: 0.2.0.1 +Version: 0.2.0.2 Date: 2026-11-18 Authors@R: c( person("Troy", "Hernandez", role = c("aut", "cre"), diff --git a/NEWS.md b/NEWS.md index be678b6..ef6dfde 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,13 @@ +# chat.api 0.2.0.2 + +* `chat_history()` on Matrix returns pictures and encrypted messages, as + `chat_poll()` does. It dropped every picture and every encrypted + event, so a bot that read history after a restart missed the pictures + sent while it was down and saw nothing of an encrypted room. History + decrypts its page with the poll's own step, which may request a + missing key from the account's other devices; a page that cannot be + decrypted is reported and read for its cleartext. + # chat.api 0.2.0.1 * `chat_send()` and `chat_edit()` take `content`, a named list of keys