Skip to content

Commit

Permalink
Merge branch 'master' into refactor_api
Browse files Browse the repository at this point in the history
  • Loading branch information
PaienNate authored Dec 31, 2024
2 parents 04b9ca5 + c81303f commit 9178976
Show file tree
Hide file tree
Showing 97 changed files with 4,518 additions and 2,473 deletions.
8 changes: 8 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# DB_TYPE=mysql
# DB_DSN="root:root@tcp(127.0.0.1:3306)/nokoti?charset=utf8mb4&parseTime=True&loc=Local"

# DB_TYPE=postgres
# DB_DSN="postgres://postgres:pinenut666@localhost:5432/nokoti?sslmode=disable"
# SQLITE IS DIFFERENT
DB_TYPE=sqlite
# DATA_DIR="./data/default"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ _help_cache

.vscode/
!.vscode/settings.json
!.vscode/extensions.json
sealdice-lock.lock
6 changes: 5 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ linters-settings:
# Default: ["200", "400", "404", "500"]
http-status-code-whitelist: ["200", "400", "404", "500"]

forbidigo:
exclude-godoc-examples: true
analyze-types: true

linters:
disable-all: true
enable:
Expand All @@ -323,7 +327,7 @@ linters:
- errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
- exhaustive # checks exhaustiveness of enum switch statements
- fatcontext # detects nested contexts in loops
# - forbidigo # forbids identifiers
- forbidigo # forbids identifiers
# - funlen # tool for detection of long functions
- gocheckcompilerdirectives # validates go compiler directive comments (//go:)
# - gochecknoglobals # checks that no global variables exist
Expand Down
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["golang.go", "task.vscode-task"]
}
36 changes: 36 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# https://taskfile.dev

version: '3'

tasks:
install:
cmds:
- go mod download
- go install github.com/golangci/golangci-lint/cmd/[email protected]
- go install golang.org/x/tools/cmd/goimports@latest
- go install github.com/pointlander/[email protected]
- go generate ./...

run:
deps: ['test-and-lint']
cmds:
- go run .
build:
deps: ['test-and-lint']
cmds:
- task: build-only

test-and-lint:
deps: ['test', 'lint']
test:
cmds:
- go test ./...
- go vet ./...
lint:
cmds:
- goimports -w .
- golangci-lint run

build-only:
cmds:
- go build .
18 changes: 15 additions & 3 deletions api/api_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ func forceStop(c echo.Context) error {
dbData := d.DBData
if dbData != nil {
d.DBData = nil
_ = dbData.Close()
db, err := dbData.DB()
if err != nil {
return
}
_ = db.Close()
}
})()

Expand All @@ -201,7 +205,11 @@ func forceStop(c echo.Context) error {
dbLogs := d.DBLogs
if dbLogs != nil {
d.DBLogs = nil
_ = dbLogs.Close()
db, err := dbLogs.DB()
if err != nil {
return
}
_ = db.Close()
}
})()

Expand All @@ -213,7 +221,11 @@ func forceStop(c echo.Context) error {
if cm != nil && cm.DB != nil {
dbCensor := cm.DB
cm.DB = nil
_ = dbCensor.Close()
db, err := dbCensor.DB()
if err != nil {
return
}
_ = db.Close()
}
})()
}
Expand Down
17 changes: 12 additions & 5 deletions api/censor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"bufio"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
Expand All @@ -20,6 +19,7 @@ import (
"sealdice-core/dice"
"sealdice-core/dice/censor"
"sealdice-core/dice/model"
log "sealdice-core/utils/kratos"
)

