-
-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
337 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cache | ||
|
||
import "time" | ||
|
||
// SetAnswerID 设置用户获得答案的ID | ||
func (s *UserService) SetAnswerID(userId, chattitle string, current uint) { | ||
s.cache.Set(userId+"_"+chattitle, current, time.Hour*24) | ||
} | ||
|
||
// GetAnswerID 获取当前用户获得答案的ID | ||
func (s *UserService) GetAnswerID(userId, chattitle string) uint { | ||
sessionContext, ok := s.cache.Get(userId + "_" + chattitle) | ||
if !ok { | ||
return 0 | ||
} | ||
return sessionContext.(uint) | ||
} | ||
|
||
// ClearUserSessionContext 清空GTP上下文,接收文本中包含 SessionClearToken | ||
func (s *UserService) ClearAnswerID(userId, chattitle string) { | ||
s.cache.Delete(userId + "_" + chattitle) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package db | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type ChatType uint | ||
|
||
const Q ChatType = 1 | ||
const A ChatType = 2 | ||
|
||
type Chat struct { | ||
gorm.Model | ||
Username string `gorm:"type:varchar(50);not null;comment:'用户名'" json:"username"` // 用户名 | ||
Source string `gorm:"type:varchar(50);comment:'用户来源:群聊名字,私聊'" json:"source"` // 对话来源 | ||
ChatType ChatType `gorm:"type:tinyint(1);default:1;comment:'类型:1问, 2答'" json:"chat_type"` // 状态 | ||
ParentContent uint `gorm:"default:0;comment:'父消息编号(编号为0时表示为首条)'" json:"parent_content"` | ||
Content string `gorm:"type:varchar(128);comment:'内容'" json:"content"` // 问题或回答的内容 | ||
} | ||
|
||
// 需要考虑下如何处理一个完整对话的问题 | ||
// 如果是单聊,那么就记录上下两句就好了 | ||
// 如果是串聊,则需要知道哪条是第一条,并依次往下记录 | ||
|
||
// Add 添加资源 | ||
func (c Chat) Add() (uint, error) { | ||
err := DB.Create(&c).Error | ||
return c.ID, err | ||
} | ||
|
||
// Find 获取单个资源 | ||
func (c Chat) Find(filter map[string]interface{}, data *Chat) error { | ||
return DB.Where(filter).First(&data).Error | ||
} | ||
|
||
type ChatListReq struct { | ||
Username string `json:"username" form:"username"` | ||
Source string `json:"source" form:"source"` | ||
} | ||
|
||
// List 获取数据列表 | ||
func (c Chat) List(req ChatListReq) ([]*Chat, error) { | ||
var list []*Chat | ||
db := DB.Model(&Chat{}).Order("created_at ASC") | ||
|
||
userName := strings.TrimSpace(req.Username) | ||
if userName != "" { | ||
db = db.Where("username LIKE ?", fmt.Sprintf("%%%s%%", userName)) | ||
} | ||
source := strings.TrimSpace(req.Source) | ||
if source != "" { | ||
db = db.Where("source LIKE ?", fmt.Sprintf("%%%s%%", source)) | ||
} | ||
|
||
err := db.Find(&list).Error | ||
return list, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package db | ||
|
||
import ( | ||
"github.com/eryajf/chatgpt-dingtalk/pkg/logger" | ||
"github.com/glebarez/sqlite" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// 全局数据库对象 | ||
var DB *gorm.DB | ||
|
||
// 初始化数据库 | ||
func InitDB() { | ||
DB = ConnSqlite() | ||
|
||
dbAutoMigrate() | ||
} | ||
|
||
// 自动迁移表结构 | ||
func dbAutoMigrate() { | ||
_ = DB.AutoMigrate( | ||
Chat{}, | ||
) | ||
} | ||
|
||
func ConnSqlite() *gorm.DB { | ||
db, err := gorm.Open(sqlite.Open("data/dingtalkbot.sqlite"), &gorm.Config{ | ||
// 禁用外键(指定外键时不会在mysql创建真实的外键约束) | ||
DisableForeignKeyConstraintWhenMigrating: true, | ||
}) | ||
if err != nil { | ||
logger.Fatal("failed to connect sqlite3: %v", err) | ||
} | ||
dbObj, err := db.DB() | ||
if err != nil { | ||
logger.Fatal("failed to get sqlite3 obj: %v", err) | ||
} | ||
// 参见: https://github.com/glebarez/sqlite/issues/52 | ||
dbObj.SetMaxOpenConns(1) | ||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.