Skip to content

Add read status to messages_fetch - #195

Merged
mattt merged 3 commits into
mattt:mainfrom
sebpretzer:unread-message-filtering
Sep 2, 2026
Merged

mattt merged 3 commits into
mattt:mainfrom
sebpretzer:unread-message-filtering

Conversation

@sebpretzer

@sebpretzer sebpretzer commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Hey @mattt, just wanted to follow-up on unread message filtering.

This PR adds an optional isRead filter to messages_fetch. I try to mirror the patterns you use in Madrid and for other iMCP endpoints. Closes #153 and closes #154

Let me know if I should adjust this in any way.

Thanks!

iMCP has been pinned to Madrid 0.1.0. Madrid has since shipped 0.2.0,
0.3.0, and 0.4.0. This bumps the pin to 0.4.0 with no behaviour change,
so that later work can use APIs added in those releases (notably
`Message.readAt` / `Message.isRead` from 0.3.0, for mattt#154).

The version requirement was already `upToNextMajorVersion` from 0.1.0,
which 0.4.0 satisfies, so this is mechanically a re-resolve. The declared
minimum is raised alongside the lockfile to keep the two in agreement.

Source compatibility was verified by compiling iMCP's Madrid call sites
(`Database()`, `Database(path:)`, `fetchParticipant(matching:)`,
`fetchMessages(with:in:limit:)`, and the `Message` properties read in
`messages_fetch`) against 0.4.0. They compile unchanged.

Behaviour was checked against 0.1.0 for the two cases that could have
differed, and matches in both:

- With no participants, 0.1.0 skipped the participant condition. In
  0.4.0 the deprecated `fetchMessages` shim likewise omits the predicate
  for an empty set, and `.and([])` compiles to an absent WHERE clause
  rather than a false one.
- Message ordering was `ORDER BY m.date DESC` in 0.1.0, and the 0.4.0
  default sort is `[.date(.descending), .id(.descending)]` — the same
  order, with an added tiebreak.

Co-Authored-By: Claude Opus 5 <[email protected]>
Closes mattt#154.

Adds an optional `isRead` filter to `messages_fetch`, and reports read
status on every message it returns:

- `isRead` (input, optional): if true, fetch read messages; if false,
  unread incoming; if omitted, fetch all.
- `isRead` (output): present on every message.
- `dateRead` (output): ISO 8601, omitted when the message is unread.

The tri-state input mirrors `reminders_fetch`'s `completed` parameter
rather than introducing a single-purpose `unreadOnly` flag, so the same
parameter also expresses "only messages I have read".

Filtering for unread excludes outgoing messages. Read receipts populate a
read date on messages you sent, so without that guard "unread" would also
report your own messages the recipient has not read yet.

Read status is derived from Madrid's `Message.readAt`, which is the read
timestamp `chat.db` records rather than a dedicated unread flag. A message
marked read without a recorded timestamp therefore counts as unread, so
this is a superset of genuinely-unread and may not match the Messages app
badge exactly.

Known limitation: `messages_fetch` fetches `max(limit, 1024)` candidates
and filters them down to `limit`, so the filter only sees the most recent
1024 messages. A large *read* backlog can push unread messages outside
that window. Filtering in SQL instead would remove the limitation, but
Madrid has no read-status predicate to compose; mattt/Madrid#17 proposes
one. The tool's schema would not change if the filtering later moves into
the query.

Co-Authored-By: Claude Opus 5 <[email protected]>
# Conflicts:
#	iMCP.xcodeproj/project.pbxproj
#	iMCP.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
@mattt

mattt commented Sep 2, 2026

Copy link
Copy Markdown
Owner

Thanks for this, @sebpretzer, and sorry for the wait. This mirrors the Madrid API nicely. I merged main in to pick up the Madrid 0.5.0 bump. Merging now.

@mattt
mattt merged commit 91fc121 into mattt:main Sep 2, 2026
toonvl added a commit to toonvl/iMCP that referenced this pull request Sep 2, 2026
Brings in the six upstream commits behind #2: the SIGTRAP
continuation fix (mattt#151), the Bonjour browser cleanup (mattt#199), the approval
setup-timeout exemption (mattt#200), the events_fetch date-only fix (mattt#175),
Madrid 0.5.0 with FetchRequest<Message> (mattt#201), and message read status
(mattt#195).

Three files conflicted, all because upstream shipped its own version of a
fix this fork already carried:

- CLI/main.swift: took upstream. Its mattt#199 supersedes our inline
  browser.cancel() (our closed upstream PR mattt#196) by extracting
  BonjourDiscovery.discoverEndpoint, which cancels on every exit path and
  comes with tests.
- App/Extensions/Foundation+Extensions.swift: took upstream. Its
  `count > 10` guard on the timezone-suffix regex is equivalent to our
  `!isDateOnlyISO8601String(...)`; staying identical to upstream is worth
  more than the marginal precision.
- App/Services/Calendar.swift: kept ours. Upstream only annotates
  `identifier` on the events_fetch and events_create returns, which our
  private encode(_:) helper already does alongside organizedByMe,
  organizer, isRecurring and coordinates.

Co-Authored-By: Claude Opus 5 <[email protected]>
@sebpretzer
sebpretzer deleted the unread-message-filtering branch September 2, 2026 23:08
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.

Feature request: Add unread message filtering to messages_fetch

2 participants