x
This commit is contained in:
@@ -1,25 +1,31 @@
|
||||
package wechatdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/sjzar/chatlog/pkg/model"
|
||||
"github.com/sjzar/chatlog/internal/model"
|
||||
"github.com/sjzar/chatlog/internal/wechatdb/datasource"
|
||||
"github.com/sjzar/chatlog/internal/wechatdb/repository"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
BasePath string
|
||||
Version int
|
||||
|
||||
contact *Contact
|
||||
message *Message
|
||||
path string
|
||||
platform string
|
||||
version int
|
||||
ds datasource.DataSource
|
||||
repo *repository.Repository
|
||||
}
|
||||
|
||||
func New(path string, version int) (*DB, error) {
|
||||
func New(path string, platform string, version int) (*DB, error) {
|
||||
|
||||
w := &DB{
|
||||
BasePath: path,
|
||||
Version: version,
|
||||
path: path,
|
||||
platform: platform,
|
||||
version: version,
|
||||
}
|
||||
|
||||
// 初始化,加载数据库文件信息
|
||||
@@ -31,87 +37,87 @@ func New(path string, version int) (*DB, error) {
|
||||
}
|
||||
|
||||
func (w *DB) Close() error {
|
||||
if w.repo != nil {
|
||||
return w.repo.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *DB) Initialize() error {
|
||||
|
||||
var err error
|
||||
w.message, err = NewMessage(w.BasePath, w.Version)
|
||||
w.ds, err = datasource.NewDataSource(w.path, w.platform, w.version)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("初始化数据源失败: %w", err)
|
||||
}
|
||||
|
||||
w.contact, err = NewContact(w.BasePath, w.Version)
|
||||
w.repo, err = repository.New(w.ds)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("初始化仓库失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *DB) GetMessages(start, end time.Time, talker string, limit, offset int) ([]*model.Message, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
if talker != "" {
|
||||
if contact := w.contact.GetContact(talker); contact != nil {
|
||||
talker = contact.UserName
|
||||
}
|
||||
}
|
||||
|
||||
messages, err := w.message.GetMessages(start, end, talker, limit, offset)
|
||||
// 使用 repository 获取消息
|
||||
messages, err := w.repo.GetMessages(ctx, start, end, talker, limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range messages {
|
||||
w.contact.MessageFillInfo(messages[i])
|
||||
return nil, fmt.Errorf("获取消息失败: %w", err)
|
||||
}
|
||||
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
type ListContactResp struct {
|
||||
type GetContactsResp struct {
|
||||
Items []*model.Contact `json:"items"`
|
||||
}
|
||||
|
||||
func (w *DB) ListContact() (*ListContactResp, error) {
|
||||
list, err := w.contact.ListContact()
|
||||
func (w *DB) GetContacts(key string, limit, offset int) (*GetContactsResp, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
contacts, err := w.repo.GetContacts(ctx, key, limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ListContactResp{
|
||||
Items: list,
|
||||
|
||||
return &GetContactsResp{
|
||||
Items: contacts,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (w *DB) GetContact(userName string) *model.Contact {
|
||||
return w.contact.GetContact(userName)
|
||||
}
|
||||
|
||||
type ListChatRoomResp struct {
|
||||
type GetChatRoomsResp struct {
|
||||
Items []*model.ChatRoom `json:"items"`
|
||||
}
|
||||
|
||||
func (w *DB) ListChatRoom() (*ListChatRoomResp, error) {
|
||||
list, err := w.contact.ListChatRoom()
|
||||
func (w *DB) GetChatRooms(key string, limit, offset int) (*GetChatRoomsResp, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
chatRooms, err := w.repo.GetChatRooms(ctx, key, limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &ListChatRoomResp{
|
||||
Items: list,
|
||||
|
||||
return &GetChatRoomsResp{
|
||||
Items: chatRooms,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (w *DB) GetChatRoom(userName string) *model.ChatRoom {
|
||||
return w.contact.GetChatRoom(userName)
|
||||
}
|
||||
|
||||
type GetSessionResp struct {
|
||||
type GetSessionsResp struct {
|
||||
Items []*model.Session `json:"items"`
|
||||
}
|
||||
|
||||
func (w *DB) GetSession(limit int) (*GetSessionResp, error) {
|
||||
sessions := w.contact.GetSession(limit)
|
||||
return &GetSessionResp{
|
||||
func (w *DB) GetSessions(key string, limit, offset int) (*GetSessionsResp, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 使用 repository 获取会话列表
|
||||
sessions, err := w.repo.GetSessions(ctx, key, limit, offset)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取会话列表失败: %w", err)
|
||||
}
|
||||
|
||||
return &GetSessionsResp{
|
||||
Items: sessions,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user