-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathsnote.ts
535 lines (441 loc) · 15.7 KB
/
snote.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
"use strict";
import sql from "../../sql.js";
import utils from "../../../services/utils.js";
import AbstractShacaEntity from "./abstract_shaca_entity.js";
import escape from "escape-html";
import type { Blob } from "../../../services/blob-interface.js";
import type SAttachment from "./sattachment.js";
import type SAttribute from "./sattribute.js";
import type SBranch from "./sbranch.js";
import type { SNoteRow } from "./rows.js";
const LABEL = "label";
const RELATION = "relation";
const CREDENTIALS = "shareCredentials";
const isCredentials = (attr: SAttribute) => attr.type === "label" && attr.name === CREDENTIALS;
class SNote extends AbstractShacaEntity {
noteId: string;
title: string;
type: string;
mime: string;
private blobId: string;
utcDateModified: string;
isProtected: boolean;
parentBranches: SBranch[];
parents: SNote[];
children: SNote[];
ownedAttributes: SAttribute[];
private __attributeCache: SAttribute[] | null;
private __inheritableAttributeCache: SAttribute[] | null;
targetRelations: SAttribute[];
attachments: SAttachment[];
constructor([noteId, title, type, mime, blobId, utcDateModified, isProtected]: SNoteRow) {
super();
this.noteId = noteId;
this.title = isProtected ? "[protected]" : title;
this.type = type;
this.mime = mime;
this.blobId = blobId;
this.utcDateModified = utcDateModified; // used for caching of images
this.isProtected = isProtected;
this.parentBranches = [];
this.parents = [];
this.children = [];
this.ownedAttributes = [];
this.__attributeCache = null;
this.__inheritableAttributeCache = null;
this.targetRelations = [];
this.attachments = [];
this.shaca.notes[this.noteId] = this;
}
getParentBranches() {
return this.parentBranches;
}
getBranches() {
return this.parentBranches;
}
getChildBranches(): SBranch[] {
return this.children.map((childNote) => this.shaca.getBranchFromChildAndParent(childNote.noteId, this.noteId));
}
getVisibleChildBranches() {
return this.getChildBranches().filter((branch) => !branch.isHidden && !branch.getNote().isLabelTruthy("shareHiddenFromTree"));
}
getParentNotes() {
return this.parents;
}
getChildNotes() {
return this.children;
}
getVisibleChildNotes() {
return this.getVisibleChildBranches().map((branch) => branch.getNote());
}
hasChildren() {
return this.children && this.children.length > 0;
}
hasVisibleChildren() {
return this.getVisibleChildNotes().length > 0;
}
getContent(silentNotFoundError = false) {
const row = sql.getRow<Pick<Blob, "content">>(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);
if (!row) {
if (silentNotFoundError) {
return undefined;
} else {
throw new Error(`Cannot find note content for note '${this.noteId}', blob '${this.blobId}'`);
}
}
const content = row.content;
if (this.hasStringContent()) {
return content === null ? "" : content.toString("utf-8");
} else {
return content;
}
}
/** @returns true if the note has string content (not binary) */
hasStringContent() {
return utils.isStringNote(this.type, this.mime);
}
/**
* @param type - (optional) attribute type to filter
* @param name - (optional) attribute name to filter
* @returns all note's attributes, including inherited ones
*/
getAttributes(type?: string, name?: string) {
let attributeCache = this.__attributeCache;
if (!attributeCache) {
attributeCache = this.__getAttributes([]);
}
if (type && name) {
return attributeCache.filter((attr) => attr.type === type && attr.name === name && !isCredentials(attr));
} else if (type) {
return attributeCache.filter((attr) => attr.type === type && !isCredentials(attr));
} else if (name) {
return attributeCache.filter((attr) => attr.name === name && !isCredentials(attr));
} else {
return attributeCache.filter((attr) => !isCredentials(attr));
}
}
getCredentials() {
return this.__getAttributes([]).filter(isCredentials);
}
__getAttributes(path: string[]) {
if (path.includes(this.noteId)) {
return [];
}
if (!this.__attributeCache) {
const parentAttributes = this.ownedAttributes.slice();
const newPath = [...path, this.noteId];
if (this.noteId !== "root") {
for (const parentNote of this.parents) {
parentAttributes.push(...parentNote.__getInheritableAttributes(newPath));
}
}
const templateAttributes: SAttribute[] = [];
for (const ownedAttr of parentAttributes) {
// parentAttributes so we process also inherited templates
if (ownedAttr.type === "relation" && ["template", "inherit"].includes(ownedAttr.name)) {
const templateNote = this.shaca.notes[ownedAttr.value];
if (templateNote) {
templateAttributes.push(...templateNote.__getAttributes(newPath));
}
}
}
this.__attributeCache = [];
const addedAttributeIds = new Set();
for (const attr of parentAttributes.concat(templateAttributes)) {
if (!addedAttributeIds.has(attr.attributeId)) {
addedAttributeIds.add(attr.attributeId);
this.__attributeCache.push(attr);
}
}
this.__inheritableAttributeCache = [];
for (const attr of this.__attributeCache) {
if (attr.isInheritable) {
this.__inheritableAttributeCache.push(attr);
}
}
}
return this.__attributeCache;
}
__getInheritableAttributes(path: string[]) {
if (path.includes(this.noteId)) {
return [];
}
if (!this.__inheritableAttributeCache) {
this.__getAttributes(path); // will refresh also this.__inheritableAttributeCache
}
return this.__inheritableAttributeCache || [];
}
/**
* @throws Error in case of invalid JSON
*/
getJsonContent(): unknown | null {
const content = this.getContent();
if (typeof content !== "string" || !content || !content.trim()) {
return null;
}
return JSON.parse(content);
}
/** @returns valid object or null if the content cannot be parsed as JSON */
getJsonContentSafely() {
try {
return this.getJsonContent();
} catch (e) {
return null;
}
}
hasAttribute(type: string, name: string) {
return !!this.getAttributes().find((attr) => attr.type === type && attr.name === name);
}
getRelationTarget(name: string) {
const relation = this.getAttributes().find((attr) => attr.type === "relation" && attr.name === name);
return relation ? relation.targetNote : null;
}
/**
* @param name - label name
* @returns true if label exists (including inherited)
*/
hasLabel(name: string) {
return this.hasAttribute(LABEL, name);
}
/**
* @param name - label name
* @returns true if label exists (including inherited) and does not have "false" value.
*/
isLabelTruthy(name: string) {
const label = this.getLabel(name);
if (!label) {
return false;
}
return !!label && label.value !== "false";
}
/**
* @param name - label name
* @returns true if label exists (excluding inherited)
*/
hasOwnedLabel(name: string) {
return this.hasOwnedAttribute(LABEL, name);
}
/**
* @param name - relation name
* @returns true if relation exists (including inherited)
*/
hasRelation(name: string) {
return this.hasAttribute(RELATION, name);
}
/**
* @param name - relation name
* @returns true if relation exists (excluding inherited)
*/
hasOwnedRelation(name: string) {
return this.hasOwnedAttribute(RELATION, name);
}
/**
* @param name - label name
* @returns label if it exists, null otherwise
*/
getLabel(name: string) {
return this.getAttribute(LABEL, name);
}
/**
* @param name - label name
* @returns label if it exists, null otherwise
*/
getOwnedLabel(name: string) {
return this.getOwnedAttribute(LABEL, name);
}
/**
* @param name - relation name
* @returns relation if it exists, null otherwise
*/
getRelation(name: string) {
return this.getAttribute(RELATION, name);
}
/**
* @param name - relation name
* @returns relation if it exists, null otherwise
*/
getOwnedRelation(name: string) {
return this.getOwnedAttribute(RELATION, name);
}
/**
* @param name - label name
* @returns label value if label exists, null otherwise
*/
getLabelValue(name: string) {
return this.getAttributeValue(LABEL, name);
}
/**
* @param name - label name
* @returns label value if label exists, null otherwise
*/
getOwnedLabelValue(name: string) {
return this.getOwnedAttributeValue(LABEL, name);
}
/**
* @param name - relation name
* @returns relation value if relation exists, null otherwise
*/
getRelationValue(name: string) {
return this.getAttributeValue(RELATION, name);
}
/**
* @param name - relation name
* @returns relation value if relation exists, null otherwise
*/
getOwnedRelationValue(name: string) {
return this.getOwnedAttributeValue(RELATION, name);
}
/**
* @param type - attribute type (label, relation, etc.)
* @param name - attribute name
* @returns true if note has an attribute with given type and name (excluding inherited)
*/
hasOwnedAttribute(type: string, name: string) {
return !!this.getOwnedAttribute(type, name);
}
/**
* @param type - attribute type (label, relation, etc.)
* @param name - attribute name
* @returns attribute of the given type and name. If there are more such attributes, first is returned.
* Returns null if there's no such attribute belonging to this note.
*/
getAttribute(type: string, name: string) {
const attributes = this.getAttributes();
return attributes.find((attr) => attr.type === type && attr.name === name);
}
/**
* @param type - attribute type (label, relation, etc.)
* @param name - attribute name
* @returns attribute value of the given type and name or null if no such attribute exists.
*/
getAttributeValue(type: string, name: string) {
const attr = this.getAttribute(type, name);
return attr ? attr.value : null;
}
/**
* @param type - attribute type (label, relation, etc.)
* @param name - attribute name
* @returns attribute value of the given type and name or null if no such attribute exists.
*/
getOwnedAttributeValue(type: string, name: string) {
const attr = this.getOwnedAttribute(type, name);
return attr ? (attr.value as string) : null; // FIXME
}
/**
* @param name - label name to filter
* @returns all note's labels (attributes with type label), including inherited ones
*/
getLabels(name: string) {
return this.getAttributes(LABEL, name);
}
/**
* @param name - label name to filter
* @returns all note's label values, including inherited ones
*/
getLabelValues(name: string) {
return this.getLabels(name).map((l) => l.value) as string[]; // FIXME
}
/**
* @param name - label name to filter
* @returns all note's labels (attributes with type label), excluding inherited ones
*/
getOwnedLabels(name: string) {
return this.getOwnedAttributes(LABEL, name);
}
/**
* @param name - label name to filter
* @returns all note's label values, excluding inherited ones
*/
getOwnedLabelValues(name: string) {
return this.getOwnedAttributes(LABEL, name).map((l) => l.value);
}
/**
* @param name - relation name to filter
* @returns all note's relations (attributes with type relation), including inherited ones
*/
getRelations(name: string) {
return this.getAttributes(RELATION, name);
}
/**
* @param name - relation name to filter
* @returns all note's relations (attributes with type relation), excluding inherited ones
*/
getOwnedRelations(name: string) {
return this.getOwnedAttributes(RELATION, name);
}
/**
* @param type - (optional) attribute type to filter
* @param name - (optional) attribute name to filter
* @returns note's "owned" attributes - excluding inherited ones
*/
getOwnedAttributes(type: string, name: string) {
// it's a common mistake to include # or ~ into attribute name
if (name && ["#", "~"].includes(name[0])) {
name = name.substr(1);
}
if (type && name) {
return this.ownedAttributes.filter((attr) => attr.type === type && attr.name === name);
} else if (type) {
return this.ownedAttributes.filter((attr) => attr.type === type);
} else if (name) {
return this.ownedAttributes.filter((attr) => attr.name === name);
} else {
return this.ownedAttributes.slice();
}
}
/**
* @returns attribute belonging to this specific note (excludes inherited attributes)
*
* This method can be significantly faster than the getAttribute()
*/
getOwnedAttribute(type: string, name: string) {
const attrs = this.getOwnedAttributes(type, name);
return attrs.length > 0 ? attrs[0] : null;
}
get isArchived() {
return this.hasAttribute("label", "archived");
}
isInherited() {
return !!this.targetRelations.find((rel) => rel.name === "template" || rel.name === "inherit");
}
getTargetRelations() {
return this.targetRelations;
}
getAttachments() {
return this.attachments;
}
getAttachmentByTitle(title: string) {
return this.attachments.find((attachment) => attachment.title === title);
}
get shareId() {
if (this.hasOwnedLabel("shareRoot")) {
return "";
}
const sharedAlias = this.getOwnedLabelValue("shareAlias");
return sharedAlias || this.noteId;
}
get escapedTitle() {
return escape(this.title);
}
get encodedTitle() {
return encodeURIComponent(this.title);
}
getPojo() {
return {
noteId: this.noteId,
title: this.title,
type: this.type,
mime: this.mime,
utcDateModified: this.utcDateModified,
attributes: this.getAttributes()
// relations could link across shared subtrees which might leak them
// individual relations might be whitelisted based on needs #3434
.filter((attr) => attr.type === "label")
.map((attr) => attr.getPojo()),
attachments: this.getAttachments().map((attachment) => attachment.getPojo()),
parentNoteIds: this.parents.map((parentNote) => parentNote.noteId),
childNoteIds: this.children.map((child) => child.noteId)
};
}
}
export default SNote;