Skip to content

Commit 6e4c868

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dnpub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr
authored
feat(emoji): latest-set-wins union for custom emoji across desktop, mobile, and CLI (#989)
Signed-off-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Signed-off-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co>
1 parent 5f8ab33 commit 6e4c868

6 files changed

Lines changed: 138 additions & 53 deletions

File tree

‎crates/buzz-cli/src/commands/emoji.rs‎

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,33 @@ fn emoji_tags_of(event: &serde_json::Value) -> Vec<EmojiEntry> {
4242
out
4343
}
4444

45-
/// Union every member's kind:30030 set, deduped by `(shortcode, url)`.
46-
/// Stable, sorted by shortcode then url, so identical input yields identical output.
45+
/// Union every member's kind:30030 set, collapsed to one entry per shortcode.
46+
/// The most recently published set (`created_at`) wins; equal timestamps
47+
/// tie-break to the lexicographically-smallest URL. Deterministic and
48+
/// fetch-order-independent. Sorted by shortcode.
4749
fn union_custom_emoji(events: &[serde_json::Value]) -> Vec<EmojiEntry> {
48-
let mut seen = std::collections::HashSet::new();
49-
let mut out: Vec<EmojiEntry> = Vec::new();
50+
let mut by_shortcode: std::collections::HashMap<String, (String, i64)> =
51+
std::collections::HashMap::new();
5052
for event in events {
53+
let created_at = event
54+
.get("created_at")
55+
.and_then(|v| v.as_i64())
56+
.unwrap_or(0);
5157
for entry in emoji_tags_of(event) {
52-
if seen.insert((entry.shortcode.clone(), entry.url.clone())) {
53-
out.push(entry);
58+
match by_shortcode.get(&entry.shortcode) {
59+
Some((url, at)) if *at > created_at || (*at == created_at && *url <= entry.url) => {
60+
}
61+
_ => {
62+
by_shortcode.insert(entry.shortcode, (entry.url, created_at));
63+
}
5464
}
5565
}
5666
}
57-
out.sort_by(|a, b| a.shortcode.cmp(&b.shortcode).then(a.url.cmp(&b.url)));
67+
let mut out: Vec<EmojiEntry> = by_shortcode
68+
.into_iter()
69+
.map(|(shortcode, (url, _))| EmojiEntry { shortcode, url })
70+
.collect();
71+
out.sort_by(|a, b| a.shortcode.cmp(&b.shortcode));
5872
out
5973
}
6074

@@ -318,21 +332,21 @@ mod tests {
318332
use super::*;
319333

320334
#[test]
321-
fn union_dedups_by_shortcode_and_url_across_members() {
335+
fn union_latest_set_wins_per_shortcode() {
322336
let events = vec![
323337
serde_json::json!({
338+
"created_at": 100,
324339
"tags": [
325340
["d", "buzz:custom-emoji"],
326341
["emoji", "zort", "https://example.com/zort.png"],
327342
["emoji", "narf", "https://example.com/narf.png"]
328343
]
329344
}),
330345
serde_json::json!({
346+
"created_at": 200,
331347
"tags": [
332348
["d", "buzz:custom-emoji"],
333-
// exact duplicate (same shortcode+url) — collapses
334-
["emoji", "narf", "https://example.com/narf.png"],
335-
// same shortcode, different url — both kept (distinct pair)
349+
// newer set claims zort with a different url — newer wins
336350
["emoji", "zort", "https://example.com/zort2.png"]
337351
]
338352
}),
@@ -346,9 +360,34 @@ mod tests {
346360
pairs,
347361
vec![
348362
("narf", "https://example.com/narf.png"),
349-
("zort", "https://example.com/zort.png"),
350363
("zort", "https://example.com/zort2.png"),
351364
]
352365
);
366+
// Order-independence: reversed input yields the identical palette.
367+
let reversed: Vec<_> = events.into_iter().rev().collect();
368+
let emojis_rev = union_custom_emoji(&reversed);
369+
let pairs_rev: Vec<(&str, &str)> = emojis_rev
370+
.iter()
371+
.map(|e| (e.shortcode.as_str(), e.url.as_str()))
372+
.collect();
373+
assert_eq!(pairs, pairs_rev);
374+
}
375+
376+
#[test]
377+
fn union_equal_timestamps_tie_break_to_smallest_url() {
378+
let events = vec![
379+
serde_json::json!({
380+
"created_at": 100,
381+
"tags": [["emoji", "zort", "https://example.com/zort2.png"]]
382+
}),
383+
serde_json::json!({
384+
"created_at": 100,
385+
"tags": [["emoji", "zort", "https://example.com/zort.png"]]
386+
}),
387+
];
388+
let emojis = union_custom_emoji(&events);
389+
assert_eq!(emojis.len(), 1);
390+
assert_eq!(emojis[0].shortcode, "zort");
391+
assert_eq!(emojis[0].url, "https://example.com/zort.png");
353392
}
354393
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ fn truncate_output(s: String) -> String {
273273
if s.len() <= LIMIT {
274274
return s;
275275
}
276-
let head_end = s.floor_char_boundary(HEAD);
277-
let tail_start = s.floor_char_boundary(s.len().saturating_sub(TAIL));
276+
let head_end = floor_char_boundary(&s, HEAD);
277+
let tail_start = floor_char_boundary(&s, s.len().saturating_sub(TAIL));
278278
let omitted = tail_start - head_end;
279279
format!(
280280
"{}\n... ({omitted} bytes omitted) ...\n{}",
@@ -283,6 +283,14 @@ fn truncate_output(s: String) -> String {
283283
)
284284
}
285285

286+
fn floor_char_boundary(s: &str, mut index: usize) -> usize {
287+
index = index.min(s.len());
288+
while index > 0 && !s.is_char_boundary(index) {
289+
index -= 1;
290+
}
291+
index
292+
}
293+
286294
#[tauri::command]
287295
pub fn discover_managed_agent_prereqs(
288296
input: DiscoverManagedAgentPrereqsRequest,

‎desktop/src/shared/api/customEmoji.test.mjs‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
suggestShortcodeFromFilename,
1010
} from "./customEmoji.ts";
1111

12-
function ev(tags) {
12+
function ev(tags, createdAt = 1) {
1313
return {
1414
id: "x",
1515
pubkey: "relay",
16-
created_at: 1,
16+
created_at: createdAt,
1717
kind: 30030,
1818
tags,
1919
content: "",
@@ -135,9 +135,9 @@ test("unionCustomEmoji merges members and sorts by shortcode", () => {
135135
});
136136

137137
test("unionCustomEmoji collapses a shortcode to ONE deterministic winner", () => {
138-
// Two members claim :party_parrot: with different URLs. The palette must
139-
// expose exactly one (lexicographically-smallest URL), since downstream
140-
// identity is shortcode-only and cannot disambiguate two URLs.
138+
// Two members claim :party_parrot: at the same created_at. The palette must
139+
// expose exactly one (tie-break: lexicographically-smallest URL), since
140+
// downstream identity is shortcode-only and cannot disambiguate two URLs.
141141
const out = unionCustomEmoji([
142142
ev([["emoji", "party_parrot", "https://relay/zebra.gif"]]),
143143
ev([["emoji", "party_parrot", "https://relay/alpha.gif"]]),
@@ -147,6 +147,15 @@ test("unionCustomEmoji collapses a shortcode to ONE deterministic winner", () =>
147147
]);
148148
});
149149

150+
test("unionCustomEmoji prefers the most recently published set", () => {
151+
// Newer event wins regardless of URL sort order or input position.
152+
const older = ev([["emoji", "dup", "https://relay/alpha.gif"]], 100);
153+
const newer = ev([["emoji", "dup", "https://relay/zebra.gif"]], 200);
154+
const expected = [{ shortcode: "dup", url: "https://relay/zebra.gif" }];
155+
assert.deepEqual(unionCustomEmoji([older, newer]), expected);
156+
assert.deepEqual(unionCustomEmoji([newer, older]), expected);
157+
});
158+
150159
test("unionCustomEmoji winner is independent of member order", () => {
151160
const a = ev([["emoji", "dup", "https://relay/alpha.gif"]]);
152161
const b = ev([["emoji", "dup", "https://relay/zebra.gif"]]);

‎desktop/src/shared/api/customEmoji.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,30 @@ export function customEmojiFromEvent(event: RelayEvent | null): CustomEmoji[] {
105105
/**
106106
* Union every member's kind:30030 set into the workspace palette, collapsed to
107107
* one entry per shortcode. When members disagree on a shortcode's URL, the
108-
* winner is the lexicographically-smallest URL: deterministic and stable across
109-
* reloads, so the same set of events always yields the same palette (no picker
110-
* reshuffle, no ambiguous shortcode→url resolution downstream). Output is
111-
* sorted by shortcode.
108+
* most recently published set wins (`created_at` is signed event data, so this
109+
* is as deterministic and fetch-order-independent as any pure function of the
110+
* events); equal timestamps tie-break to the lexicographically-smallest URL so
111+
* the same set of events always yields the same palette. Output is sorted by
112+
* shortcode.
112113
*/
113114
export function unionCustomEmoji(
114115
events: ReadonlyArray<RelayEvent>,
115116
): CustomEmoji[] {
116-
const urlByShortcode = new Map<string, string>();
117+
const byShortcode = new Map<string, { url: string; createdAt: number }>();
117118
for (const event of events) {
118119
for (const { shortcode, url } of customEmojiFromTags(event.tags)) {
119-
const existing = urlByShortcode.get(shortcode);
120-
if (existing === undefined || url < existing) {
121-
urlByShortcode.set(shortcode, url);
120+
const winner = byShortcode.get(shortcode);
121+
if (
122+
winner === undefined ||
123+
event.created_at > winner.createdAt ||
124+
(event.created_at === winner.createdAt && url < winner.url)
125+
) {
126+
byShortcode.set(shortcode, { url, createdAt: event.created_at });
122127
}
123128
}
124129
}
125-
return [...urlByShortcode]
126-
.map(([shortcode, url]) => ({ shortcode, url }))
130+
return [...byShortcode]
131+
.map(([shortcode, { url }]) => ({ shortcode, url }))
127132
.sort((a, b) => a.shortcode.localeCompare(b.shortcode));
128133
}
129134

‎mobile/lib/features/custom_emoji/custom_emoji.dart‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,26 @@ List<CustomEmoji> customEmojiFromTags(List<List<String>> tags) {
6969

7070
/// Union every member's kind:30030 set into the workspace palette, collapsed to
7171
/// one entry per shortcode. When members disagree on a shortcode's URL, the
72-
/// lexicographically-smallest URL wins — deterministic and stable across
73-
/// reloads. Output is sorted by shortcode.
72+
/// most recently published set wins (`created_at` is signed event data, so the
73+
/// result stays deterministic and fetch-order-independent); equal timestamps
74+
/// tie-break to the lexicographically-smallest URL. Output is sorted by
75+
/// shortcode.
7476
List<CustomEmoji> unionCustomEmoji(Iterable<NostrEvent> events) {
75-
final urlByShortcode = <String, String>{};
77+
final byShortcode = <String, ({String url, int createdAt})>{};
7678
for (final event in events) {
7779
for (final e in customEmojiFromTags(event.tags)) {
78-
final existing = urlByShortcode[e.shortcode];
79-
if (existing == null || e.url.compareTo(existing) < 0) {
80-
urlByShortcode[e.shortcode] = e.url;
80+
final winner = byShortcode[e.shortcode];
81+
if (winner == null ||
82+
event.createdAt > winner.createdAt ||
83+
(event.createdAt == winner.createdAt &&
84+
e.url.compareTo(winner.url) < 0)) {
85+
byShortcode[e.shortcode] = (url: e.url, createdAt: event.createdAt);
8186
}
8287
}
8388
}
8489
final result =
85-
urlByShortcode.entries
86-
.map((e) => CustomEmoji(shortcode: e.key, url: e.value))
90+
byShortcode.entries
91+
.map((e) => CustomEmoji(shortcode: e.key, url: e.value.url))
8792
.toList()
8893
..sort((a, b) => a.shortcode.compareTo(b.shortcode));
8994
return result;

‎mobile/test/features/custom_emoji/custom_emoji_test.dart‎

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import 'package:flutter_test/flutter_test.dart';
22
import 'package:buzz/features/custom_emoji/custom_emoji.dart';
33
import 'package:buzz/shared/relay/nostr_models.dart';
44

5-
NostrEvent _event(String pubkey, List<List<String>> emojiTags) {
5+
NostrEvent _event(
6+
String pubkey,
7+
List<List<String>> emojiTags, {
8+
int createdAt = 0,
9+
}) {
610
return NostrEvent(
711
id: 'id-$pubkey',
812
pubkey: pubkey,
9-
createdAt: 0,
13+
createdAt: createdAt,
1014
kind: kindEmojiSet,
1115
tags: [
1216
['d', customEmojiSetDTag],
@@ -60,20 +64,35 @@ void main() {
6064
});
6165

6266
group('unionCustomEmoji', () {
63-
test('collapses to one per shortcode, smallest URL wins, sorted', () {
64-
final palette = unionCustomEmoji([
65-
_event('alice', [
66-
['emoji', 'meow', 'https://z/meow.png'],
67-
['emoji', 'wave', 'https://a/wave.png'],
68-
]),
69-
_event('bob', [
70-
['emoji', 'meow', 'https://a/meow.png'], // smaller URL wins
71-
]),
72-
]);
73-
expect(palette, [
74-
const CustomEmoji(shortcode: 'meow', url: 'https://a/meow.png'),
75-
const CustomEmoji(shortcode: 'wave', url: 'https://a/wave.png'),
76-
]);
67+
test(
68+
'collapses to one per shortcode; same-time tie-breaks to smaller URL',
69+
() {
70+
final palette = unionCustomEmoji([
71+
_event('alice', [
72+
['emoji', 'meow', 'https://z/meow.png'],
73+
['emoji', 'wave', 'https://a/wave.png'],
74+
]),
75+
_event('bob', [
76+
['emoji', 'meow', 'https://a/meow.png'], // tie → smaller URL wins
77+
]),
78+
]);
79+
expect(palette, [
80+
const CustomEmoji(shortcode: 'meow', url: 'https://a/meow.png'),
81+
const CustomEmoji(shortcode: 'wave', url: 'https://a/wave.png'),
82+
]);
83+
},
84+
);
85+
86+
test('most recently published set wins, regardless of URL order', () {
87+
final older = _event('alice', [
88+
['emoji', 'x', 'https://a/x.png'],
89+
], createdAt: 100);
90+
final newer = _event('bob', [
91+
['emoji', 'x', 'https://z/x.png'],
92+
], createdAt: 200);
93+
const expected = [CustomEmoji(shortcode: 'x', url: 'https://z/x.png')];
94+
expect(unionCustomEmoji([older, newer]), expected);
95+
expect(unionCustomEmoji([newer, older]), expected);
7796
});
7897

7998
test('deterministic regardless of event order', () {

0 commit comments

Comments
 (0)