Skip to content

Commit

Permalink
brig: Untangle sending user notifs and maintaining internal state
Browse files Browse the repository at this point in the history
Brig.IO.Intra.onUserEvent used to keep the search index up to date, send user
events and send journal events. The keeping the search index up to date part is
now bubbled up to all the places which were calling this function.

This commit also deals with there no longer being UserSearchSubsystem.
  • Loading branch information
akshaymankar committed Aug 26, 2024
1 parent 0c3c26a commit ea066db
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 470 deletions.
49 changes: 12 additions & 37 deletions services/brig/src/Brig/API/Auth.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import Brig.API.Types
import Brig.API.User
import Brig.App
import Brig.Data.User qualified as User
import Brig.Effects.ConnectionStore (ConnectionStore)
import Brig.Options
import Brig.User.Auth qualified as Auth
import Brig.ZAuth hiding (Env, settings)
Expand All @@ -36,14 +35,12 @@ import Data.List1 (List1 (..))
import Data.Qualified
import Data.Text qualified as T
import Data.Text.Lazy qualified as LT
import Data.Time.Clock (UTCTime)
import Data.ZAuth.Token qualified as ZAuth
import Imports
import Network.HTTP.Types
import Network.Wai.Utilities ((!>>))
import Network.Wai.Utilities.Error qualified as Wai
import Polysemy
import Polysemy.Input (Input)
import Polysemy.TinyLog (TinyLog)
import Wire.API.Error
import Wire.API.Error.Brig qualified as E
Expand All @@ -54,24 +51,18 @@ import Wire.API.User.Auth.ReAuth
import Wire.API.User.Auth.Sso
import Wire.BlockListStore
import Wire.EmailSubsystem (EmailSubsystem)
import Wire.Events (Events)
import Wire.GalleyAPIAccess
import Wire.NotificationSubsystem
import Wire.PasswordStore (PasswordStore)
import Wire.Sem.Paging.Cassandra (InternalPaging)
import Wire.UserKeyStore
import Wire.UserSearchSubsystem (UserSearchSubsystem)
import Wire.UserStore
import Wire.UserSubsystem
import Wire.VerificationCodeSubsystem (VerificationCodeSubsystem)

accessH ::
( Member TinyLog r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r,
Member UserSearchSubsystem r
Member UserSubsystem r,
Member Events r
) =>
Maybe ClientId ->
[Either Text SomeUserToken] ->
Expand All @@ -86,12 +77,8 @@ accessH mcid ut' mat' = do
access ::
( TokenPair u a,
Member TinyLog r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r,
Member UserSearchSubsystem r
Member UserSubsystem r,
Member Events r
) =>
Maybe ClientId ->
NonEmpty (Token u) ->
Expand All @@ -109,16 +96,12 @@ sendLoginCode _ =
login ::
( Member GalleyAPIAccess r,
Member TinyLog r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r,
Member PasswordStore r,
Member UserKeyStore r,
Member UserStore r,
Member VerificationCodeSubsystem r,
Member UserSearchSubsystem r
Member UserSubsystem r,
Member Events r
) =>
Login ->
Maybe Bool ->
Expand Down Expand Up @@ -146,7 +129,7 @@ changeSelfEmailH ::
( Member BlockListStore r,
Member UserKeyStore r,
Member EmailSubsystem r,
Member UserSearchSubsystem r
Member UserSubsystem r
) =>
[Either Text SomeUserToken] ->
Maybe (Either Text SomeAccessToken) ->
Expand Down Expand Up @@ -180,13 +163,9 @@ removeCookies lusr (RemoveCookies pw lls ids) =

legalHoldLogin ::
( Member GalleyAPIAccess r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member TinyLog r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r,
Member UserSearchSubsystem r
Member UserSubsystem r,
Member Events r
) =>
LegalHoldLogin ->
Handler r SomeAccess
Expand All @@ -197,12 +176,8 @@ legalHoldLogin lhl = do

