chatpb/helpers.go

96 lines
1.8 KiB
Go

package chatpb
import (
"fmt"
"os"
"github.com/google/uuid"
"go.wit.com/log"
"google.golang.org/protobuf/proto"
)
func (c *Chats) AddGeminiComment(s string) *ChatEntry {
log.Info("FIX")
return nil
}
func (c *Chats) AddUserComment(s string) *ChatEntry {
log.Info("FIX")
return nil
}
func UnmarshalChats(data []byte) (*Chats, error) {
c := new(Chats)
err := c.Unmarshal(data)
return c, err
}
func UnmarshalChatsTEXT(data []byte) (*Chats, error) {
c := new(Chats)
err := c.UnmarshalTEXT(data)
return c, err
}
func (all *Chats) AddFile(filename string) error {
// Nil checks for safety.
if all == nil {
return fmt.Errorf("cannot call AddFile on a nil *Chats object")
}
if all.Chats == nil {
all.Chats = make([]*Chat, 0)
}
data, err := os.ReadFile(filename)
if err != nil {
log.Fatalf("Error reading file %s: %v", filename, err)
return err
}
logData, err := UnmarshalChatsTEXT(data)
if err != nil {
log.Fatalf("Error unmarshaling log file %s: %v", filename, err)
return err
}
// New, simplified logic assumes all files use the new nested format.
for _, chatGroup := range logData.GetChats() {
// It's now a direct append since the structure is the same.
// We still clone to ensure the appended data is a safe copy.
newChatGroup := proto.Clone(chatGroup).(*Chat)
all.AppendByUuid(newChatGroup)
}
return nil
}
func (chats *Chats) VerifyUuids() bool {
var changed bool
all := chats.SortByUuid()
for all.Scan() {
chat := all.Next()
if chat.Uuid == "" {
chat.Uuid = uuid.New().String()
changed = true
}
}
return changed
}
func (c *Chat) VerifyUuid() bool {
if c.Uuid == "" {
c.Uuid = uuid.New().String()
return true
}
return false
}
func (x *Chats) AppendNew(y *Chat) {
x.Lock()
defer x.Unlock()
var chat *Chat
chat = proto.Clone(y).(*Chat)
x.Chats = append(x.Chats, chat)
}