Files
native/src/lib/capabilities.ts
T
Linus Rath a86cd1d0eb feat: manage filters and vacation of shared and group accounts
The phone only ever read and wrote Sieve scripts and the vacation
responder of the user's own account, and the "Shared with me" list in
Account settings said to use the webmail for the rest. Port the
webmail's scoped settings (4c6c1aab) for these two panes:

- `api/sieve.ts` and `api/vacation.ts` take an optional account id and
  gate on the account's own `accountCapabilities` for
  `urn:ietf:params:jmap:sieve` and `urn:ietf:params:jmap:vacationresponse`,
  treating non-personal accounts as capable because Stalwart doesn't
  always advertise capabilities on them. Script blobs are uploaded and
  downloaded against the same account.
- Tapping a shared account in Account settings scopes Settings to it:
  only the Filters and Vacation Responder panes it supports are listed,
  behind a "Managing: <name>" banner that leads back to the own account.
  The scope is dropped when the signed-in account changes.
- The filter and vacation stores remember which account they hold,
  reset when it changes and drop replies for an account the user has
  already left, so one account's rules are never saved into another.
- The rule editor offers the managed account's folders as targets, and
  the "vacation active" banner only shows for the own account.
2026-09-24 19:01:24 +02:00

93 lines
3.8 KiB
TypeScript

import { CAPABILITIES } from '../api/types';
import type { JMAPAccountInfo, JMAPSession } from '../api/types';
import { useAuthStore } from '../stores/auth-store';
import { accountSupportsFiles } from '../api/files';
import { accountSupportsSieve, isSieveSupported } from '../api/sieve';
import { accountSupportsVacation, isVacationSupported } from '../api/vacation';
// When the session is null (cold start, offline restore) we assume features
// are available so they don't flicker off mid-restore. Once the live session
// arrives, the real capability set takes over.
function sessionHasCapability(capability: string): boolean {
const session = useAuthStore.getState().session;
if (!session) return true;
return capability in session.capabilities;
}
export function hasCalendarCapability(): boolean {
return sessionHasCapability(CAPABILITIES.CALENDARS);
}
export function hasContactsCapability(): boolean {
return sessionHasCapability(CAPABILITIES.CONTACTS);
}
// Files is gated on the ACCOUNT capability (or a non-personal account), not
// just the server-wide session capability: an account whose filenode
// permissions were revoked still sees the capability in the session but
// every FileNode call fails (#563).
function sessionSupportsFiles(session: JMAPSession | null): boolean {
if (!session) return true;
const filesAccountId = session.primaryAccounts?.[CAPABILITIES.FILES];
const accountId = filesAccountId ?? useAuthStore.getState().activeAccountId ?? '';
const account = session.accounts?.[accountId]
?? (filesAccountId ? undefined : Object.values(session.accounts ?? {}).find((a) => a.isPersonal));
return accountSupportsFiles(account, session.capabilities);
}
export function hasFilesCapability(): boolean {
return sessionSupportsFiles(useAuthStore.getState().session);
}
export function useHasCalendar(): boolean {
return useAuthStore((s) => (s.session ? CAPABILITIES.CALENDARS in s.session.capabilities : true));
}
export function useHasContacts(): boolean {
return useAuthStore((s) => (s.session ? CAPABILITIES.CONTACTS in s.session.capabilities : true));
}
export function useHasFiles(): boolean {
return useAuthStore((s) => sessionSupportsFiles(s.session));
}
// The account api/sieve.ts and api/vacation.ts target by default: the
// capability's primary account, else the mail account jmapClient resolves.
function primaryAccount(session: JMAPSession, capability: string): JMAPAccountInfo | undefined {
const id = session.primaryAccounts?.[capability]
?? session.primaryAccounts?.[CAPABILITIES.MAIL]
?? session.primaryAccounts?.[CAPABILITIES.CORE]
?? Object.keys(session.accounts ?? {})[0];
return id ? session.accounts?.[id] : undefined;
}
// Settings tabs for Sieve filters and the vacation responder are gated on the
// same per-account checks api/sieve.ts and api/vacation.ts make, so the tab is
// disabled up front instead of opening onto a "not supported" screen.
export function useHasSieve(): boolean {
return useAuthStore((s) => (s.session
? accountSupportsSieve(primaryAccount(s.session, CAPABILITIES.SIEVE), s.session.capabilities)
: true));
}
export function useHasVacation(): boolean {
return useAuthStore((s) => (s.session
? accountSupportsVacation(primaryAccount(s.session, CAPABILITIES.MAIL), s.session.capabilities)
: true));
}
export type SharedAccountSettingsTab = 'filters' | 'vacation';
/**
* Settings panes a shared/group account can be managed in, the first one
* being where "Shared with me" lands. Mirrors the webmail's scoped settings
* tabs (its calendar and contacts panes aren't scoped on mobile).
*/
export function sharedAccountSettingsTabs(accountId: string): SharedAccountSettingsTab[] {
const tabs: SharedAccountSettingsTab[] = [];
if (isSieveSupported(accountId)) tabs.push('filters');
if (isVacationSupported(accountId)) tabs.push('vacation');
return tabs;
}