From e5bfa9e5d3549d68d5c2bcafddeee3c165116ac0 Mon Sep 17 00:00:00 2001 From: fisx Date: Tue, 29 Dec 2020 12:13:50 +0100 Subject: [PATCH] Random nit-picks (#1300) * Make error message more clear. * Fix garbled english. * Don't use one-letter identifiers; don't use pointless abstractions. * ormolu. --- services/brig/src/Brig/API/Public.hs | 2 +- services/brig/src/Brig/User/Handle.hs | 42 ++++++++++++--------------- services/spar/src/Spar/Scim/User.hs | 2 +- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/services/brig/src/Brig/API/Public.hs b/services/brig/src/Brig/API/Public.hs index 130c87f2900..71038788882 100644 --- a/services/brig/src/Brig/API/Public.hs +++ b/services/brig/src/Brig/API/Public.hs @@ -1301,7 +1301,7 @@ changeLocaleH (u ::: conn ::: req) = do lift $ API.changeLocale u conn l return empty --- | (zusr are is ignored by this handler, ie. checking handles is allowed as long as you have +-- | (zusr is ignored by this handler, ie. checking handles is allowed as long as you have -- *any* account.) checkHandleH :: UserId ::: Text -> Handler Response checkHandleH (_uid ::: hndl) = do diff --git a/services/brig/src/Brig/User/Handle.hs b/services/brig/src/Brig/User/Handle.hs index b04e959bc15..6dea85029c6 100644 --- a/services/brig/src/Brig/User/Handle.hs +++ b/services/brig/src/Brig/User/Handle.hs @@ -35,29 +35,25 @@ import Imports -- | Claim a new handle for an existing 'User'. claimHandle :: UserId -> Maybe Handle -> Handle -> AppIO Bool -claimHandle uid oldHandle newHandle = isJust <$> claimHandleWith (User.updateHandle) uid oldHandle newHandle - --- | Claim a handle for an invitation or a user. Invitations can be referenced by the coerced --- 'UserId'. -claimHandleWith :: (UserId -> Handle -> AppIO a) -> UserId -> Maybe Handle -> Handle -> AppIO (Maybe a) -claimHandleWith updOperation uid oldHandle h = do - owner <- lookupHandle h - case owner of - Just uid' | uid /= uid' -> return Nothing - _ -> do - env <- ask - let key = "@" <> fromHandle h - withClaim uid key (30 # Minute) $ - runAppT env $ - do - -- Record ownership - retry x5 $ write handleInsert (params Quorum (h, uid)) - -- Update profile - result <- updOperation uid h - -- Free old handle (if it changed) - for_ (mfilter (/= h) oldHandle) $ - freeHandle uid - return result +claimHandle uid oldHandle newHandle = + isJust <$> do + owner <- lookupHandle newHandle + case owner of + Just uid' | uid /= uid' -> return Nothing + _ -> do + env <- ask + let key = "@" <> fromHandle newHandle + withClaim uid key (30 # Minute) $ + runAppT env $ + do + -- Record ownership + retry x5 $ write handleInsert (params Quorum (newHandle, uid)) + -- Update profile + result <- User.updateHandle uid newHandle + -- Free old handle (if it changed) + for_ (mfilter (/= newHandle) oldHandle) $ + freeHandle uid + return result -- | Free a 'Handle', making it available to be claimed again. freeHandle :: UserId -> Handle -> AppIO () diff --git a/services/spar/src/Spar/Scim/User.hs b/services/spar/src/Spar/Scim/User.hs index 5136428d998..8fbb9a6c1ba 100644 --- a/services/spar/src/Spar/Scim/User.hs +++ b/services/spar/src/Spar/Scim/User.hs @@ -581,7 +581,7 @@ assertHandleNotUsedElsewhere :: UserId -> Handle -> Scim.ScimHandler Spar () assertHandleNotUsedElsewhere uid hndl = do musr <- lift $ Brig.getBrigUser Brig.WithPendingInvitations uid unless ((userHandle =<< musr) == Just hndl) $ - assertHandleUnused' "userName does not match UserId" hndl + assertHandleUnused' "userName already in use by another wire user" hndl -- | Helper function that translates a given brig user into a 'Scim.StoredUser', with some -- effects like updating the 'ManagedBy' field in brig and storing creation and update time