ssoLogin ::
( Member TinyLog r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r,
Member UserSearchSubsystem r
Member UserSubsystem r,
Member Events r
) =>
SsoLogin ->
Maybe Bool ->
Expand Down
54 changes: 9 additions & 45 deletions services/brig/src/Brig/API/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import Brig.App
import Brig.Data.Client qualified as Data
import Brig.Data.Nonce as Nonce
import Brig.Data.User qualified as Data
import Brig.Effects.ConnectionStore (ConnectionStore)
import Brig.Effects.JwtTools (JwtTools)
import Brig.Effects.JwtTools qualified as JwtTools
import Brig.Effects.PublicKeyBundle (PublicKeyBundle)
Expand Down Expand Up @@ -84,13 +83,10 @@ import Data.Qualified
import Data.Set qualified as Set
import Data.Text.Encoding qualified as T
import Data.Text.Encoding.Error
import Data.Time.Clock (UTCTime)
import Imports
import Network.HTTP.Types.Method (StdMethod)
import Network.Wai.Utilities
import Polysemy
import Polysemy.Input (Input)
import Polysemy.TinyLog
import Servant (Link, ToHttpApiData (toUrlPiece))
import System.Logger.Class (field, msg, val, (~~))
import System.Logger.Class qualified as Log
Expand All @@ -109,14 +105,14 @@ import Wire.API.UserEvent
import Wire.API.UserMap (QualifiedUserMap (QualifiedUserMap, qualifiedUserMap), UserMap (userMap))
import Wire.DeleteQueue
import Wire.EmailSubsystem (EmailSubsystem, sendNewClientEmail)
import Wire.Events (Events)
import Wire.Events qualified as Events
import Wire.GalleyAPIAccess (GalleyAPIAccess)
import Wire.GalleyAPIAccess qualified as GalleyAPIAccess
import Wire.NotificationSubsystem
import Wire.Sem.Concurrency
import Wire.Sem.FromUTC (FromUTC (fromUTCTime))
import Wire.Sem.Now as Now
import Wire.Sem.Paging.Cassandra (InternalPaging)
import Wire.UserSearchSubsystem (UserSearchSubsystem)
import Wire.VerificationCodeSubsystem (VerificationCodeSubsystem)

lookupLocalClient :: UserId -> ClientId -> (AppT r) (Maybe Client)
Expand Down Expand Up @@ -164,16 +160,11 @@ lookupLocalPubClientsBulk = lift . wrapClient . Data.lookupPubClientsBulk

addClient ::
( Member GalleyAPIAccess r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member TinyLog r,
Member DeleteQueue r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r,
Member EmailSubsystem r,
Member VerificationCodeSubsystem r,
Member UserSearchSubsystem r
Member Events r
) =>
UserId ->
Maybe ConnId ->
Expand All @@ -186,16 +177,11 @@ addClient = addClientWithReAuthPolicy Data.reAuthForNewClients
addClientWithReAuthPolicy ::
forall r.
( Member GalleyAPIAccess r,
Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member TinyLog r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member DeleteQueue r,
Member (ConnectionStore InternalPaging) r,
Member EmailSubsystem r,
Member VerificationCodeSubsystem r,
Member UserSearchSubsystem r
Member Events r
) =>
Data.ReAuthPolicy ->
UserId ->
Expand Down Expand Up @@ -224,7 +210,7 @@ addClientWithReAuthPolicy policy u con new = do
for_ old $ execDelete u con
liftSem $ GalleyAPIAccess.newClient u (clientId clt)
liftSem $ Intra.onClientEvent u con (ClientAdded clt)
when (clientType clt == LegalHoldClientType) $ liftSem $ Intra.onUserEvent u con (UserLegalHoldEnabled u)
when (clientType clt == LegalHoldClientType) $ liftSem $ Events.generateUserEvent u con (UserLegalHoldEnabled u)
when (count > 1) $
for_ (userEmail usr) $
\email ->
Expand Down Expand Up @@ -517,20 +503,9 @@ pubClient c =
pubClientClass = clientClass c
}

