fix(users/Profpatsch/whatcd-resolver): handle weird search results

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 <mail@profpatsch.de>
This commit is contained in:
Profpatsch 2025-01-04 23:30:23 +01:00
parent 428f574b75
commit a14a7e6ec9

View file

@ -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,7 +153,12 @@ redactedSearchAndInsert extraArguments = do
Json.key "results" $ do
tourGroups <-
label @"tourGroups"
<$> ( 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 <-
@ -168,7 +174,8 @@ redactedSearchAndInsert extraArguments = do
torrentId <- Json.keyLabel @"torrentId" "torrentId" (Json.asIntegral @_ @Int)
fullJsonResultT <- label @"fullJsonResult" <$> Json.asValue
pure $ T2 torrentId fullJsonResultT
pure (T2 (label @"tourGroup" tourGroup) torrents)
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