Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split Members into multiple constraints #3093

Merged
merged 5 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5-internal/split-members-constraints
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Split polysemy `Members` constraints into multiple `Member` constraints
7 changes: 5 additions & 2 deletions hack/bin/split-member-constraints.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ def make_constraint(e):

def f(m):
effects = re.split(r'\s*,\s*', m.group(1))
constraints = ', '.join(make_constraint(e) for e in effects)
return f'({constraints})'
constraints = [make_constraint(e) for e in effects]
s = ',\n '.join(constraints)
if len(constraints) > 1:
s = f'({s})'
return s

code = open(sys.argv[1]).read()
print(re.sub(r"Members\s+'\[\s*([^\]]*)\s*\]\s+r", f, code, flags=re.MULTILINE),
Expand Down
17 changes: 7 additions & 10 deletions services/brig/src/Brig/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,13 @@ import Wire.Sem.Concurrency

sitemap ::
forall r p.
( Members
'[ BlacklistPhonePrefixStore,
BlacklistStore,
GalleyProvider,
CodeStore,
Concurrency 'Unsafe,
PasswordResetStore,
UserPendingActivationStore p
]
r
( Member BlacklistPhonePrefixStore r,
Member BlacklistStore r,
Member GalleyProvider r,
Member CodeStore r,
Member (Concurrency 'Unsafe) r,
Member PasswordResetStore r,
Member (UserPendingActivationStore p) r
) =>
Routes () (Handler r) ()
sitemap = do
Expand Down
10 changes: 5 additions & 5 deletions services/brig/src/Brig/API/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ import Data.String.Conversions (cs)
import Imports
import Network.HTTP.Types.Method (StdMethod)
import Network.Wai.Utilities
import Polysemy (Member, Members)
import Polysemy (Member)
import Servant (Link, ToHttpApiData (toUrlPiece))
import System.Logger.Class (field, msg, val, (~~))
import qualified System.Logger.Class as Log
Expand Down Expand Up @@ -146,7 +146,7 @@ lookupLocalPubClientsBulk :: [UserId] -> ExceptT ClientError (AppT r) (UserMap (
lookupLocalPubClientsBulk = lift . wrapClient . Data.lookupPubClientsBulk

addClient ::
(Members '[GalleyProvider] r, CallsFed 'Brig "on-user-deleted-connections") =>
(Member GalleyProvider r, CallsFed 'Brig "on-user-deleted-connections") =>
UserId ->
Maybe ConnId ->
Maybe IP ->
Expand All @@ -158,7 +158,7 @@ addClient = addClientWithReAuthPolicy Data.reAuthForNewClients
-- a superset of the clients known to galley.
addClientWithReAuthPolicy ::
forall r.
(Members '[GalleyProvider] r, CallsFed 'Brig "on-user-deleted-connections") =>
(Member GalleyProvider r, CallsFed 'Brig "on-user-deleted-connections") =>
Data.ReAuthPolicy ->
UserId ->
Maybe ConnId ->
Expand Down Expand Up @@ -293,7 +293,7 @@ claimRemotePrekeyBundle quser = do

claimMultiPrekeyBundles ::
forall r.
(Members '[Concurrency 'Unsafe] r, CallsFed 'Brig "claim-multi-prekey-bundle") =>
(Member (Concurrency 'Unsafe) r, CallsFed 'Brig "claim-multi-prekey-bundle") =>
LegalholdProtectee ->
QualifiedUserClients ->
ExceptT ClientError (AppT r) QualifiedUserClientPrekeyMap
Expand Down Expand Up @@ -333,7 +333,7 @@ claimMultiPrekeyBundles protectee quc = do

claimLocalMultiPrekeyBundles ::
forall r.
Members '[Concurrency 'Unsafe] r =>
Member (Concurrency 'Unsafe) r =>
LegalholdProtectee ->
UserClients ->
ExceptT ClientError (AppT r) UserClientPrekeyMap
Expand Down
12 changes: 6 additions & 6 deletions services/brig/src/Brig/API/Connection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ import Data.Qualified
import Data.Range
import qualified Data.UUID.V4 as UUID
import Imports
import Polysemy (Members)
import Polysemy (Member)
import qualified System.Logger.Class as Log
import System.Logger.Message
import Wire.API.Connection hiding (relationWithHistory)
import Wire.API.Conversation
import Wire.API.Conversation hiding (Member)
import Wire.API.Error
import qualified Wire.API.Error.Brig as E
import Wire.API.Federation.API
Expand All @@ -68,15 +68,15 @@ ensureIsActivated lusr = do
active <- lift . wrapClient $ Data.isActivated (tUnqualified lusr)
guard active

ensureNotSameTeam :: Members '[GalleyProvider] r => Local UserId -> Local UserId -> (ConnectionM r) ()
ensureNotSameTeam :: Member GalleyProvider r => Local UserId -> Local UserId -> (ConnectionM r) ()
ensureNotSameTeam self target = do
selfTeam <- lift $ liftSem $ GalleyProvider.getTeamId (tUnqualified self)
targetTeam <- lift $ liftSem $ GalleyProvider.getTeamId (tUnqualified target)
when (isJust selfTeam && selfTeam == targetTeam) $
throwE ConnectSameBindingTeamUsers

createConnection ::
(Members '[GalleyProvider] r, CallsFed 'Brig "send-connection-action") =>
(Member GalleyProvider r, CallsFed 'Brig "send-connection-action") =>
Local UserId ->
ConnId ->
Qualified UserId ->
Expand All @@ -96,7 +96,7 @@ createConnection self con target = do
target

createConnectionToLocalUser ::
Members '[GalleyProvider] r =>
Member GalleyProvider r =>
Local UserId ->
ConnId ->
Local UserId ->
Expand Down Expand Up @@ -185,7 +185,7 @@ createConnectionToLocalUser self conn target = do
-- FUTUREWORK: we may want to move this to the LH application logic, so we can recycle it for
-- group conv creation and possibly other situations.
checkLegalholdPolicyConflict ::
Members '[GalleyProvider] r =>
Member GalleyProvider r =>
UserId ->
UserId ->
ExceptT ConnectionError (AppT r) ()
Expand Down
22 changes: 7 additions & 15 deletions services/brig/src/Brig/API/Federation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ import Wire.Sem.Concurrency
type FederationAPI = "federation" :> BrigApi

federationSitemap ::
Members
'[ GalleyProvider,
Concurrency 'Unsafe
]
r =>
( Member GalleyProvider r,
Member (Concurrency 'Unsafe) r
) =>
ServerT FederationAPI (Handler r)
federationSitemap =
Named @"api-version" (\_ _ -> pure versionInfo)
Expand Down Expand Up @@ -103,10 +101,7 @@ sendConnectionAction originDomain NewConnectionRequest {..} = do
else pure NewConnectionResponseUserNotActivated

getUserByHandle ::
Members
'[ GalleyProvider
]
r =>
Member GalleyProvider r =>
Domain ->
Handle ->
ExceptT Error (AppT r) (Maybe UserProfile)
Expand All @@ -129,10 +124,7 @@ getUserByHandle domain handle = do
listToMaybe <$> API.lookupLocalProfiles Nothing [ownerId]

getUsersByIds ::
Members
'[ GalleyProvider
]
r =>
Member GalleyProvider r =>
Domain ->
[UserId] ->
ExceptT Error (AppT r) [UserProfile]
Expand All @@ -148,7 +140,7 @@ claimPrekeyBundle _ user =
API.claimLocalPrekeyBundle LegalholdPlusFederationNotImplemented user !>> clientError

claimMultiPrekeyBundle ::
Members '[Concurrency 'Unsafe] r =>
Member (Concurrency 'Unsafe) r =>
Domain ->
UserClients ->
Handler r UserClientPrekeyMap
Expand All @@ -169,7 +161,7 @@ fedClaimKeyPackages domain ckpr =
-- (This decision may change in the future)
searchUsers ::
forall r.
Members '[GalleyProvider] r =>
Member GalleyProvider r =>
Domain ->
SearchRequest ->
ExceptT Error (AppT r) SearchResponse
Expand Down
70 changes: 25 additions & 45 deletions services/brig/src/Brig/API/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,9 @@ import Wire.API.User.RichInfo
-- Sitemap (servant)

servantSitemap ::
( Members
'[ BlacklistStore,
GalleyProvider,
UserPendingActivationStore p
]
r
( Member BlacklistStore r,
Member GalleyProvider r,
Member (UserPendingActivationStore p) r
) =>
ServerT BrigIRoutes.API (Handler r)
servantSitemap =
Expand All @@ -121,10 +118,7 @@ servantSitemap =
:<|> authAPI

ejpdAPI ::
Members
'[ GalleyProvider
]
r =>
Member GalleyProvider r =>
ServerT BrigIRoutes.EJPD_API (Handler r)
ejpdAPI =
Brig.User.EJPD.ejpdRequest
Expand All @@ -149,12 +143,9 @@ mlsAPI =
:<|> Named @"put-key-package-add" upsertKeyPackage

accountAPI ::
( Members
'[ BlacklistStore,
GalleyProvider,
UserPendingActivationStore p
]
r
( Member BlacklistStore r,
Member GalleyProvider r,
Member (UserPendingActivationStore p) r
) =>
ServerT BrigIRoutes.AccountAPI (Handler r)
accountAPI =
Expand Down Expand Up @@ -281,15 +272,12 @@ getVerificationCode uid action = do
-- Sitemap (wai-route)

sitemap ::
( Members
'[ CodeStore,
PasswordResetStore,
BlacklistStore,
BlacklistPhonePrefixStore,
GalleyProvider,
UserPendingActivationStore p
]
r
( Member CodeStore r,
Member PasswordResetStore r,
Member BlacklistStore r,
Member BlacklistPhonePrefixStore r,
Member GalleyProvider r,
Member (UserPendingActivationStore p) r
) =>
Routes a (Handler r) ()
sitemap = unsafeCallsFed @'Brig @"on-user-deleted-connections" $ do
Expand Down Expand Up @@ -454,10 +442,7 @@ sitemap = unsafeCallsFed @'Brig @"on-user-deleted-connections" $ do

-- | Add a client without authentication checks
addClientInternalH ::
( Members
'[ GalleyProvider
]
r,
( Member GalleyProvider r,
CallsFed 'Brig "on-user-deleted-connections"
) =>
UserId ::: Maybe Bool ::: JsonRequest NewClient ::: Maybe ConnId ::: JSON ->
Expand All @@ -467,10 +452,7 @@ addClientInternalH (usr ::: mSkipReAuth ::: req ::: connId ::: _) = do
setStatus status201 . json <$> addClientInternal usr mSkipReAuth new connId

addClientInternal ::
( Members
'[ GalleyProvider
]
r,
( Member GalleyProvider r,
CallsFed 'Brig "on-user-deleted-connections"
) =>
UserId ->
Expand Down Expand Up @@ -513,12 +495,9 @@ internalListFullClients (UserSet usrs) =
UserClientsFull <$> wrapClient (Data.lookupClientsBulk (Set.toList usrs))

createUserNoVerify ::
( Members
'[ BlacklistStore,
GalleyProvider,
UserPendingActivationStore p
]
r,
( Member BlacklistStore r,
Member GalleyProvider r,
Member (UserPendingActivationStore p) r,
CallsFed 'Brig "on-user-deleted-connections"
) =>
NewUser ->
Expand All @@ -537,10 +516,7 @@ createUserNoVerify uData = lift . runExceptT $ do
pure . SelfProfile $ usr

createUserNoVerifySpar ::
( Members
'[ GalleyProvider
]
r,
( Member GalleyProvider r,
CallsFed 'Brig "on-user-deleted-connections"
) =>
NewUserSpar ->
Expand Down Expand Up @@ -640,14 +616,18 @@ instance ToJSON GetActivationCodeResp where
toJSON (GetActivationCodeResp (k, c)) = object ["key" .= k, "code" .= c]

getPasswordResetCodeH ::
Members '[CodeStore, PasswordResetStore] r =>
( Member CodeStore r,
Member PasswordResetStore r
) =>
JSON ::: Either Email Phone ->
(Handler r) Response
getPasswordResetCodeH (_ ::: emailOrPhone) = do
maybe (throwStd (errorToWai @'E.InvalidPasswordResetKey)) (pure . json) =<< lift (getPasswordResetCode emailOrPhone)

getPasswordResetCode ::
Members '[CodeStore, PasswordResetStore] r =>
( Member CodeStore r,
Member PasswordResetStore r
) =>
Either Email Phone ->
(AppT r) (Maybe GetPasswordResetCodeResp)
getPasswordResetCode emailOrPhone =
Expand Down
Loading