Skip to content

Commit 6d82054

Browse files
brandonchinn178ysangkok
authored andcommitted
Move NotifyTriggerBounds into NotifyDependencyUpdate
1 parent 364ffe9 commit 6d82054

File tree

2 files changed

+28
-48
lines changed

2 files changed

+28
-48
lines changed

src/Distribution/Server/Features/UserNotify.hs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -707,10 +707,10 @@ userNotifyFeature serverEnv@ServerEnv{serverCron}
707707

708708
idx <- queryGetPackageIndex
709709
revIdx <- liftIO queryReverseIndex
710-
dependencyUpdateNotifications <- concatMapM (genDependencyUpdateList idx revIdx . pkgInfoToPkgId) revisionsAndUploads
710+
dependencyUpdateNotifications <- concatMapM (genDependencyUpdateList notifyPrefs idx revIdx . pkgInfoToPkgId) revisionsAndUploads
711711

712712
emails <-
713-
getNotificationEmails serverEnv userDetailsFeature queryGetUserNotifyPref users $
713+
getNotificationEmails serverEnv userDetailsFeature users $
714714
concat
715715
[ revisionUploadNotifications
716716
, groupActionNotifications
@@ -844,13 +844,16 @@ userNotifyFeature serverEnv@ServerEnv{serverCron}
844844
, notifyDeletedTags = deletedTags
845845
}
846846

847-
genDependencyUpdateList idx revIdx pkg = do
848-
let toNotif watchedPkgs =
847+
genDependencyUpdateList notifyPrefs idx revIdx pkg = do
848+
let toNotif uid watchedPkgs =
849849
NotifyDependencyUpdate
850850
{ notifyPackageId = pkg
851851
, notifyWatchedPackages = watchedPkgs
852+
, notifyTriggerBounds =
853+
notifyDependencyTriggerBounds $
854+
fromMaybe defaultNotifyPrefs (Map.lookup uid notifyPrefs)
852855
}
853-
Map.toList . fmap toNotif
856+
Map.toList . Map.mapWithKey toNotif
854857
<$> getUserNotificationsOnRelease (queryUserGroup . maintainersGroup) idx revIdx queryGetUserNotifyPref pkg
855858

856859
sendNotifyEmailAndDelay :: Mail -> IO ()
@@ -892,6 +895,7 @@ data Notification
892895
-- ^ Dependency that was updated
893896
, notifyWatchedPackages :: [PackageId]
894897
-- ^ Packages maintained by user that depend on updated dep
898+
, notifyTriggerBounds :: NotifyTriggerBounds
895899
}
896900
deriving (Show)
897901

@@ -911,24 +915,19 @@ data NotificationGroup
911915
getNotificationEmails
912916
:: ServerEnv
913917
-> UserDetailsFeature
914-
-> (UserId -> IO (Maybe NotifyPref))
915918
-> Users.Users
916919
-> [(UserId, Notification)]
917920
-> IO [Mail]
918921
getNotificationEmails
919922
ServerEnv{serverBaseURI}
920923
UserDetailsFeature{queryUserDetails}
921-
queryGetUserNotifyPref
922924
allUsers
923925
notifications = do
924926
let userIds = Set.fromList $ map fst notifications
925927
userIdToDetails <- Map.mapMaybe id <$> fromSetM queryUserDetails userIds
926-
userIdToNotifyPref <- Map.mapMaybe id <$> fromSetM queryGetUserNotifyPref userIds
927928

928929
pure $
929-
let emails =
930-
groupNotifications . flip mapMaybe notifications $ \(uid, notif) ->
931-
fmap (uid,) $ renderNotification userIdToNotifyPref uid notif
930+
let emails = groupNotifications $ map (fmap renderNotification) notifications
932931
in flip mapMaybe (Map.toList emails) $ \((uid, group), emailContent) ->
933932
case uid `Map.lookup` userIdToDetails of
934933
Nothing -> Nothing
@@ -984,8 +983,8 @@ getNotificationEmails
984983

985984
{----- Render notifications -----}
986985

987-
renderNotification :: Map UserId NotifyPref -> UserId -> Notification -> Maybe (EmailContent, NotificationGroup)
988-
renderNotification userIdToNotifyPref uid = \case
986+
renderNotification :: Notification -> (EmailContent, NotificationGroup)
987+
renderNotification = \case
989988
NotifyNewVersion{..} ->
990989
generalNotification $
991990
renderNotifyNewVersion
@@ -1016,18 +1015,14 @@ getNotificationEmails
10161015
notifyAddedTags
10171016
notifyDeletedTags
10181017
NotifyDependencyUpdate{..} ->
1019-
case uid `Map.lookup` userIdToNotifyPref of
1020-
Nothing -> Nothing
1021-
Just notifyPref ->
1022-
Just
1023-
( renderNotifyDependencyUpdate
1024-
notifyPref
1025-
notifyPackageId
1026-
notifyWatchedPackages
1027-
, DependencyNotification notifyPackageId
1028-
)
1018+
( renderNotifyDependencyUpdate
1019+
notifyTriggerBounds
1020+
notifyPackageId
1021+
notifyWatchedPackages
1022+
, DependencyNotification notifyPackageId
1023+
)
10291024
where
1030-
generalNotification emailContent = Just (emailContent, GeneralNotification)
1025+
generalNotification = (, GeneralNotification)
10311026

10321027
renderNotifyNewVersion pkg =
10331028
EmailContentParagraph $
@@ -1065,20 +1060,20 @@ getNotificationEmails
10651060
where
10661061
showTags = emailContentIntercalate ", " . map emailContentDisplay . Set.toList
10671062

