| 26 | import { SQLCollection } from "../database/sql-collection.js"; |
| 27 | |
| 28 | export class SessionContent implements ICollection { |
| 29 | name = "sessioncontent"; |
| 30 | readonly collection: SQLCollection<"sessioncontent", SessionContentItem>; |
| 31 | constructor(private readonly db: Database) { |
| 32 | this.collection = new SQLCollection( |
| 33 | db.sql, |
| 34 | db.transaction, |
| 35 | "sessioncontent", |
| 36 | db.eventManager, |
| 37 | db.sanitizer |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | async init() { |
| 42 | await this.collection.init(); |
| 43 | } |
| 44 | |
| 45 | async add<TLocked extends boolean>( |
| 46 | sessionId: string, |
| 47 | content: Partial<NoteContent<TLocked>> & { title?: string; noteId: string }, |
| 48 | locked?: TLocked |
| 49 | ) { |
| 50 | if (!sessionId || !content) return; |
| 51 | // const data = |
| 52 | // locked || isCipher(content.data) |
| 53 | // ? content.data |
| 54 | // : await this.db.compressor().compress(content.data); |
| 55 | const sessionContentItemId = makeSessionContentId(sessionId); |
| 56 | const sessionContentExists = await this.collection.exists( |
| 57 | sessionContentItemId |
| 58 | ); |
| 59 | const sessionItem: Partial<SessionContentItem> = { |
| 60 | type: "sessioncontent", |
| 61 | id: sessionContentItemId, |
| 62 | compressed: false, |
| 63 | localOnly: true, |
| 64 | locked: locked || false, |
| 65 | dateCreated: Date.now(), |
| 66 | dateModified: Date.now() |
| 67 | }; |
| 68 | |
| 69 | if (content.data && content.type) { |
| 70 | sessionItem.data = content.data; |
| 71 | sessionItem.contentType = content.type; |
| 72 | |
| 73 | if (typeof content.title !== "string" && !sessionContentExists) { |
| 74 | const note = await this.db.notes.note(content.noteId); |
| 75 | sessionItem.title = note?.title; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (content.title) { |
| 80 | sessionItem.title = content.title; |
| 81 | |
| 82 | if (!content.data && !content.type && !sessionContentExists) { |
| 83 | const note = await this.db.notes.note(content.noteId); |
| 84 | if (note?.contentId) { |
| 85 | const noteContent = await this.db.content.get(note?.contentId); |
nothing calls this directly
no outgoing calls
no test coverage detected