mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
fix(docsync): offer the sync button only where something is behind it
The Documents addon put a "Document sync" button on every trip's Files tab, and the seed ships every provider switched off, so after the upgrade each of those buttons led into "No document providers are available". The button now shows on a trip that is already bound, to every member, and otherwise only to somebody who may bind the trip on an instance that offers a provider. Without it the Files toolbar is the one from before the feature, trash flush right, on both shells. /api/addons carries no document providers for a normal user, so the answer comes from the docsync routes through a new online-only docSyncRepo and is kept per session in docSyncOfferStore. A revisit renders the last answer at once and checks it again; offline nothing is asked and the last answer stands. Creating a binding, removing one and removing a connection now ping the trip with docsync:changed and zero counts, so a member already on the Files tab sees the button come and go without leaving it. Both shells keep listing a binding whose provider the admin has since switched off instead of claiming there are no providers, and name its store through one storeName() helper. A load that answers 403 or 404 clears the bindings as well, so they do not linger once the Documents addon is gone. The phone admin screen gets the Documents shelf the desktop one has: the document providers as sub rows under the addon, toggled through the same admin route and re-read after Documents itself is toggled, because the server switches the providers off with it. isTripOwner and the isOwner prop become canManageSync and canManage, which is what canManageDocSync answers. Both folder pickers take loadScopes out of the sync object before their effect, so exhaustive-deps is satisfied without the effect running on every render.
This commit is contained in:
@@ -22,8 +22,9 @@ import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { TripAccessGuard } from '../permissions/trip-access.guard';
|
||||
import { DatabaseService } from '../database/database.service';
|
||||
import { RealtimeService } from '../realtime/realtime.service';
|
||||
import { docFailed } from './document-provider';
|
||||
import { DocSyncConfigService } from './doc-sync-config.service';
|
||||
import { DocSyncConfigService, type LinkRow } from './doc-sync-config.service';
|
||||
import { DocSyncService } from './doc-sync.service';
|
||||
import { DocumentProviderRegistry } from './document-provider.registry';
|
||||
import {
|
||||
@@ -37,7 +38,7 @@ import {
|
||||
} from './doc-sync.dto';
|
||||
|
||||
/**
|
||||
* `/api/trips/:tripId/docsync` — a trip's document provider binding.
|
||||
* `/api/trips/:tripId/docsync`: a trip's document provider binding.
|
||||
*
|
||||
* Trip-scoped rather than user-scoped, and that is the whole point of the
|
||||
* feature: one connection per trip, which every member reads through. The
|
||||
@@ -53,7 +54,7 @@ import {
|
||||
* overrides trip permissions everywhere else too). It hands TREK a credential
|
||||
* that usually reaches the owner's entire document archive, so widening this to
|
||||
* every member would let any member point the trip at a folder the owner never
|
||||
* meant to share. Reading the status is open to all members — they need to know
|
||||
* meant to share. Reading the status is open to all members: they need to know
|
||||
* where their documents are going.
|
||||
*/
|
||||
@Controller('api/trips/:tripId/docsync')
|
||||
@@ -65,8 +66,23 @@ export class DocSyncController {
|
||||
private readonly sync: DocSyncService,
|
||||
private readonly registry: DocumentProviderRegistry,
|
||||
private readonly db: DatabaseService,
|
||||
private readonly realtime: RealtimeService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Tell the trip that a binding came or went.
|
||||
*
|
||||
* Whether a trip is bound decides whether its members see the sync button
|
||||
* at all, and a run only pings when it moved a document. Binding two empty
|
||||
* sides, or unbinding, moves nothing, so a member sitting on the Files tab
|
||||
* kept the old answer until they left it. The whole room, the caller
|
||||
* included: it is a ping to re-read, not a change to apply, and the owner's
|
||||
* own button depends on it too once every provider is switched off.
|
||||
*/
|
||||
private announceBinding(link: Pick<LinkRow, 'id' | 'trip_id'>): void {
|
||||
this.realtime.broadcast(link.trip_id, 'docsync:changed', { linkId: link.id, pulled: 0, pushed: 0 });
|
||||
}
|
||||
|
||||
/**
|
||||
* The trip owner or an instance admin, the same rule as `canManageDocSync`
|
||||
* on the client. Checked here rather than through @RequirePermission: the
|
||||
@@ -132,7 +148,7 @@ export class DocSyncController {
|
||||
}
|
||||
|
||||
/**
|
||||
* Probe form values. Always 200 — the result is in the body, because this is
|
||||
* Probe form values. Always 200: the result is in the body, because this is
|
||||
* a form field, not a failure of the request. The photo providers pin the
|
||||
* same contract with a test their e2e suite marks CRITICAL.
|
||||
*/
|
||||
@@ -157,7 +173,7 @@ export class DocSyncController {
|
||||
.find((c) => c.provider_id === body.providerId);
|
||||
// Only for the address the credential was stored against. Merging it into a
|
||||
// probe of an arbitrary baseUrl turns this route into a way to have TREK
|
||||
// post a stored API token at a server of the caller's choosing — which is
|
||||
// post a stored API token at a server of the caller's choosing, which is
|
||||
// exactly what somebody who inherited a trip but not its credentials would
|
||||
// reach for. Same host, same scheme, same port, or the caller types it in.
|
||||
const sameTarget = !!existing && sameOrigin(existing.base_url, urlCheck.data.url);
|
||||
@@ -174,6 +190,7 @@ export class DocSyncController {
|
||||
|
||||
const res = await provider.probe({
|
||||
connectionId: existing?.id ?? 0,
|
||||
createdAt: existing?.created_at ?? '',
|
||||
ownerId: Number(user.id),
|
||||
baseUrl: urlCheck.data.url,
|
||||
secrets,
|
||||
@@ -190,13 +207,15 @@ export class DocSyncController {
|
||||
deleteConnection(
|
||||
@Param('tripId') tripId: string,
|
||||
@Param('connectionId') connectionId: string,
|
||||
createdAt: existing?.created_at ?? '',
|
||||
@CurrentUser() user: User,
|
||||
) {
|
||||
this.assertCanManage(tripId, user);
|
||||
const conn = this.config.getConnection(Number(connectionId));
|
||||
if (!conn || conn.trip_id !== Number(tripId)) throw new HttpException('Connection not found', 404);
|
||||
// Its bindings go with it (ON DELETE CASCADE), so read them first.
|
||||
const unbound = this.config.listLinks(conn.trip_id).filter((l) => l.connection_id === conn.id);
|
||||
this.config.deleteConnection(conn.id);
|
||||
for (const link of unbound) this.announceBinding(link);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
@@ -256,9 +275,10 @@ export class DocSyncController {
|
||||
this.assertCanManage(tripId, user);
|
||||
const res = this.config.createLink(Number(tripId), Number(user.id), body);
|
||||
if (docFailed(res)) throw new HttpException(res.error.detail || res.error.code, 400);
|
||||
this.announceBinding(res.data);
|
||||
|
||||
// Subscribe where the provider lets TREK do it itself. A failure here is
|
||||
// not a failure of the binding — polling still carries it — so it is logged
|
||||
// not a failure of the binding (polling still carries it), so it is logged
|
||||
// into the link state rather than thrown at the user.
|
||||
const conn = this.config.getConnection(res.data.connection_id);
|
||||
const provider = conn ? this.registry.get(conn.provider_id) : undefined;
|
||||
@@ -318,6 +338,7 @@ export class DocSyncController {
|
||||
await provider.unregisterWebhook(this.config.toRef(conn), link.webhook_subscription_id);
|
||||
}
|
||||
this.config.deleteLink(link.id);
|
||||
this.announceBinding(link);
|
||||
// Unbinding keeps both copies. Nothing is deleted anywhere.
|
||||
return { success: true, documentsKept: true };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user