refactoring, fixes and laniakea v0.8

This commit is contained in:
2026-02-19 14:02:25 +03:00
parent 0804398b6c
commit c9a5a81643
30 changed files with 219 additions and 190 deletions

15
database/ctx.go Normal file
View File

@@ -0,0 +1,15 @@
package database
import (
"git.nix13.pw/scuroneko/laniakea"
"github.com/redis/go-redis/v9"
"github.com/vinovest/sqlx"
"go.mongodb.org/mongo-driver/v2/mongo"
)
type Context struct {
laniakea.DbContext
Postgres *sqlx.DB
Mongo *mongo.Client
Redis *redis.Client
}

View File

@@ -2,14 +2,13 @@ package mdb
import (
"context"
"kurumibot/database"
"time"
"ymgb/database"
"git.nix13.pw/scuroneko/laniakea"
"go.mongodb.org/mongo-driver/v2/bson"
)
func GetGptChatHistory(db *laniakea.DatabaseContext, chatId string) ([]AiChatMessage, error) {
func GetGptChatHistory(db *database.Context, chatId string) ([]AiChatMessage, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "gpt_chat_messages")
@@ -21,7 +20,7 @@ func GetGptChatHistory(db *laniakea.DatabaseContext, chatId string) ([]AiChatMes
err = cursor.All(ctx, &result)
return result, err
}
func UpdateGptChatHistory(db *laniakea.DatabaseContext, chatId, role, message string) error {
func UpdateGptChatHistory(db *database.Context, chatId, role, message string) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "gpt_chat_messages")

View File

@@ -2,10 +2,9 @@ package mdb
import (
"context"
"kurumibot/database"
"time"
"ymgb/database"
"git.nix13.pw/scuroneko/laniakea"
"go.mongodb.org/mongo-driver/v2/bson"
)
@@ -22,7 +21,7 @@ type CodeEntry struct {
TotalUsages uint `bson:"totalUsages"`
}
func FindCode(db *laniakea.DatabaseContext, code string) (CodeEntry, error) {
func FindCode(db *database.Context, code string) (CodeEntry, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := database.GetMongoCollection(db, "codes")

View File

@@ -2,11 +2,10 @@ package mdb
import (
"context"
"kurumibot/database"
"time"
"ymgb/database"
"git.nix13.pw/scuroneko/extypes"
"git.nix13.pw/scuroneko/laniakea"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
@@ -20,14 +19,14 @@ type ConsoleLogEntry struct {
TimeStamp int64 `bson:"timeStamp" json:"time_stamp"`
}
func WriteConsoleLog(db *laniakea.DatabaseContext, e ConsoleLogEntry) error {
func WriteConsoleLog(db *database.Context, e ConsoleLogEntry) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "logs")
_, err := col.InsertOne(ctx, e)
return err
}
func GetConsoleLogs(db *laniakea.DatabaseContext) ([]ConsoleLogEntry, error) {
func GetConsoleLogs(db *database.Context) ([]ConsoleLogEntry, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "logs")
@@ -52,14 +51,14 @@ type MessageLogEntry struct {
TimeStamp int64 `bson:"timestamp" json:"timestamp"`
}
func WriteMessageLog(db *laniakea.DatabaseContext, e *MessageLogEntry) error {
func WriteMessageLog(db *database.Context, e *MessageLogEntry) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "msg_logs")
_, err := col.InsertOne(ctx, e)
return err
}
func GetMessageLogs(db *laniakea.DatabaseContext) (extypes.Slice[*MessageLogEntry], error) {
func GetMessageLogs(db *database.Context) (extypes.Slice[*MessageLogEntry], error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "msg_logs")

View File

@@ -2,10 +2,9 @@ package mdb
import (
"context"
"kurumibot/database"
"time"
"ymgb/database"
"git.nix13.pw/scuroneko/laniakea"
"go.mongodb.org/mongo-driver/v2/bson"
)
@@ -17,7 +16,7 @@ type AiChatMessage struct {
Index int `bson:"index"`
}
func GetRPChatHistory(db *laniakea.DatabaseContext, chatId string) ([]AiChatMessage, error) {
func GetRPChatHistory(db *database.Context, chatId string) ([]AiChatMessage, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "rp_chat_messages")
@@ -29,7 +28,7 @@ func GetRPChatHistory(db *laniakea.DatabaseContext, chatId string) ([]AiChatMess
err = cursor.All(ctx, &result)
return result, err
}
func UpdateRPChatHistory(db *laniakea.DatabaseContext, chatId, role, message string, index int) error {
func UpdateRPChatHistory(db *database.Context, chatId, role, message string, index int) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "rp_chat_messages")
@@ -41,13 +40,13 @@ func UpdateRPChatHistory(db *laniakea.DatabaseContext, chatId, role, message str
})
return err
}
func GetRPChatHistorySize(db *laniakea.DatabaseContext, chatId string) (int64, error) {
func GetRPChatHistorySize(db *database.Context, chatId string) (int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "rp_chat_messages")
return col.CountDocuments(ctx, bson.M{"chatId": chatId})
}
func DeleteRPChatEntry(db *laniakea.DatabaseContext, entry AiChatMessage) error {
func DeleteRPChatEntry(db *database.Context, entry AiChatMessage) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
col := database.GetMongoCollection(db, "rp_chat_messages")

View File

@@ -5,7 +5,6 @@ import (
"log"
"os"
"git.nix13.pw/scuroneko/laniakea"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
@@ -32,6 +31,6 @@ func ConnectMongo() {
}
}
func GetMongoCollection(db *laniakea.DatabaseContext, name string) *mongo.Collection {
return db.MongoDB.Database(os.Getenv("MONGO_NAME")).Collection(name)
func GetMongoCollection(db *Context, name string) *mongo.Collection {
return db.Mongo.Database(os.Getenv("MONGO_NAME")).Collection(name)
}

View File

@@ -1,7 +1,8 @@
package psql
import (
"git.nix13.pw/scuroneko/laniakea"
"ymgb/database"
"github.com/vinovest/sqlx"
)
@@ -19,8 +20,8 @@ type AIRepository struct {
func newAiRepository(db *sqlx.DB) AIRepository {
return AIRepository{db}
}
func NewAIRepository(db *laniakea.DatabaseContext) AIRepository {
return newAiRepository(db.PostgresSQL)
func NewAIRepository(db *database.Context) AIRepository {
return newAiRepository(db.Postgres)
}
func (rep AIRepository) GetModel(id string) (AIModel, error) {

View File

@@ -1,7 +1,8 @@
package psql
import (
"git.nix13.pw/scuroneko/laniakea"
"ymgb/database"
"github.com/shopspring/decimal"
"github.com/vinovest/sqlx"
)
@@ -22,8 +23,8 @@ type FractionRepository struct {
func newFractionRepository(db *sqlx.DB) FractionRepository {
return FractionRepository{db}
}
func NewFractionRepository(db *laniakea.DatabaseContext) FractionRepository {
return newFractionRepository(db.PostgresSQL)
func NewFractionRepository(db *database.Context) FractionRepository {
return newFractionRepository(db.Postgres)
}
func (rep FractionRepository) GetAll() ([]Fraction, error) {

View File

@@ -1,7 +1,8 @@
package psql
import (
"git.nix13.pw/scuroneko/laniakea"
"ymgb/database"
"github.com/shopspring/decimal"
"github.com/vinovest/sqlx"
)
@@ -25,8 +26,8 @@ type GroupRepository struct {
func newGroupRepository(db *sqlx.DB) GroupRepository {
return GroupRepository{db}
}
func NewGroupRepository(db *laniakea.DatabaseContext) GroupRepository {
return newGroupRepository(db.PostgresSQL)
func NewGroupRepository(db *database.Context) GroupRepository {
return newGroupRepository(db.Postgres)
}
func (rep GroupRepository) GetAll() ([]Group, error) {

View File

@@ -3,8 +3,8 @@ package psql
import (
"database/sql"
"errors"
"ymgb/database"
"git.nix13.pw/scuroneko/laniakea"
"github.com/vinovest/sqlx"
)
@@ -56,8 +56,8 @@ type RPRepository struct {
func newRpRepository(db *sqlx.DB) RPRepository {
return RPRepository{db}
}
func NewRPRepository(db *laniakea.DatabaseContext) RPRepository {
return newRpRepository(db.PostgresSQL)
func NewRPRepository(db *database.Context) RPRepository {
return newRpRepository(db.Postgres)
}
func (rep RPRepository) GetOrCreateUser(id int64) (RPUser, error) {

View File

@@ -1,7 +1,8 @@
package psql
import (
"git.nix13.pw/scuroneko/laniakea"
"ymgb/database"
"github.com/shopspring/decimal"
"github.com/vinovest/sqlx"
)
@@ -37,10 +38,10 @@ type ShopRepository struct {
db *sqlx.DB
}
func NewShopRepository(db *laniakea.DatabaseContext) ShopRepository {
return newShopRepository(db.PostgresSQL)
}
func newShopRepository(db *sqlx.DB) ShopRepository { return ShopRepository{db} }
func NewShopRepository(db *database.Context) ShopRepository {
return newShopRepository(db.Postgres)
}
func (rep ShopRepository) GetAllAuto() ([]ShopAuto, error) {
auto := make([]ShopAuto, 0)

View File

@@ -6,8 +6,8 @@ import (
"errors"
"math"
"time"
"ymgb/database"
"git.nix13.pw/scuroneko/laniakea"
"github.com/shopspring/decimal"
"github.com/vinovest/sqlx"
)
@@ -56,8 +56,8 @@ type UserRepository struct {
}
func newUserRepository(db *sqlx.DB) UserRepository { return UserRepository{db} }
func NewUserRepository(db *laniakea.DatabaseContext) UserRepository {
return newUserRepository(db.PostgresSQL)
func NewUserRepository(db *database.Context) UserRepository {
return newUserRepository(db.Postgres)
}
func (rep UserRepository) GetOrCreate(tgId int, name string) (*User, error) {

View File

@@ -2,8 +2,8 @@ package psql
import (
"database/sql"
"ymgb/database"
"git.nix13.pw/scuroneko/laniakea"
"github.com/shopspring/decimal"
"github.com/vinovest/sqlx"
)
@@ -26,8 +26,9 @@ type WaifuRepository struct {
db *sqlx.DB
}
func NewWaifuRepository(db *laniakea.DatabaseContext) *WaifuRepository {
return &WaifuRepository{db: db.PostgresSQL}
func newWaifuRepository(db *sqlx.DB) *WaifuRepository { return &WaifuRepository{db} }
func NewWaifuRepository(db *database.Context) *WaifuRepository {
return newWaifuRepository(db.Postgres)
}
func (rep *WaifuRepository) GetAll() ([]*Waifu, error) {

View File

@@ -1,7 +1,8 @@
package psql
import (
"git.nix13.pw/scuroneko/laniakea"
"ymgb/database"
"github.com/shopspring/decimal"
"github.com/vinovest/sqlx"
)
@@ -22,8 +23,8 @@ type WorkRepository struct {
func newWorkRepository(db *sqlx.DB) WorkRepository {
return WorkRepository{db}
}
func NewWorkRepository(db *laniakea.DatabaseContext) WorkRepository {
return newWorkRepository(db.PostgresSQL)
func NewWorkRepository(db *database.Context) WorkRepository {
return newWorkRepository(db.Postgres)
}
func (rep WorkRepository) GetById(id int) (Work, error) {

View File

@@ -2,8 +2,8 @@ package red
import (
"fmt"
"ymgb/database"
"git.nix13.pw/scuroneko/laniakea"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
)
@@ -12,7 +12,7 @@ type AiRepository struct {
client *redis.Client
}
func NewAiRepository(db *laniakea.DatabaseContext) AiRepository {
func NewAiRepository(db *database.Context) AiRepository {
return AiRepository{client: db.Redis}
}

View File

@@ -5,11 +5,11 @@ import (
"database/sql"
"errors"
"fmt"
"kurumibot/database/psql"
"kurumibot/utils"
"strings"
"ymgb/database"
"ymgb/database/psql"
"ymgb/utils"
"git.nix13.pw/scuroneko/laniakea"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
)
@@ -18,7 +18,7 @@ var ctx = context.Background()
type RPRepository struct {
client *redis.Client
db *laniakea.DatabaseContext
db *database.Context
}
type RPChat struct {
@@ -35,7 +35,7 @@ type RPChat struct {
Scenarios []psql.RPScenario
}
func NewRPRepository(db *laniakea.DatabaseContext) RPRepository {
func NewRPRepository(db *database.Context) RPRepository {
return RPRepository{db.Redis, db}
}