1068-
renderNotifyDependencyUpdate NotifyPref{..} dep revDeps =
1063+
renderNotifyDependencyUpdate triggerBounds dep revDeps =
10691064
let depName = emailContentDisplay (packageName dep)
10701065
depVersion = emailContentDisplay (packageVersion dep)
10711066
in
10721067
foldMap EmailContentParagraph
10731068
[ "The dependency " <> renderPkgLink dep <> " has been uploaded or revised."
1074-
, case notifyDependencyTriggerBounds of
1069+
, case triggerBounds of
10751070
Always ->
10761071
"You have requested to be notified for each upload or revision \
10771072
\of a dependency."
10781073
_ ->
10791074
"You have requested to be notified when a dependency isn't \
10801075
\accepted by any of your maintained packages."
1081-
, case notifyDependencyTriggerBounds of
1076+
, case triggerBounds of
10821077
Always ->
10831078
"These are your packages that depend on " <> depName <> ":"
10841079
BoundsOutOfRange ->

tests/ReverseDependenciesTest.hs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -391,36 +391,36 @@ getNotificationEmailsTests =
391391
. getNotificationEmail
392392
testServerEnv
393393
testUserDetailsFeature
394-
(\_ -> pure $ Just notifyEverything{notifyDependencyTriggerBounds = Always})
395394
allUsers
396395
userWatcher
397396
$ NotifyDependencyUpdate
398397
{ notifyPackageId = PackageIdentifier "base" (mkVersion [4, 18, 0, 0])
399398
, notifyWatchedPackages = [PackageIdentifier "mtl" (mkVersion [2, 3])]
399+
, notifyTriggerBounds = Always
400400
}
401401
, testGolden "Render NotifyDependencyUpdate-NewIncompatibility" "getNotificationEmails-NotifyDependencyUpdate-NewIncompatibility.golden" $
402402
fmap renderMail
403403
. getNotificationEmail
404404
testServerEnv
405405
testUserDetailsFeature
406-
(\_ -> pure $ Just notifyEverything{notifyDependencyTriggerBounds = NewIncompatibility})
407406
allUsers
408407
userWatcher
409408
$ NotifyDependencyUpdate
410409
{ notifyPackageId = PackageIdentifier "base" (mkVersion [4, 18, 0, 0])
411410
, notifyWatchedPackages = [PackageIdentifier "mtl" (mkVersion [2, 3])]
411+
, notifyTriggerBounds = NewIncompatibility
412412
}
413413
, testGolden "Render NotifyDependencyUpdate-BoundsOutOfRange" "getNotificationEmails-NotifyDependencyUpdate-BoundsOutOfRange.golden" $
414414
fmap renderMail
415415
. getNotificationEmail
416416
testServerEnv
417417
testUserDetailsFeature
418-
(\_ -> pure $ Just notifyEverything{notifyDependencyTriggerBounds = BoundsOutOfRange})
419418
allUsers
420419
userWatcher
421420
$ NotifyDependencyUpdate
422421
{ notifyPackageId = PackageIdentifier "base" (mkVersion [4, 18, 0, 0])
423422
, notifyWatchedPackages = [PackageIdentifier "mtl" (mkVersion [2, 3])]
423+
, notifyTriggerBounds = BoundsOutOfRange
424424
}
425425
, testGolden "Render general notifications in single batched email" "getNotificationEmails-batched.golden" $ do
426426
emails <-
@@ -478,8 +478,8 @@ getNotificationEmailsTests =
478478
<*> addUser "user-actor"
479479
<*> addUser "user-subject"
480480

481-
getNotificationEmail env details pref users uid notif =
482-
getNotificationEmails env details pref users [(uid, notif)] >>= \case
481+
getNotificationEmail env details users uid notif =
482+
getNotificationEmails env details users [(uid, notif)] >>= \case
483483
[email] -> pure email
484484
_ -> error "Did not get exactly one email"
485485

@@ -500,31 +500,15 @@ getNotificationEmailsTests =
500500
, accountAdminNotes = ""
501501
}
502502
}
503-
notifyEverything =
504-
NotifyPref
505-
{ notifyOptOut = False
506-
, notifyRevisionRange = NotifyAllVersions
507-
, notifyUpload = True
508-
, notifyMaintainerGroup = True
509-
, notifyDocBuilderReport = True
510-
, notifyPendingTags = True
511-
, notifyDependencyForMaintained = True
512-
, notifyDependencyTriggerBounds = Always
513-
}
514-
testGetUserNotifyPref uid = pure $ do
515-
guard $ uid == userWatcher
516-
Just notifyEverything
517503
getNotificationEmailsMocked =
518504
getNotificationEmails
519505
testServerEnv
520506
testUserDetailsFeature
521-
testGetUserNotifyPref
522507
allUsers
523508
getNotificationEmailMocked =
524509
getNotificationEmail
525510
testServerEnv
526511
testUserDetailsFeature
527-
testGetUserNotifyPref
528512
allUsers
529513

530514
renderMail = fst . Mail.renderMail (mkStdGen 0)
@@ -554,6 +538,7 @@ getNotificationEmailsTests =
554538
, NotifyDependencyUpdate
555539
<$> genPackageId
556540
<*> Gen.list (Range.linear 1 10) genPackageId
541+
<*> Gen.element [Always, NewIncompatibility, BoundsOutOfRange]
557542
]
558543

559544
genPackageName = mkPackageName <$> Gen.string (Range.linear 1 30) Gen.unicode

0 commit comments

Comments
 (0)