Skip to content

fix(desktop): populate member_count in get_channels so channel browser shows real counts#548

Merged
tlongwell-block merged 2 commits into
mainfrom
sami/channel-browser-member-count
May 15, 2026
Merged

fix(desktop): populate member_count in get_channels so channel browser shows real counts#548
tlongwell-block merged 2 commits into
mainfrom
sami/channel-browser-member-count

Conversation

@tlongwell-block
Copy link
Copy Markdown
Collaborator

Summary

The channel browser dialog shows 0 members for every channel because get_channels always returns member_count: 0. This PR fixes that by computing the count from kind:39002 events in the same Tauri command.

Root cause

ChannelInfo.member_count is populated in nostr_convert::channel_info_from_event, which reads it from a kind:40901 "channel summary" sidecar event. Two problems:

  1. Every caller in commands/channels.rs passes None for the summary.
  2. The relay never emits kind:40901 — the const is defined in sprout-core::kind but nothing publishes it.

The top bar in ChannelMembersBar appears to work only because it ignores channel.memberCount and does its own live kind:39002 fetch via useChannelMembersQuery, counting members client-side. The browser dialog renders channel.memberCount directly and so always shows 0.

Fix

After get_channels assembles the channel list, issue a single batched query for kind:39002 events with #d set to every listed channel id, then overwrite member_count for each channel from the unique p-tag pubkey count. One extra relay round-trip total, not N.

If the query fails we silently fall back to 0 (the prior behavior) so a sidecar miss can't break the whole channel listing.

Why fix at the Tauri layer

channel.memberCount flows through every channel listing in the UI. Fixing it at the command level means anything that reads the field gets the correct value — not just the browser dialog.

Future work (not in this PR)

If/when the kind:40901 channel summary sidecar is implemented relay-side, the existing summary path in channel_info_from_event can supersede this extra query and it can be removed.

Verification

  • cargo check — clean
  • cargo test --lib — all 245 tests pass
  • cargo clippy — no new warnings on changed file
  • pnpm build (desktop) — clean
  • All pre-push hooks pass (rust-fmt, desktop-tauri-fmt, rust-clippy, rust-tests, desktop-check, desktop-tauri-check, web-check, web-build, desktop-build, mobile-check, mobile-test)

…r shows real counts

The channel browser dialog renders `channel.memberCount` directly, but
`get_channels` was always returning 0 for that field. The value comes
from a kind:40901 'channel summary' sidecar in
`channel_info_from_event`, and every caller passed `None` for the
summary — the relay doesn't emit kind:40901 yet either.

The top bar's count appeared to work only because
`ChannelMembersBar` does its own live kind:39002 fetch via
`useChannelMembersQuery` and counts that, ignoring
`channel.memberCount`.

Fix at the Tauri layer: after assembling the channel list, issue a
single batched kind:39002 query for every channel id and count unique
p-tag pubkeys. One extra relay round-trip total, not N. If the query
fails we fall back to 0 (prior behavior) rather than breaking the
whole listing.

When the kind:40901 summary sidecar is implemented relay-side this
extra query can be removed in favor of the existing summary path.

Signed-off-by: tlongwell-block <109685178+tlongwell-block@users.noreply.github.com>
… add tests

Replace the inline kind:39002 dedupe/count loop with a small helper that
delegates p-tag parsing to nostr_convert::channel_members_from_event,
matching get_channel_members. One source of truth for member dedupe.

Add five unit tests for count_members_by_channel covering: per-channel
counts, duplicate-pubkey dedupe, missing d-tag (skipped), zero-member
channel (recorded as 0 so it overwrites the default), and empty input.
Tests live in a sibling channels_tests.rs to keep channels.rs under the
per-file line cap.

Signed-off-by: tlongwell-block <109685178+tlongwell-block@users.noreply.github.com>
@tlongwell-block tlongwell-block force-pushed the sami/channel-browser-member-count branch from deeddcd to ebaae2f Compare May 12, 2026 20:12
@wesbillman
Copy link
Copy Markdown
Collaborator

@codex review please

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ebaae2f7e3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +131 to +133
"kinds": [39002],
"#d": all_d_tags,
"limit": all_d_tags.len(),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Restrict member-count query to relay-signed 39002 events

The new get_channels member-count fetch filters only by kinds and #d, so any kind:39002 event with a matching d tag is accepted regardless of author. Because count_members_by_channel trusts these events, a non-relay publisher can inject a forged members event (same channel id, inflated p tags) and the browser will show an incorrect member_count. This is especially reachable because 39002 is not treated as relay-only in ingest; please constrain this query to the trusted relay author (or the same author as the channel metadata event) before counting.

Useful? React with 👍 / 👎.

@tlongwell-block tlongwell-block merged commit 5cdb306 into main May 15, 2026
15 checks passed
@tlongwell-block tlongwell-block deleted the sami/channel-browser-member-count branch May 15, 2026 00:38
tlongwell-block added a commit that referenced this pull request May 15, 2026
* origin/main: (33 commits)
  dev-mcp: add view_image tool (#602)
  fix(relay,desktop): only advertise NIP-43 when enforced; probe pairing by supported_nips (#601)
  fix(desktop): derive unread state from NIP-RS + relay catch-up only (#599)
  docs(testing): rewrite TESTING.md for current API and CLI-first workflow (#597)
  fix(agent): fix OpenAI-compat request body serialization and max_tokens (#595)
  feat(desktop): per-persona and per-agent env var overrides (#594)
  fix(desktop): stop pinning agents to deprecated SPROUT_ACP_TURN_TIMEOUT (#592)
  fix(desktop): populate member_count in get_channels so channel browser shows real counts (#548)
  fix(desktop): autofocus message composer on channel/thread open (#572)
  refactor(cli): restructure flat commands into 12 subcommand groups (#585)
  feat(sdk): add builder functions for workflows, DMs, and presence (#589)
  feat(desktop): add message more-actions dropdown menu (#590)
  fix(mobile): preserve channel list across background/resume reconnection (#588)
  Redesign Home as an inbox (#582)
  fix(desktop): drive unread badges from live subscription, not refetched lastMessageAt (#581)
  fix(desktop): refine header scaling and shadow (#573)
  fix(desktop): keep day dividers below header (#574)
  Move agent activity below composer (#579)
  docs(nips): NIP-AE — Agent Engrams (#575)
  refactor: extract shared @mention resolver into sprout-sdk (#580)
  ...

Signed-off-by: Tyler Longwell <tlongwell@squareup.com>
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.

2 participants