Store fixes

We were taking double pointers (`**T`) in some areas, and in
we were not setting foreign references correctly in `StartConversation`
and `Reply`.
This commit is contained in:
Matt Low 2024-06-09 04:10:26 +00:00
parent e334d9fc4f
commit c963747066

View File

@ -58,24 +58,28 @@ func NewSQLStore(db *gorm.DB) (*SQLStore, error) {
return &SQLStore{db, _sqids}, nil return &SQLStore{db, _sqids}, nil
} }
func (s *SQLStore) saveNewConversation(c *model.Conversation) error { func (s *SQLStore) createConversation() (*model.Conversation, error) {
// Save the new conversation // Create the new conversation
err := s.db.Save(&c).Error c := &model.Conversation{}
err := s.db.Save(c).Error
if err != nil { if err != nil {
return err return nil, err
} }
// Generate and save its "short name" // Generate and save its "short name"
shortName, _ := s.sqids.Encode([]uint64{uint64(c.ID)}) shortName, _ := s.sqids.Encode([]uint64{uint64(c.ID)})
c.ShortName = sql.NullString{String: shortName, Valid: true} c.ShortName = sql.NullString{String: shortName, Valid: true}
return s.UpdateConversation(c) err = s.db.Updates(c).Error
if err != nil {
return nil, err
}
return c, nil
} }
func (s *SQLStore) UpdateConversation(c *model.Conversation) error { func (s *SQLStore) UpdateConversation(c *model.Conversation) error {
if c == nil || c.ID == 0 { if c == nil || c.ID == 0 {
return fmt.Errorf("Conversation is nil or invalid (missing ID)") return fmt.Errorf("Conversation is nil or invalid (missing ID)")
} }
return s.db.Updates(&c).Error return s.db.Updates(c).Error
} }
func (s *SQLStore) DeleteConversation(c *model.Conversation) error { func (s *SQLStore) DeleteConversation(c *model.Conversation) error {
@ -84,7 +88,7 @@ func (s *SQLStore) DeleteConversation(c *model.Conversation) error {
if err != nil { if err != nil {
return err return err
} }
return s.db.Delete(&c).Error return s.db.Delete(c).Error
} }
func (s *SQLStore) DeleteMessage(message *model.Message, prune bool) error { func (s *SQLStore) DeleteMessage(message *model.Message, prune bool) error {
@ -96,7 +100,7 @@ func (s *SQLStore) UpdateMessage(m *model.Message) error {
if m == nil || m.ID == 0 { if m == nil || m.ID == 0 {
return fmt.Errorf("Message is nil or invalid (missing ID)") return fmt.Errorf("Message is nil or invalid (missing ID)")
} }
return s.db.Updates(&m).Error return s.db.Updates(m).Error
} }
func (s *SQLStore) ConversationShortNameCompletions(shortName string) []string { func (s *SQLStore) ConversationShortNameCompletions(shortName string) []string {
@ -149,14 +153,13 @@ func (s *SQLStore) StartConversation(messages ...model.Message) (*model.Conversa
} }
// Create new conversation // Create new conversation
conversation := &model.Conversation{} conversation, err := s.createConversation()
err := s.saveNewConversation(conversation)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
// Create first message // Create first message
messages[0].ConversationID = conversation.ID messages[0].Conversation = *conversation
err = s.db.Create(&messages[0]).Error err = s.db.Create(&messages[0]).Error
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -187,12 +190,11 @@ func (s *SQLStore) CloneConversation(toClone model.Conversation) (*model.Convers
return nil, 0, err return nil, 0, err
} }
clone := &model.Conversation{ clone, err := s.createConversation()
Title: toClone.Title + " - Clone", if err != nil {
}
if err := s.saveNewConversation(clone); err != nil {
return nil, 0, fmt.Errorf("Could not create clone: %s", err) return nil, 0, fmt.Errorf("Could not create clone: %s", err)
} }
clone.Title = toClone.Title + " - Clone"
var errors []error var errors []error
var messageCnt uint = 0 var messageCnt uint = 0
@ -231,8 +233,8 @@ func (s *SQLStore) Reply(to *model.Message, messages ...model.Message) ([]model.
currentParent := to currentParent := to
for i := range messages { for i := range messages {
message := messages[i] message := messages[i]
message.ConversationID = currentParent.ConversationID message.Parent = currentParent
message.ParentID = &currentParent.ID message.Conversation = currentParent.Conversation
message.ID = 0 message.ID = 0
message.CreatedAt = time.Time{} message.CreatedAt = time.Time{}