fix(comments): preserve emoji text fallback

Keep attachment text intact when a channel emoji image comes from an unsupported host instead of rewriting it to an invalid /ggpht path. Add regression coverage for the fallback.
This commit is contained in:
¨chico10117¨ 2026-08-07 16:00:30 -06:00
parent c5aacd1772
commit a570e0b43e
2 changed files with 45 additions and 7 deletions

View File

@ -72,4 +72,28 @@ Spectator.describe "parse_description" do
expect(parse_description(description, "video-id")).to eq("hello:custom-emoji:")
end
it "preserves attachment text when the image host is unsupported" do
description = JSON.parse(<<-JSON)
{
"content": "one:custom-emoji:two",
"attachmentRuns": [{
"startIndex": 3,
"length": 14,
"element": {
"type": {
"imageType": {
"image": {
"sources": [{"url": "https://example.com/custom.png", "width": 16, "height": 16}]
}
}
}
},
"properties": {"accessibilityProperties": {"label": "custom-emoji"}}
}]
}
JSON
expect(parse_description(description, "video-id")).to eq("one:custom-emoji:two")
end
end

View File

@ -52,26 +52,40 @@ private def skip_string(iter : Iterator, count : Int) : Int
skipped
end
private def parse_attachment_run(attachment) : String?
image_source = attachment.dig?("element", "type", "imageType", "image", "sources", 0)
return if image_source.nil?
private def image_source(url : String) : String?
uri = URI.parse(url)
url = image_source["url"]?.try &.as_s?
case uri.host
when "yt3.ggpht.com", "lh3.googleusercontent.com"
"/ggpht#{HTML.escape(uri.request_target)}"
end
rescue
nil
end
private def parse_attachment_run(attachment) : String?
source = attachment.dig?("element", "type", "imageType", "image", "sources", 0)
return if source.nil?
url = source["url"]?.try &.as_s?
return if url.nil?
src = image_source(url)
return if src.nil?
label = attachment.dig?("properties", "accessibilityProperties", "label").try &.as_s? || ""
escaped_label = HTML.escape(label)
String.build do |str|
str << %(<img alt=") << escaped_label << %(" )
str << %(src="/ggpht) << HTML.escape(URI.parse(url).request_target) << %(" )
str << %(src=") << src << %(" )
str << %(title=") << escaped_label << %(")
if width = image_source["width"]?.try &.as_i?
if width = source["width"]?.try &.as_i?
str << %( width=") << width << %(")
end
if height = image_source["height"]?.try &.as_i?
if height = source["height"]?.try &.as_i?
str << %( height=") << height << %(")
end