func check(c echo.Context) (bool, error) {
Expand Down Expand Up @@ -64,7 +64,14 @@ func censorStop(c echo.Context) error {

(&myDice.Config).EnableCensor = false
myDice.MarkModified()
_ = myDice.CensorManager.DB.Close()
db, err2 := myDice.CensorManager.DB.DB()
if err2 != nil {
return Error(&c, "关闭拦截引擎失败", Response{})
}
err = db.Close()
if err != nil {
return err
}
myDice.CensorManager = nil

return Success(&c, Response{})
Expand Down Expand Up @@ -152,7 +159,7 @@ func censorSetConfig(c echo.Context) error {
jsonMap := make(map[string]interface{})
err = json.NewDecoder(c.Request().Body).Decode(&jsonMap)
if err != nil {
fmt.Println(err)
log.Error("censorSetConfig", err)
return c.JSON(http.StatusInternalServerError, err)
}

Expand Down Expand Up @@ -438,7 +445,7 @@ func censorDeleteWordFiles(c echo.Context) error {
}{}
err = c.Bind(&v)
if err != nil {
fmt.Println(err)
log.Error("censorDeleteWordFiles", err)
return c.JSON(http.StatusInternalServerError, err)
}

Expand Down Expand Up @@ -513,7 +520,7 @@ func censorGetLogPage(c echo.Context) error {
v := model.QueryCensorLog{}
err = c.Bind(&v)
if err != nil {
fmt.Println(err)
log.Error("censorGetLogPage", err)
return c.JSON(http.StatusInternalServerError, err)
}
if v.PageNum < 1 {
Expand Down
11 changes: 6 additions & 5 deletions api/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"encoding/base64"
"fmt"
"net/http"
"sort"
"strconv"
Expand All @@ -11,6 +10,7 @@ import (
"github.com/labstack/echo/v4"

"sealdice-core/dice"
log "sealdice-core/utils/kratos"
)

func ImConnections(c echo.Context) error {
Expand Down Expand Up @@ -165,7 +165,7 @@ func ImConnectionsRWSignServerUrl(c echo.Context) error {
}
if i.ProtocolType == "onebot" {
pa := i.Adapter.(*dice.PlatformAdapterGocq)
if pa.BuiltinMode == "lagrange" {
if pa.BuiltinMode == "lagrange" || pa.BuiltinMode == "lagrange-gocq" {
signServerUrl, signServerVersion := dice.RWLagrangeSignServerUrl(myDice, i, v.SignServerUrl, v.W, v.SignServerVersion)
if signServerUrl != "" {
return Success(&c, Response{
Expand Down Expand Up @@ -207,7 +207,7 @@ func ImConnectionsDel(c echo.Context) error {
myDice.ImSession.EndPoints = append(myDice.ImSession.EndPoints[:index], myDice.ImSession.EndPoints[index+1:]...)
if i.ProtocolType == "onebot" {
pa := i.Adapter.(*dice.PlatformAdapterGocq)
if pa.BuiltinMode == "lagrange" {
if pa.BuiltinMode == "lagrange" || pa.BuiltinMode == "lagrange-gocq" {
dice.BuiltinQQServeProcessKillBase(myDice, i, true)
// 经测试,若不延时,可能导致清理对应目录失败(原因:文件被占用)
time.Sleep(1 * time.Second)
Expand Down Expand Up @@ -437,7 +437,7 @@ func ImConnectionsGocqhttpRelogin(c echo.Context) error {
if err == nil {
for _, i := range myDice.ImSession.EndPoints {
if i.ID == v.ID {
fmt.Print("!!! relogin ", v.ID)
log.Warnf("relogin %s", v.ID)
i.Adapter.DoRelogin()
return c.JSON(http.StatusOK, nil)
}
Expand Down Expand Up @@ -969,6 +969,7 @@ func ImConnectionsAddBuiltinLagrange(c echo.Context) error {
Account string `yaml:"account" json:"account"`
SignServerUrl string `yaml:"signServerUrl" json:"signServerUrl"`
SignServerVersion string `yaml:"signServerVersion" json:"signServerVersion"`
IsGocq bool `yaml:"isGocq" json:"isGocq"`
}{}
err := c.Bind(&v)
if err == nil {
Expand All @@ -977,7 +978,7 @@ func ImConnectionsAddBuiltinLagrange(c echo.Context) error {
return nil
}

conn := dice.NewLagrangeConnectInfoItem(v.Account)
conn := dice.NewLagrangeConnectInfoItem(v.Account, v.IsGocq)
conn.UserID = dice.FormatDiceIDQQ(uid)
conn.Session = myDice.ImSession
pa := conn.Adapter.(*dice.PlatformAdapterGocq)
Expand Down
71 changes: 41 additions & 30 deletions api/deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,18 @@ func deckEnable(c echo.Context) error {
}

v := struct {
Index int `json:"index"`
Enable bool `json:"enable"`
Filename string `json:"filename"`
Enable bool `json:"enable"`
}{}
err := c.Bind(&v)

if err == nil {
if v.Index >= 0 && v.Index < len(myDice.DeckList) {
myDice.DeckList[v.Index].Enable = v.Enable
myDice.MarkModified()
for _, deck := range myDice.DeckList {
if deck.Filename == v.Filename {
deck.Enable = v.Enable
myDice.MarkModified()
break
}
}
}

Expand All @@ -118,14 +121,17 @@ func deckDelete(c echo.Context) error {
}

v := struct {
Index int `json:"index"`
Filename string `json:"filename"`
}{}
err := c.Bind(&v)

if err == nil {
if v.Index >= 0 && v.Index < len(myDice.DeckList) {
dice.DeckDelete(myDice, myDice.DeckList[v.Index])
myDice.MarkModified()
if err == nil && v.Filename != "" {
for _, deck := range myDice.DeckList {
if deck.Filename == v.Filename {
dice.DeckDelete(myDice, deck)
myDice.MarkModified()
break
}
}
}

Expand All @@ -140,23 +146,25 @@ func deckCheckUpdate(c echo.Context) error {
return Error(&c, "展示模式不支持该操作", Response{"testMode": true})
}
v := struct {
Index int `json:"index"`
Filename string `json:"filename"`
}{}
err := c.Bind(&v)

if err == nil {
if v.Index >= 0 && v.Index < len(myDice.DeckList) {
deck := myDice.DeckList[v.Index]
oldDeck, newDeck, tempFileName, err := myDice.DeckCheckUpdate(deck)
if err != nil {
return Error(&c, err.Error(), Response{})
if err == nil && v.Filename != "" {
for _, deck := range myDice.DeckList {
if deck.Filename == v.Filename {
oldDeck, newDeck, tempFileName, err := myDice.DeckCheckUpdate(deck)
if err != nil {
return Error(&c, err.Error(), Response{})
}
return Success(&c, Response{
"old": oldDeck,
"new": newDeck,
"format": deck.FileFormat,
"filename": deck.Filename,
"tempFileName": tempFileName,
})
}
return Success(&c, Response{
"old": oldDeck,
"new": newDeck,
"format": deck.FileFormat,
"tempFileName": tempFileName,
})
}
}
return Success(&c, Response{})
Expand All @@ -170,18 +178,21 @@ func deckUpdate(c echo.Context) error {
return Error(&c, "展示模式不支持该操作", Response{"testMode": true})
}
v := struct {
Index int `json:"index"`
Filename string `json:"filename"`
TempFileName string `json:"tempFileName"`
}{}
err := c.Bind(&v)

if err == nil {
if v.Index >= 0 && v.Index < len(myDice.DeckList) {
err := myDice.DeckUpdate(myDice.DeckList[v.Index], v.TempFileName)
if err != nil {
return Error(&c, err.Error(), Response{})
if err == nil && v.Filename != "" {
for _, deck := range myDice.DeckList {
if deck.Filename == v.Filename {
err := myDice.DeckUpdate(deck, v.TempFileName)
if err != nil {
return Error(&c, err.Error(), Response{})
}
myDice.MarkModified()
break
}
myDice.MarkModified()
}
}
return Success(&c, Response{})
Expand Down
4 changes: 2 additions & 2 deletions api/dice_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
Expand All @@ -13,6 +12,7 @@ import (

"sealdice-core/dice"
"sealdice-core/utils"
log "sealdice-core/utils/kratos"
)

type DiceConfigInfo struct {
Expand Down Expand Up @@ -109,7 +109,7 @@ func DiceConfigSet(c echo.Context) error {
}

if err != nil {
fmt.Println(err)
log.Error("DiceConfigSet", err)
return c.JSON(http.StatusOK, nil)
}
if val, ok := jsonMap["commandPrefix"]; ok {
Expand Down
Loading

0 comments on commit 9178976

Please sign in to comment.