@@ -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.
4749fn 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}
0 commit comments