attachments have a position

This commit is contained in:
zadam 2023-04-11 22:55:50 +02:00
parent 3f8bf7cacc
commit 6cf0fe0b73
6 changed files with 20 additions and 7 deletions

View file

@ -6,6 +6,7 @@ CREATE TABLE IF NOT EXISTS "attachments"
mime TEXT not null, mime TEXT not null,
title TEXT not null, title TEXT not null,
isProtected INT not null DEFAULT 0, isProtected INT not null DEFAULT 0,
position INT default 0 not null,
blobId TEXT DEFAULT null, blobId TEXT DEFAULT null,
dateModified TEXT NOT NULL, dateModified TEXT NOT NULL,
utcDateModified TEXT not null, utcDateModified TEXT not null,

View file

@ -117,6 +117,7 @@ CREATE TABLE IF NOT EXISTS "attachments"
mime TEXT not null, mime TEXT not null,
title TEXT not null, title TEXT not null,
isProtected INT not null DEFAULT 0, isProtected INT not null DEFAULT 0,
position INT default 0 not null,
blobId TEXT DEFAULT null, blobId TEXT DEFAULT null,
dateModified TEXT NOT NULL, dateModified TEXT NOT NULL,
utcDateModified TEXT not null, utcDateModified TEXT not null,

View file

@ -4,10 +4,9 @@ const utils = require('../../services/utils');
const dateUtils = require('../../services/date_utils'); const dateUtils = require('../../services/date_utils');
const becca = require('../becca'); const becca = require('../becca');
const AbstractBeccaEntity = require("./abstract_becca_entity"); const AbstractBeccaEntity = require("./abstract_becca_entity");
const sql = require("../../services/sql");
/** /**
* FIXME: how to order attachments?
*
* Attachment represent data related/attached to the note. Conceptually similar to attributes, but intended for * Attachment represent data related/attached to the note. Conceptually similar to attributes, but intended for
* larger amounts of data and generally not accessible to the user. * larger amounts of data and generally not accessible to the user.
* *
@ -42,6 +41,8 @@ class BAttachment extends AbstractBeccaEntity {
this.mime = row.mime; this.mime = row.mime;
/** @type {string} */ /** @type {string} */
this.title = row.title; this.title = row.title;
/** @type {number} */
this.position = row.position;
/** @type {string} */ /** @type {string} */
this.blobId = row.blobId; this.blobId = row.blobId;
/** @type {boolean} */ /** @type {boolean} */
@ -80,6 +81,12 @@ class BAttachment extends AbstractBeccaEntity {
beforeSaving() { beforeSaving() {
super.beforeSaving(); super.beforeSaving();
if (this.position === undefined || this.position === null) {
this.position = 10 + sql.getValue(`SELECT COALESCE(MAX(position), 0)
FROM attachments
WHERE parentId = ?`, [this.noteId]);
}
this.dateModified = dateUtils.localNowDateTime(); this.dateModified = dateUtils.localNowDateTime();
this.utcDateModified = dateUtils.utcNowDateTime(); this.utcDateModified = dateUtils.utcNowDateTime();
} }
@ -91,6 +98,7 @@ class BAttachment extends AbstractBeccaEntity {
role: this.role, role: this.role,
mime: this.mime, mime: this.mime,
title: this.title, title: this.title,
position: this.position,
blobId: this.blobId, blobId: this.blobId,
isProtected: !!this.isProtected, isProtected: !!this.isProtected,
isDeleted: false, isDeleted: false,

View file

@ -188,9 +188,11 @@ class BAttribute extends AbstractBeccaEntity {
this.value = ""; this.value = "";
} }
if (this.position === undefined) { if (this.position === undefined || this.position === null) {
// TODO: can be calculated from becca const maxExistingPosition = this.getNote().getAttributes()
this.position = 1 + sql.getValue(`SELECT COALESCE(MAX(position), 0) FROM attributes WHERE noteId = ?`, [this.noteId]); .reduce((maxPosition, attr) => Math.max(maxPosition, attr.position), 0);
this.position = maxExistingPosition + 10;
} }
if (!this.isInheritable) { if (!this.isInheritable) {

View file

@ -1218,10 +1218,10 @@ class BNote extends AbstractBeccaEntity {
* @param {string} name - name of the attribute, not including the leading ~/# * @param {string} name - name of the attribute, not including the leading ~/#
* @param {string} [value] - value of the attribute - text for labels, target note ID for relations; optional. * @param {string} [value] - value of the attribute - text for labels, target note ID for relations; optional.
* @param {boolean} [isInheritable=false] * @param {boolean} [isInheritable=false]
* @param {int} [position] * @param {int|null} [position]
* @returns {BAttribute} * @returns {BAttribute}
*/ */
addAttribute(type, name, value = "", isInheritable = false, position = 1000) { addAttribute(type, name, value = "", isInheritable = false, position = null) {
const BAttribute = require("./battribute"); const BAttribute = require("./battribute");
return new BAttribute({ return new BAttribute({

View file

@ -37,6 +37,7 @@ function requestCredentials(res) {
.sendStatus(401); .sendStatus(401);
} }
/** @returns {SNote|boolean} */
function checkNoteAccess(noteId, req, res) { function checkNoteAccess(noteId, req, res) {
const note = shaca.getNote(noteId); const note = shaca.getNote(noteId);