-
Notifications
You must be signed in to change notification settings - Fork 13
/
database.go
86 lines (74 loc) · 1.7 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// archiver - database.go
// 2024-02-20 20:00
// Benny <[email protected]>
package main
import (
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
var db, _ = gorm.Open(sqlite.Open("archiver.db"), &gorm.Config{})
type User struct {
gorm.Model
UserID int64 `gorm:"index;unique"` // 假设Telegram的用户ID是整数
Mode string // AI模式和普通模式
Link string
Token string // Future use
}
type Chat struct {
gorm.Model
UserID int64 `gorm:"index"` // userID
Role string // 发送者角色:model 或 user
Text string // 保存的消息文本
}
func getUser(userID int64) *User {
var user User
db.Where("user_id = ?", userID).First(&user)
return &user
}
func enableAI(userID int64, link string, data string) *User {
user := getUser(userID)
user.Mode = modeAI
user.Link = link
if user.UserID == 0 {
// create new user
user.UserID = userID
db.Create(&user)
} else {
// update db
db.Save(&user)
}
// OCR the text
text := imageToText(link, data)
addChat(userID, systemRole, text)
return user
}
func disableAI(userId int64) {
// reset mode to normal, clear link
user := getUser(userId)
user.Mode = modeNormal
user.Link = ""
db.Save(&user)
}
func getChats(userID int64) []Chat {
var chats []Chat
db.Where("user_id = ?", userID).Find(&chats)
return chats
}
func getChatsCount(userID int64) int64 {
var count int64
db.Model(&Chat{}).Where("user_id = ?", userID).Count(&count)
return count
}
func addChat(userId int64, role, text string) {
chat := Chat{
UserID: userId,
Role: role,
Text: text,
}
db.Create(&chat)
}
func deleteChat(userID int64) int64 {
// delete according to userID
t := db.Where("user_id = ?", userID).Delete(&Chat{})
return t.RowsAffected
}