Skip to content
Closed
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
5 changes: 5 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ system channels out of these reads unless the filter asks for them (see
[evalexpr](https://docs.rs/evalexpr) for condition evaluation. Keep expressions
simple and testable.

Workflow mention candidates include ordinary display names and verified
`display_name`, `username`, and `full_name` aliases for destination-channel
members in the same community. Alias collisions across distinct keys remain
ambiguous. Verified-only profiles do not require a separate kind:0 profile.

**Thread counters**: `reply_count` and `descendant_count` are materialized on
thread root events. Any code that inserts replies must update these counters —
check existing reply handlers for the pattern.
Expand Down
2 changes: 1 addition & 1 deletion crates/buzz-db/src/runtime/observability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ mod tests {
.get_members_for_event_write(test_scope, Uuid::new_v4())
.await;
let _ = writer_db
.get_users_bulk_for_event_write(test_scope, &[vec![11; 32]])
.get_mention_names_for_event_write(test_scope, &[vec![11; 32]], true)
.await;
let _ = writer_db
.huddle_started_link_exists_for_event_write(
Expand Down
61 changes: 53 additions & 8 deletions crates/buzz-db/src/store/channel_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2781,20 +2781,65 @@ impl Db {
get_users_bulk(&self.pool, community_id, pubkeys).await
}

/// Bulk-fetch user names while constructing an event and its mention tags.
#[datastore_span(name = "get_users_bulk_for_event_write", system = "postgresql")]
pub async fn get_users_bulk_for_event_write(
/// Fetch ordinary display names and, when `include_verified`, verified
/// name aliases for mention tags.
///
/// Returns `(name, pubkey_hex)` pairs for the supplied community-local members.
/// Keep every alias so the caller can reject names shared by distinct keys.
#[datastore_span(name = "get_mention_names_for_event_write", system = "postgresql")]
pub async fn get_mention_names_for_event_write(
&self,
community_id: CommunityId,
pubkeys: &[Vec<u8>],
) -> Result<Vec<UserRecord>> {
get_users_bulk_with_operation(
include_verified: bool,
) -> Result<Vec<(String, String)>> {
if pubkeys.is_empty() {
return Ok(Vec::new());
}
let mut connection = crate::observability::acquire_writer(
&self.pool,
community_id,
pubkeys,
crate::observability::WriterOperation::EventWrite,
)
.await
.await?;
let rows = sqlx::query(
r#"
SELECT member.pubkey, u.display_name,
jsonb_path_query_first(v.event, 'strict $.tags[*] ? (@[0] == "display_name")[1]', '{}', true) #>> '{}' AS verified_display_name,
jsonb_path_query_first(v.event, 'strict $.tags[*] ? (@[0] == "username")[1]', '{}', true) #>> '{}' AS verified_username,
jsonb_path_query_first(v.event, 'strict $.tags[*] ? (@[0] == "full_name")[1]', '{}', true) #>> '{}' AS verified_full_name
FROM unnest($2::bytea[]) AS member(pubkey)
LEFT JOIN users u ON u.community_id = $1 AND u.pubkey = member.pubkey
LEFT JOIN nip_fi_profile_heads v
ON $3 AND v.community_id = $1 AND v.pubkey = encode(member.pubkey, 'hex')
"#,
)
.bind(community_id.as_uuid())
.bind(pubkeys)
.bind(include_verified)
.fetch_all(&mut *connection)
.await?;
drop(connection);

// One entry per case-folded name and member, so a member whose aliases
// repeat a name does not make the resolver scan for it again.
let mut seen = std::collections::HashSet::new();
let mut names = Vec::new();
for row in rows {
let pubkey = hex::encode(row.try_get::<Vec<u8>, _>("pubkey")?);
for column in [
"display_name",
"verified_display_name",
"verified_username",
"verified_full_name",
] {
if let Some(name) = row.try_get::<Option<String>, _>(column)? {
if seen.insert((name.to_lowercase(), pubkey.clone())) {
names.push((name, pubkey.clone()));
}
}
}
}
Ok(names)
}

/// Returns the count of active members in a channel.
Expand Down
4 changes: 2 additions & 2 deletions crates/buzz-db/tests/observability_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ fn p0_pool_acquisitions_use_typed_operation_pairs_without_other() {
assert!(channel_members.contains("async fn get_members_with_operation("));
assert!(channel_members.contains("pub async fn get_members_for_event_write("));
assert!(channel_members.contains("async fn get_users_bulk_with_operation("));
assert!(channel_members.contains("pub async fn get_users_bulk_for_event_write("));
assert!(channel_members.contains("pub async fn get_mention_names_for_event_write("));

let huddle_link = event
.split_once("async fn huddle_started_link_exists_with_operation(")
Expand All @@ -331,7 +331,7 @@ fn p0_pool_acquisitions_use_typed_operation_pairs_without_other() {

let workflow_sink = include_str!("../../buzz-relay/src/workflow_sink.rs");
assert!(workflow_sink.contains(".get_members_for_event_write("));
assert!(workflow_sink.contains(".get_users_bulk_for_event_write("));
assert!(workflow_sink.contains(".get_mention_names_for_event_write("));

for write_caller in [
include_str!("../../buzz-relay/src/handlers/side_effects.rs"),
Expand Down
Loading
Loading