Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 4001fbe

Browse files
committed
infra: fix presentation name deploy
1 parent b5aeaa4 commit 4001fbe

File tree

2 files changed

+62
-53
lines changed

2 files changed

+62
-53
lines changed

infra/handler/app/Test.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,13 @@ testPresDeploys = withQueueName $ withEnv $ \env -> withSQS env $ withS3 env $ d
175175
, presentationBackground = Nothing
176176
}
177177

178-
deployPresentation env (Username "josph") somePres
178+
let uname = Username "josph"
179+
let psname = sanitizePresentationName (presentationName somePres)
180+
181+
deployPresentation env uname psname somePres
179182
-- XXX: tests the obj diffing by making sure we can upload a presentation
180183
-- twice without errors
181-
deployPresentation env (Username "josph") somePres
184+
deployPresentation env uname psname somePres
182185
where
183186
testQueueName = "the-queue"
184187
withQueueName =

infra/handler/src/DeckGo/Handler.hs

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ instance ToSchema UserInfo where
233233
instance ToParamSchema (Item UserId UserInfo) where
234234
toParamSchema _ = mempty
235235

236-
newtype PresentationName = PresentationName { unPresentatinName :: T.Text }
236+
newtype PresentationName = PresentationName { unPresentationName :: T.Text }
237237
deriving stock (Show, Eq)
238238
deriving newtype (Aeson.FromJSON, Aeson.ToJSON)
239239

@@ -413,31 +413,29 @@ presentationsPost env conn _userId pinfo = do
413413

414414
liftIO $ putStrLn $ "Got user: " <> show uname
415415

416-
liftIO $ deployPresentation env uname pinfo
416+
let psname = sanitizePresentationName (presentationName pinfo)
417417

418-
presId <- liftIO $ PresentationId <$> newId
419-
420-
let presName = presentationPrefix uname (presentationName pinfo)
418+
liftIO $ deployPresentation env uname psname pinfo
421419

