feat(teams): auto-link Teams users by email on connect and from settings
What
Follow-up to #30156. The Slack app auto-matches users by email in two ways that Teams did not: it sweeps the roster when the connection is added, and it offers an Auto-link by email button in settings to re-run that sweep. Teams only matched one sender at a time, when they first messaged the bot.
This ports both to Teams:
- On connect: once a workspace-shared connection with chat enabled has claimed its tenant, the hook reads the Microsoft organization directory and links every user whose
mail(oruserPrincipalName) matches a workspace member's email, as anAUTOlink. - Settings: a new
teams-match-user-linksroute (ROLES-gated, 120s) runs the same sweep on demand. The settings show the run summary next to the button, and a warning callout when the last run did not finish.
Demo
Running Auto-link by email in settings: every unlinked directory user whose email matches a workspace member is linked in one click, with a run summary, and existing manual links are left untouched.
How
- Source is Microsoft Graph
/usersvia the existingcollectGraphUserspaging ($top=999, up to 10 pages), using theUser.ReadBasic.Allscope added in #30156. The Bot Framework roster is not used: it rarely returns an email. - Guests invited from other tenants (
#EXT#in the principal name) are skipped.userTypeis not readable withUser.ReadBasic.All, so the marker is the only signal. - Users who already have a link, manual or automatic, are never touched, so
MANUALlinks stay authoritative. - Member emails are resolved in batches of 50 with a ported
findWorkspaceMemberIdsByEmails; an email that belongs to more than one member is skipped and counted. - Links are created in bulk batches of 200, falling back to one-by-one on a failed batch (duplicates count as already linked), with a bounded retry budget so a bad run cannot eat the whole timeout.
- The run outcome (
{ isSuccessful, errorMessage? }) is stored in the workspace key-value store and returned ashasAutoMatchFailedon the existing unlinked-users response, so the settings need no extra request. - The on-connect sweep is best-effort: a failure is logged and never fails the tenant claim. The
teams-register-connectiontimeout grows from 30s to 120s, matching Slack.
Differences from Slack, on purpose
- No installer-specific step: the installer is in the directory.
- No soft-deleted-link clearing: Teams hard-deletes links and soft delete is disabled on the object.
- Simpler batch fallback than Slack's re-list reconciliation; same observable counts.
- Outcome surfaced through the unlinked-users response instead of a dedicated status route.
- No consent flow, matching the existing per-message behavior and what Slack's own sweep does (it writes
ACTIVElinks directly).
Notes
- An
AUTOlink an admin removed comes back on the next sweep or on the user's next message. Slack behaves the same. - No new DB entity, no migration. One new logic function with a fresh universal identifier.
Tests
Unit tests for the email picker, batch lookup, planner, linker, match util, run-outcome reader, summary message, route handler, the register-connection sweep gating, the unlinked-users flag, and the settings button and callout. yarn lint, yarn typecheck and yarn test:unit pass in the app directory.