fix: keep GIFs moving in reply contexts (v2.20.1)
Mastodon turns GIFs into silent looping MP4s (gifv); only the first-frame preview was kept. The MP4 is now saved locally and played muted and looping with the still as poster. Animated GIFs from other sites are saved as animated WebP. Migration 025 adds reply_contexts.video and has fediverse contexts with an image fetched again once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
03a1c8fcea
commit
952ca300c0
9 files changed
+193
-19
No files matched your search
@@ -268,3 +268,59 @@ def test_reply_page_and_home_page_show_the_context(app, web):
|
||||
reply_to = entry["properties"]["in-reply-to"][0]
|
||||
assert (reply_to if isinstance(reply_to, str) else reply_to["properties"]["url"][0]) == TOOT
|
||||
assert "Ada Lovelace" not in json.dumps(entry["properties"].get("author", ""))
|
||||
|
||||
|
||||
# --- Moving pictures (v2.20.1) --------------------------------------------------
|
||||
|
||||
MP4 = b"\x00\x00\x00\x18ftypmp42" + b"\x00" * 64
|
||||
|
||||
|
||||
def animated_gif():
|
||||
buffer = io.BytesIO()
|
||||
frames = [Image.new("RGB", (800, 400), color) for color in ("red", "green", "blue")]
|
||||
frames[0].save(buffer, "GIF", save_all=True, append_images=frames[1:], duration=80, loop=0)
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def test_mastodon_gifv_is_kept_as_a_looping_video(app, web, monkeypatch):
|
||||
status = {**STATUS, "media_attachments": [{
|
||||
"type": "gifv", "url": "https://masto.example/media/original.mp4",
|
||||
"preview_url": "https://masto.example/media/small.png", "description": "Engineering report"}]}
|
||||
web.routes["https://masto.example/api/v1/statuses/117611186265622833"] = \
|
||||
lambda r: httpx.Response(200, json=status)
|
||||
web.routes["https://masto.example/media/original.mp4"] = \
|
||||
lambda r: httpx.Response(200, content=MP4, headers={"Content-Type": "video/mp4"})
|
||||
|
||||
note = _reply(app, TOOT, "Same here")
|
||||
context = fetch(app, TOOT)
|
||||
assert context["video"] == "https://masto.example/media/original.mp4"
|
||||
assert avatars.cached_video(app.config["DATA_PATH"], context["video"])
|
||||
|
||||
client = app.test_client()
|
||||
page = client.get(f"/note/{note.slug}").data.decode()
|
||||
assert "<video" in page and "autoplay loop muted playsinline" in page
|
||||
assert 'aria-label="Engineering report"' in page
|
||||
src = page.split('<video class="reply-photo" src="', 1)[1].split('"', 1)[0]
|
||||
response = client.get(src)
|
||||
assert response.status_code == 200 and response.mimetype == "video/mp4"
|
||||
|
||||
|
||||
def test_animated_gif_stays_animated(app, web):
|
||||
web.routes[ARTICLE] = lambda r: httpx.Response(200, html=ARTICLE_PAGE.replace("/img/lead.png", "/img/lead.gif"))
|
||||
web.routes["https://news.example/img/lead.gif"] = \
|
||||
lambda r: httpx.Response(200, content=animated_gif(), headers={"Content-Type": "image/gif"})
|
||||
context = fetch(app, ARTICLE)
|
||||
data = app.config["DATA_PATH"]
|
||||
with Image.open(avatars.preview_folder(data) / avatars.cached_preview(data, context["photo"])) as image:
|
||||
assert image.n_frames == 3
|
||||
assert image.width == avatars.PREVIEW_WIDTH
|
||||
|
||||
|
||||
def test_unused_videos_are_removed(app, web):
|
||||
data = app.config["DATA_PATH"]
|
||||
folder = avatars.preview_folder(data)
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
stray = folder / avatars.video_name_for("https://gone.example/x.mp4")
|
||||
stray.write_bytes(MP4)
|
||||
avatars.remove_unused(app.config["DATABASE_PATH"], data)
|
||||
assert not stray.exists()
|
||||
Reference in new issue
Block a user