feat(teams): auto-link Teams users by email on connect and from settings

Merged
AB
13h ago
+1570-22

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 (or userPrincipalName) matches a workspace member's email, as an AUTO link.
  • Settings: a new teams-match-user-links route (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.

Auto-link by email

How

  • Source is Microsoft Graph /users via the existing collectGraphUsers paging ($top=999, up to 10 pages), using the User.ReadBasic.All scope 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. userType is not readable with User.ReadBasic.All, so the marker is the only signal.
  • Users who already have a link, manual or automatic, are never touched, so MANUAL links 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 as hasAutoMatchFailed on 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-connection timeout 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 ACTIVE links directly).

Notes

  • An AUTO link 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.