* Publish a note as a monograph
(noteId: string, title: string, opts: PublishOptions = {})
| 77 | * Publish a note as a monograph |
| 78 | */ |
| 79 | async publish(noteId: string, title: string, opts: PublishOptions = {}) { |
| 80 | if (title === "") throw new Error("Title cannot be empty."); |
| 81 | |
| 82 | if (!this.monographs.length) await this.refresh(); |
| 83 | |
| 84 | const update = !!this.isPublished(noteId); |
| 85 | |
| 86 | const user = await this.db.user.getUser(); |
| 87 | const token = await this.db.tokenManager.getAccessToken(); |
| 88 | if (!user || !token) throw new Error("Please login to publish a note."); |
| 89 | |
| 90 | const note = await this.db.notes.note(noteId); |
| 91 | if (!note) throw new Error("No such note found."); |
| 92 | if (!note.contentId) throw new Error("Cannot publish an empty note."); |
| 93 | |
| 94 | const contentItem = await this.db.content.get(note.contentId); |
| 95 | |
| 96 | if (!contentItem || isDeleted(contentItem)) |
| 97 | throw new Error("Could not find content for this note."); |
| 98 | |
| 99 | if (contentItem.locked) throw new Error("Cannot published locked notes."); |
| 100 | |
| 101 | const content = await this.db.content.downloadMedia( |
| 102 | `monograph-${noteId}`, |
| 103 | contentItem, |
| 104 | false |
| 105 | ); |
| 106 | |
| 107 | const monographPasswordsKey = await this.db.user.getMonographPasswordsKey(); |
| 108 | const monograph: MonographApiRequest = { |
| 109 | id: noteId, |
| 110 | title, |
| 111 | userId: user.id, |
| 112 | selfDestruct: opts.selfDestruct || false, |
| 113 | ...(opts.password |
| 114 | ? { |
| 115 | password: monographPasswordsKey |
| 116 | ? await this.db |
| 117 | .storage() |
| 118 | .encrypt(monographPasswordsKey, opts.password) |
| 119 | : undefined, |
| 120 | encryptedContent: await this.db |
| 121 | .storage() |
| 122 | .encrypt( |
| 123 | { password: opts.password }, |
| 124 | JSON.stringify({ type: content.type, data: content.data }) |
| 125 | ) |
| 126 | } |
| 127 | : { |
| 128 | password: undefined, |
| 129 | content: JSON.stringify({ |
| 130 | type: content.type, |
| 131 | data: content.data |
| 132 | }) |
| 133 | }) |
| 134 | }; |
| 135 | |
| 136 | const deviceId = await this.db.kv().read("deviceId"); |
no test coverage detected