From 7ef3a4faba68c0b7f9e1b039d76ca6e9008aba0a Mon Sep 17 00:00:00 2001 From: Zed Date: Sun, 22 Dec 2019 01:31:50 +0100 Subject: [PATCH] Fix rare edge case where gifs get parsed as videos --- src/api/media.nim | 8 ++++++++ src/cache.nim | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/api/media.nim b/src/api/media.nim index 988a91c..f82194c 100644 --- a/src/api/media.nim +++ b/src/api/media.nim @@ -93,11 +93,19 @@ proc getVideoFetch(tweet: Tweet; agent, token: string; retry=true): Future[Optio result = some video tokenUses.inc +proc videoIsInvalid(video: Video): bool = + not video.available and video.url.len == 0 + proc getVideo*(tweet: Tweet; agent, token: string; force=false) {.async.} = let token = if token.len == 0: guestToken else: token var video = getCachedVideo(tweet.id) if video.isNone: video = await getVideoFetch(tweet, agent, token) + elif videoIsInvalid(get(video)) and tweet.gif.isSome: + # gif was mistakenly parsed as a gif + uncache(tweet.id) + return + getVideoVar(tweet) = video if tweet.card.isSome: tweet.video = none Video diff --git a/src/cache.nim b/src/cache.nim index 99383f9..7916165 100644 --- a/src/cache.nim +++ b/src/cache.nim @@ -63,6 +63,14 @@ proc cache*(video: var Video) = if video.videoId.len > 0: video.insert() +proc uncache*(id: int64) = + withDb: + try: + var video = Video.getOne("videoId = ?", $id) + video.delete() + except: + discard + proc getCachedVideo*(id: int64): Option[Video] = withDb: try: