Skip to content

Add multi-account support - #143

Open
justin13888 wants to merge 11 commits into
viktorstrate:mainfrom
justin13888:multi-account
Open

justin13888 wants to merge 11 commits into
viktorstrate:mainfrom
justin13888:multi-account

Conversation

@justin13888

@justin13888 justin13888 commented Nov 23, 2026 •

Copy link
Copy Markdown

Summary

Lets Mactrix stay signed in to several Matrix accounts at once. One account is active app-wide; every account keeps syncing in the background so notifications and the Dock badge cover all of them.

Built as 7 self-contained commits that each build and pass tests, so it can be reviewed commit by commit:

  1. Store sessions per account in the keychain — AccountStore replaces the single UserSession item with session.<storeID> items, plus a crash-safe migration of the existing session. Sign-out no longer wipes the whole keychain service.
  2. Keep signed-out accounts and clean up abandoned stores — a server-side logout shows a banner instead of dropping the client; store dirs left by abandoned logins are swept at launch.
  3. Support multiple signed-in accounts — AppState.accounts + active account (matrixClient stays as an alias, so views are untouched); duplicate sign-ins are rejected; per-account room selection per window.
  4. Add account switcher — sidebar header menu, Account menu with ⌘⌃1…9, Add Account / Sign In Again flows, settings show the active account.
  5. Route notifications and the Dock badge across accounts — one app-level notification delegate, per-account grouping/labels, click switches account; summed Dock badge.
  6. Start a fresh login after each sign-in attempt — a login client can only sign in once, so the Add Account sheet fetches new login details after a failed or rejected attempt (fixes signing in to a second account on the same server after a rejected duplicate).
  7. Show avatars and Matrix IDs in the account switcher — the sidebar switcher becomes a button + popover with each account's avatar, display name and Matrix ID, since a native menu drops custom labels and display names aren't unique.

Migration

  • Existing sessions are migrated on first launch: the new item is written and verified before the legacy item is deleted; an unreadable legacy item is never deleted. Store directories are already keyed by storeID, so no files move.
  • Saved window state from older versions still decodes and reopens on the same room.
  • Downgrading is not supported: an older build won't find the migrated session and will ask to sign in again.

Testing

  • xcodebuild test passes on every commit (184 tests; new: AccountStoreTests, StoreDirectoriesTests, WindowStateAccountTests). build analyze is clean.
  • Manual checklist (not yet done against live accounts):
    • Upgrade from main keeps the existing session and selected room
    • Add a second account; both sync; switch via sidebar and ⌘⌃2
    • Notification from the inactive account switches account and opens the room
    • Signing out of one account leaves the other signed in
    • Duplicate sign-in is rejected with a clear error
    • After a rejected duplicate, another account on the same server signs in from the same sheet
    • Switcher shows avatar, name and Matrix ID; popover switches, adds and signs out accounts
    • Session revoked elsewhere shows "Sign in again"

Not covered

  • Viewing two accounts side by side in separate windows (could be added later on top of this).

Replace the single "UserSession" keychain item with one item per account
("session.<storeID>"), managed by a new AccountStore. Display order and the
active account are kept in UserDefaults; the keychain stays the source of
truth for which accounts exist.

Existing sessions are migrated on launch. The new item is written and read
back before the legacy item is deleted, so an interrupted migration is
retried safely, and an unreadable legacy item is left untouched. Store
directories are already keyed by storeID, so no files move.

Signing out now removes only that account's item instead of every item in
the app's keychain service. The SDK session delegate reads and writes the
client's own item, so token refreshes can't overwrite another account.

Also removes the unused MatrixClientSessionDelegate.
When the server ends a session, stop that client's sync and show a
"You were signed out" banner in the sidebar instead of dropping the client
immediately. Previously the stale session stayed in the keychain and was
restored again on the next launch.

Every login attempt creates a store directory before the user signs in, and
abandoned attempts left those directories behind. On launch, remove
UUID-named store directories that belong to no account and are more than ten
minutes old. The sweep is skipped while an unmigrated legacy session exists,
and it keeps the stores of sessions that can't be decoded.

Store path helpers move to StoreDirectories.
AppState now holds every signed-in account and which one is active.
`matrixClient` returns the active account, so existing views are unchanged.
All accounts are restored at launch and keep syncing in the background. An
account that fails to restore is skipped instead of blocking the others.

Signing in adds an account rather than replacing the current one. Signing in
to an account that's already listed is rejected, and the new session is
signed out again so it doesn't linger as an extra device. If the listed
account was signed out by the server, the new session replaces it in the
same position.

Each window remembers the selected room and space per account and restores
them when switching back. Window state saved by earlier versions has no
account and is adopted by the first account shown, so existing windows
reopen on the same room. Signing out of one account no longer quits the app
when others remain.
A menu at the top of the sidebar shows the active account. It lists every
account with its unread count, and has Add Account and Sign Out (with a
confirmation). A new Account menu in the menu bar lists accounts with
Command-Control-1 through 9 and also offers Add Account.

The sign-in sheet now knows why it's shown. When adding an account it has a
Cancel button. When an account was signed out by the server, the banner
offers Sign In Again, which opens the sheet pre-filled with that user ID and
replaces the old session in place. It also offers Remove Account.

The Account and Sessions settings tabs show which account they apply to.
MatrixClient loads its own display name and avatar for these labels.
The notification center has a single delegate, but every account's
MatrixNotifications set itself as that delegate, so only the last account
could handle clicks. A NotificationRouter owned by AppState now handles
clicks for all accounts.

Notifications carry the receiving account's storeID and are grouped by
account. When more than one account is signed in, the subtitle names the
account. Clicking a notification switches to its account and opens the
room. A click that launches the app is held until accounts are restored.

The Dock badge shows unread rooms summed across all accounts. AppState is now
its only writer; previously each client overwrote the others' count.

Logs for sync, sign-out, auth errors and session saves now include the
account's storeID, so logs from different accounts can be told apart.
A login client can only sign in once. After a rejected duplicate
sign-in, the Add Account sheet reused the spent client, so signing in
as another account on the same homeserver failed with "new logged-in
session is different than current client session". Fetch new login
details after every failed attempt.
The sidebar switcher was a native menu, which drops custom labels, so
it showed only the display name without an avatar. Display names
aren't unique either. Replace it with a button that opens a popover
listing each account's avatar, display name, and Matrix ID, with the
active account checked and unread counts or a signed-out tag. The
Account menu now titles items "Name — @user:server".
The SDK sizes each store's connection pool at four connections per CPU
core, and every connection holds two file descriptors. With four
databases per account, two accounts on a 12-core Mac held over 300
descriptors, past the default limit of 256. Further sign-in attempts,
which each build a client with its own store, then failed to open
databases ("unable to open database file") or sockets, stalled syncing
accounts, and crashed when AppKit could not create a Metal context.

Limit each pool to 8 connections, about 64 descriptors per account.
Apps start with a soft limit of 256 open files. Every signed-in account
keeps its SQLite stores and sync connections open, and each sign-in
attempt opens another store, so a few accounts can run out even with
capped connection pools. Raise the soft limit to 4096 (or the hard
limit, if lower) before any account opens its stores.
macOS hides notifications from the frontmost app unless its delegate
asks to present them, so while Mactrix was focused nothing showed that
another account had received a message. Present those with a banner and
sound. The active account stays silent as before, since its rooms are
already in the sidebar.
Unread rooms in inactive accounts were only visible after opening the
switcher popover. Show their total next to the switcher's chevron and
include it in the button's accessibility label. Signed-out accounts are
left out, matching the popover, which marks them instead of counting.
@justin13888
justin13888 marked this pull request as ready for review November 23, 2026 23:01
@justin13888

Copy link
Copy Markdown
Author

Finally verified all the individual checks on checklist one-by-one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant