Skip to content
Snippets Groups Projects
Commit 136fd5c4 authored by Sebastian Mohr's avatar Sebastian Mohr
Browse files

Added a small trigger to append to user id to snips on insert

and updates
parent 13d999f6
No related branches found
No related tags found
1 merge request!17Refresh of the toolbar
Pipeline #479333 failed
......@@ -118,7 +118,11 @@ export function MoveToolContextProvider({ children }: ToolContextProps) {
const page = pageWrappersRef.current.get(key);
if (page) {
const parent = page.parentElement;
parent.scrollTo(value[0], value[1]);
parent.scrollTo({
left: value[0],
top: value[1],
behavior: "instant",
});
}
});
}, [pageWrappersRef, scrollPosition]);
......
......@@ -129,6 +129,7 @@ async function insert(
// Sanity check book id
snip_data.book_id = socket.book_id;
snip_data.created_by = socket.perms.entity_id;
// Sanity check to return incase of placement
const old_id = snip_data.id;
......@@ -168,6 +169,10 @@ async function update(
}
logWS(socket, "[Page: " + page_id + "] snip:update");
// Sanity check book id
snip_data.book_id = socket.book_id;
snip_data.created_by = socket.perms.entity_id;
// Sanity check if snip is in database
let inserted_data;
if (!snip_data.id || snip_data.id < 0) {
......
......@@ -15,8 +15,7 @@ export interface Socket extends SocketIo {
perms: ParsedPermissionACLData;
session: SnipSessionData;
data: {
id?: ID;
// If ui token id is token id else it is user id
id: ID; //user id
auth_method: "user" | "ui_token";
email: string;
allowedPages: ID[];
......
use snip_data;
-- add user_id column to snips table
ALTER TABLE snips ADD COLUMN created_by INT(11) NULL;
ALTER TABLE snips ADD FOREIGN KEY FK_created_by(created_by) REFERENCES snip_perms.users(id);
-- a trigger which does not allow to update or insert a snip without a user_id if page_id is not null
DELIMITER //
CREATE TRIGGER snip_user_id_check
BEFORE INSERT ON snips
FOR EACH ROW
BEGIN
IF NEW.page_id IS NOT NULL AND NEW.created_by IS NULL THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Snip was not assigned a user_id', MYSQL_ERRNO = 3410;
END IF;
END;
//
DELIMITER ;
DELIMITER //
CREATE TRIGGER snip_user_id_check_update
BEFORE UPDATE ON snips
FOR EACH ROW
BEGIN
IF NEW.page_id IS NOT NULL AND NEW.created_by IS NULL THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Snip was not assigned a user_id', MYSQL_ERRNO = 3410;
END IF;
END;
//
DELIMITER ;
\ No newline at end of file
......@@ -15,6 +15,8 @@ SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'My custom error message', MYSQL_ERRN
| 3401 | `Invite has already been accepted` | An collaborator invite has already been accepted. |
| 3402 | `Invite has expired` | An collaborator invite has expired. |
| 3403 | `Invite does not exist` | An collaborator invite does not exist. |
| 3410 | `Snip was not assigned a user_id` | Placed snips must have a user_id. |
#### Example
......
......@@ -130,7 +130,7 @@ export async function perms_by_ui_token(
return {
id: tokenData.id,
entity_id: -1,
entity_id: tokenData.created_by,
entity_type: ENTITY_TOKEN,
resource_id: tokenData.book_id,
resource_type: RESOURCE_BOOK,
......@@ -169,7 +169,7 @@ export async function perms_by_bearer_token(
return {
id: tokenData.id,
entity_id: -1,
entity_id: tokenData.created_by,
entity_type: ENTITY_TOKEN,
resource_id: tokenData.book_id,
resource_type: RESOURCE_BOOK,
......
......@@ -6,8 +6,6 @@
import { SqlError } from "mariadb";
import { ID } from "./types";
export function check_mariadb_warning<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
T extends { warningStatus: number } = any,
......@@ -65,6 +63,8 @@ export function catch_db_signals(e: SqlError): void {
throw new InviteExpiredError(e.sqlMessage);
case 3403:
throw new NotFoundError("Collaboration Invite", e.sqlMessage);
case 3410:
throw new SnipUserIdNotAssignedError(e.sqlMessage);
default:
console.error(e);
throw e;
......@@ -86,3 +86,10 @@ export class InviteAlreadyAcceptedError extends Error {
this.name = "InviteAlreadyAcceptedError";
}
}
export class SnipUserIdNotAssignedError extends Error {
constructor(msg: string) {
super(msg);
this.name = "SnipUserIdNotAssignedError";
}
}
......@@ -134,7 +134,7 @@ export class SnipService implements SnipStrategy {
}
const snip_sql =
"INSERT INTO `snips` (`page_id`, `book_id`, `type`, `data_json`, `view_json`, `blob_id`) VALUES (?, ?, ?, ?, ?, ?)";
"INSERT INTO `snips` (`page_id`, `book_id`, `type`, `data_json`, `view_json`, `blob_id`, `created_by`) VALUES (?, ?, ?, ?, ?, ?, ?)";
//Stringify json data adn view
const [data_json, removed] = encodeData(snipData.data);
......@@ -149,6 +149,7 @@ export class SnipService implements SnipStrategy {
data_json,
view_json,
blob_id,
snipData.created_by,
])
.then(check_mariadb_warning)
.then((res) => {
......@@ -250,7 +251,7 @@ export class SnipService implements SnipStrategy {
// Upload
const sql_data =
"UPDATE `snips` SET `page_id`=?,`book_id`=?,`type`=?,`data_json`=?,`view_json`=? WHERE `id`=?";
"UPDATE `snips` SET `page_id`=?,`book_id`=?,`type`=?,`data_json`=?,`view_json`=?,`created_by`=? WHERE `id`=?";
await pool
.query(sql_data, [
......@@ -260,6 +261,7 @@ export class SnipService implements SnipStrategy {
data_json,
view_json,
snipData.id,
snipData.created_by,
])
.then(check_mariadb_warning)
.then((res) => {
......
......@@ -322,6 +322,7 @@ export interface snipsData {
'blob_id'?: number | null;
'book_id': number;
'created'?: Date | null;
'created_by'?: number | null;
'data_json'?: string | null;
'hide'?: number | null;
'id'?: number;
......
import { serialize } from "node:v8";
import * as t from "./sql.typings";
/** !!README!!
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment