From a14a7e6ec9d678f79b0ec0013f3ffa6500fac0cd Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 4 Jan 2025 23:30:23 +0100 Subject: [PATCH] fix(users/Profpatsch/whatcd-resolver): handle weird search results MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently they added the ability to add random files (e.g. pdfs), and the API returns undocumented objects if that happens. Let’s skip these. Change-Id: Icd783a6ed2114520e5c524f2a2c3acfcb67d792e Reviewed-on: https://cl.tvl.fyi/c/depot/+/12954 Tested-by: BuildkiteCI Reviewed-by: Profpatsch --- .../whatcd-resolver/src/Redacted.hs | 51 ++++++++++++------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/users/Profpatsch/whatcd-resolver/src/Redacted.hs b/users/Profpatsch/whatcd-resolver/src/Redacted.hs index e6a1b4be2..0a2919f40 100644 --- a/users/Profpatsch/whatcd-resolver/src/Redacted.hs +++ b/users/Profpatsch/whatcd-resolver/src/Redacted.hs @@ -12,6 +12,7 @@ import Data.Aeson.BetterErrors qualified as Json import Data.Aeson.KeyMap qualified as KeyMap import Data.Error.Tree import Data.List qualified as List +import Data.Maybe (catMaybes) import Database.PostgreSQL.Simple (Binary (Binary), Only (..)) import Database.PostgreSQL.Simple.Types (PGArray (PGArray)) import FieldParser qualified as Field @@ -152,23 +153,29 @@ redactedSearchAndInsert extraArguments = do Json.key "results" $ do tourGroups <- label @"tourGroups" - <$> ( Json.eachInArray $ do - groupId <- Json.keyLabel @"groupId" "groupId" (Json.asIntegral @_ @Int) - groupName <- Json.keyLabel @"groupName" "groupName" Json.asText - fullJsonResult <- - label @"fullJsonResult" - <$> ( Json.asObject - -- remove torrents cause they are inserted separately below - <&> KeyMap.filterWithKey (\k _ -> k /= "torrents") - <&> Json.Object - ) - let tourGroup = T3 groupId groupName fullJsonResult - torrents <- Json.keyLabel @"torrents" "torrents" $ - Json.eachInArray $ do - torrentId <- Json.keyLabel @"torrentId" "torrentId" (Json.asIntegral @_ @Int) - fullJsonResultT <- label @"fullJsonResult" <$> Json.asValue - pure $ T2 torrentId fullJsonResultT - pure (T2 (label @"tourGroup" tourGroup) torrents) + <$> ( catMaybes + <$> ( Json.eachInArray $ do + Json.keyMay "torrents" (pure ()) >>= \case + -- not a torrent group, maybe some files or something (e.g. guitar tabs see Dream Theater Systematic Chaos) + Nothing -> pure Nothing + Just () -> do + groupId <- Json.keyLabel @"groupId" "groupId" (Json.asIntegral @_ @Int) + groupName <- Json.keyLabel @"groupName" "groupName" Json.asText + fullJsonResult <- + label @"fullJsonResult" + <$> ( Json.asObject + -- remove torrents cause they are inserted separately below + <&> KeyMap.filterWithKey (\k _ -> k /= "torrents") + <&> Json.Object + ) + let tourGroup = T3 groupId groupName fullJsonResult + torrents <- Json.keyLabel @"torrents" "torrents" $ + Json.eachInArray $ do + torrentId <- Json.keyLabel @"torrentId" "torrentId" (Json.asIntegral @_ @Int) + fullJsonResultT <- label @"fullJsonResult" <$> Json.asValue + pure $ T2 torrentId fullJsonResultT + pure $ Just (T2 (label @"tourGroup" tourGroup) torrents) + ) ) pure ( T2 @@ -580,3 +587,13 @@ redactedApiRequestJson span dat parser = do addAttribute span "redacted.request" (toOtelJsonAttr (T2 (getLabel @"action" dat) (getLabel @"actionArgs" dat))) mkRedactedApiRequest dat >>= Http.httpJson defaults parser + +-- test :: (MonadThrow m, MonadRedacted m, MonadOtel m) => m () +-- test = +-- inSpan' "test" $ \span -> do +-- redactedApiRequestJson +-- span +-- (T2 (label @"action" "browse") (label @"actionArgs" [("searchstr", Just "dream theater")])) +-- (Json.asValue) +-- <&> Pretty.showPrettyJson +-- >>= liftIO . putStderrLn