Skip to content

Commit d6d0d94

Browse files
brandonchinn178ysangkok
authored andcommitted
Move describeGroupAction into getNotificationEmails
1 parent b896c75 commit d6d0d94

File tree

1 file changed

+69
-43
lines changed

1 file changed

+69
-43
lines changed

src/Distribution/Server/Features/UserNotify.hs

Lines changed: 69 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ import Text.XHtml hiding (base, text, (</>))
8080
import qualified Data.Aeson as Aeson
8181
import qualified Data.Aeson.Types as Aeson
8282
import qualified Data.ByteString.Lazy.Char8 as BS
83+
import Data.Text (Text)
8384
import qualified Data.Text as T
85+
import qualified Data.Text.Lazy as TL
86+
import qualified Data.Text.Lazy.Encoding as TL
8487
import qualified Data.Vector as Vec
8588

8689
-- A feature to manage notifications to users when package metadata, etc is updated.
@@ -689,8 +692,7 @@ userNotifyFeature serverEnv@ServerEnv{serverBaseURI, serverCron}
689692
revisionUploadNotifications <- concatMapM (genRevUploadList notifyPrefs trimLastTime now) revisionsAndUploads
690693

691694
groupActions <- collectAdminActions trimLastTime now
692-
groupActionNotifications <- foldM (genGroupUploadList notifyPrefs) Map.empty groupActions
693-
let groupActionEmails = mconcat . mapMaybe (describeGroupAction users) <$> groupActionNotifications
695+
groupActionNotifications <- concatMapM (genGroupUploadList notifyPrefs) groupActions
694696

