Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 18 additions & 6 deletions R/contract.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
#'
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions R/irc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion R/loopback.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 44 additions & 20 deletions R/matrix-config.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions R/matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
11 changes: 9 additions & 2 deletions R/slack.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions R/telegram.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions inst/tinytest/test_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down
2 changes: 2 additions & 0 deletions man/chat_capabilities.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion man/chat_matrix.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ chat_matrix(
.identity = NULL,
.edit = NULL,
.rich = NULL,
.call = NULL
.call = NULL,
.avatar = NULL
)
}
\arguments{
Expand Down Expand Up @@ -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}.
Expand Down
20 changes: 15 additions & 5 deletions man/chat_set_identity.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
Expand All @@ -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")
}
}
Loading