Skip to content

Commit

Permalink
Send confirmation email
Browse files Browse the repository at this point in the history
  • Loading branch information
pcapriotti committed Sep 19, 2024
1 parent ab24c5d commit 6c871cf
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 7 deletions.
2 changes: 1 addition & 1 deletion libs/wire-api/src/Wire/API/Team.hs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ instance ToSchema TeamList where
--------------------------------------------------------------------------------
-- NewTeam

newtype BindingNewTeam = BindingNewTeam (NewTeam ())
newtype BindingNewTeam = BindingNewTeam {bntTeam :: NewTeam ()}
deriving stock (Eq, Show, Generic)
deriving (ToJSON, FromJSON, S.ToSchema) via (Schema BindingNewTeam)

Expand Down
14 changes: 10 additions & 4 deletions libs/wire-api/src/Wire/API/User.hs
Original file line number Diff line number Diff line change
Expand Up @@ -791,11 +791,14 @@ instance ToSchema CreateUserTeam where
<$> createdTeamId .= field "team_id" schema
<*> createdTeamName .= field "team_name" schema

data UpgradePersonalToTeamError = UpgradePersonalToTeamErrorAlreadyInATeam
data UpgradePersonalToTeamError
= UpgradePersonalToTeamErrorAlreadyInATeam
| UpgradePersonalToTeamErrorUserNotFound
deriving (Show)

type UpgradePersonalToTeamResponses =
'[ ErrorResponse UserAlreadyInATeam,
ErrorResponse UserNotFound,
Respond 200 "Team created" CreateUserTeam
]

Expand All @@ -806,11 +809,14 @@ instance
where
toUnion (Left UpgradePersonalToTeamErrorAlreadyInATeam) =
Z (I (dynError @(MapError UserAlreadyInATeam)))
toUnion (Right x) = S (Z (I x))
toUnion (Left UpgradePersonalToTeamErrorUserNotFound) =
S (Z (I (dynError @(MapError UserNotFound))))
toUnion (Right x) = S (S (Z (I x)))

fromUnion (Z (I _)) = Left UpgradePersonalToTeamErrorAlreadyInATeam
fromUnion (S (Z (I x))) = Right x
fromUnion (S (S x)) = case x of {}
fromUnion (S (Z (I _))) = Left UpgradePersonalToTeamErrorAlreadyInATeam
fromUnion (S (S (Z (I x)))) = Right x
fromUnion (S (S (S x))) = case x of {}

data RegisterError
= RegisterErrorAllowlistError
Expand Down
1 change: 1 addition & 0 deletions libs/wire-subsystems/src/Wire/EmailSubsystem.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ data EmailSubsystem m a where
SendAccountDeletionEmail :: EmailAddress -> Name -> Code.Key -> Code.Value -> Locale -> EmailSubsystem m ()
SendTeamActivationMail :: EmailAddress -> Name -> ActivationKey -> ActivationCode -> Maybe Locale -> Text -> EmailSubsystem m ()
SendTeamDeletionVerificationMail :: EmailAddress -> Code.Value -> Maybe Locale -> EmailSubsystem m ()
SendUpgradePersonalToTeamConfirmationEmail :: EmailAddress -> Name -> Text -> Locale -> EmailSubsystem m ()

makeSem ''EmailSubsystem
37 changes: 37 additions & 0 deletions libs/wire-subsystems/src/Wire/EmailSubsystem/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ emailSubsystemInterpreter tpls branding = interpret \case
SendTeamActivationMail email name key code mLocale teamName -> sendTeamActivationMailImpl tpls branding email name key code mLocale teamName
SendNewClientEmail email name client locale -> sendNewClientEmailImpl tpls branding email name client locale
SendAccountDeletionEmail email name key code locale -> sendAccountDeletionEmailImpl tpls branding email name key code locale
SendUpgradePersonalToTeamConfirmationEmail email name teamName locale -> sendUpgradePersonalToTeamConfirmationEmailImpl tpls branding email name teamName locale

-------------------------------------------------------------------------------
-- Verification Email for
Expand Down Expand Up @@ -395,6 +396,42 @@ renderDeletionEmail email name cKey cValue DeletionEmailTemplate {..} branding =
replace2 "code" = code
replace2 x = x

--------------------------------------------------------------------------------
-- Upgrade personal user to team owner confirmation email

sendUpgradePersonalToTeamConfirmationEmailImpl ::
(Member EmailSending r) =>
Localised UserTemplates ->
TemplateBranding ->
EmailAddress ->
Name ->
Text ->
Locale ->
Sem r ()
sendUpgradePersonalToTeamConfirmationEmailImpl userTemplates branding email name teamName locale = do
let tpl = upgradePersonalToTeamEmail . snd $ forLocale (Just locale) userTemplates
sendMail $ renderUpgradePersonalToTeamConfirmationEmail email name teamName tpl branding

