diff options
Diffstat (limited to 'helpers.go')
| -rw-r--r-- | helpers.go | 70 |
1 files changed, 70 insertions, 0 deletions
@@ -1,8 +1,14 @@ package chatpb import ( + "fmt" + "os" + "path/filepath" "time" + "github.com/google/uuid" + "go.wit.com/log" + "google.golang.org/protobuf/proto" timestamppb "google.golang.org/protobuf/types/known/timestamppb" ) @@ -40,3 +46,67 @@ func UnmarshalChatsTEXT(data []byte) (*Chats, error) { err := c.UnmarshalTEXT(data) return c, err } + +func (all *Chats) AddFile(filename string) error { + 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 + } + + for _, chat := range logData.GetChats() { + // make a copy + newc := proto.Clone(chat).(*Chat) + + // Handle content: prefer content_file, fallback to content. + var content string + if contentFile := chat.GetContentFile(); contentFile != "" { + // Construct the full path relative to the log file's directory. + logDir := filepath.Dir(filename) + contentPath := filepath.Join(logDir, contentFile) + + contentBytes, err := os.ReadFile(contentPath) + if err != nil { + content = fmt.Sprintf("--- ERROR: Could not read content file %s: %v ---", contentPath, err) + } else { + content = string(contentBytes) + } + } else { + // Fallback for older log formats. + content = chat.GetContent() + } + newc.Content = content + newc.VerifyUuid() + all.AppendByUuid(newc) + } + + 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 +} |
