mirror of
https://github.com/block/buzz.git
synced 2026-12-01 05:32:24 +00:00
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>
This commit is contained in:
@@ -9,11 +9,11 @@ import {
|
||||
suggestShortcodeFromFilename,
|
||||
} from "./customEmoji.ts";
|
||||
|
||||
function ev(tags) {
|
||||
function ev(tags, createdAt = 1) {
|
||||
return {
|
||||
id: "x",
|
||||
pubkey: "relay",
|
||||
created_at: 1,
|
||||
created_at: createdAt,
|
||||
kind: 30030,
|
||||
tags,
|
||||
content: "",
|
||||
@@ -135,9 +135,9 @@ test("unionCustomEmoji merges members and sorts by shortcode", () => {
|
||||
});
|
||||
|
||||
test("unionCustomEmoji collapses a shortcode to ONE deterministic winner", () => {
|
||||
// Two members claim :party_parrot: with different URLs. The palette must
|
||||
// expose exactly one (lexicographically-smallest URL), since downstream
|
||||
// identity is shortcode-only and cannot disambiguate two URLs.
|
||||
// Two members claim :party_parrot: at the same created_at. The palette must
|
||||
// expose exactly one (tie-break: lexicographically-smallest URL), since
|
||||
// downstream identity is shortcode-only and cannot disambiguate two URLs.
|
||||
const out = unionCustomEmoji([
|
||||
ev([["emoji", "party_parrot", "https://relay/zebra.gif"]]),
|
||||
ev([["emoji", "party_parrot", "https://relay/alpha.gif"]]),
|
||||
@@ -147,6 +147,15 @@ test("unionCustomEmoji collapses a shortcode to ONE deterministic winner", () =>
|
||||
]);
|
||||
});
|
||||
|
||||
test("unionCustomEmoji prefers the most recently published set", () => {
|
||||
// Newer event wins regardless of URL sort order or input position.
|
||||
const older = ev([["emoji", "dup", "https://relay/alpha.gif"]], 100);
|
||||
const newer = ev([["emoji", "dup", "https://relay/zebra.gif"]], 200);
|
||||
const expected = [{ shortcode: "dup", url: "https://relay/zebra.gif" }];
|
||||
assert.deepEqual(unionCustomEmoji([older, newer]), expected);
|
||||
assert.deepEqual(unionCustomEmoji([newer, older]), expected);
|
||||
});
|
||||
|
||||
test("unionCustomEmoji winner is independent of member order", () => {
|
||||
const a = ev([["emoji", "dup", "https://relay/alpha.gif"]]);
|
||||
const b = ev([["emoji", "dup", "https://relay/zebra.gif"]]);
|
||||
|
||||
@@ -105,25 +105,30 @@ export function customEmojiFromEvent(event: RelayEvent | null): CustomEmoji[] {
|
||||
/**
|
||||
* Union every member's kind:30030 set into the workspace palette, collapsed to
|
||||
* one entry per shortcode. When members disagree on a shortcode's URL, the
|
||||
* winner is the lexicographically-smallest URL: deterministic and stable across
|
||||
* reloads, so the same set of events always yields the same palette (no picker
|
||||
* reshuffle, no ambiguous shortcode→url resolution downstream). Output is
|
||||
* sorted by shortcode.
|
||||
* most recently published set wins (`created_at` is signed event data, so this
|
||||
* is as deterministic and fetch-order-independent as any pure function of the
|
||||
* events); equal timestamps tie-break to the lexicographically-smallest URL so
|
||||
* the same set of events always yields the same palette. Output is sorted by
|
||||
* shortcode.
|
||||
*/
|
||||
export function unionCustomEmoji(
|
||||
events: ReadonlyArray<RelayEvent>,
|
||||
): CustomEmoji[] {
|
||||
const urlByShortcode = new Map<string, string>();
|
||||
const byShortcode = new Map<string, { url: string; createdAt: number }>();
|
||||
for (const event of events) {
|
||||
for (const { shortcode, url } of customEmojiFromTags(event.tags)) {
|
||||
const existing = urlByShortcode.get(shortcode);
|
||||
if (existing === undefined || url < existing) {
|
||||
urlByShortcode.set(shortcode, url);
|
||||
const winner = byShortcode.get(shortcode);
|
||||
if (
|
||||
winner === undefined ||
|
||||
event.created_at > winner.createdAt ||
|
||||
(event.created_at === winner.createdAt && url < winner.url)
|
||||
) {
|
||||
byShortcode.set(shortcode, { url, createdAt: event.created_at });
|
||||
}
|
||||
}
|
||||
}
|
||||
return [...urlByShortcode]
|
||||
.map(([shortcode, url]) => ({ shortcode, url }))
|
||||
return [...byShortcode]
|
||||
.map(([shortcode, { url }]) => ({ shortcode, url }))
|
||||
.sort((a, b) => a.shortcode.localeCompare(b.shortcode));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user