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
}
func (s *SQLStore) saveNewConversation(c *model.Conversation) error {
// Save the new conversation
err := s.db.Save(&c).Error
func (s *SQLStore) createConversation() (*model.Conversation, error) {
// Create the new conversation
c := &model.Conversation{}
err := s.db.Save(c).Error
if err != nil {
return err
return nil, err
}
// Generate and save its "short name"
shortName, _ := s.sqids.Encode([]uint64{uint64(c.ID)})
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 {
if c == nil || c.ID == 0 {
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 {
@ -84,7 +88,7 @@ func (s *SQLStore) DeleteConversation(c *model.Conversation) error {
if err != nil {
return err
}
return s.db.Delete(&c).Error
return s.db.Delete(c).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 {
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 {
@ -149,14 +153,13 @@ func (s *SQLStore) StartConversation(messages ...model.Message) (*model.Conversa
}
// Create new conversation
conversation := &model.Conversation{}
err := s.saveNewConversation(conversation)
conversation, err := s.createConversation()
if err != nil {
return nil, nil, err
}
// Create first message
messages[0].ConversationID = conversation.ID
messages[0].Conversation = *conversation
err = s.db.Create(&messages[0]).Error
if err != nil {
return nil, nil, err
@ -187,12 +190,11 @@ func (s *SQLStore) CloneConversation(toClone model.Conversation) (*model.Convers
return nil, 0, err
}
clone := &model.Conversation{
Title: toClone.Title + " - Clone",
}
if err := s.saveNewConversation(clone); err != nil {
clone, err := s.createConversation()
if err != nil {
return nil, 0, fmt.Errorf("Could not create clone: %s", err)
}
clone.Title = toClone.Title + " - Clone"
var errors []error
var messageCnt uint = 0
@ -231,8 +233,8 @@ func (s *SQLStore) Reply(to *model.Message, messages ...model.Message) ([]model.
currentParent := to
for i := range messages {
message := messages[i]
message.ConversationID = currentParent.ConversationID
message.ParentID = &currentParent.ID
message.Parent = currentParent
message.Conversation = currentParent.Conversation
message.ID = 0
message.CreatedAt = time.Time{}