Small refactor for user preferences

This commit is contained in:
Magnus Åhall 2026-06-18 09:21:23 +02:00
parent 1a712fb7a9
commit 81d02b82dc
13 changed files with 202 additions and 112 deletions

View file

@ -8,6 +8,9 @@ import (
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/lib/pq" "github.com/lib/pq"
// Internal
appUser "notes2/user"
// Standard // Standard
"database/sql" "database/sql"
"encoding/hex" "encoding/hex"
@ -27,12 +30,6 @@ type Manager struct {
ExpireDays int ExpireDays int
} }
type User struct {
ID int
Username string
Name string
}
func httpError(w http.ResponseWriter, err error) { // {{{ func httpError(w http.ResponseWriter, err error) { // {{{
j, _ := json.Marshal(struct { j, _ := json.Marshal(struct {
OK bool OK bool
@ -165,16 +162,16 @@ func (mngr *Manager) AuthenticationHandler(w http.ResponseWriter, r *http.Reques
mngr.log.Info("authentication", "username", request.Username, "status", "accepted") mngr.log.Info("authentication", "username", request.Username, "status", "accepted")
j, _ := json.Marshal(struct { j, _ := json.Marshal(struct {
OK bool OK bool
User User User appUser.User
Token string Token string
}{true, user, token}) }{true, user, token})
w.Write(j) w.Write(j)
} // }}} } // }}}
func (mngr *Manager) Authenticate(username, password string) (authenticated bool, user User, err error) { // {{{ func (mngr *Manager) Authenticate(username, password string) (authenticated bool, user appUser.User, err error) { // {{{
var row *sql.Row var row *sql.Row
row = mngr.db.QueryRow(` row = mngr.db.QueryRow(`
SELECT id, username, name SELECT id, username, name, preferences
FROM public.user FROM public.user
WHERE WHERE
LOWER(username) = LOWER($1) AND LOWER(username) = LOWER($1) AND
@ -183,13 +180,21 @@ func (mngr *Manager) Authenticate(username, password string) (authenticated bool
username, username,
password, password,
) )
err = row.Scan(&user.ID, &user.Username, &user.Name) var data []byte
err = row.Scan(&user.ID, &user.Username, &user.Name, &data)
if err != nil && err.Error() == "sql: no rows in result set" { if err != nil && err.Error() == "sql: no rows in result set" {
err = nil err = nil
authenticated = false authenticated = false
return return
} }
if err != nil { if err != nil {
authenticated = false
return
}
err = json.Unmarshal(data, &user.Preferences)
if err != nil {
authenticated = false
return return
} }
@ -278,7 +283,7 @@ func (mngr *Manager) ChangePassword(username, currentPassword, newPassword strin
changed = (rowsAffected == 1) changed = (rowsAffected == 1)
return return
} // }}} } // }}}
func (mngr *Manager) NewClientUUID(user User) (clientUUID string, err error) { // {{{ func (mngr *Manager) NewClientUUID(user appUser.User) (clientUUID string, err error) { // {{{
// Each client session has its own UUID. // Each client session has its own UUID.
// Loop through until a unique one is established. // Loop through until a unique one is established.
var proposedClientUUID string var proposedClientUUID string

27
main.go
View file

@ -4,6 +4,7 @@ import (
// Internal // Internal
"notes2/authentication" "notes2/authentication"
"notes2/html_template" "notes2/html_template"
appUser "notes2/user"
"os" "os"
// Standard // Standard
@ -134,6 +135,7 @@ func main() { // {{{
http.HandleFunc("/offline", pageOffline) http.HandleFunc("/offline", pageOffline)
http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler) http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler)
http.HandleFunc("/user/preferences", authenticated(actionUserPreferences))
http.HandleFunc("/sync/from_server/count/{sequence}", authenticated(actionSyncFromServerCount)) http.HandleFunc("/sync/from_server/count/{sequence}", authenticated(actionSyncFromServerCount))
http.HandleFunc("/sync/from_server/{sequence}/{offset}", authenticated(actionSyncFromServer)) http.HandleFunc("/sync/from_server/{sequence}/{offset}", authenticated(actionSyncFromServer))
@ -178,7 +180,7 @@ func authenticated(fn func(http.ResponseWriter, *http.Request)) func(http.Respon
} }
// User object is added to the context for the next handler. // User object is added to the context for the next handler.
user := NewUser(claims) user := appUser.NewUser(claims)
r = r.WithContext(context.WithValue(r.Context(), CONTEXT_USER, user)) r = r.WithContext(context.WithValue(r.Context(), CONTEXT_USER, user))
Log.Debug("webserver", "op", "request", "method", r.Method, "url", r.URL.String(), "username", user.Username, "client", user.ClientUUID) Log.Debug("webserver", "op", "request", "method", r.Method, "url", r.URL.String(), "username", user.Username, "client", user.ClientUUID)
@ -360,8 +362,8 @@ func actionNodeHistoryCount(w http.ResponseWriter, r *http.Request) { // {{{
} }
responseData(w, map[string]any{ responseData(w, map[string]any{
"OK": true, "OK": true,
"Count": count, "Count": count,
}) })
} // }}} } // }}}
func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{ func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
@ -389,6 +391,20 @@ func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
}) })
} // }}} } // }}}
func actionUserPreferences(w http.ResponseWriter, r *http.Request) { // {{{
user := getUser(r)
prefs, err := user.Preferences()
if err != nil {
httpError(w, err)
return
}
responseData(w, map[string]any{
"OK": true,
"Preferences": prefs,
})
} // }}}
func createNewUser(username string) { // {{{ func createNewUser(username string) { // {{{
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
@ -431,7 +447,8 @@ func changePassword(username string) { // {{{
fmt.Printf("\nPassword changed\n") fmt.Printf("\nPassword changed\n")
} // }}} } // }}}
func getUser(r *http.Request) UserSession { // {{{ func getUser(r *http.Request) appUser.UserSession { // {{{
user, _ := r.Context().Value(CONTEXT_USER).(UserSession) user, _ := r.Context().Value(CONTEXT_USER).(appUser.UserSession)
user.Db = db
return user return user
} // }}} } // }}}

1
sql/00010.sql Normal file
View file

@ -0,0 +1 @@
ALTER TABLE public."user" ADD preferences jsonb DEFAULT '{}' NOT NULL;

View file

@ -73,9 +73,10 @@ button {
1fr; 1fr;
} }
&.page-history { /* The other pages just gets the whole page without dividing it up. */
&:not(.page-node) {
grid-template-areas: grid-template-areas:
"tree-expander tree pad1 n2-pagehistory pad2" "tree-expander tree pad1 n2-page pad2"
; ;
grid-template-columns: grid-template-columns:
@ -245,7 +246,6 @@ button {
#notes2 { #notes2 {
&.page-node { &.page-node {
#page-root { #page-root {
display: none; display: none;
} }
@ -260,7 +260,7 @@ button {
display: contents; display: contents;
n2-pagestorage { n2-pagestorage {
grid-area: content; grid-area: n2-page;
} }
} }
} }
@ -268,9 +268,14 @@ button {
&.page-history { &.page-history {
#page-history { #page-history {
display: grid; display: grid;
grid-area: n2-pagehistory; grid-area: n2-page;
}
}
n2-pagehistory {} &.page-preferences {
#page-preferences {
display: grid !important;
grid-area: n2-page;
} }
} }
@ -282,7 +287,6 @@ button {
#page-root { #page-root {
display: contents !important; display: contents !important;
} }
} }
} }

