Skip to content

Commit 5bfd5ca

Browse files
wesbillmanBrain
andauthored
feat: per-community workspace icon set by admins, served via NIP-11 (#1463)
Signed-off-by: Wes <[email protected]> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
1 parent 01b92fa commit 5bfd5ca

22 files changed

Lines changed: 858 additions & 69 deletions

File tree

‎NOSTR.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ the sender to be authenticated (NIP-42) as the relay owner or an admin.
262262
| 9030 | Add member | `["p", "<hex-pubkey>"]`, optional `["role", "member\|admin"]` |
263263
| 9031 | Remove member | `["p", "<hex-pubkey>"]`, optional `["role", "member\|admin"]` |
264264
| 9032 | Change role | `["p", "<hex-pubkey>"]`, `["role", "member\|admin"]` |
265+
| 9033 | Set workspace profile (icon) | `["icon", "<https-url or data:image/* URL>"]` (empty clears) |
265266

266267
Example using `nak`:
267268

@@ -295,6 +296,13 @@ After each add/remove/role-change, the relay publishes a kind:13534 membership l
295296
nak req -k 13534 --auth --sec <privkey> ws://localhost:3000
296297
```
297298

299+
A kind:9033 command similarly makes the relay store the workspace icon (per
300+
community) and serve it in the standard NIP-11 `icon` field of its relay
301+
information document. Clients render it in the workspace rail/switcher; anyone
302+
can read it (`curl -H 'Accept: application/nostr+json' http://localhost:3000`),
303+
but only admins/owners can set it. Full spec:
304+
[docs/nips/NIP-WP.md](docs/nips/NIP-WP.md).
305+
298306
### Known Limitations
299307

300308
1. **CLI intentionally does not emit kind 8000/8001 deltas** — `publish_nip43_delta` is

‎crates/buzz-core/src/kind.rs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ pub const RELAY_ADMIN_ADD_MEMBER: u32 = 9030;
187187
pub const RELAY_ADMIN_REMOVE_MEMBER: u32 = 9031;
188188
/// NIP-43: Change the role of an existing relay member.
189189
pub const RELAY_ADMIN_CHANGE_ROLE: u32 = 9032;
190+
/// Buzz: Set the workspace profile (icon). Admin/owner-signed command.
191+
pub const RELAY_ADMIN_SET_WORKSPACE_PROFILE: u32 = 9033;
190192
// NIP-43 relay membership announcement events (relay-signed)
191193
/// NIP-43: Relay membership list snapshot (relay-signed, replaceable by convention).
192194
pub const KIND_NIP43_MEMBERSHIP_LIST: u32 = 13534;
@@ -455,6 +457,7 @@ pub const ALL_KINDS: &[u32] = &[
455457
RELAY_ADMIN_ADD_MEMBER,
456458
RELAY_ADMIN_REMOVE_MEMBER,
457459
RELAY_ADMIN_CHANGE_ROLE,
460+
RELAY_ADMIN_SET_WORKSPACE_PROFILE,
458461
KIND_NIP43_MEMBERSHIP_LIST,
459462
KIND_NIP43_MEMBER_ADDED,
460463
KIND_NIP43_MEMBER_REMOVED,
@@ -568,11 +571,15 @@ pub const fn is_workflow_execution_kind(kind: u32) -> bool {
568571
kind >= KIND_WORKFLOW_TRIGGERED && kind <= KIND_WORKFLOW_APPROVAL_DENIED
569572
}
570573

571-
/// Returns `true` if `kind` is a NIP-43 relay membership admin command (9030–9032).
574+
/// Returns `true` if `kind` is a NIP-43 relay membership admin command (9030–9032)
575+
/// or the Buzz workspace-profile admin command (9033).
572576
pub const fn is_relay_admin_kind(kind: u32) -> bool {
573577
matches!(
574578
kind,
575-
RELAY_ADMIN_ADD_MEMBER | RELAY_ADMIN_REMOVE_MEMBER | RELAY_ADMIN_CHANGE_ROLE
579+
RELAY_ADMIN_ADD_MEMBER
580+
| RELAY_ADMIN_REMOVE_MEMBER
581+
| RELAY_ADMIN_CHANGE_ROLE
582+
| RELAY_ADMIN_SET_WORKSPACE_PROFILE
576583
)
577584
}
578585

‎crates/buzz-db/src/lib.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,49 @@ impl Db {
321321
.transpose()
322322
}
323323

324+
/// Returns the community's workspace icon (NIP-11 `icon`), if set.
325+
///
326+
/// Set by relay admins/owners via the kind:9033 command; the value is
327+
/// validated and size-capped at that write path.
328+
pub async fn get_community_icon(&self, community_id: CommunityId) -> Result<Option<String>> {
329+
let row = sqlx::query(
330+
r#"
331+
SELECT icon
332+
FROM communities
333+
WHERE id = $1
334+
"#,
335+
)
336+
.bind(community_id.as_uuid())
337+
.fetch_optional(&self.pool)
338+
.await?;
339+
340+
Ok(row
341+
.map(|row| row.try_get::<Option<String>, _>("icon"))
342+
.transpose()?
343+
.flatten()
344+
.filter(|icon| !icon.is_empty()))
345+
}
346+
347+
/// Sets or clears (`None`) the community's workspace icon.
348+
pub async fn set_community_icon(
349+
&self,
350+
community_id: CommunityId,
351+
icon: Option<&str>,
352+
) -> Result<()> {
353+
sqlx::query(
354+
r#"
355+
UPDATE communities
356+
SET icon = $2
357+
WHERE id = $1
358+
"#,
359+
)
360+
.bind(community_id.as_uuid())
361+
.bind(icon)
362+
.execute(&self.pool)
363+
.await?;
364+
Ok(())
365+
}
366+
324367
/// Ensure a configured community host exists and return its row.
325368
///
326369
/// This is the startup/config seeding path for N=1 deployments. Migrations

‎crates/buzz-db/src/migration.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ mod tests {
471471
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
472472
migrations.sort_by_key(|migration| migration.version);
473473

474-
assert_eq!(migrations.len(), 2);
474+
assert_eq!(migrations.len(), 3);
475475
assert_eq!(migrations[0].version, 1);
476476
assert_eq!(&*migrations[0].description, "initial schema");
477477
assert!(migrations[0]
@@ -506,6 +506,15 @@ mod tests {
506506
.as_str()
507507
.contains("CREATE TABLE git_repo_names"));
508508
assert!(!migrations[0].sql.as_str().contains("git_repo_names"));
509+
510+
// Same additive-migration rule for the per-community workspace icon
511+
// (NIP-11 `icon`): its own version, never folded into 0001.
512+
assert_eq!(migrations[2].version, 3);
513+
assert!(migrations[2]
514+
.sql
515+
.as_str()
516+
.contains("ALTER TABLE communities ADD COLUMN icon"));
517+
assert!(!migrations[0].sql.as_str().contains("icon"));
509518
}
510519

511520
#[test]

‎crates/buzz-relay/src/handlers/ingest.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use buzz_core::kind::{
3232
KIND_STREAM_MESSAGE_PINNED, KIND_STREAM_MESSAGE_SCHEDULED, KIND_STREAM_MESSAGE_V2,
3333
KIND_STREAM_REMINDER, KIND_TEAM, KIND_TEXT_NOTE, KIND_USER_STATUS, KIND_WORKFLOW_DEF,
3434
KIND_WORKFLOW_TRIGGER, RELAY_ADMIN_ADD_MEMBER, RELAY_ADMIN_CHANGE_ROLE,
35-
RELAY_ADMIN_REMOVE_MEMBER,
35+
RELAY_ADMIN_REMOVE_MEMBER, RELAY_ADMIN_SET_WORKSPACE_PROFILE,
3636
};
3737
use buzz_core::tenant::TenantContext;
3838
use buzz_core::verification::verify_event;
@@ -188,10 +188,12 @@ fn required_scope_for_kind(kind: u32, event: &Event) -> Result<Scope, &'static s
188188
KIND_NIP29_PUT_USER | KIND_NIP29_REMOVE_USER | KIND_NIP29_DELETE_GROUP => {
189189
Ok(Scope::AdminChannels)
190190
}
191-
// NIP-43: relay membership admin commands (9030–9032)
191+
// NIP-43: relay membership admin commands (9030–9032) + Buzz
192+
// workspace-profile command (9033)
192193
k if k == RELAY_ADMIN_ADD_MEMBER
193194
|| k == RELAY_ADMIN_REMOVE_MEMBER
194-
|| k == RELAY_ADMIN_CHANGE_ROLE =>
195+
|| k == RELAY_ADMIN_CHANGE_ROLE
196+
|| k == RELAY_ADMIN_SET_WORKSPACE_PROFILE =>
195197
{
196198
Ok(Scope::AdminUsers)
197199
}
@@ -367,6 +369,7 @@ pub(crate) fn is_global_only_kind(kind: u32) -> bool {
367369
| RELAY_ADMIN_ADD_MEMBER
368370
| RELAY_ADMIN_REMOVE_MEMBER
369371
| RELAY_ADMIN_CHANGE_ROLE
372+
| RELAY_ADMIN_SET_WORKSPACE_PROFILE
370373
| KIND_NIP43_LEAVE_REQUEST
371374
// NIP-IA: identity archive/unarchive requests drive relay-global
372375
// archive state (8002/8003/13535) and are audited as global request

‎crates/buzz-relay/src/handlers/relay_admin.rs‎

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010
//! | 9030 | Add member | admin or owner |
1111
//! | 9031 | Remove member | admin or owner |
1212
//! | 9032 | Change role | owner only |
13+
//! | 9033 | Set workspace profile (icon) | admin or owner |
1314
1415
use std::sync::Arc;
1516

1617
use nostr::Event;
1718
use tracing::{info, warn};
1819

19-
use buzz_core::kind::{RELAY_ADMIN_ADD_MEMBER, RELAY_ADMIN_CHANGE_ROLE, RELAY_ADMIN_REMOVE_MEMBER};
20+
use buzz_core::kind::{
21+
RELAY_ADMIN_ADD_MEMBER, RELAY_ADMIN_CHANGE_ROLE, RELAY_ADMIN_REMOVE_MEMBER,
22+
RELAY_ADMIN_SET_WORKSPACE_PROFILE,
23+
};
2024
use buzz_core::tenant::TenantContext;
2125
use buzz_db::relay_members::RemoveResult;
2226

@@ -52,7 +56,45 @@ fn extract_tag_value(event: &Event, name: &str) -> Option<String> {
5256
None
5357
}
5458

55-
/// Validate and execute a relay admin command (kinds 9030–9032).
59+
/// Maximum accepted workspace icon https URL length.
60+
const MAX_WORKSPACE_ICON_URL_LEN: usize = 2048;
61+
62+
/// Maximum accepted workspace icon data-URL length (~96 KB of base64 ≈ 72 KB
63+
/// image — generous for a 128px icon).
64+
const MAX_WORKSPACE_ICON_DATA_URL_LEN: usize = 98_304;
65+
66+
/// Validate a workspace icon: empty (clear), an http(s) URL, or an inline
67+
/// `data:image/*` URL (what the desktop publishes — it renders across
68+
/// workspaces without cross-relay media fetches).
69+
fn validate_workspace_icon(icon: &str) -> Result<(), String> {
70+
if icon.is_empty() {
71+
return Ok(());
72+
}
73+
if icon.chars().any(|c| c.is_control() || c.is_whitespace()) {
74+
return Err("icon contains invalid characters".to_string());
75+
}
76+
if icon.starts_with("data:image/") {
77+
if icon.len() > MAX_WORKSPACE_ICON_DATA_URL_LEN {
78+
return Err(format!(
79+
"icon data URL too long: {} bytes (max {MAX_WORKSPACE_ICON_DATA_URL_LEN})",
80+
icon.len()
81+
));
82+
}
83+
return Ok(());
84+
}
85+
if !icon.starts_with("https://") && !icon.starts_with("http://") {
86+
return Err("icon must be an http(s) URL or data:image/* URL".to_string());
87+
}
88+
if icon.len() > MAX_WORKSPACE_ICON_URL_LEN {
89+
return Err(format!(
90+
"icon URL too long: {} bytes (max {MAX_WORKSPACE_ICON_URL_LEN})",
91+
icon.len()
92+
));
93+
}
94+
Ok(())
95+
}
96+
97+
/// Validate and execute a relay admin command (kinds 9030–9033).
5698
///
5799
/// The handler:
58100
/// 1. Extracts the target pubkey from the `["p", ...]` tag.
@@ -88,10 +130,6 @@ pub async fn handle_relay_admin_event(
88130
}
89131
}
90132

91-
let target_hex = extract_p_tag_hex(event)
92-
.ok_or_else(|| "missing or invalid p tag".to_string())?
93-
.to_ascii_lowercase();
94-
95133
let sender_member = state
96134
.db
97135
.get_relay_member(tenant.community(), &sender_hex)
@@ -103,6 +141,34 @@ pub async fn handle_relay_admin_event(
103141
.map(|m| m.role.as_str())
104142
.unwrap_or("");
105143

144+
// kind:9033 — Set workspace profile (icon). Handled before p-tag
145+
// extraction: it targets the relay itself, not a member pubkey.
146+
if kind == RELAY_ADMIN_SET_WORKSPACE_PROFILE {
147+
if sender_role != "admin" && sender_role != "owner" {
148+
return Err("actor not authorized: must be admin or owner".to_string());
149+
}
150+
151+
// Empty or missing icon tag clears the workspace icon.
152+
let icon = extract_tag_value(event, "icon").unwrap_or_default();
153+
validate_workspace_icon(&icon)?;
154+
155+
state
156+
.db
157+
.set_community_icon(
158+
tenant.community(),
159+
(!icon.is_empty()).then_some(icon.as_str()),
160+
)
161+
.await
162+
.map_err(|e| format!("failed to store workspace icon: {e}"))?;
163+
164+
info!(sender = %sender_hex, icon_len = icon.len(), "workspace profile updated");
165+
return Ok(());
166+
}
167+
168+
let target_hex = extract_p_tag_hex(event)
169+
.ok_or_else(|| "missing or invalid p tag".to_string())?
170+
.to_ascii_lowercase();
171+
106172
match kind {
107173
// kind:9030 — Add relay member
108174
k if k == RELAY_ADMIN_ADD_MEMBER => {
@@ -364,4 +430,39 @@ mod tests {
364430
let event = make_test_event(9030, vec![vec!["role", "admin"]]);
365431
assert_eq!(extract_tag_value(&event, "p"), None);
366432
}
433+
434+
#[test]
435+
fn workspace_icon_empty_ok() {
436+
assert!(validate_workspace_icon("").is_ok());
437+
}
438+
439+
#[test]
440+
fn workspace_icon_https_ok() {
441+
assert!(validate_workspace_icon("https://example.com/icon.png").is_ok());
442+
}
443+
444+
#[test]
445+
fn workspace_icon_data_url_ok() {
446+
assert!(validate_workspace_icon("data:image/webp;base64,UklGRg==").is_ok());
447+
}
448+
449+
#[test]
450+
fn workspace_icon_rejects_non_url() {
451+
assert!(validate_workspace_icon("javascript:alert(1)").is_err());
452+
assert!(validate_workspace_icon("data:text/html;base64,PGI+").is_err());
453+
}
454+
455+
#[test]
456+
fn workspace_icon_rejects_whitespace_and_control() {
457+
assert!(validate_workspace_icon("https://example.com/a b.png").is_err());
458+
assert!(validate_workspace_icon("https://example.com/a\nb.png").is_err());
459+
}
460+
461+
#[test]
462+
fn workspace_icon_rejects_oversized() {
463+
let long_url = format!("https://example.com/{}.png", "a".repeat(2048));
464+
assert!(validate_workspace_icon(&long_url).is_err());
465+
let long_data = format!("data:image/png;base64,{}", "A".repeat(98_304));
466+
assert!(validate_workspace_icon(&long_data).is_err());
467+
}
367468
}

0 commit comments

Comments
 (0)