@deveye/types 0.17.0 → 0.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/domain/sharing.ts +48 -1
- package/src/features/sharing.ts +35 -1
- package/src/index.ts +6 -0
- package/src/sdk/move.test.ts +140 -0
- package/src/sdk/providers.ts +10 -0
- package/src/sdk/server.ts +180 -0
package/package.json
CHANGED
package/src/domain/sharing.ts
CHANGED
|
@@ -57,10 +57,57 @@ export const itemShareStateSchema = z.object({
|
|
|
57
57
|
/** Les espaces de l'appelant, l'origine comprise et marquée. */
|
|
58
58
|
workspaces: z.array(itemShareSchema),
|
|
59
59
|
/** Renseigné quand le partage est impossible ; les cases sont alors inertes. */
|
|
60
|
-
blocker: shareBlockerSchema.nullable()
|
|
60
|
+
blocker: shareBlockerSchema.nullable(),
|
|
61
|
+
/**
|
|
62
|
+
* L'élément peut changer d'espace : sa fonctionnalité sait le re-chiffrer,
|
|
63
|
+
* il est chez lui ici, et l'appelant a le droit de l'y écrire. Faux ne dit
|
|
64
|
+
* pas pourquoi : c'est `share.movePreview`, une fois la cible choisie, qui
|
|
65
|
+
* porte les motifs.
|
|
66
|
+
*/
|
|
67
|
+
movable: z.boolean().default(false)
|
|
61
68
|
});
|
|
62
69
|
export type ItemShareState = z.infer<typeof itemShareStateSchema>;
|
|
63
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Une liaison que le déplacement va rompre. Une liaison ne traverse jamais un
|
|
73
|
+
* espace : ce qui vise l'élément depuis celui qu'il quitte est retiré, et
|
|
74
|
+
* l'écran le nomme avant de confirmer.
|
|
75
|
+
*/
|
|
76
|
+
export const itemMoveDependencySchema = z.object({
|
|
77
|
+
feature: featureIdSchema,
|
|
78
|
+
itemId: z.string().min(1).max(64),
|
|
79
|
+
/** Son intitulé sous le codec de son espace ; « Élément disparu » si illisible. */
|
|
80
|
+
label: z.string(),
|
|
81
|
+
/** Pourquoi elle ne peut pas suivre, dit à la place de l'utilisateur. */
|
|
82
|
+
reason: z.string()
|
|
83
|
+
});
|
|
84
|
+
export type ItemMoveDependency = z.infer<typeof itemMoveDependencySchema>;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Ce qu'un déplacement ferait, calculé sans rien écrire. C'est le contenu du
|
|
88
|
+
* popup de confirmation : ce qui l'empêche, ce qu'il détruit, ce qu'il emmène.
|
|
89
|
+
*/
|
|
90
|
+
export const itemMovePreviewSchema = z.object({
|
|
91
|
+
workspaceId: z.number().int().positive(),
|
|
92
|
+
workspaceName: z.string(),
|
|
93
|
+
/** L'espace d'origine, nommé : la phrase du popup le cite. */
|
|
94
|
+
homeWorkspaceName: z.string(),
|
|
95
|
+
/**
|
|
96
|
+
* L'origine est un espace partagé : ses autres membres perdront l'accès.
|
|
97
|
+
* Dit plutôt que gardé, la suppression n'exigeant pas davantage.
|
|
98
|
+
*/
|
|
99
|
+
losesSharedAccess: z.boolean(),
|
|
100
|
+
/** Vide = le déplacement est possible. Sinon la première phrase est le motif affiché. */
|
|
101
|
+
blockers: z.array(z.string()),
|
|
102
|
+
/** Ce qui sera détruit ou retiré, nommé : projections, restrictions, historique. */
|
|
103
|
+
drops: z.array(z.string()),
|
|
104
|
+
/** Cellules chiffrées à convertir, pour annoncer l'ampleur plutôt que faire attendre. */
|
|
105
|
+
rows: z.number().int().nonnegative(),
|
|
106
|
+
/** Les liaisons rompues, nommées une par une. */
|
|
107
|
+
dependencies: z.array(itemMoveDependencySchema)
|
|
108
|
+
});
|
|
109
|
+
export type ItemMovePreview = z.infer<typeof itemMovePreviewSchema>;
|
|
110
|
+
|
|
64
111
|
/**
|
|
65
112
|
* Ce qu'un rôle peut faire sur un élément, **en surcharge** de ce que la
|
|
66
113
|
* fonctionnalité lui donne : `none` masque, `read` passe en lecture seule,
|
package/src/features/sharing.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
itemAccessSchema,
|
|
5
5
|
itemExtraOverridesSchema,
|
|
6
6
|
itemGrantStateSchema,
|
|
7
|
+
itemMovePreviewSchema,
|
|
7
8
|
itemRefSchema,
|
|
8
9
|
itemShareStateSchema
|
|
9
10
|
} from '../domain/sharing';
|
|
@@ -77,4 +78,37 @@ export const itemGrantSet = {
|
|
|
77
78
|
output: itemGrantStateSchema
|
|
78
79
|
};
|
|
79
80
|
|
|
80
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Ce qu'un déplacement vers cet espace ferait, sans rien écrire : ce qui
|
|
83
|
+
* l'empêche, ce qu'il détruit, les liaisons qui suivent et celles qu'on peut
|
|
84
|
+
* emmener. Le popup de confirmation n'affiche rien d'autre.
|
|
85
|
+
*/
|
|
86
|
+
export const itemMovePreview = {
|
|
87
|
+
command: 'share.movePreview' as const,
|
|
88
|
+
input: itemRefSchema.extend({ workspaceId: z.number().int().positive() }),
|
|
89
|
+
output: itemMovePreviewSchema
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Déplace l'élément : sa donnée est déchiffrée sous la clé de son espace et
|
|
94
|
+
* re-chiffrée sous celle de la cible. La seule opération du système qui change
|
|
95
|
+
* un blob de clé, et donc la seule qui ne se rejoue pas.
|
|
96
|
+
*
|
|
97
|
+
* Il arrive nu : ses projections, les surcharges de rôle posées dessus, sa route
|
|
98
|
+
* de notification et les liaisons qui le visaient tombent, toutes désignant
|
|
99
|
+
* l'espace qu'il quitte.
|
|
100
|
+
*/
|
|
101
|
+
export const itemMove = {
|
|
102
|
+
command: 'share.move' as const,
|
|
103
|
+
input: itemRefSchema.extend({ workspaceId: z.number().int().positive() }),
|
|
104
|
+
output: z.object({ workspaceId: z.number().int().positive() })
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const sharingCommands = [
|
|
108
|
+
shareGet,
|
|
109
|
+
shareSet,
|
|
110
|
+
itemGrantList,
|
|
111
|
+
itemGrantSet,
|
|
112
|
+
itemMovePreview,
|
|
113
|
+
itemMove
|
|
114
|
+
] as const;
|
package/src/index.ts
CHANGED
|
@@ -561,6 +561,8 @@ export {
|
|
|
561
561
|
itemAccessSchema,
|
|
562
562
|
itemExtraOverridesSchema,
|
|
563
563
|
itemGrantStateSchema,
|
|
564
|
+
itemMoveDependencySchema,
|
|
565
|
+
itemMovePreviewSchema,
|
|
564
566
|
itemRefSchema,
|
|
565
567
|
itemRoleGrantSchema,
|
|
566
568
|
itemRoleGrantViewSchema,
|
|
@@ -573,6 +575,8 @@ export type {
|
|
|
573
575
|
ItemAccess,
|
|
574
576
|
ItemExtraOverrides,
|
|
575
577
|
ItemGrantState,
|
|
578
|
+
ItemMoveDependency,
|
|
579
|
+
ItemMovePreview,
|
|
576
580
|
ItemRef,
|
|
577
581
|
ItemRoleGrant,
|
|
578
582
|
ItemRoleGrantRow,
|
|
@@ -585,6 +589,8 @@ export type {
|
|
|
585
589
|
export {
|
|
586
590
|
itemGrantList,
|
|
587
591
|
itemGrantSet,
|
|
592
|
+
itemMove,
|
|
593
|
+
itemMovePreview,
|
|
588
594
|
shareGet,
|
|
589
595
|
shareSet,
|
|
590
596
|
sharingCommands
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
countMovableCells,
|
|
6
|
+
FeatureError,
|
|
7
|
+
resealCells,
|
|
8
|
+
type MovableCell,
|
|
9
|
+
type SdkCipher,
|
|
10
|
+
type SdkQueryable
|
|
11
|
+
} from './server';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Le rescellement d'un élément qui change d'espace. Ce qui se vérifie ici est
|
|
15
|
+
* ce qui ne pardonne pas : une seule cellule illisible doit tout annuler AVANT
|
|
16
|
+
* la première écriture, un arbre à moitié converti étant définitivement illisible
|
|
17
|
+
* sans que rien puisse le détecter.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
interface Row {
|
|
21
|
+
row_id: number;
|
|
22
|
+
value: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Un queryable qui ne comprend du SQL que la table et la colonne lues, et qui
|
|
27
|
+
* retient ce qu'on a tenté d'écrire.
|
|
28
|
+
*/
|
|
29
|
+
function fakeQ(cells: Record<string, Row[]>) {
|
|
30
|
+
const writes: { sql: string; params: unknown[] }[] = [];
|
|
31
|
+
const q: SdkQueryable = {
|
|
32
|
+
query: async <T extends object>(sql: string, params?: unknown[]) => {
|
|
33
|
+
void params;
|
|
34
|
+
const count = /SELECT COUNT\(\*\) AS n FROM (\w+)\s+WHERE (\w+)/.exec(sql);
|
|
35
|
+
if (count) {
|
|
36
|
+
return [
|
|
37
|
+
{ n: (cells[`${count[1]}.${columnOf(sql)}`] ?? []).length }
|
|
38
|
+
] as unknown as T[];
|
|
39
|
+
}
|
|
40
|
+
const read = /AS row_id, (\w+) AS value\s+FROM (\w+)/.exec(sql);
|
|
41
|
+
return (read ? (cells[`${read[2]}.${read[1]}`] ?? []) : []) as unknown as T[];
|
|
42
|
+
},
|
|
43
|
+
execute: async (sql: string, params?: unknown[]) => {
|
|
44
|
+
writes.push({ sql, params: params ?? [] });
|
|
45
|
+
return { affectedRows: 1, insertId: 0 };
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
return { q, writes };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** La colonne d'un COUNT, que le SQL ne nomme que dans son `WHERE`. */
|
|
52
|
+
function columnOf(sql: string): string {
|
|
53
|
+
return /AND (\w+) IS NOT NULL/.exec(sql)?.[1] ?? '';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function cipherOf(tag: string): SdkCipher {
|
|
57
|
+
return {
|
|
58
|
+
encrypt: async (plain) => `${tag}:${plain}`,
|
|
59
|
+
decrypt: async (blob) => blob.slice(tag.length + 1),
|
|
60
|
+
tryDecrypt: async (blob) => (blob.startsWith(`${tag}:`) ? blob.slice(tag.length + 1) : null)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const ciphers = { from: cipherOf('A'), to: cipherOf('B') };
|
|
65
|
+
|
|
66
|
+
const CELLS: MovableCell[] = [
|
|
67
|
+
{ table: 'ft_x_items', idColumn: 'id', ownerColumn: 'id', column: 'content' },
|
|
68
|
+
{ table: 'ft_x_children', idColumn: 'id', ownerColumn: 'item_id', column: 'body' }
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
describe('resealCells', () => {
|
|
72
|
+
it('rescelle chaque cellule sous la clé cible, la garde du propriétaire comprise', async () => {
|
|
73
|
+
const { q, writes } = fakeQ({
|
|
74
|
+
'ft_x_items.content': [{ row_id: 7, value: 'A:corps' }],
|
|
75
|
+
'ft_x_children.body': [
|
|
76
|
+
{ row_id: 1, value: 'A:un' },
|
|
77
|
+
{ row_id: 2, value: 'A:deux' }
|
|
78
|
+
]
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
assert.equal(await resealCells(q, CELLS, 7, ciphers), 3);
|
|
82
|
+
assert.deepEqual(
|
|
83
|
+
writes.map((w) => w.params),
|
|
84
|
+
[
|
|
85
|
+
['B:corps', 7, 7],
|
|
86
|
+
['B:un', 1, 7],
|
|
87
|
+
['B:deux', 2, 7]
|
|
88
|
+
]
|
|
89
|
+
);
|
|
90
|
+
// La ligne se cible par son identifiant ET son propriétaire : une
|
|
91
|
+
// conversion ne peut pas déborder sur un voisin.
|
|
92
|
+
assert.match(writes[1]!.sql, /WHERE id = \? AND item_id = \?/);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('annule tout sur une cellule illisible, sans avoir écrit une seule ligne', async () => {
|
|
96
|
+
const { q, writes } = fakeQ({
|
|
97
|
+
'ft_x_items.content': [{ row_id: 7, value: 'A:corps' }],
|
|
98
|
+
// Scellée sous une troisième clé : la conversion doit renoncer.
|
|
99
|
+
'ft_x_children.body': [{ row_id: 1, value: 'Z:perdu' }]
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
await assert.rejects(
|
|
103
|
+
resealCells(q, CELLS, 7, ciphers),
|
|
104
|
+
(err: unknown) => err instanceof FeatureError && /illisible/.test(err.message)
|
|
105
|
+
);
|
|
106
|
+
assert.equal(writes.length, 0);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('suit un sous-parcours pour une ligne qui pend indirectement', async () => {
|
|
110
|
+
const indirect: MovableCell[] = [
|
|
111
|
+
{
|
|
112
|
+
table: 'ft_x_leaves',
|
|
113
|
+
idColumn: 'id',
|
|
114
|
+
ownerColumn: 'child_id',
|
|
115
|
+
ownerScope: 'SELECT id FROM ft_x_children WHERE item_id = ?',
|
|
116
|
+
column: 'body'
|
|
117
|
+
}
|
|
118
|
+
];
|
|
119
|
+
const { q, writes } = fakeQ({ 'ft_x_leaves.body': [{ row_id: 5, value: 'A:feuille' }] });
|
|
120
|
+
|
|
121
|
+
await resealCells(q, indirect, 7, ciphers);
|
|
122
|
+
assert.match(
|
|
123
|
+
writes[0]!.sql,
|
|
124
|
+
/WHERE id = \? AND child_id IN \(SELECT id FROM ft_x_children WHERE item_id = \?\)/
|
|
125
|
+
);
|
|
126
|
+
assert.deepEqual(writes[0]!.params, ['B:feuille', 5, 7]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('compte ce qu’il y aurait à convertir sans rien convertir', async () => {
|
|
130
|
+
const { q, writes } = fakeQ({
|
|
131
|
+
'ft_x_items.content': [{ row_id: 7, value: 'A:corps' }],
|
|
132
|
+
'ft_x_children.body': [
|
|
133
|
+
{ row_id: 1, value: 'A:un' },
|
|
134
|
+
{ row_id: 2, value: 'A:deux' }
|
|
135
|
+
]
|
|
136
|
+
});
|
|
137
|
+
assert.equal(await countMovableCells(q, CELLS, 7), 3);
|
|
138
|
+
assert.equal(writes.length, 0);
|
|
139
|
+
});
|
|
140
|
+
});
|
package/src/sdk/providers.ts
CHANGED
|
@@ -261,6 +261,16 @@ export interface ProjectsUsageProvider {
|
|
|
261
261
|
usageOf(feature: string, itemId: number, workspaceId: number): Promise<readonly ProjectUsage[]>;
|
|
262
262
|
/** How many projects of the workspace link each item of this feature (absent = zero). */
|
|
263
263
|
countByItem(feature: string, workspaceId: number): Promise<ReadonlyMap<number, number>>;
|
|
264
|
+
/**
|
|
265
|
+
* The item leaves the workspace: drop every link pointing at it from
|
|
266
|
+
* there, and answer how many projects lost one. A link never spans two
|
|
267
|
+
* workspaces, so a departing item takes none with it; left alone the rows
|
|
268
|
+
* would keep naming an item that lives elsewhere now.
|
|
269
|
+
*
|
|
270
|
+
* Called by the app when an item is moved out, not by the module that owns
|
|
271
|
+
* the item. Idempotent, and zero for a feature that links nothing.
|
|
272
|
+
*/
|
|
273
|
+
detach(feature: string, itemId: number, workspaceId: number): Promise<number>;
|
|
264
274
|
/**
|
|
265
275
|
* Writes one line in a project's timeline (a deployment triggered from a
|
|
266
276
|
* project's tab). Open tier only: an event aimed at a guarded project is
|
package/src/sdk/server.ts
CHANGED
|
@@ -835,6 +835,186 @@ export interface FeatureItemsEntry<Repo = unknown> {
|
|
|
835
835
|
* HOME workspace.
|
|
836
836
|
*/
|
|
837
837
|
shareable?(repo: Repo, itemId: string, workspaceId: number): Promise<boolean>;
|
|
838
|
+
/**
|
|
839
|
+
* Move this item to another workspace. The ONLY operation of the whole
|
|
840
|
+
* system that re-encrypts (read under key A, written under key B); every
|
|
841
|
+
* other one keeps a blob under the key it was sealed with. Omit it and
|
|
842
|
+
* your items cannot be moved: the screen offers nothing and `item.move`
|
|
843
|
+
* refuses. That default is the safe one, and staying on it is a valid
|
|
844
|
+
* answer for a feature whose item depends on a workspace source (a
|
|
845
|
+
* credential, a destination) that cannot follow it.
|
|
846
|
+
*/
|
|
847
|
+
move?: FeatureItemsMove<Repo>;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
/**
|
|
851
|
+
* What moving one item would cost, decided while it is still at home and
|
|
852
|
+
* before a single row is written.
|
|
853
|
+
*/
|
|
854
|
+
export interface SdkMovePlan {
|
|
855
|
+
/**
|
|
856
|
+
* Why the move is impossible, in sentences the screen shows as they are.
|
|
857
|
+
* Empty means it can go. Put the decisive one first: a name already taken
|
|
858
|
+
* in the target, a source of the origin workspace that cannot follow.
|
|
859
|
+
*/
|
|
860
|
+
blockers: readonly string[];
|
|
861
|
+
/**
|
|
862
|
+
* What the move destroys, named one by one, for the confirmation to list
|
|
863
|
+
* verbatim: a history left behind, a setting that means nothing there.
|
|
864
|
+
* Say it here or the caller discovers it afterwards.
|
|
865
|
+
*/
|
|
866
|
+
drops: readonly string[];
|
|
867
|
+
/**
|
|
868
|
+
* Encrypted cells to convert, so the confirmation can say how much rather
|
|
869
|
+
* than spin. An estimate is fine; zero is a legitimate answer.
|
|
870
|
+
*/
|
|
871
|
+
rows: number;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
/** See {@link FeatureItemsEntry.move}. */
|
|
875
|
+
export interface FeatureItemsMove<Repo = unknown> {
|
|
876
|
+
/**
|
|
877
|
+
* Everything the confirmation must say, and every refusal, with the item
|
|
878
|
+
* still at home and nothing written yet. Answer for THAT target: a name
|
|
879
|
+
* collision or a missing source is decidable only against it.
|
|
880
|
+
*/
|
|
881
|
+
plan(ctx: FeatureItemsMovePlanContext<Repo>): Promise<SdkMovePlan>;
|
|
882
|
+
/**
|
|
883
|
+
* Re-home the item: `workspace_id` on every row it owns, and every
|
|
884
|
+
* encrypted cell read with `ciphers.from` and written with `ciphers.to`.
|
|
885
|
+
*
|
|
886
|
+
* Read and convert EVERYTHING before the first write. A tree half
|
|
887
|
+
* converted is unreadable forever and nothing can detect it: one encrypted
|
|
888
|
+
* blob is indistinguishable from another. `reencryptProjectTree` in the
|
|
889
|
+
* Projects module is the reference implementation.
|
|
890
|
+
*/
|
|
891
|
+
apply(ctx: FeatureItemsMoveContext<Repo>): Promise<void>;
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
/** The handles {@link FeatureItemsMove.plan} works with. Reads only. */
|
|
895
|
+
export interface FeatureItemsMovePlanContext<Repo = unknown> {
|
|
896
|
+
/** Not transactional: planning writes nothing. {@link countMovableCells} takes it. */
|
|
897
|
+
q: SdkQueryable;
|
|
898
|
+
repo: Repo;
|
|
899
|
+
itemId: string;
|
|
900
|
+
fromWorkspaceId: number;
|
|
901
|
+
toWorkspaceId: number;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/** The handles {@link FeatureItemsMove.apply} works with. */
|
|
905
|
+
export interface FeatureItemsMoveContext<Repo = unknown> {
|
|
906
|
+
/**
|
|
907
|
+
* Transactional: what you write here commits with the app's own cleanup,
|
|
908
|
+
* or rolls back with it. Write through this, NEVER through `repo`, which
|
|
909
|
+
* is bound to the pool and would land outside the transaction.
|
|
910
|
+
*/
|
|
911
|
+
q: SdkQueryable;
|
|
912
|
+
/** Your repo. For reads only, see `q`. */
|
|
913
|
+
repo: Repo;
|
|
914
|
+
itemId: string;
|
|
915
|
+
fromWorkspaceId: number;
|
|
916
|
+
toWorkspaceId: number;
|
|
917
|
+
/** The open ciphers of the two workspaces: decrypt with `from`, seal with `to`. */
|
|
918
|
+
ciphers: { from: SdkCipher; to: SdkCipher };
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
/**
|
|
922
|
+
* One column of encrypted text hanging off an item, for {@link resealCells}.
|
|
923
|
+
*
|
|
924
|
+
* ⚠️ The list you build out of these is held BY HAND: an encrypted column left
|
|
925
|
+
* out of it stays under the old key and becomes unreadable, and nothing can
|
|
926
|
+
* detect it, one encrypted blob being indistinguishable from another. Revisit
|
|
927
|
+
* it whenever you add an encrypted column.
|
|
928
|
+
*/
|
|
929
|
+
export interface MovableCell {
|
|
930
|
+
table: string;
|
|
931
|
+
/** Identifying column of the row, to rewrite exactly one. */
|
|
932
|
+
idColumn: string;
|
|
933
|
+
/** Column tying the row to the item. */
|
|
934
|
+
ownerColumn: string;
|
|
935
|
+
column: string;
|
|
936
|
+
/**
|
|
937
|
+
* For a row hanging off the item INDIRECTLY: a SQL subquery selecting the
|
|
938
|
+
* `ownerColumn` values that belong to it, taking the item's id as its only
|
|
939
|
+
* `?` (a mail account's messages hang off its folders:
|
|
940
|
+
* `SELECT id FROM mail_folders WHERE account_id = ?`).
|
|
941
|
+
*
|
|
942
|
+
* Interpolated into the query, so it must be a literal written in your
|
|
943
|
+
* module, exactly like `table` and `column`. Never build it from input.
|
|
944
|
+
*/
|
|
945
|
+
ownerScope?: string;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
/** `WHERE` fragment tying a cell's rows to the item, subquery included. */
|
|
949
|
+
function ownerFilter(cell: MovableCell): string {
|
|
950
|
+
return cell.ownerScope
|
|
951
|
+
? `${cell.ownerColumn} IN (${cell.ownerScope})`
|
|
952
|
+
: `${cell.ownerColumn} = ?`;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
/** How many cells {@link resealCells} would convert: a `plan`'s `rows`. */
|
|
956
|
+
export async function countMovableCells(
|
|
957
|
+
q: SdkQueryable,
|
|
958
|
+
cells: readonly MovableCell[],
|
|
959
|
+
ownerId: string | number
|
|
960
|
+
): Promise<number> {
|
|
961
|
+
let total = 0;
|
|
962
|
+
for (const cell of cells) {
|
|
963
|
+
const rows = await q.query<{ n: number }>(
|
|
964
|
+
`SELECT COUNT(*) AS n FROM ${cell.table}
|
|
965
|
+
WHERE ${ownerFilter(cell)} AND ${cell.column} IS NOT NULL AND ${cell.column} <> ''`,
|
|
966
|
+
[ownerId]
|
|
967
|
+
);
|
|
968
|
+
total += Number(rows[0]?.n ?? 0);
|
|
969
|
+
}
|
|
970
|
+
return total;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
/**
|
|
974
|
+
* Re-seals every encrypted cell of an item from one workspace's key to
|
|
975
|
+
* another's, for a `move`'s `apply`.
|
|
976
|
+
*
|
|
977
|
+
* Reads and converts EVERYTHING before the first write, and throws before
|
|
978
|
+
* writing anything if one cell resists: half a converted tree is unreadable
|
|
979
|
+
* forever. Call it before you change the item's `workspace_id`, so a failure
|
|
980
|
+
* leaves the item where it was.
|
|
981
|
+
*/
|
|
982
|
+
export async function resealCells(
|
|
983
|
+
q: SdkQueryable,
|
|
984
|
+
cells: readonly MovableCell[],
|
|
985
|
+
ownerId: string | number,
|
|
986
|
+
ciphers: { from: SdkCipher; to: SdkCipher }
|
|
987
|
+
): Promise<number> {
|
|
988
|
+
const pending: { cell: MovableCell; id: string | number; value: string }[] = [];
|
|
989
|
+
for (const cell of cells) {
|
|
990
|
+
// A cell that is empty has nothing to convert, and that is the common
|
|
991
|
+
// case for an error message or an optional body.
|
|
992
|
+
const rows = await q.query<{ row_id: string | number; value: string }>(
|
|
993
|
+
`SELECT ${cell.idColumn} AS row_id, ${cell.column} AS value FROM ${cell.table}
|
|
994
|
+
WHERE ${ownerFilter(cell)} AND ${cell.column} IS NOT NULL AND ${cell.column} <> ''`,
|
|
995
|
+
[ownerId]
|
|
996
|
+
);
|
|
997
|
+
for (const row of rows) {
|
|
998
|
+
const plain = await ciphers.from.tryDecrypt(String(row.value));
|
|
999
|
+
if (plain === null) {
|
|
1000
|
+
throw new FeatureError(
|
|
1001
|
+
'internal',
|
|
1002
|
+
`Une ligne de ${cell.table} est illisible : déplacement annulé, rien n’a été modifié.`
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
pending.push({ cell, id: row.row_id, value: await ciphers.to.encrypt(plain) });
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
for (const { cell, id, value } of pending) {
|
|
1010
|
+
// La garde du propriétaire dans le `WHERE` même quand l'identifiant
|
|
1011
|
+
// suffit : une conversion ne peut alors pas déborder sur un voisin.
|
|
1012
|
+
await q.execute(
|
|
1013
|
+
`UPDATE ${cell.table} SET ${cell.column} = ? WHERE ${cell.idColumn} = ? AND ${ownerFilter(cell)}`,
|
|
1014
|
+
[value, id, ownerId]
|
|
1015
|
+
);
|
|
1016
|
+
}
|
|
1017
|
+
return pending.length;
|
|
838
1018
|
}
|
|
839
1019
|
|
|
840
1020
|
// Le conteneur chiffré que CloudSync et Backup partagent (voir `devb.ts`).
|