HEAVY: migrate accounts and logs to service/repo architecture

This commit is contained in:
ari melody 2026-07-31 02:43:45 +01:00
parent 5c255fb34b
commit 49e14b5bc5
Signed by: ari
GPG key ID: CF99829C92678188
37 changed files with 1019 additions and 687 deletions

View file

@ -1,21 +1,21 @@
package controller
import (
"database/sql"
"fmt"
"net/http"
"strings"
"time"
"database/sql"
"fmt"
"net/http"
"strings"
"time"
"arimelody-web/log"
"arimelody-web/model"
"arimelody-web/model"
"arimelody-web/model/app"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx"
)
const TOKEN_LEN = 64
func GetSessionFromRequest(app *model.AppState, r *http.Request) (*model.Session, error) {
func GetSessionFromRequest(app *app.AppState, r *http.Request) (*model.Session, error) {
sessionCookie, err := r.Cookie(model.COOKIE_TOKEN)
if err != nil && err != http.ErrNoCookie {
return nil, fmt.Errorf("Failed to retrieve session cookie: %v", err)
@ -25,7 +25,7 @@ func GetSessionFromRequest(app *model.AppState, r *http.Request) (*model.Session
if sessionCookie != nil {
// fetch existing session
session, err = GetSession(app.DB, sessionCookie.Value)
session, err = GetSession(app, sessionCookie.Value)
if err != nil && !strings.Contains(err.Error(), "no rows") {
return nil, fmt.Errorf("Failed to retrieve session: %v", err)
@ -35,13 +35,13 @@ func GetSessionFromRequest(app *model.AppState, r *http.Request) (*model.Session
if session.UserAgent != r.UserAgent() {
msg := "Session user agent mismatch. A cookie may have been hijacked!"
if session.Account != nil {
account, _ := GetAccountByID(app.DB, session.Account.ID)
account, _ := app.AccountService.GetByID(session.Account.ID)
msg += " (Account \"" + account.Username + "\")"
}
app.Log.Warn(log.TYPE_ACCOUNT, msg)
app.Log.Warn(model.LOG_ACCOUNT, msg)
err = DeleteSession(app.DB, session.Token)
if err != nil {
app.Log.Warn(log.TYPE_ACCOUNT, "Failed to delete affected session")
app.Log.Warn(model.LOG_ACCOUNT, "Failed to delete affected session")
}
return nil, nil
}
@ -137,7 +137,7 @@ func SetSessionError(db *sqlx.DB, session *model.Session, message string) error
return err
}
func GetSession(db *sqlx.DB, token string) (*model.Session, error) {
func GetSession(app *app.AppState, token string) (*model.Session, error) {
type dbSession struct {
model.Session
AttemptAccountID sql.NullString `db:"attempt_account"`
@ -145,7 +145,7 @@ func GetSession(db *sqlx.DB, token string) (*model.Session, error) {
}
session := dbSession{}
err := db.Get(
err := app.DB.Get(
&session,
"SELECT * FROM session WHERE token=$1",
token,
@ -155,14 +155,14 @@ func GetSession(db *sqlx.DB, token string) (*model.Session, error) {
}
if session.AccountID.Valid {
session.Account, err = GetAccountByID(db, session.AccountID.String)
session.Account, err = app.AccountService.GetByID(session.AccountID.String)
if err != nil {
return nil, err
}
}
if session.AttemptAccountID.Valid {
session.AttemptAccount, err = GetAccountByID(db, session.AttemptAccountID.String)
session.AttemptAccount, err = app.AccountService.GetByID(session.AttemptAccountID.String)
if err != nil {
return nil, err
}