mirror of
https://github.com/iv-org/invidious.git
synced 2026-09-07 09:33:04 -05:00
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:
parent
c5aacd1772
commit
a570e0b43e
@ -72,4 +72,28 @@ Spectator.describe "parse_description" do
|
|||||||
|
|
||||||
expect(parse_description(description, "video-id")).to eq("hello:custom-emoji:")
|
expect(parse_description(description, "video-id")).to eq("hello:custom-emoji:")
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@ -52,26 +52,40 @@ private def skip_string(iter : Iterator, count : Int) : Int
|
|||||||
skipped
|
skipped
|
||||||
end
|
end
|
||||||
|
|
||||||
private def parse_attachment_run(attachment) : String?
|
private def image_source(url : String) : String?
|
||||||
image_source = attachment.dig?("element", "type", "imageType", "image", "sources", 0)
|
uri = URI.parse(url)
|
||||||
return if image_source.nil?
|
|
||||||
|
|
||||||
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?
|
return if url.nil?
|
||||||
|
|
||||||
|
src = image_source(url)
|
||||||
|
return if src.nil?
|
||||||
|
|
||||||
label = attachment.dig?("properties", "accessibilityProperties", "label").try &.as_s? || ""
|
label = attachment.dig?("properties", "accessibilityProperties", "label").try &.as_s? || ""
|
||||||
escaped_label = HTML.escape(label)
|
escaped_label = HTML.escape(label)
|
||||||
|
|
||||||
String.build do |str|
|
String.build do |str|
|
||||||
str << %(<img alt=") << escaped_label << %(" )
|
str << %(<img alt=") << escaped_label << %(" )
|
||||||
str << %(src="/ggpht) << HTML.escape(URI.parse(url).request_target) << %(" )
|
str << %(src=") << src << %(" )
|
||||||
str << %(title=") << escaped_label << %(")
|
str << %(title=") << escaped_label << %(")
|
||||||
|
|
||||||
if width = image_source["width"]?.try &.as_i?
|
if width = source["width"]?.try &.as_i?
|
||||||
str << %( width=") << width << %(")
|
str << %( width=") << width << %(")
|
||||||
end
|
end
|
||||||
|
|
||||||
if height = image_source["height"]?.try &.as_i?
|
if height = source["height"]?.try &.as_i?
|
||||||
str << %( height=") << height << %(")
|
str << %( height=") << height << %(")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user