legalHoldClientRequested ::
( Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member TinyLog r,
Member (Input (Local ())) r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r,
Member UserSearchSubsystem r
) =>
UserId ->
LegalHoldClientRequest ->
AppT r ()
legalHoldClientRequested :: (Member Events r) => UserId -> LegalHoldClientRequest -> AppT r ()
legalHoldClientRequested targetUser (LegalHoldClientRequest _requester lastPrekey') =
liftSem $ Intra.onUserEvent targetUser Nothing lhClientEvent
liftSem $ Events.generateUserEvent targetUser Nothing lhClientEvent
where
clientId :: ClientId
clientId = clientIdFromPrekey $ unpackLastPrekey lastPrekey'
Expand All @@ -539,25 +514,14 @@ legalHoldClientRequested targetUser (LegalHoldClientRequest _requester lastPreke
lhClientEvent :: UserEvent
lhClientEvent = LegalHoldClientRequested eventData

removeLegalHoldClient ::
( Member (Embed HttpClientIO) r,
Member NotificationSubsystem r,
Member TinyLog r,
Member (Input (Local ())) r,
Member DeleteQueue r,
Member (Input UTCTime) r,
Member (ConnectionStore InternalPaging) r,
Member UserSearchSubsystem r
) =>
UserId ->
AppT r ()
removeLegalHoldClient :: (Member DeleteQueue r, Member Events r) => UserId -> AppT r ()
removeLegalHoldClient uid = do
clients <- wrapClient $ Data.lookupClients uid
-- Should only be one; but just in case we'll treat it as a list
let legalHoldClients = filter ((== LegalHoldClientType) . clientType) clients
-- maybe log if this isn't the case
forM_ legalHoldClients (execDelete uid Nothing)
liftSem $ Intra.onUserEvent uid Nothing (UserLegalHoldDisabled uid)
liftSem $ Events.generateUserEvent uid Nothing (UserLegalHoldDisabled uid)

createAccessToken ::
(Member JwtTools r, Member Now r, Member PublicKeyBundle r) =>
Expand Down
9 changes: 5 additions & 4 deletions services/brig/src/Brig/API/Federation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ import Wire.GalleyAPIAccess (GalleyAPIAccess)
import Wire.NotificationSubsystem
import Wire.Sem.Concurrency
import Wire.UserStore
import Wire.UserSubsystem
import Wire.UserSubsystem (UserSubsystem)
import Wire.UserSubsystem qualified as UserSubsystem

type FederationAPI = "federation" :> BrigApi

Expand Down Expand Up @@ -167,7 +168,7 @@ getUserByHandle domain handle = do
pure Nothing
Just ownerId -> do
localOwnerId <- qualifyLocal ownerId
liftSem $ getLocalUserProfile localOwnerId
liftSem $ UserSubsystem.getLocalUserProfile localOwnerId

getUsersByIds ::
(Member UserSubsystem r) =>
Expand All @@ -176,7 +177,7 @@ getUsersByIds ::
ExceptT HttpError (AppT r) [UserProfile]
getUsersByIds _ uids = do
luids <- qualifyLocal uids
lift $ liftSem $ getLocalUserProfiles luids
lift $ liftSem $ UserSubsystem.getLocalUserProfiles luids

claimPrekey :: (Member DeleteQueue r) => Domain -> (UserId, ClientId) -> (Handler r) (Maybe ClientPrekey)
claimPrekey _ (user, client) = do
Expand Down Expand Up @@ -252,7 +253,7 @@ searchUsers domain (SearchRequest searchTerm mTeam mOnlyInTeams) = do
mFoundUserTeamId <- lift $ wrapClient $ Data.lookupUserTeam foundUser
localFoundUser <- qualifyLocal foundUser
if isTeamAllowed mOnlyInTeams mFoundUserTeamId
then lift $ liftSem $ (fmap contactFromProfile . maybeToList) <$> getLocalUserProfile localFoundUser
then lift $ liftSem $ (fmap contactFromProfile . maybeToList) <$> UserSubsystem.getLocalUserProfile localFoundUser
else pure []
| otherwise = pure []

Expand Down
Loading

0 comments on commit ea066db

Please sign in to comment.