422-
-- something fishy going on here
423-
let presName' = sanitizePresentationName (presentationName pinfo)
424-
425-
liftIO $ putStrLn $ unwords
426-
[ "Presentation info:"
427-
, show (presId, presName')
428-
]
420+
presId <- liftIO $ PresentationId <$> newId
429421

430422
presUrl <- do
431423
purl <- liftIO (getEnv "DECKGO_PRESENTATIONS_URL")
432-
pure $ "https://" <> T.pack purl <> "/" <> presName
424+
pure $ mconcat
425+
[ "https://"
426+
, T.pack purl
427+
, "/"
428+
, unPresentationPrefix $
429+
presentationPrefix uname psname
430+
]
433431

434432
liftIO $ putStrLn $ unwords
435433
[ "Presentation info:"
436-
, show (presId, presName')
434+
, show (presUrl, presId, psname)
437435
]
438436

439437
-- TODO: make unique
440-
liftIO (dbCreatePresentation iface presId presName' presUrl userId)
438+
liftIO (dbCreatePresentation iface presId psname presUrl userId)
441439

442440
pure $ Item
443441
{ itemId = presId
@@ -483,11 +481,7 @@ presentationsPut env conn _uid pid pinfo = do
483481

484482
liftIO $ putStrLn $ "Got presentation: " <> show (presName, presUrl)
485483

486-
-- XXX: huge hack because we know we stored the "correct" presentation name
487-
let pinfo' = pinfo { presentationName = PresentationName presName }
488-
489-
liftIO $ putStrLn $ "Updated presentation info: " <> show pinfo
490-
liftIO $ deployPresentation env uname pinfo'
484+
liftIO $ deployPresentation env uname presName pinfo
491485

492486
pure $ Item
493487
{ itemId = pid
@@ -753,12 +747,12 @@ usersDeleteStatement = Statement sql encoder decoder True
753747
-- PRESENTATIONS
754748

755749

756-
presentationsPostSession :: PresentationId -> T.Text -> T.Text -> UserId -> HS.Session ()
750+
presentationsPostSession :: PresentationId -> PresShortname -> T.Text -> UserId -> HS.Session ()
757751
presentationsPostSession pid pnam purl uid = do
758752
liftIO $ putStrLn "Creating presentation in DB"
759753
HS.statement (pid, pnam, purl, uid) presentationsPostStatement
760754

761-
presentationsPostStatement :: Statement (PresentationId, T.Text, T.Text, UserId) ()
755+
presentationsPostStatement :: Statement (PresentationId, PresShortname, T.Text, UserId) ()
762756
presentationsPostStatement = Statement sql encoder decoder True
763757
where
764758
sql = BS8.unwords
@@ -768,19 +762,19 @@ presentationsPostStatement = Statement sql encoder decoder True
768762
]
769763
encoder =
770764
contramap (unPresentationId . view _1) (HE.param HE.text) <>
771-
contramap (view _2) (HE.param HE.text) <>
765+
contramap (unPresShortname . view _2) (HE.param HE.text) <>
772766
contramap (view _3) (HE.param HE.text) <>
773767
contramap
774768
(unFirebaseId . unUserId . view _4)
775769
(HE.param HE.text)
776770
decoder = HD.unit
777771

778-
presentationsGetByIdSession :: PresentationId -> HS.Session (Maybe (T.Text, T.Text))
772+
presentationsGetByIdSession :: PresentationId -> HS.Session (Maybe (PresShortname, T.Text))
779773
presentationsGetByIdSession pid = do
780774
liftIO $ putStrLn $ "Getting presentation by id"
781775
HS.statement pid presentationsGetByIdStatement
782776

783-
presentationsGetByIdStatement :: Statement PresentationId (Maybe (T.Text, T.Text))
777+
presentationsGetByIdStatement :: Statement PresentationId (Maybe (PresShortname, T.Text))
784778
presentationsGetByIdStatement = Statement sql encoder decoder True
785779
where
786780
sql = BS8.unwords
@@ -789,7 +783,7 @@ presentationsGetByIdStatement = Statement sql encoder decoder True
789783
]
790784
encoder = contramap unPresentationId (HE.param HE.text)
791785
decoder = HD.rowMaybe $ (,) <$>
792-
HD.column HD.text <*>
786+
(PresShortname <$> HD.column HD.text) <*>
793787
HD.column HD.text -- <*>
794788
-- TODO: return user ID
795789

@@ -814,8 +808,8 @@ data DbInterface = DbInterface
814808
, dbDeleteUser :: UserId -> IO (Either () ())
815809

816810
-- TODO: dbCreateSlide: if duplicated, no error !?
817-
, dbCreatePresentation :: PresentationId -> T.Text -> T.Text -> UserId -> IO ()
818-
, dbGetPresentationById :: PresentationId -> IO (Maybe (T.Text, T.Text))
811+
, dbCreatePresentation :: PresentationId -> PresShortname -> T.Text -> UserId -> IO ()
812+
, dbGetPresentationById :: PresentationId -> IO (Maybe (PresShortname, T.Text))
819813
}
820814

821815
data DbVersion
@@ -1167,13 +1161,16 @@ newId = randomText 32 (['0' .. '9'] <> ['a' .. 'z'])
11671161
tshow :: Show a => a -> T.Text
11681162
tshow = T.pack . show
11691163

1164+
newtype PresentationPrefix = PresentationPrefix { unPresentationPrefix :: T.Text }
1165+
11701166
-- TODO: what happens when the deckname is "-" ?
1171-
presentationPrefix :: Username -> PresentationName -> T.Text
1172-
presentationPrefix uname pname =
1173-
unUsername uname <> "/" <> sanitizePresentationName pname <> "/"
1167+
presentationPrefix :: Username -> PresShortname -> PresentationPrefix
1168+
presentationPrefix uname psname =
1169+
PresentationPrefix $
1170+
unUsername uname <> "/" <> unPresShortname psname <> "/"
11741171

1175-
sanitizePresentationName :: PresentationName -> T.Text
1176-
sanitizePresentationName = T.toLower . strip . dropBadChars . unPresentatinName
1172+
sanitizePresentationName :: PresentationName -> PresShortname
1173+
sanitizePresentationName = PresShortname . T.toLower . strip . dropBadChars . unPresentationName
11771174
where
11781175
strip :: T.Text -> T.Text
11791176
strip = T.dropAround ( == '-' )
@@ -1213,18 +1210,18 @@ diffObjects news0 (HMS.fromList -> olds0) = second HMS.keys $
12131210
listPresentationObjects
12141211
:: AWS.Env
12151212
-> S3.BucketName
1216-
-> Username
1217-
-> PresentationName
1213+
-> PresentationPrefix
12181214
-> IO [S3.Object]
1219-
listPresentationObjects env bucket uname pname =
1220-
listObjects env bucket (Just $ presentationPrefix uname pname)
1215+
listPresentationObjects env bucket pprefix =
1216+
listObjects env bucket (Just $ unPresentationPrefix pprefix)
12211217

12221218
withPresentationFiles
12231219
:: Username
1220+
-> PresShortname
12241221
-> PresentationInfo
12251222
-> ([(FilePath, S3.ObjectKey, S3.ETag)] -> IO a)
12261223
-> IO a
1227-
withPresentationFiles uname presentationInfo act = do
1224+
withPresentationFiles uname psname presentationInfo act = do
12281225
deckgoStarterDist <- getEnv "DECKGO_STARTER_DIST"
12291226
Temp.withSystemTempDirectory "dist" $ \dir -> do
12301227
Tar.extract dir deckgoStarterDist
@@ -1234,7 +1231,7 @@ withPresentationFiles uname presentationInfo act = do
12341231
files <- listDirectoryRecursive dir
12351232
files' <- forM files $ \(fp, components) -> do
12361233
etag <- fileETag fp
1237-
let okey = mkObjectKey uname pname components
1234+
let okey = mkObjectKey uname psname components
12381235
pure (fp, okey, etag)
12391236
act files'
12401237
where
@@ -1244,17 +1241,17 @@ withPresentationFiles uname presentationInfo act = do
12441241
TagSoup.renderTags . processTags presentationInfo . TagSoup.parseTags .
12451242
interpol
12461243
interpol =
1247-
T.replace "{{DECKDECKGO_TITLE}}" (unPresentatinName pname) .
1248-
T.replace "{{DECKDECKGO_TITLE_SHORT}}" (T.take 12 $ unPresentatinName pname) .
1244+
T.replace "{{DECKDECKGO_TITLE}}" (unPresentationName pname) .
1245+
T.replace "{{DECKDECKGO_TITLE_SHORT}}" (T.take 12 $ unPresentationName pname) .
12491246
T.replace "{{DECKDECKGO_AUTHOR}}" (unUsername uname) .
12501247
T.replace "{{DECKDECKGO_USERNAME}}" (unUsername uname) .
12511248
T.replace "{{DECKDECKGO_USER_ID}}"
12521249
(unFirebaseId . unUserId $ presentationOwner presentationInfo) .
1253-
T.replace "{{DECKDECKGO_DECKNAME}}" (sanitizePresentationName pname) .
1250+
T.replace "{{DECKDECKGO_DECKNAME}}" (unPresShortname psname) .
12541251
-- TODO: description
12551252
T.replace "{{DECKDECKGO_DESCRIPTION}}" "(no description given)" .
12561253
T.replace "{{DECKDECKGO_BASE_HREF}}"
1257-
("/" <> presentationPrefix uname pname)
1254+
("/" <> unPresentationPrefix (presentationPrefix uname psname))
12581255

12591256
mapFile :: (T.Text -> T.Text) -> FilePath -> IO ()
12601257
mapFile f fp = do
@@ -1317,16 +1314,24 @@ deleteObjects' env bname okeys =
13171314
Right {} -> pure ()
13181315
Left e -> error $ "Could not delete object: " <> show e
13191316

1320-
deployPresentation :: AWS.Env -> Username -> PresentationInfo -> IO ()
1321-
deployPresentation env uname presentationInfo = do
1317+
newtype PresShortname = PresShortname { unPresShortname :: T.Text }
1318+
deriving (Show)
1319+
1320+
deployPresentation
1321+
:: AWS.Env
1322+
-> Username
1323+
-> PresShortname
1324+
-> PresentationInfo
1325+
-> IO ()
1326+
deployPresentation env uname psname presentationInfo = do
1327+
let pprefix = presentationPrefix uname psname
13221328
bucketName <- getEnv "BUCKET_NAME"
13231329
let bucket = S3.BucketName (T.pack bucketName)
1324-
let pname = presentationName presentationInfo
13251330
putStrLn "Listing current objects"
1326-
currentObjs <- listPresentationObjects env bucket uname pname
1331+
currentObjs <- listPresentationObjects env bucket pprefix
13271332
putStrLn "Listing presentations files"
13281333

1329-
withPresentationFiles uname presentationInfo $ \files -> do
1334+
withPresentationFiles uname psname presentationInfo $ \files -> do
13301335
let
13311336
currentObjs' =
13321337
(\obj ->
@@ -1352,7 +1357,8 @@ deployPresentation env uname presentationInfo = do
13521357
liftIO $ print queueUrl
13531358

13541359
res <- runAWS env $ AWS.send $ SQS.sendMessage queueUrl $
1355-
T.decodeUtf8 $ BL.toStrict $ Aeson.encode (presentationPrefix uname pname)
1360+
T.decodeUtf8 $ BL.toStrict $ Aeson.encode $
1361+
unPresentationPrefix pprefix
13561362

13571363
case res of
13581364
Right r -> do
@@ -1400,9 +1406,9 @@ fixupS3ETag (S3.ETag etag) =
14001406
T.dropWhile (== '"') $
14011407
T.decodeUtf8 etag
14021408

1403-
mkObjectKey :: Username -> PresentationName -> [T.Text] -> S3.ObjectKey
1409+
mkObjectKey :: Username -> PresShortname -> [T.Text] -> S3.ObjectKey
14041410
mkObjectKey uname pname components = S3.ObjectKey $
1405-
presentationPrefix uname pname <> T.intercalate "/" components
1411+
unPresentationPrefix (presentationPrefix uname pname) <> T.intercalate "/" components
14061412

14071413
fileETag :: FilePath -> IO S3.ETag
14081414
fileETag fp =

0 commit comments

Comments
 (0)