From fe7514769077ee5c5dfd874cda901fa1fd637f4e Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 21 Oct 2026 16:56:59 -0500 Subject: [PATCH 1/2] chat_set_identity() can set the account's picture `avatar` is a path to an image, alongside `display`. Matrix uploads it and points the profile at it (testing seam `.avatar`); Slack, Telegram and IRC have no API for a bot's picture, so they ignore it and answer FALSE when it was all that was asked for. `chat_capabilities()$avatar` says which adapters set it. Both arguments are optional now. --- NEWS.md | 10 ++++++ R/contract.R | 24 ++++++++++---- R/irc.R | 11 +++++-- R/loopback.R | 3 +- R/matrix-config.R | 64 +++++++++++++++++++++++++------------ R/matrix.R | 12 +++++-- R/slack.R | 11 +++++-- R/telegram.R | 11 +++++-- inst/tinytest/test_matrix.R | 24 ++++++++++++++ man/chat_capabilities.Rd | 2 ++ man/chat_matrix.Rd | 8 ++++- man/chat_set_identity.Rd | 20 +++++++++--- 12 files changed, 158 insertions(+), 42 deletions(-) diff --git a/NEWS.md b/NEWS.md index c068457..e1023fd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,13 @@ +# chat.api 0.1.0.3 + +* `chat_set_identity()` takes `avatar`, a path to an image, alongside + `display`: both are the account's persistent identity. Matrix uploads + the file and points the profile at it (testing seam `.avatar`); + adapters whose platform sets a bot's picture outside its API ignore + it and answer FALSE when it was all that was asked for; + `chat_capabilities()$avatar` says which is which. Both arguments are + optional now, and asking for nothing is FALSE. + # chat.api 0.1.0.2 * `chat_channel_invite()` invites a user to a channel, the other side diff --git a/R/contract.R b/R/contract.R index f22b124..adc01f4 100644 --- a/R/contract.R +++ b/R/contract.R @@ -150,6 +150,8 @@ chat_resolve <- function(client, name, ...) { #' media comes back out of \code{\link{chat_poll}} as #' \code{\link{chat_attachment}} records), \code{typing}, #' \code{e2ee}, \code{calls} (\code{\link{chat_call_join}} works), +#' \code{avatar} (\code{\link{chat_set_identity}} can set the +#' account's picture, not only its name), #' \code{identity_override} (logicals), #' \code{user_identity} (a send can authenticate as a real member of #' the platform rather than as the bot -- Slack's @@ -1006,8 +1008,9 @@ chat_mark_read.default <- function(client, channel, message_id, ...) { #' Set this client's persistent identity #' -#' The account's own display name, as everyone in every channel sees it -#' until it is changed again. Distinct from \code{\link{chat_send}}'s +#' The account's own display name and, where the platform lets a bot +#' set one, its picture, as everyone in every channel sees them until +#' they are changed again. Distinct from \code{\link{chat_send}}'s #' \code{identity} argument, which decorates a single message on #' platforms that allow it. #' @@ -1020,22 +1023,31 @@ chat_mark_read.default <- function(client, channel, message_id, ...) { #' has to know it happened. #' #' @param client A \code{chat_client}. -#' @param display New display name. +#' @param display New display name, or NULL to leave it as it is. +#' @param avatar Path to an image file for the account's picture, or +#' NULL to leave it. Set where the platform lets a bot change its own +#' picture: Matrix uploads the file and points the profile at it. An +#' adapter whose platform sets a bot's picture outside its API +#' ignores it, on \code{\link{chat_typing}}'s reasoning, and answers +#' FALSE when it was all that was asked for. #' @param ... Adapter-specific options. -#' @return TRUE if the identity was changed, invisibly. +#' @return TRUE if the identity was changed, invisibly; FALSE when +#' nothing was asked for that the adapter can set. #' @examples #' \dontrun{ #' # Requires a saved Matrix configuration and account credentials. #' cl <- chat_matrix(app = "mybot") #' chat_set_identity(cl, "Example Bot") +#' chat_set_identity(cl, avatar = "face.png") #' } #' @export -chat_set_identity <- function(client, display, ...) { +chat_set_identity <- function(client, display = NULL, avatar = NULL, ...) { UseMethod("chat_set_identity") } #' @export -chat_set_identity.default <- function(client, display, ...) { +chat_set_identity.default <- function(client, display = NULL, avatar = NULL, + ...) { stop("chat_set_identity() is not supported by this adapter (", paste(class(client), collapse = "/"), "). Check chat_capabilities()$set_identity.", call. = FALSE) diff --git a/R/irc.R b/R/irc.R index 939621c..57a12c6 100644 --- a/R/irc.R +++ b/R/irc.R @@ -136,7 +136,8 @@ chat_capabilities.chat_irc <- function(client, ...) { reactions = FALSE, reaction_events = FALSE, channel_info = FALSE, members = FALSE, invites = FALSE, join = FALSE, whoami = TRUE, channels = FALSE, history = FALSE, pending = FALSE, - mark_read = FALSE, set_identity = TRUE, relogin = FALSE, + mark_read = FALSE, set_identity = TRUE, avatar = FALSE, + relogin = FALSE, # IRC JOIN would create a channel implicitly, but this adapter # has no join verb yet, so neither flag can be TRUE honestly. channel_create = FALSE, leave = FALSE, channel_invite = FALSE, @@ -193,7 +194,13 @@ chat_disconnect.chat_irc <- function(client, ...) { } #' @export -chat_set_identity.chat_irc <- function(client, display, ...) { +chat_set_identity.chat_irc <- function(client, display = NULL, avatar = NULL, + ...) { + # IRC has no pictures, and no display name distinct from the nick, + # so this is a NICK change or nothing. + if (is.null(display)) { + return(invisible(FALSE)) + } # IRC has no display name distinct from the nick, so this is a NICK # change. The server can refuse it (collision, length, restricted # characters) and says so asynchronously in a numeric this adapter diff --git a/R/loopback.R b/R/loopback.R index 41d2831..f6f53d7 100644 --- a/R/loopback.R +++ b/R/loopback.R @@ -159,7 +159,8 @@ chat_capabilities.chat_loopback <- function(client, ...) { reactions = FALSE, reaction_events = FALSE, channel_info = FALSE, members = FALSE, invites = FALSE, join = FALSE, whoami = TRUE, channels = TRUE, history = TRUE, pending = FALSE, - mark_read = FALSE, set_identity = FALSE, relogin = FALSE, + mark_read = FALSE, set_identity = FALSE, avatar = FALSE, + relogin = FALSE, channel_create = TRUE, leave = FALSE, channel_invite = TRUE, set_state = TRUE, # files records the paths it was handed; attachments hands diff --git a/R/matrix-config.R b/R/matrix-config.R index 7b14e41..6011848 100644 --- a/R/matrix-config.R +++ b/R/matrix-config.R @@ -208,30 +208,54 @@ chat_relogin.chat_matrix <- function(client, ...) { } #' @export -chat_set_identity.chat_matrix <- function(client, display, ...) { - fn <- client$identity_fn %||% mx.client::mx_set_displayname - # The failure is held, not swallowed: the reload below has to run - # either way, and the caller still has to hear that the rename did - # not happen. - # - # mx_set_displayname() wraps itself in mx_with_relogin(), which - # persists a refreshed config *before* retrying and then returns only - # TRUE, discarding the client it refreshed. So a relogin whose retry - # then failed -- rate limit, transient 5xx -- has still rotated the - # token and still written it, and the live one is on disk while this - # client holds the rejected one. Reloading only on success is exactly - # how the next send goes out on a token the homeserver already - # refused, and it fails silently, because nothing in this stack - # relogins on a send. - err <- NULL - tryCatch(fn(client$env$mx, display), error = function(e) err <<- e) - matrix_reload_into(client) - if (!is.null(err)) { - stop(err) +chat_set_identity.chat_matrix <- function(client, display = NULL, + avatar = NULL, ...) { + if (is.null(display) && is.null(avatar)) { + return(invisible(FALSE)) + } + if (!is.null(display)) { + fn <- client$identity_fn %||% mx.client::mx_set_displayname + # The failure is held, not swallowed: the reload below has to run + # either way, and the caller still has to hear that the rename did + # not happen. + # + # mx_set_displayname() wraps itself in mx_with_relogin(), which + # persists a refreshed config *before* retrying and then returns only + # TRUE, discarding the client it refreshed. So a relogin whose retry + # then failed -- rate limit, transient 5xx -- has still rotated the + # token and still written it, and the live one is on disk while this + # client holds the rejected one. Reloading only on success is exactly + # how the next send goes out on a token the homeserver already + # refused, and it fails silently, because nothing in this stack + # relogins on a send. + err <- NULL + tryCatch(fn(client$env$mx, display), error = function(e) err <<- e) + matrix_reload_into(client) + if (!is.null(err)) { + stop(err) + } + } + if (!is.null(avatar)) { + # After the rename, so a token the rename rotated is the one the + # upload goes out on. Errors propagate: a picture that silently + # did not change is the old picture, and the caller believes + # otherwise. + avatar_fn <- client$avatar_fn %||% matrix_set_avatar + avatar_fn(client$env$mx, avatar) } invisible(TRUE) } +# Upload an image and make it the account's picture. mx_upload() picks +# the content type from the file's extension, which is what a client +# renders the picture by. +matrix_set_avatar <- function(mx, path) { + sess <- mx.client::mx_client_session(mx) + mxc <- mx.api::mx_upload(sess, path) + mx.api::mx_set_avatar_url(sess, mxc) + invisible(mxc) +} + # Re-read the config from wherever this client's was loaded and adopt it. # A client with no app and no path has nothing to re-read -- it was # handed a config directly and nobody is persisting it -- and a read that diff --git a/R/matrix.R b/R/matrix.R index 148a6c3..4964d76 100644 --- a/R/matrix.R +++ b/R/matrix.R @@ -151,6 +151,10 @@ #' \code{mx.api::mx_read_receipt}. Leave NULL in production. #' @param .identity Testing seam: replacement for #' \code{mx.client::mx_set_displayname}. Leave NULL in production. +#' @param .avatar Testing seam: replacement for the upload-then-set +#' pair behind \code{chat_set_identity(avatar =)} +#' (\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. #' @param .edit Testing seam: replacement for \code{mx.api::mx_send} on @@ -187,7 +191,8 @@ chat_matrix <- function(app = NULL, path = NULL, save_cursor = TRUE, .get_state = NULL, .extract_media = NULL, .download = NULL, .channels = NULL, .history = NULL, .pending = NULL, .read = NULL, .identity = NULL, - .edit = NULL, .rich = NULL, .call = NULL) { + .edit = NULL, .rich = NULL, .call = NULL, + .avatar = NULL) { seams <- list(.sync, .extract, .send, .media) if ((is.null(mx) || any(vapply(seams, is.null, logical(1)))) && !requireNamespace("mx.client", quietly = TRUE)) { @@ -248,7 +253,8 @@ chat_matrix <- function(app = NULL, path = NULL, save_cursor = TRUE, download_fn = .download, channels_fn = .channels, history_fn = .history, pending_fn = .pending, read_fn = .read, - identity_fn = .identity, edit_fn = .edit, + identity_fn = .identity, avatar_fn = .avatar, + edit_fn = .edit, rich_fn = .rich, crypto_ops = matrix_crypto_ops(.crypto), call_ops = matrix_call_ops(.call), call_override = .call), @@ -995,7 +1001,7 @@ chat_capabilities.chat_matrix <- function(client, ...) { invites = matrix_invites_available(), join = TRUE, whoami = TRUE, channels = TRUE, history = TRUE, pending = matrix_invites_available(), mark_read = TRUE, - set_identity = TRUE, relogin = TRUE, + set_identity = TRUE, avatar = TRUE, relogin = TRUE, channel_create = TRUE, leave = TRUE, channel_invite = TRUE, set_state = TRUE, files = !isTRUE(client$e2ee), diff --git a/R/slack.R b/R/slack.R index 5c9cbb0..7b09265 100644 --- a/R/slack.R +++ b/R/slack.R @@ -463,7 +463,8 @@ chat_capabilities.chat_slack <- function(client, ...) { # different thing, and does work. invites = FALSE, join = TRUE, whoami = TRUE, channels = TRUE, history = TRUE, pending = FALSE, - mark_read = TRUE, set_identity = TRUE, relogin = FALSE, + mark_read = TRUE, set_identity = TRUE, avatar = FALSE, + relogin = FALSE, channel_create = TRUE, leave = TRUE, channel_invite = FALSE, set_state = FALSE, files = FALSE, attachments = FALSE, typing = FALSE, e2ee = FALSE, @@ -623,7 +624,13 @@ chat_mark_read.chat_slack <- function(client, channel, message_id, ...) { } #' @export -chat_set_identity.chat_slack <- function(client, display, ...) { +chat_set_identity.chat_slack <- function(client, display = NULL, + avatar = NULL, ...) { + # A bot's picture is set in the app configuration, not through the + # API; the display name is the one piece this adapter can change. + if (is.null(display)) { + return(invisible(FALSE)) + } api <- client$api_fn %||% slackr::call_slack_api resp <- api("/api/users.profile.set", .method = "POST", token = client$token, diff --git a/R/telegram.R b/R/telegram.R index daa148d..dddf9df 100644 --- a/R/telegram.R +++ b/R/telegram.R @@ -651,7 +651,8 @@ chat_capabilities.chat_telegram <- function(client, ...) { # No history, no list of chats, no read markers in the Bot # API. The chats a bot is in are whatever sends it updates. channels = FALSE, history = FALSE, pending = FALSE, - mark_read = FALSE, set_identity = TRUE, relogin = FALSE, + mark_read = FALSE, set_identity = TRUE, avatar = FALSE, + relogin = FALSE, channel_create = FALSE, leave = TRUE, channel_invite = FALSE, set_state = FALSE, files = TRUE, attachments = TRUE, typing = TRUE, e2ee = FALSE, @@ -711,7 +712,13 @@ chat_addressed.chat_telegram <- function(client, message, ...) { } #' @export -chat_set_identity.chat_telegram <- function(client, display, ...) { +chat_set_identity.chat_telegram <- function(client, display = NULL, + avatar = NULL, ...) { + # The picture is BotFather's to set, so only a name is this + # adapter's to change. + if (is.null(display)) { + return(invisible(FALSE)) + } # setMyName changes the bot's name, not its username: the handle # people address it by is fixed at BotFather. The cached identity # carried the old display and is now wrong. diff --git a/inst/tinytest/test_matrix.R b/inst/tinytest/test_matrix.R index 7df8356..24916b6 100644 --- a/inst/tinytest/test_matrix.R +++ b/inst/tinytest/test_matrix.R @@ -2119,12 +2119,36 @@ expect_error(chat_set_identity(structure(list(), "chat_client")), "x"), "not supported by this adapter") +# The picture goes through one seam standing for upload-then-set, and +# the display name is left alone when only the picture was asked for. +# Nothing asked for is FALSE; a refused upload propagates. +local({ + seen <- NULL + renamed <- FALSE + cl <- seam_client(.identity = function(client, name, ...) { + renamed <<- TRUE + TRUE + }, .avatar = function(mx, path) { + seen <<- path + "mxc://ex/abc" + }) + expect_true(chat_set_identity(cl, avatar = "face.png")) + expect_identical(seen, "face.png") + expect_false(renamed) + expect_false(chat_set_identity(cl)) +}) + +expect_error(chat_set_identity(seam_client(.avatar = function(...) { + stop("M_TOO_LARGE") +}), avatar = "face.png"), "M_TOO_LARGE") + local({ caps <- chat_capabilities(seam_client()) expect_true(caps$channels) expect_true(caps$history) expect_true(caps$mark_read) expect_true(caps$set_identity) + expect_true(caps$avatar) expect_true(caps$relogin) expect_identical(caps$pending, chat.api:::matrix_invites_available()) }) diff --git a/man/chat_capabilities.Rd b/man/chat_capabilities.Rd index 390e4ca..07783a3 100644 --- a/man/chat_capabilities.Rd +++ b/man/chat_capabilities.Rd @@ -32,6 +32,8 @@ A list with at least: \code{threads} (can post into media comes back out of \code{\link{chat_poll}} as \code{\link{chat_attachment}} records), \code{typing}, \code{e2ee}, \code{calls} (\code{\link{chat_call_join}} works), + \code{avatar} (\code{\link{chat_set_identity}} can set the + account's picture, not only its name), \code{identity_override} (logicals), \code{user_identity} (a send can authenticate as a real member of the platform rather than as the bot -- Slack's diff --git a/man/chat_matrix.Rd b/man/chat_matrix.Rd index 83fc6d0..e4f1091 100644 --- a/man/chat_matrix.Rd +++ b/man/chat_matrix.Rd @@ -36,7 +36,8 @@ chat_matrix( .identity = NULL, .edit = NULL, .rich = NULL, - .call = NULL + .call = NULL, + .avatar = NULL ) } \arguments{ @@ -222,6 +223,11 @@ the rich-send path. Leave NULL in production.} call operations -- \code{join}, \code{handle}, \code{leave} (\code{mx.client::mx_call_join}, \code{mx_call_handle}, \code{mx_call_leave}). Leave NULL in production.} + +\item{.avatar}{Testing seam: replacement for the upload-then-set +pair behind \code{chat_set_identity(avatar =)} +(\code{mx.api::mx_upload} and \code{mx_set_avatar_url}). Leave +NULL in production.} } \value{ A \code{chat_client} of class \code{chat_matrix}. diff --git a/man/chat_set_identity.Rd b/man/chat_set_identity.Rd index 3e8d7d5..6778195 100644 --- a/man/chat_set_identity.Rd +++ b/man/chat_set_identity.Rd @@ -3,21 +3,30 @@ \alias{chat_set_identity} \title{Set this client's persistent identity} \usage{ -chat_set_identity(client, display, ...) +chat_set_identity(client, display = NULL, avatar = NULL, ...) } \arguments{ \item{client}{A \code{chat_client}.} -\item{display}{New display name.} +\item{display}{New display name, or NULL to leave it as it is.} + +\item{avatar}{Path to an image file for the account's picture, or +NULL to leave it. Set where the platform lets a bot change its own +picture: Matrix uploads the file and points the profile at it. An +adapter whose platform sets a bot's picture outside its API +ignores it, on \code{\link{chat_typing}}'s reasoning, and answers +FALSE when it was all that was asked for.} \item{...}{Adapter-specific options.} } \value{ -TRUE if the identity was changed, invisibly. +TRUE if the identity was changed, invisibly; FALSE when + nothing was asked for that the adapter can set. } \description{ -The account's own display name, as everyone in every channel sees it -until it is changed again. Distinct from \code{\link{chat_send}}'s +The account's own display name and, where the platform lets a bot +set one, its picture, as everyone in every channel sees them until +they are changed again. Distinct from \code{\link{chat_send}}'s \code{identity} argument, which decorates a single message on platforms that allow it. } @@ -36,5 +45,6 @@ has to know it happened. # Requires a saved Matrix configuration and account credentials. cl <- chat_matrix(app = "mybot") chat_set_identity(cl, "Example Bot") +chat_set_identity(cl, avatar = "face.png") } } From f65d4d03b84cf85ed0464cd8853501d3dedd877b Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Wed, 21 Oct 2026 16:56:59 -0500 Subject: [PATCH 2/2] Bump version to 0.1.0.3 --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 962f9f9..1090ff4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: chat.api Type: Package Title: Transport-Agnostic Chat Contract -Version: 0.1.0.2 +Version: 0.1.0.3 Date: 2026-10-21 Authors@R: c( person("Troy", "Hernandez", role = c("aut", "cre"),