Skip to content

Read media content with [[, not $ - #24

Merged
TroyHernandez merged 2 commits into
mainfrom
media-filename-fix
Aug 21, 2026
Merged

TroyHernandez merged 2 commits into
mainfrom
media-filename-fix

Conversation

@TroyHernandez

Copy link
Copy Markdown
Contributor

mx_extract_media_events() threw $ operator is invalid for atomic vectors on the first live image it ever saw — a photo posted to a bot room, which came back as no media at all because the consumer wraps the call in a tryCatch.

Cause

$ partial-matches on a list. An image whose content carries filename — what Element sends for an ordinary photo, and what any client sends when body is a caption rather than the file name — has no exact file key, so content$file resolved to that filename string:

ev$content$file            # "IMG_0942.png", not NULL
ev$content$file$hashes     # $ operator is invalid for atomic vectors

encrypted = !is.null(ev$content$file) would also have been TRUE for a cleartext picture, had it got that far.

Fix

Every content field is read with [[. The three that can hold a non-list are checked with is.list() before being indexed into (file, info, m.mentions), and a content that is not an object at all is skipped rather than taken apart.

Why the tests missed it

None of the cleartext fixtures carried a filename, which is the field that creates the ambiguity. The encrypted fixture does carry one — but it also carries a real file, so $ matched exactly there and nothing was ever wrong.

A cleartext-with-filename case is in now, asserting encrypted and file by value rather than only that the call returns something. Reverting the fix makes it error.

mx_extract_text_events() reads content with $ too. I left it alone: its reads are body, msgtype, m.mentions, m.relates_to, none of which is followed by a second $ on a possibly-atomic value, so the same ambiguity cannot produce this failure there. Worth a sweep at some point, not in this fix.

239 tests pass.

mx_extract_media_events() threw "$ operator is invalid for atomic
vectors" on the first live image it ever saw.

`$` partial-matches on a list. An image whose content carries
`filename` -- what Element sends for an ordinary photo, and what any
client sends when `body` is a caption rather than the file name -- has
no exact `file` key, so `content$file` resolved to that filename
string. `content$file$hashes` is then `$` on a character vector, which
is the error, and `encrypted` would have been TRUE for a cleartext
picture had it got that far.

Every content field is read with [[ now, and the three that can hold a
non-list are checked with is.list() before being indexed into: `file`,
`info`, and `m.mentions`. A content that is not an object at all is
skipped rather than taken apart.

The fixtures did not catch it because none of the cleartext ones
carried a `filename`, which is the field that creates the ambiguity.
The encrypted fixture does carry one, but it also carries a real
`file`, so `$` matched exactly there and nothing was ever wrong. A
cleartext-with-filename case is in now, asserting `encrypted` and
`file` by value rather than only that the call returns.
@TroyHernandez
TroyHernandez merged commit 202485d into main Aug 21, 2026
2 checks passed
@TroyHernandez
TroyHernandez deleted the media-filename-fix branch August 21, 2026 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant