Skip to content

Commit b792aa4

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
fix(desktop): populate last_message_at in channel browser (#951)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent e5f0c32 commit b792aa4

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎desktop/src-tauri/src/commands/channels.rs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,51 @@ pub async fn get_channels(state: State<'_, AppState>) -> Result<Vec<ChannelInfo>
156156
}
157157
}
158158

159+
// Populate last_message_at by fetching the most recent human message per
160+
// channel. Uses per-channel filters (single #h value each) so the relay can
161+
// push the query to its indexed channel_id column. Multi-value #h is NOT
162+
// SQL-pushed and would silently drop quieter channels under the global limit.
163+
let channel_ids: Vec<String> = channels.iter().map(|c| c.id.clone()).collect();
164+
if !channel_ids.is_empty() {
165+
let filters: Vec<serde_json::Value> = channel_ids
166+
.iter()
167+
.map(|id| {
168+
serde_json::json!({
169+
"kinds": [9, 40002],
170+
"#h": [id],
171+
"limit": 1
172+
})
173+
})
174+
.collect();
175+
176+
let message_events = query_relay(&state, &filters).await.unwrap_or_default();
177+
178+
let mut last_message_by_channel: std::collections::HashMap<String, u64> =
179+
std::collections::HashMap::new();
180+
for ev in &message_events {
181+
if let Some(ch_id) = ev.tags.iter().find_map(|t| {
182+
let s = t.as_slice();
183+
(s.len() >= 2 && s[0] == "h").then(|| s[1].clone())
184+
}) {
185+
let ts = ev.created_at.as_secs();
186+
last_message_by_channel
187+
.entry(ch_id)
188+
.and_modify(|existing| {
189+
if ts > *existing {
190+
*existing = ts;
191+
}
192+
})
193+
.or_insert(ts);
194+
}
195+
}
196+
197+
for channel in &mut channels {
198+
if let Some(&ts) = last_message_by_channel.get(&channel.id) {
199+
channel.last_message_at = Some(nostr_convert::timestamp_to_iso(ts));
200+
}
201+
}
202+
}
203+
159204
// NIP-DV: drop DMs the viewer has hidden. The relay maintains a per-viewer
160205
// parameterized-replaceable snapshot (kind:30622, d=my pubkey) whose `h`
161206
// tags list currently-hidden DM channel ids. The snapshot also carries

‎desktop/src-tauri/src/nostr_convert.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ pub fn relay_members_from_event(event: &Event) -> Value {
533533
// ── Time helpers ────────────────────────────────────────────────────────────
534534

535535
/// Convert a unix-seconds timestamp to a UTC RFC-3339 string.
536-
fn timestamp_to_iso(secs: u64) -> String {
536+
pub(crate) fn timestamp_to_iso(secs: u64) -> String {
537537
use std::time::{Duration, SystemTime, UNIX_EPOCH};
538538
let dt = UNIX_EPOCH + Duration::from_secs(secs);
539539
// Format manually as RFC-3339 — the `time` crate is already a transitive

0 commit comments

Comments
 (0)