Skip to content

Commit

Permalink
[wip]: Update user search index on team changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
elland committed Sep 17, 2024
1 parent 1806e82 commit 16cf3af
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
8 changes: 8 additions & 0 deletions integration/test/Test/Teams.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,37 @@ testInvitePersonalUserToTeam = do
bindResponse (listInvitations owner tid) $ \resp -> do
resp.status `shouldMatchInt` 200
resp.json %. "invitations" `shouldMatch` ([] :: [()])

ownerId <- owner %. "id" & asString
setTeamFeatureStatus domain tid "exposeInvitationURLsToTeamAdmin" "enabled" >>= assertSuccess
user <- createUser domain def >>= getJSON 201
uid <- user %. "id" >>= asString
email <- user %. "email" >>= asString

inv <- postInvitation owner (PostInvitation (Just email) Nothing) >>= getJSON 201
checkListInvitations owner tid email
code <- getInvitationCode owner inv >>= getJSON 200 >>= (%. "code") & asString
inv %. "url" & asString >>= assertUrlContainsCode code
acceptTeamInvitation user code Nothing >>= assertStatus 400
acceptTeamInvitation user code (Just "wrong-password") >>= assertStatus 403

void $ withWebSockets [user] $ \wss -> do
acceptTeamInvitation user code (Just defPassword) >>= assertSuccess
for wss $ \ws -> do
n <- awaitMatch isUserUpdatedNotif ws
n %. "payload.0.user.team" `shouldMatch` tid

bindResponse (getSelf user) $ \resp -> do
resp.status `shouldMatchInt` 200
resp.json %. "team" `shouldMatch` tid

-- a team member can now find the former personal user in the team
bindResponse (getTeamMembers tm tid) $ \resp -> do
resp.status `shouldMatchInt` 200
members <- resp.json %. "members" >>= asList
ids <- for members ((%. "user") >=> asString)
ids `shouldContain` [uid]

-- the former personal user can now see other team members
bindResponse (getTeamMembers user tid) $ \resp -> do
resp.status `shouldMatchInt` 200
Expand All @@ -82,12 +88,14 @@ testInvitePersonalUserToTeam = do
tmId <- tm %. "id" & asString
ids `shouldContain` [ownerId]
ids `shouldContain` [tmId]

-- the former personal user can now search for the owner
bindResponse (searchContacts user (owner %. "name") domain) $ \resp -> do
resp.status `shouldMatchInt` 200
documents <- resp.json %. "documents" >>= asList
ids <- for documents ((%. "id") >=> asString)
ids `shouldContain` [ownerId]

refreshIndex domain
-- a team member can now search for the former personal user
bindResponse (searchContacts tm (user %. "name") domain) $ \resp -> do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,13 @@ syncAllUsersWithVersion mkVersion =

mkUserDocs :: ConduitT [IndexUser] [(ES.DocId, UserDoc, ES.VersionControl)] (Sem r) ()
mkUserDocs = Conduit.mapM $ \page -> do
visMap <- fmap Map.fromList . unsafePooledForConcurrentlyN 16 (Set.fromList $ mapMaybe (.teamId) page) $ \t ->
let teamIds = Set.fromList do
let teamIdsWithTime = (.teamId) <$> page
let vals = (value <$$> teamIdsWithTime)
catMaybes vals
visMap <- fmap Map.fromList . unsafePooledForConcurrentlyN 16 teamIds $ \t ->
(t,) <$> teamSearchVisibilityInbound t
let vis indexUser = fromMaybe defaultSearchVisibilityInbound $ flip Map.lookup visMap =<< indexUser.teamId
let vis indexUser = fromMaybe defaultSearchVisibilityInbound $ (flip Map.lookup visMap . value =<< indexUser.teamId)
mkUserDoc indexUser = indexUserToDoc (vis indexUser) indexUser
mkDocVersion = mkVersion . ES.ExternalDocVersion . docVersion . indexUserToVersion
pure $ map (\u -> (userIdToDocId u.userId, mkUserDoc u, mkDocVersion u)) page
Expand Down
14 changes: 8 additions & 6 deletions libs/wire-subsystems/src/Wire/UserStore/IndexUser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data WithWritetime a = WithWriteTime {value :: a, writetime :: Writetime a}

