Skip to content

Commit f39dd8a

Browse files
brandonchinn178ysangkok
authored andcommitted
Direct translation from plain text to EmailContent
1 parent 8d62bbe commit f39dd8a

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

src/Distribution/Server/Features/UserNotify.hs

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import Distribution.Server.Features.Upload
4747
import Distribution.Server.Features.UserDetails
4848
import Distribution.Server.Features.Users
4949

50+
import Distribution.Server.Util.Email
51+
5052
import qualified Data.Map as Map
5153
import qualified Data.Set as Set
5254

@@ -59,7 +61,6 @@ import Data.Bimap (lookup, lookupR)
5961
import Data.Graph (Vertex)
6062
import Data.Hashable (Hashable(..))
6163
import Data.List (maximumBy, sortOn)
62-
import Data.List (intercalate)
6364
import Data.Maybe (fromJust, fromMaybe, listToMaybe, mapMaybe, maybeToList)
6465
import Data.Ord (Down(..), comparing)
6566
import Data.SafeCopy (Migrate(migrate), MigrateFrom, base, deriveSafeCopy, extension)
@@ -713,12 +714,13 @@ userNotifyFeature ServerEnv{serverBaseURI, serverCron}
713714
-- Dependency email notifications consist of multiple paragraphs, so it would be confusing if concatenated.
714715
-- So they're sent independently.
715716
mapM_ (sendNotifyEmailAndDelay users) . Map.toList $
716-
Map.mapKeys fst . Map.mapWithKey (\(_, dep) ebody -> ("Dependency Update: " <> T.pack (display dep), ebody)) $
717+
Map.mapKeys fst . Map.mapWithKey (\(_, dep) emailContent -> ("Dependency Update: " <> T.pack (display dep), emailContent)) $
717718
dependencyEmails
718719

719720
updateState notifyState (SetNotifyTime now)
720721

721722
formatTimeUser users t u =
723+
EmailContentText . T.pack $
722724
display (Users.userIdToName users u) ++ " [" ++
723725
(formatTime defaultTimeLocale "%c" t) ++ "]"
724726

@@ -808,58 +810,58 @@ userNotifyFeature ServerEnv{serverBaseURI, serverCron}
808810

809811
describeRevision users earlier now pkg
810812
| pkgNumRevisions pkg <= 1 =
811-
"Package upload, " ++ display (packageName pkg) ++ ", by " ++
812-
formatTimeUser users (pkgLatestUploadTime pkg) (pkgLatestUploadUser pkg)
813+
"Package upload, " <> emailContentDisplay (packageName pkg) <> ", by " <>
814+
formatTimeUser users (pkgLatestUploadTime pkg) (pkgLatestUploadUser pkg)
813815
| otherwise =
814-
"Package metadata revision(s), " ++ display (packageName pkg) ++ ":\n" ++
815-
unlines (map (uncurry (formatTimeUser users) . snd) recentRevs)
816+
"Package metadata revision(s), " <> emailContentDisplay (packageName pkg) <> ":" <> EmailContentSoftBreak
817+
<> foldMap (<> EmailContentSoftBreak) (map (uncurry (formatTimeUser users) . snd) recentRevs)
816818
where
817819
revs = reverse $ Vec.toList (pkgMetadataRevisions pkg)
818820
recentRevs = filter ((\x -> x > earlier && x <= now) . fst . snd) revs
819821

820822
describeGroupAction users (time, uid, act, reason) =
821823
fmap
822824
( \message ->
823-
"Group modified by " ++ formatTimeUser users time uid ++ ":\n"
824-
++ message ++ "\n"
825-
++ "Reason: " ++ BS.unpack reason
825+
"Group modified by " <> formatTimeUser users time uid <> ":" <> EmailContentSoftBreak
826+
<> message <> EmailContentSoftBreak
827+
<> "Reason: " <> emailContentLBS reason
826828
)
827829
$ case act of
828830
(Admin_GroupAddUser tn (MaintainerGroup pkg)) ->
829831
Just $
830-
display (Users.userIdToName users tn)
832+
emailContentDisplay (Users.userIdToName users tn)
831833
<> " added to maintainers for "
832-
<> BS.unpack pkg
834+
<> emailContentLBS pkg
833835
(Admin_GroupDelUser tn (MaintainerGroup pkg)) ->
834836
Just $
835-
display (Users.userIdToName users tn)
837+
emailContentDisplay (Users.userIdToName users tn)
836838
<> " removed from maintainers for "
837-
<> BS.unpack pkg
839+
<> emailContentLBS pkg
838840
_ -> Nothing
839841