695697
docReports <- collectDocReport trimLastTime now
696698
docReportNotifications <- foldM (genDocReportList notifyPrefs) Map.empty docReports
@@ -708,14 +710,14 @@ userNotifyFeature serverEnv@ServerEnv{serverBaseURI, serverCron}
708710
emails <-
709711
getNotificationEmails serverEnv userDetailsFeature users
710712
( foldr1 (Map.unionWith (<>))
711-
[ groupActionEmails
712-
, docReportEmails
713+
[ docReportEmails
713714
, tagProposalEmails
714715
]
715716
, dependencyEmails
716717
) $
717718
concat
718719
[ revisionUploadNotifications
720+
, groupActionNotifications
719721
]
720722
mapM_ sendNotifyEmailAndDelay emails
721723

@@ -728,11 +730,6 @@ userNotifyFeature serverEnv@ServerEnv{serverBaseURI, serverCron}
728730
{ uriPath = "/package/" <> display (packageName pkg) <> "-" <> display (packageVersion pkg)
729731
}
730732

731-
formatTimeUser users t u =
732-
EmailContentText . T.pack $
733-
display (Users.userIdToName users u) ++ " [" ++
734-
(formatTime defaultTimeLocale "%c" t) ++ "]"
735-
736733
collectRevisionsAndUploads earlier now = do
737734
pkgIndex <- queryGetPackageIndex
738735
let isRecent pkgInfo =
@@ -797,18 +794,36 @@ userNotifyFeature serverEnv@ServerEnv{serverBaseURI, serverCron}
797794
{ notifyPackageInfo = pkg
798795
}
799796

800-
genGroupUploadList notifyPrefs mp ga =
801-
let (actor,gdesc) = case ga of (_,uid,Admin_GroupAddUser _ gd,_) -> (uid, gd)
802-
(_,uid,Admin_GroupDelUser _ gd,_) -> (uid, gd)
803-
addNotification uid m = if not (notifyOptOut npref) && notifyMaintainerGroup npref
804-
then Map.insertWith (++) uid [ga] m
805-
else m
806-
where npref = fromMaybe defaultNotifyPrefs (Map.lookup uid notifyPrefs)
807-
in case gdesc of
808-
(MaintainerGroup pkg) -> do
809-
maintainers <- queryUserGroup $ maintainersGroup (mkPackageName $ BS.unpack pkg)
810-
return $ foldr addNotification mp (toList (delete actor maintainers))
811-
_ -> return mp
797+
genGroupUploadList notifyPrefs groupAction =
798+
let notifyAllMaintainers actor pkg notif = do
799+
maintainers <- queryUserGroup $ maintainersGroup (mkPackageName $ BS.unpack pkg)
800+
pure . flip mapMaybe (toList maintainers) $ \uid -> do
801+
let NotifyPref{..} = fromMaybe defaultNotifyPrefs (Map.lookup uid notifyPrefs)
802+
guard $ uid /= actor
803+
guard $ not notifyOptOut
804+
Just (uid, notif)
805+
in case groupAction of
806+
(time, userActor, Admin_GroupAddUser userSubject (MaintainerGroup pkg), reason) ->
807+
notifyAllMaintainers userActor pkg $
808+
NotifyMaintainerUpdate
809+
{ notifyMaintainerUpdateType = MaintainerAdded
810+
, notifyUserActor = userActor
811+
, notifyUserSubject = userSubject
812+
, notifyPackageName = mkPackageName $ BS.unpack pkg
813+
, notifyReason = TL.toStrict $ TL.decodeUtf8 reason
814+
, notifyUpdatedAt = time
815+
}
816+
(time, userActor, Admin_GroupDelUser userSubject (MaintainerGroup pkg), reason) ->
817+
notifyAllMaintainers userActor pkg $
818+
NotifyMaintainerUpdate
819+
{ notifyMaintainerUpdateType = MaintainerRemoved
820+
, notifyUserActor = userActor
821+
, notifyUserSubject = userSubject
822+
, notifyPackageName = mkPackageName $ BS.unpack pkg
823+
, notifyReason = TL.toStrict $ TL.decodeUtf8 reason
824+
, notifyUpdatedAt = time
825+
}
826+
_ -> pure []
812827

813828
genDocReportList notifyPrefs mp pkgDoc = do
814829
let addNotification uid m =
@@ -832,28 +847,6 @@ userNotifyFeature serverEnv@ServerEnv{serverBaseURI, serverCron}
832847
Map.mapKeys (, pid) <$>
833848
getUserNotificationsOnRelease (queryUserGroup . maintainersGroup) idx revIdx queryGetUserNotifyPref pid
834849

835-
describeGroupAction users (time, uid, act, reason) =
836-
fmap
837-
( \message ->
838-
EmailContentParagraph ("Group modified by " <> formatTimeUser users time uid <> ":")
839-
<> EmailContentList
840-
[ message
841-
, "Reason: " <> emailContentLBS reason
842-
]
843-
)
844-
$ case act of
845-
(Admin_GroupAddUser tn (MaintainerGroup pkg)) ->
846-
Just $
847-
emailContentDisplay (Users.userIdToName users tn)
848-
<> " added to maintainers for "
849-
<> emailContentLBS pkg
850-
(Admin_GroupDelUser tn (MaintainerGroup pkg)) ->
851-
Just $
852-
emailContentDisplay (Users.userIdToName users tn)
853-
<> " removed from maintainers for "
854-
<> emailContentLBS pkg
855-
_ -> Nothing
856-
857850
describeDocReport (pkg, success) =
858851
EmailContentParagraph $
859852
"Package doc build for " <> emailContentDisplay (packageName pkg) <> ":" <> EmailContentSoftBreak <>
@@ -917,6 +910,16 @@ data Notification
917910
{ notifyPackageId :: PackageId
918911
, notifyRevisions :: [UploadInfo]
919912
}
913+
| NotifyMaintainerUpdate
914+
{ notifyMaintainerUpdateType :: NotifyMaintainerUpdateType
915+
, notifyUserActor :: UserId
916+
, notifyUserSubject :: UserId
917+
, notifyPackageName :: PackageName
918+
, notifyReason :: Text
919+
, notifyUpdatedAt :: UTCTime
920+
}
921+
922+
data NotifyMaintainerUpdateType = MaintainerAdded | MaintainerRemoved
920923

921924
-- | Notifications in the same group are batched in the same email.
922925
--
@@ -1024,6 +1027,15 @@ getNotificationEmails
10241027
renderNotifyNewRevision
10251028
notifyPackageId
10261029
notifyRevisions
1030+
NotifyMaintainerUpdate{..} ->
1031+
generalNotification $
1032+
renderNotifyMaintainerUpdate
1033+
notifyMaintainerUpdateType
1034+
notifyUserActor
1035+
notifyUserSubject
1036+
notifyPackageName
1037+
notifyReason
1038+
notifyUpdatedAt
10271039
where
10281040
generalNotification emailContent = Just (emailContent, GeneralNotification)
10291041

@@ -1036,8 +1048,22 @@ getNotificationEmails
10361048
EmailContentParagraph ("Package metadata revision(s), " <> renderPkgLink pkg <> ":")
10371049
<> EmailContentList (map (uncurry $ flip renderUserTime) $ sortOn (Down . fst) revs)
10381050

1051+
renderNotifyMaintainerUpdate updateType userActor userSubject pkg reason time =
1052+
EmailContentParagraph ("Group modified by " <> renderUserTime userActor time <> ":")
1053+
<> EmailContentList
1054+
[ case updateType of
1055+
MaintainerAdded ->
1056+
renderUser userSubject <> " added to maintainers for " <> renderPackageName pkg
1057+
MaintainerRemoved ->
1058+
renderUser userSubject <> " removed from maintainers for " <> renderPackageName pkg
1059+
, "Reason: " <> EmailContentText reason
1060+
]
1061+
1062+
10391063
{----- Rendering helpers -----}
10401064

1065+
renderPackageName = emailContentStr . unPackageName
1066+
10411067
renderPkgLink pkg =
10421068
EmailContentLink
10431069
(T.pack $ display pkg)

0 commit comments

Comments
 (0)