View file

@ -2,18 +2,18 @@
<!-- Created with Inkscape (http://www.inkscape.org/) --> <!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg <svg
width="26.666645" width="12"
height="24" height="23.999981"
viewBox="0 0 7.0555498 6.35" viewBox="0 0 3.1750001 6.349995"
version="1.1" version="1.1"
id="svg1" id="svg1"
inkscape:version="1.4.2 (ebf0e94, 2025-05-08)" inkscape:version="1.4.4 (dcaf3e7d9e, 2026-05-05)"
sodipodi:docname="icon_menu.svg" sodipodi:docname="icon_menu.svg"
xml:space="preserve"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"> xmlns:svg="http://www.w3.org/2000/svg"><sodipodi:namedview
<sodipodi:namedview
id="namedview1" id="namedview1"
pagecolor="#ffffff" pagecolor="#ffffff"
bordercolor="#000000" bordercolor="#000000"
@ -23,29 +23,34 @@
inkscape:pagecheckerboard="0" inkscape:pagecheckerboard="0"
inkscape:deskcolor="#d1d1d1" inkscape:deskcolor="#d1d1d1"
inkscape:document-units="px" inkscape:document-units="px"
inkscape:zoom="2.096401" inkscape:zoom="11.859035"
inkscape:cx="10.255672" inkscape:cx="8.6010372"
inkscape:cy="9.0631517" inkscape:cy="17.32856"
inkscape:window-width="1916" inkscape:window-width="2190"
inkscape:window-height="1041" inkscape:window-height="1401"
inkscape:window-x="1920" inkscape:window-x="1463"
inkscape:window-y="1080" inkscape:window-y="18"
inkscape:window-maximized="1" inkscape:window-maximized="1"
inkscape:current-layer="layer1" /> inkscape:current-layer="layer1" /><defs
<defs id="defs1" /><g
id="defs1" />
<g
inkscape:label="Layer 1" inkscape:label="Layer 1"
inkscape:groupmode="layer" inkscape:groupmode="layer"
id="layer1" id="layer1"
transform="translate(-146.57917,-92.339583)"> transform="translate(-147.15925,-92.339586)"><title
<title id="title1">menu</title><title
id="title1">menu</title> id="title1-6">hamburger</title><circle
<title style="fill:#000000;stroke:none;stroke-width:0.264583"
id="title1-6">hamburger</title> id="path3"
<path cx="149.55338"
d="m 153.63472,95.867362 c 0,0.391582 -0.31398,0.705554 -0.70555,0.705554 h -5.64445 c -0.38806,0 -0.70555,-0.313972 -0.70555,-0.705554 0,-0.391584 0.31749,-0.705556 0.70555,-0.705556 h 3.175 l 0.88194,0.705556 0.88195,-0.705556 h 0.70556 c 0.39157,0 0.70555,0.3175 0.70555,0.705556 m -3.52778,-3.527779 c -3.175,0 -3.175,2.116667 -3.175,2.116667 h 6.35 c 0,0 0,-2.116667 -3.175,-2.116667 m -3.175,5.291667 c 0,0.585612 0.47272,1.058333 1.05834,1.058333 h 4.23333 c 0.58561,0 1.05833,-0.472721 1.05833,-1.058333 v -0.352778 h -6.35 z" cy="93.120461"
id="path1-2" r="0.78087437" /><circle
style="stroke-width:0.352777" /> style="fill:#000000;stroke:none;stroke-width:0.264583"
</g> id="circle4"
</svg> cx="149.55338"
cy="97.908707"
r="0.78087437" /><circle
style="fill:#000000;stroke:none;stroke-width:0.264583"
id="circle5"
cx="149.55338"
cy="95.514587"
r="0.78087437" /></g></svg>

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

Before After
Before After

View file

@ -1,7 +1,7 @@
export class API { export class API {
// query resolves into the JSON data produced by the application, or an exception with 'type' and 'error' properties. // query resolves into the JSON data produced by the application, or an exception with 'type' and 'error' properties.
static async query(method, path, request) { static async query(method, path, request) {
return new Promise((resolve, reject) => { try {
const body = JSON.stringify(request) const body = JSON.stringify(request)
const headers = {} const headers = {}
@ -12,33 +12,22 @@ export class API {
headers.Authorization = `Bearer ${token}` headers.Authorization = `Bearer ${token}`
} }
fetch(path, { method, headers, body }) const res = await fetch(path, { method, headers, body })
.then(response => { // An HTTP communication level error occured.
// An HTTP communication level error occured. if (!res.ok || res.status != 200)
if (!response.ok || response.status != 200) throw new Error('HTTP error', { cause: { type: 'http', error: res, }})
return reject({
type: 'http', // Application level response are handled here.
error: response, const json = await res.json()
}) if (!json.OK)
return response.json() throw new Error(json.Error, { cause: { type: 'application', application: json, }})
})
.then(json => { return json
// Application level response are handled here.
if (!json.OK) } catch (err) {
return reject({ // Catch any other errors from fetch.
type: 'application', throw new Error(err.message, { cause: { type: 'http', error: err, }})
error: json.Error, }
application: json,
})
resolve(json)
})
.catch(err =>
// Catch any other errors from fetch.
reject({
type: 'http',
error: err,
}))
})
} }
static hasAuthenticationToken() {//{{{ static hasAuthenticationToken() {//{{{

View file

@ -0,0 +1,32 @@
import { CustomHTMLElement } from "./lib/custom_html_element.mjs"
import { API } from './api.mjs'
export class N2PagePreferences extends CustomHTMLElement {
static {// {{{
this.tmpl = document.createElement('template')
this.tmpl.innerHTML = `
<h1>Preferences</h1>
`
}// }}}
constructor() {// {{{
super()
window._mbus.subscribe('SHOW_PAGE', event => {
if (event.detail.data?.page == 'preferences')
this.render()
})
}// }}}
async render() {// {{{
}// }}}
getPreferences() {
API.query('GET', '/user/preferences')
}
}
customElements.define('n2-pagepreferences', N2PagePreferences)
// Preferences is a set of preferences, of which there can be many named.
class Preferences {
constructor(name, data) {
this.name = name
this.data = data
}
}

View file

@ -13,7 +13,10 @@ export class N2PageStorage extends CustomHTMLElement {
constructor() { constructor() {
super() super()
window._mbus.subscribe('SHOW_PAGE', () => this.render()) window._mbus.subscribe('SHOW_PAGE', event => {
if (event.detail.data?.page == 'storage')
this.render()
})
} }
async render() { async render() {
const countNodes = await globalThis.nodeStore.nodeCount() const countNodes = await globalThis.nodeStore.nodeCount()

View file

@ -128,6 +128,7 @@ export class N2Sidebar extends CustomHTMLElement {
this.elSearch.addEventListener('click', () => _mbus.dispatch('op-search')) this.elSearch.addEventListener('click', () => _mbus.dispatch('op-search'))
this.elSync.addEventListener('click', () => _sync.run()) this.elSync.addEventListener('click', () => _sync.run())
this.elLogo.addEventListener('click', () => _app.goToNode(ROOT_NODE, false, false)) this.elLogo.addEventListener('click', () => _app.goToNode(ROOT_NODE, false, false))
this.elSettings.addEventListener('click', ()=> _mbus.dispatch('SHOW_PAGE', { page: 'preferences' }))
this.elHideTree.addEventListener('click', event => { this.elHideTree.addEventListener('click', event => {
event.stopPropagation() event.stopPropagation()
_mbus.dispatch('TREE_EXPANSION', { expand: false }) _mbus.dispatch('TREE_EXPANSION', { expand: false })

View file

@ -90,6 +90,7 @@ export class Sync {
nodeStore.setAppState('latest_sync_node', currMax) nodeStore.setAppState('latest_sync_node', currMax)
} catch (e) { } catch (e) {
console.error('sync node tree', e) console.error('sync node tree', e)
alert(e.message)
} finally { } finally {
syncEnd = Date.now() syncEnd = Date.now()
const duration = (syncEnd - syncStart) / 1000 const duration = (syncEnd - syncStart) / 1000
@ -157,8 +158,8 @@ export class Sync {
_mbus.dispatch('SYNC_UPLOADED', { count: nodesToSend.length }) _mbus.dispatch('SYNC_UPLOADED', { count: nodesToSend.length })
} catch (e) { } catch (e) {
console.trace(e) console.error(e)
alert(e.error) alert(e.message)
return return
} }
} }

27
user.go
View file

@ -1,27 +0,0 @@
package main
import (
// External
"github.com/golang-jwt/jwt/v5"
)
type UserSession struct {
UserID int
Username string
Password string
Name string
ClientUUID string
}
func NewUser(claims jwt.MapClaims) (u UserSession) {
uid, _ := claims["uid"].(float64)
name, _ := claims["name"].(string)
username, _ := claims["login"].(string)
clientUUID, _ := claims["cid"].(string)
u.UserID = int(uid)
u.Username = username
u.Name = name
u.ClientUUID = clientUUID
return
}

57
user/pkg.go Normal file
View file

@ -0,0 +1,57 @@
package user
import (
// External
"github.com/golang-jwt/jwt/v5"
"github.com/jmoiron/sqlx"
// Standard
"encoding/json"
)
type User struct {
ID int
Username string
Name string
Preferences map[string]UserPreferences
}
type UserSession struct {
UserID int
Username string
Password string
Name string
ClientUUID string
Db *sqlx.DB
}
type UserPreferences struct {
DownloadImages bool
DownloadFiles bool
}
func NewUser(claims jwt.MapClaims) (u UserSession) {
uid, _ := claims["uid"].(float64)
name, _ := claims["name"].(string)
username, _ := claims["login"].(string)
clientUUID, _ := claims["cid"].(string)
u.UserID = int(uid)
u.Username = username
u.Name = name
u.ClientUUID = clientUUID
return
}
func (u UserSession) Preferences() (prefs map[string]UserPreferences, err error) {
row := u.Db.QueryRow(`SELECT preferences FROM public.user WHERE id=$1`, u.UserID)
var data []byte
err = row.Scan(&data)
if err != nil {
return
}
err = json.Unmarshal(data, &prefs)
return
}

View file

@ -32,9 +32,10 @@
</div> </div>
<!-- History --> <!-- History -->
<div id="page-history"> <n2-pagehistory id="page-history"></n2-pagehistory>
<n2-pagehistory></n2-pagehistory>
</div> <!-- Preferences -->
<n2-pagepreferences id="page-preferences"></n2-pagepreferences>
</div> </div>
<n2-syncprogress></n2-syncprogress> <n2-syncprogress></n2-syncprogress>
@ -46,6 +47,7 @@
import {App} from "/js/{{ .VERSION }}/app.mjs" import {App} from "/js/{{ .VERSION }}/app.mjs"
import {API} from 'api' import {API} from 'api'
import {Sync} from 'sync' import {Sync} from 'sync'
import { } from '/js/{{ .VERSION }}/page_preferences.mjs'
import { } from '/js/{{ .VERSION }}/page_storage.mjs' import { } from '/js/{{ .VERSION }}/page_storage.mjs'
import { } from '/js/{{ .VERSION }}/page_history.mjs' import { } from '/js/{{ .VERSION }}/page_history.mjs'
import { } from '/js/{{ .VERSION }}/file.mjs' import { } from '/js/{{ .VERSION }}/file.mjs'