840842
describeDocReport (pkg, success) =
841-
"Package doc build for " ++ display (packageName pkg) ++ ":\n" ++
843+
"Package doc build for " <> emailContentDisplay (packageName pkg) <> ":" <> EmailContentSoftBreak <>
842844
if success
843845
then "Build successful."
844846
else "Build failed."
845847

846848
describeTagProposal (pkgName, (addTags, delTags)) =
847-
"Pending tag proposal for " ++ display pkgName ++ ":\n" ++
848-
"Additions: " ++ showTags addTags ++ "\n" ++
849-
"Deletions: " ++ showTags delTags
849+
"Pending tag proposal for " <> emailContentDisplay pkgName <> ":" <> EmailContentSoftBreak
850+
<> "Additions: " <> showTags addTags <> EmailContentSoftBreak
851+
<> "Deletions: " <> showTags delTags
850852
where
851-
showTags = intercalate ", " . map display . Set.toList
853+
showTags = emailContentIntercalate ", " . map emailContentDisplay . Set.toList
852854

853855
describeDependencyUpdate (uId, dep) revDeps = do
854856
mPrefs <- queryGetUserNotifyPref uId
855857
pure $
856858
case mPrefs of
857859
Nothing -> []
858860
Just NotifyPref{notifyDependencyTriggerBounds} ->
859-
let depName = display (packageName dep)
860-
depVersion = display (packageVersion dep)
861+
let depName = emailContentDisplay (packageName dep)
862+
depVersion = emailContentDisplay (packageVersion dep)
861863
in
862-
[ "The dependency " <> display dep <> " has been uploaded or revised."
864+
[ "The dependency " <> emailContentDisplay dep <> " has been uploaded or revised."
863865
, case notifyDependencyTriggerBounds of
864866
Always ->
865867
"You have requested to be notified for each upload or revision \
@@ -878,10 +880,10 @@ userNotifyFeature ServerEnv{serverBaseURI, serverCron}
878880
<> " but don't accept " <> depVersion
879881
<> " (they do accept the second-highest version):"
880882
]
881-
++ map display revDeps
883+
++ map emailContentDisplay revDeps
882884

883-
sendNotifyEmailAndDelay :: Users.Users -> (UserId, (T.Text, [String])) -> IO ()
884-
sendNotifyEmailAndDelay users (uid, (subject, ebody)) = do
885+
sendNotifyEmailAndDelay :: Users.Users -> (UserId, (T.Text, [EmailContent])) -> IO ()
886+
sendNotifyEmailAndDelay users (uid, (subject, emailContent)) = do
885887
mudetails <- queryUserDetails uid
886888
case mudetails of
887889
Nothing -> return ()
@@ -891,22 +893,23 @@ userNotifyFeature ServerEnv{serverBaseURI, serverCron}
891893
mail = (emptyMail mailFrom) {
892894
mailTo = [Address (Just aname) eml],
893895
mailHeaders = [(BSS.pack "Subject", "[Hackage] " <> subject)],
894-
mailParts = [[Part (T.pack "text/plain; charset=utf-8")
895-
None DefaultDisposition []
896-
(PartContent $ BS.pack $
897-
intercalate "\n\n" (ebody <> [adjustmentLinkParagraph])
898-
)
899-
]]
896+
mailParts =
897+
[ fromEmailContent $
898+
foldMap EmailContentParagraph $
899+
emailContent <> [updatePreferencesText]
900+
]
900901
}
901902
Just ourHost = uriAuthority serverBaseURI
902903

903904
renderSendMail mail --TODO: if we need any configuration of
904905
-- sendmail stuff, has to go here
905906
threadDelay 250000
906907
where
907-
adjustmentLinkParagraph =
908-
"You can adjust your notification preferences at\n"
909-
<> uriToString id serverBaseURI ""
910-
<> "/user/"
911-
<> display (Users.userIdToName users uid)
912-
<> "/notify"
908+
updatePreferencesText =
909+
"You can adjust your notification preferences at" <> EmailContentSoftBreak
910+
<> (EmailContentText . T.pack)
911+
( uriToString id serverBaseURI ""
912+
<> "/user/"
913+
<> display (Users.userIdToName users uid)
914+
<> "/notify"
915+
)

0 commit comments

Comments
 (0)