renderUpgradePersonalToTeamConfirmationEmail :: EmailAddress -> Name -> Text -> UpgradePersonalToTeamEmailTemplate -> TemplateBranding -> Mail
renderUpgradePersonalToTeamConfirmationEmail email name _teamName UpgradePersonalToTeamEmailTemplate {..} branding =
(emptyMail from)
{ mailTo = [to],
mailHeaders =
[ ("Subject", toStrict subj),
("X-Zeta-Purpose", "Upgrade")
],
mailParts = [[plainPart txt, htmlPart html]]
}
where
from = Address (Just upgradePersonalToTeamEmailSenderName) (fromEmail upgradePersonalToTeamEmailSender)
to = mkMimeAddress name email
txt = renderTextWithBranding upgradePersonalToTeamEmailBodyText replace1 branding
html = renderHtmlWithBranding upgradePersonalToTeamEmailBodyHtml replace1 branding
subj = renderTextWithBranding upgradePersonalToTeamEmailSubject replace1 branding
replace1 "email" = fromEmail email
replace1 "name" = fromName name
replace1 x = x

-------------------------------------------------------------------------------
-- MIME Conversions

Expand Down
10 changes: 10 additions & 0 deletions libs/wire-subsystems/src/Wire/EmailSubsystem/Template.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Wire.EmailSubsystem.Template
LoginCallTemplate (..),
DeletionSmsTemplate (..),
DeletionEmailTemplate (..),
UpgradePersonalToTeamEmailTemplate (..),
NewClientEmailTemplate (..),
SecondFactorVerificationEmailTemplate (..),

Expand Down Expand Up @@ -105,6 +106,7 @@ data UserTemplates = UserTemplates
loginCall :: LoginCallTemplate,
deletionSms :: DeletionSmsTemplate,
deletionEmail :: DeletionEmailTemplate,
upgradePersonalToTeamEmail :: UpgradePersonalToTeamEmailTemplate,
newClientEmail :: NewClientEmailTemplate,
verificationLoginEmail :: SecondFactorVerificationEmailTemplate,
verificationScimTokenEmail :: SecondFactorVerificationEmailTemplate,
Expand Down Expand Up @@ -157,6 +159,14 @@ data DeletionEmailTemplate = DeletionEmailTemplate
deletionEmailSenderName :: Text
}

data UpgradePersonalToTeamEmailTemplate = UpgradePersonalToTeamEmailTemplate
{ upgradePersonalToTeamEmailSubject :: Template,
upgradePersonalToTeamEmailBodyText :: Template,
upgradePersonalToTeamEmailBodyHtml :: Template,
upgradePersonalToTeamEmailSender :: EmailAddress,
upgradePersonalToTeamEmailSenderName :: Text
}

data PasswordResetEmailTemplate = PasswordResetEmailTemplate
{ passwordResetEmailUrl :: Template,
passwordResetEmailSubject :: Template,
Expand Down
1 change: 1 addition & 0 deletions services/brig/src/Brig/API/Public.hs
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ createAccessToken method luid cid proof = do
upgradePersonalToTeam ::
( Member (ConnectionStore InternalPaging) r,
Member (Embed HttpClientIO) r,
Member EmailSubsystem r,
Member GalleyAPIAccess r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Expand Down
18 changes: 16 additions & 2 deletions services/brig/src/Brig/API/User.hs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ createUserSpar new = do
upgradePersonalToTeam ::
forall r.
( Member GalleyAPIAccess r,
Member EmailSubsystem r,
Member UserSubsystem r,
Member TinyLog r,
Member (Embed HttpClientIO) r,
Expand All @@ -273,8 +274,12 @@ upgradePersonalToTeam ::
upgradePersonalToTeam luid bNewTeam = do
-- check that the user is not part of a team
mSelfProfile <- lift $ liftSem $ getSelfProfile luid
let mTid = mSelfProfile >>= userTeam . selfUser
when (isJust mTid) $
user <-
maybe
(throwE UpgradePersonalToTeamErrorUserNotFound)
(pure . selfUser)
mSelfProfile
when (isJust user.userTeam) $
throwE UpgradePersonalToTeamErrorAlreadyInATeam

lift $ do
Expand All @@ -291,6 +296,15 @@ upgradePersonalToTeam luid bNewTeam = do
liftSem $ Intra.sendUserEvent uid Nothing (teamUpdated uid tid)
initAccountFeatureConfig uid

-- send confirmation email
for_ (userEmail user) $ \email -> do
liftSem $
sendUpgradePersonalToTeamConfirmationEmail
email
user.userDisplayName
bNewTeam.bnuTeam.bntTeam._newTeamName.fromRange
user.userLocale

pure $! createUserTeam

-- docs/reference/user/registration.md {#RefRegistration}
Expand Down
8 changes: 8 additions & 0 deletions services/brig/src/Brig/User/Template.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Brig.User.Template
LoginCallTemplate (..),
DeletionSmsTemplate (..),
DeletionEmailTemplate (..),
UpgradePersonalToTeamEmailTemplate (..),
NewClientEmailTemplate (..),
SecondFactorVerificationEmailTemplate (..),
loadUserTemplates,
Expand Down Expand Up @@ -109,6 +110,13 @@ loadUserTemplates o = readLocalesDir defLocale templateDir "user" $ \fp ->
<*> pure emailSender
<*> readText fp "email/sender.txt"
)
<*> ( UpgradePersonalToTeamEmailTemplate
<$> readTemplate fp "email/upgrade-subject.txt"
<*> readTemplate fp "email/upgrade.txt"
<*> readTemplate fp "email/upgrade.html"
<*> pure emailSender
<*> readText fp "email/sender.txt"
)
<*> ( NewClientEmailTemplate
<$> readTemplate fp "email/new-client-subject.txt"
<*> readTemplate fp "email/new-client.txt"
Expand Down

0 comments on commit 6c871cf

Please sign in to comment.