Merge branch 'feature/typescript_backend_2' into feature/typescript_backend_3

This commit is contained in:
Elian Doran 2024-04-03 20:21:13 +03:00
commit 5baabecdee
No known key found for this signature in database
14 changed files with 29 additions and 58 deletions

View File

@ -11,7 +11,7 @@ import protectedSessionService = require('../../services/protected_session');
import blobService = require('../../services/blob');
import Becca, { ConstructorData } from '../becca-interface';
let becca: Becca | null = null;
let becca: Becca;
interface ContentOpts {
forceSave?: boolean;
@ -92,7 +92,9 @@ abstract class AbstractBeccaEntity<T extends AbstractBeccaEntity<T>> {
abstract getPojo(): {};
abstract init(): void;
init() {
// Do nothing by default, can be overriden in derived classes.
}
abstract updateFromRow(row: unknown): void;

View File

@ -83,10 +83,6 @@ class BAttachment extends AbstractBeccaEntity<BAttachment> {
this.contentLength = row.contentLength;
}
init(): void {
// Do nothing.
}
copy(): BAttachment {
return new BAttachment({
ownerId: this.ownerId,

View File

@ -26,10 +26,6 @@ class BBlob extends AbstractBeccaEntity<BBlob> {
this.utcDateModified = row.utcDateModified;
}
init() {
// Nothing to do.
}
getPojo() {
return {
blobId: this.blobId,

View File

@ -32,10 +32,6 @@ class BOption extends AbstractBeccaEntity<BOption> {
this.utcDateModified = row.utcDateModified;
}
init(): void {
// Do nothing.
}
beforeSaving() {
super.beforeSaving();

View File

@ -29,10 +29,6 @@ class BRecentNote extends AbstractBeccaEntity<BRecentNote> {
this.utcDateCreated = row.utcDateCreated || dateUtils.utcNowDateTime();
}
init(): void {
// Do nothing.
}
getPojo() {
return {
noteId: this.noteId,

View File

@ -68,10 +68,6 @@ class BRevision extends AbstractBeccaEntity<BRevision> {
this.contentLength = row.contentLength;
}
init() {
// Do nothing.
}
getNote() {
return becca.notes[this.noteId];
}

View File

@ -5,7 +5,7 @@ export interface AttachmentRow {
ownerId?: string;
role: string;
mime: string;
title?: string;
title: string;
position?: number;
blobId?: string;
isProtected?: boolean;

View File

@ -153,7 +153,7 @@ function buildRewardMap(note: BNote) {
const mimeCache: Record<string, string> = {};
function trimMime(mime?: string) {
function trimMime(mime: string) {
if (!mime || mime === 'text/html') {
return;
}

View File

@ -7,13 +7,11 @@ import utils = require('./utils');
import passwordEncryptionService = require('./encryption/password_encryption');
import config = require('./config');
import passwordService = require('./encryption/password');
import type { NextFunction, Request, Response } from 'express';
const noAuthentication = config.General && config.General.noAuthentication === true;
// TODO: We are using custom types for request & response because couldn't extract those pesky express types.
interface Request {
method: string;
path: string;
interface AppRequest extends Request {
headers: {
authorization?: string;
"trilium-cred"?: string;
@ -23,14 +21,7 @@ interface Request {
}
}
interface Response {
redirect(url: string): void;
setHeader(key: string, value: string): any
}
type Callback = () => void;
function checkAuth(req: Request, res: Response, next: Callback) {
function checkAuth(req: AppRequest, res: Response, next: NextFunction) {
if (!sqlInit.isDbInitialized()) {
res.redirect("setup");
}
@ -44,7 +35,7 @@ function checkAuth(req: Request, res: Response, next: Callback) {
// for electron things which need network stuff
// currently, we're doing that for file upload because handling form data seems to be difficult
function checkApiAuthOrElectron(req: Request, res: Response, next: Callback) {
function checkApiAuthOrElectron(req: AppRequest, res: Response, next: NextFunction) {
if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) {
reject(req, res, "Logged in session not found");
}
@ -53,7 +44,7 @@ function checkApiAuthOrElectron(req: Request, res: Response, next: Callback) {
}
}
function checkApiAuth(req: Request, res: Response, next: Callback) {
function checkApiAuth(req: AppRequest, res: Response, next: NextFunction) {
if (!req.session.loggedIn && !noAuthentication) {
reject(req, res, "Logged in session not found");
}
@ -62,7 +53,7 @@ function checkApiAuth(req: Request, res: Response, next: Callback) {
}
}
function checkAppInitialized(req: Request, res: Response, next: Callback) {
function checkAppInitialized(req: AppRequest, res: Response, next: NextFunction) {
if (!sqlInit.isDbInitialized()) {
res.redirect("setup");
}
@ -71,7 +62,7 @@ function checkAppInitialized(req: Request, res: Response, next: Callback) {
}
}
function checkPasswordSet(req: Request, res: Response, next: Callback) {
function checkPasswordSet(req: AppRequest, res: Response, next: NextFunction) {
if (!utils.isElectron() && !passwordService.isPasswordSet()) {
res.redirect("set-password");
} else {
@ -79,7 +70,7 @@ function checkPasswordSet(req: Request, res: Response, next: Callback) {
}
}
function checkPasswordNotSet(req: Request, res: Response, next: Callback) {
function checkPasswordNotSet(req: AppRequest, res: Response, next: NextFunction) {
if (!utils.isElectron() && passwordService.isPasswordSet()) {
res.redirect("login");
} else {
@ -87,7 +78,7 @@ function checkPasswordNotSet(req: Request, res: Response, next: Callback) {
}
}
function checkAppNotInitialized(req: Request, res: Response, next: Callback) {
function checkAppNotInitialized(req: AppRequest, res: Response, next: NextFunction) {
if (sqlInit.isDbInitialized()) {
reject(req, res, "App already initialized.");
}
@ -96,7 +87,7 @@ function checkAppNotInitialized(req: Request, res: Response, next: Callback) {
}
}
function checkEtapiToken(req: Request, res: Response, next: Callback) {
function checkEtapiToken(req: AppRequest, res: Response, next: NextFunction) {
if (etapiTokenService.isValidAuthHeader(req.headers.authorization)) {
next();
}
@ -105,7 +96,7 @@ function checkEtapiToken(req: Request, res: Response, next: Callback) {
}
}
function reject(req: Request, res: Response, message: string) {
function reject(req: AppRequest, res: Response, message: string) {
log.info(`${req.method} ${req.path} rejected with 401 ${message}`);
res.setHeader("Content-Type", "text/plain")
@ -113,7 +104,7 @@ function reject(req: Request, res: Response, message: string) {
.send(message);
}
function checkCredentials(req: Request, res: Response, next: Callback) {
function checkCredentials(req: AppRequest, res: Response, next: NextFunction) {
if (!sqlInit.isDbInitialized()) {
res.setHeader("Content-Type", "text/plain")
.status(400)

View File

@ -80,7 +80,7 @@ function validateLocalDateTime(str: string | null | undefined) {
}
}
function validateUtcDateTime(str?: string) {
function validateUtcDateTime(str: string | undefined) {
if (!str) {
return;
}

View File

@ -25,7 +25,7 @@ function createToken(tokenName: string) {
};
}
function parseAuthToken(auth?: string | null) {
function parseAuthToken(auth: string | undefined) {
if (!auth) {
return null;
}
@ -64,7 +64,7 @@ function parseAuthToken(auth?: string | null) {
}
}
function isValidAuthHeader(auth?: string | null) {
function isValidAuthHeader(auth: string | undefined) {
const parsed = parseAuthToken(auth);
if (!parsed) {

View File

@ -615,7 +615,7 @@ function getKeyboardActions() {
for (const option of optionService.getOptions()) {
if (option.name.startsWith('keyboardShortcuts')) {
let actionName = option.name.substr(17);
let actionName = option.name.substring(17);
actionName = actionName.charAt(0).toLowerCase() + actionName.slice(1);
const action = actions.find(ea => ea.actionName === actionName);

View File

@ -745,7 +745,7 @@ function saveRevisionIfNeeded(note: BNote) {
}
}
function updateNoteData(noteId: string, content: string, attachments: BAttachment[] = []) {
function updateNoteData(noteId: string, content: string, attachments: AttachmentRow[] = []) {
const note = becca.getNote(noteId);
if (!note || !note.isContentAvailable()) {
@ -761,11 +761,7 @@ function updateNoteData(noteId: string, content: string, attachments: BAttachmen
if (attachments?.length > 0) {
const existingAttachmentsByTitle = utils.toMap(note.getAttachments({includeContentLength: false}), 'title');
for (const attachment of attachments) {
// TODO: The content property was extracted directly instead of `getContent`. To investigate.
const {attachmentId, role, mime, title, position} = attachment;
const content = attachment.getContent();
for (const {attachmentId, role, mime, title, position, content} of attachments) {
if (attachmentId || !(title in existingAttachmentsByTitle)) {
note.saveAttachment({attachmentId, role, mime, title, content, position});
} else {
@ -773,7 +769,9 @@ function updateNoteData(noteId: string, content: string, attachments: BAttachmen
existingAttachment.role = role;
existingAttachment.mime = mime;
existingAttachment.position = position;
existingAttachment.setContent(content, {forceSave: true});
if (content) {
existingAttachment.setContent(content, {forceSave: true});
}
}
}
}
@ -876,7 +874,7 @@ function getUndeletedParentBranchIds(noteId: string, deleteId: string) {
AND parentNote.isDeleted = 0`, [noteId, deleteId]);
}
function scanForLinks(note: BNote | null, content: string) {
function scanForLinks(note: BNote, content: string) {
if (!note || !['text', 'relationMap'].includes(note.type)) {
return;
}

View File

@ -144,7 +144,7 @@ function getRows<T>(query: string, params: Params = []): T[] {
}
function getRawRows<T extends {} | unknown[]>(query: string, params: Params = []): T[] {
return (wrap(query, s => s.raw().all(params)) as T[] | null) || [];
return (wrap(query, s => s.raw().all(params)) as T[]) || [];
}
function iterateRows<T>(query: string, params: Params = []): IterableIterator<T> {