"smart" titles

This commit is contained in:
maaa 2024-03-10 23:00:40 +01:00
parent 4a26a6b04b
commit f7920de812
3 changed files with 48 additions and 39 deletions

View File

@ -1,9 +1,9 @@
# Burgernotes API docs # 🍔 Burgernotes API docs
Use the Burgernotes API to automate tasks, build your own client, and more! Use the Burgernotes API to automate tasks, build your own client, and more!
Headers should be: "Content-type: application/json; charset=UTF-8" for all POSTs Headers should be: "Content-type: application/json; charset=UTF-8" for all POSTs
## Authentication ## 🔑 Authentication
POST - /api/signup - provide "username" and "password". POST - /api/signup - provide "username" and "password".
@ -46,13 +46,13 @@ You'll need to store two things in local storage:
- The secret key you just got, used to fetch notes, save stuff etc. - The secret key you just got, used to fetch notes, save stuff etc.
- A SHA512 hashed password, used as encryption key - A SHA512 hashed password, used as encryption key
## Encryption ## 🔐 Encryption
Note content and title is encrypted using AES 256-bit. Note content and title is encrypted using AES 256-bit.
Encryption password is the SHA512 hashed password we talked about earlier. Encryption password is the SHA512 hashed password we talked about earlier.
## Basic stuff ## 🕹️ Basic stuff
POST - /api/userinfo - get user info such as username, provide "secretKey" POST - /api/userinfo - get user info such as username, provide "secretKey"
@ -65,15 +65,16 @@ POST - /api/newnote - create a note, provide "secretKey" and "noteName"
POST - /api/readnote - read notes, provide "secretKey" and "noteId" POST - /api/readnote - read notes, provide "secretKey" and "noteId"
note content will have to be decrypted. note content will have to be decrypted.
POST - /api/editnote - edit notes, provide "secretKey", "noteId", and "content" POST - /api/editnote - edit notes, provide "secretKey", "noteId", "title", and "content"
"content" should be encrypted. "content" should be encrypted.
"title" is the first line of the note content, and should be encrypted. the title should be truncated if it's above 16 characters, meaning "sillyburgermuncher" -> "sillyburgermunch.."
POST - /api/editnotetitle - edit note titles, provide "secretKey", "noteId", and "content" **(Deprecated ⚠️)** POST - /api/editnotetitle - edit note titles, provide "secretKey", "noteId", and "content"
"content" should be encrypted. "content" should be encrypted.
POST - /api/removenote - remove notes, provide "secretKey" and "noteId" POST - /api/removenote - remove notes, provide "secretKey" and "noteId"
## More stuff ## ⚙️ More stuff
POST - /api/deleteaccount - delete account, provide "secretKey" POST - /api/deleteaccount - delete account, provide "secretKey"
please display a warning before this action please display a warning before this action

3
main
View File

@ -354,6 +354,7 @@ async def apieditnote():
secretKey = data["secretKey"] secretKey = data["secretKey"]
noteId = data["noteId"] noteId = data["noteId"]
content = data["content"] content = data["content"]
title = data["title"]
userCookie = get_session(secretKey) userCookie = get_session(secretKey)
user = get_user(userCookie["id"]) user = get_user(userCookie["id"])
@ -366,7 +367,7 @@ async def apieditnote():
if (note != None): if (note != None):
if (user["id"] == note["creator"]): if (user["id"] == note["creator"]):
conn = get_db_connection() conn = get_db_connection()
conn.execute("UPDATE notes SET content = ?, edited = ? WHERE id = ?", (content, str(time.time()), noteId)) conn.execute("UPDATE notes SET content = ?, title = ?, edited = ? WHERE id = ?", (content, title, str(time.time()), noteId))
conn.commit() conn.commit()
conn.close() conn.close()

View File

@ -359,6 +359,14 @@ function updateWordCount() {
wordCountBox.innerText = wordCount + " words" wordCountBox.innerText = wordCount + " words"
} }
function truncateString(str, num) {
if (str.length > num) {
return str.slice(0, num) + "..";
} else {
return str;
}
}
function selectNote(nameithink) { function selectNote(nameithink) {
document.querySelectorAll(".noteButton").forEach((el) => el.classList.remove("selected")); document.querySelectorAll(".noteButton").forEach((el) => el.classList.remove("selected"));
let thingArray = Array.from(document.querySelectorAll(".noteButton")).find(el => el.id == nameithink); let thingArray = Array.from(document.querySelectorAll(".noteButton")).find(el => el.id == nameithink);
@ -399,6 +407,13 @@ function selectNote(nameithink) {
updateWordCount() updateWordCount()
clearTimeout(timer); clearTimeout(timer);
timer = setTimeout(() => { timer = setTimeout(() => {
let encryptedTitle = "empty note"
if (noteBox.value != "") {
let firstTitle = truncateString(noteBox.value.slice(0, noteBox.value.indexOf("\n")), 16)
document.getElementById(nameithink).innerText = firstTitle
encryptedTitle = CryptoJS.AES.encrypt(firstTitle, password).toString();
}
let encryptedText = CryptoJS.AES.encrypt(noteBox.value, password).toString(); let encryptedText = CryptoJS.AES.encrypt(noteBox.value, password).toString();
if (selectedNote == nameithink) { if (selectedNote == nameithink) {
@ -408,6 +423,7 @@ function selectNote(nameithink) {
secretKey: secretkey, secretKey: secretkey,
noteId: nameithink, noteId: nameithink,
content: encryptedText, content: encryptedText,
title: encryptedTitle
}), }),
headers: { headers: {
"Content-Type": "application/json; charset=UTF-8" "Content-Type": "application/json; charset=UTF-8"
@ -494,14 +510,7 @@ function updateNotes() {
updateNotes() updateNotes()
newNote.addEventListener("click", (event) => { newNote.addEventListener("click", (event) => {
let noteName = displayPrompt("Note name?", "E.G Shopping list", burgerFunction) let noteName = "empty note"
function burgerFunction(noteName) {
if (noteName != null) {
if (noteName.length > 21) {
displayError("Invalid note name: Too long (max 21 characters)");
return;
}
let encryptedName = CryptoJS.AES.encrypt(noteName, password).toString(); let encryptedName = CryptoJS.AES.encrypt(noteName, password).toString();
fetch(remote + "/api/newnote", { fetch(remote + "/api/newnote", {
method: "POST", method: "POST",
@ -524,8 +533,6 @@ newNote.addEventListener("click", (event) => {
updateNotes() updateNotes()
} }
}); });
}
}
}); });
function downloadObjectAsJson(exportObj, exportName) { function downloadObjectAsJson(exportObj, exportName) {
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj)); var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));