data IndexUser = IndexUser
{ userId :: UserId,
teamId :: Maybe TeamId,
teamId :: Maybe (WithWritetime TeamId),
name :: WithWritetime Name,
accountStatus :: Maybe (WithWritetime AccountStatus),
handle :: Maybe (WithWritetime Handle),
Expand All @@ -42,7 +42,7 @@ data IndexUser = IndexUser
type instance
TupleType IndexUser =
( UserId,
Maybe TeamId,
Maybe TeamId, Maybe (Writetime TeamId),
Name, Writetime Name,
Maybe AccountStatus, Maybe (Writetime AccountStatus),
Maybe Handle, Maybe (Writetime Handle),
Expand All @@ -57,7 +57,8 @@ type instance

instance Record IndexUser where
asTuple (IndexUser {..}) =
( userId, teamId,
( userId,
value <$> teamId, writetime <$> teamId,
name.value, name.writetime,
value <$> accountStatus, writetime <$> accountStatus,
value <$> handle, writetime <$> handle,
Expand All @@ -71,7 +72,8 @@ instance Record IndexUser where
)

asRecord
( u, mteam,
( u,
mTeam, tTeam,
name, tName,
status, tStatus,
handle, tHandle,
Expand All @@ -84,7 +86,7 @@ instance Record IndexUser where
emailUnvalidated, tEmailUnvalidated
) = IndexUser {
userId = u,
teamId = mteam,
teamId = WithWriteTime <$> mTeam <*> tTeam,
name = WithWriteTime name tName,
accountStatus = WithWriteTime <$> status <*> tStatus,
handle = WithWriteTime <$> handle <*> tHandle,
Expand Down Expand Up @@ -133,7 +135,7 @@ indexUserToDoc searchVisInbound IndexUser {..} =
udHandle = value <$> handle,
udNormalized = Just $ normalized name.value.fromName,
udName = Just name.value,
udTeam = teamId,
udTeam = value <$> teamId,
udId = userId
}
else -- We insert a tombstone-style user here, as it's easier than
Expand Down
2 changes: 1 addition & 1 deletion libs/wire-subsystems/src/Wire/UserSubsystem/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ syncUserIndex uid =
vis <-
maybe
(pure defaultSearchVisibilityInbound)
teamSearchVisibilityInbound
(teamSearchVisibilityInbound . value)
indexUser.teamId
let userDoc = indexUserToDoc vis indexUser
version = ES.ExternalGT . ES.ExternalDocVersion . docVersion $ indexUserToVersion indexUser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ storedUserToIndexUser storedUser =
let withDefaultTime x = WithWriteTime x $ Writetime $ UTCTime (YearDay 0 1) 0
in IndexUser
{ userId = storedUser.id,
teamId = storedUser.teamId,
teamId = withDefaultTime <$> storedUser.teamId,
name = withDefaultTime storedUser.name,
accountStatus = withDefaultTime <$> storedUser.status,
handle = withDefaultTime <$> storedUser.handle,
Expand Down
3 changes: 2 additions & 1 deletion services/brig/src/Brig/API/User.hs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ createUser new = do
unless added $
throwE RegisterErrorTooManyTeamMembers
lift $ do
wrapClient $ activateUser uid ident -- ('insertAccount' sets column activated to False; here it is set to True.)
-- ('insertAccount' sets column activated to False; here it is set to True.)
wrapClient $ activateUser uid ident
void $ onActivated (AccountActivated account)
liftSem do
Log.info $
Expand Down
2 changes: 2 additions & 0 deletions services/brig/src/Brig/Team/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import Wire.PasswordStore
import Wire.Sem.Concurrency
import Wire.UserKeyStore
import Wire.UserSubsystem
import Wire.UserSubsystem qualified as User
import Wire.UserSubsystem.Error

servantAPI ::
Expand Down Expand Up @@ -592,6 +593,7 @@ acceptTeamInvitationByPersonalUser luid req = do
lift $ do
wrapClient $ User.updateUserTeam uid tid
liftSem $ Store.deleteInvitation inv.teamId inv.invitationId
liftSem $ User.internalUpdateSearchIndex uid
liftSem $ Events.generateUserEvent uid Nothing (teamUpdated uid tid)
where
checkPassword = do
Expand Down

0 comments on commit 16cf3af

Please sign in to comment.