fix: updating a list might remove its background

resolves #1039
This commit is contained in:
kolaente 2021-11-13 17:52:14 +01:00
parent 2ae1da473e
commit cf05de19b3
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
3 changed files with 106 additions and 89 deletions

View file

@ -540,12 +540,10 @@ func (l *List) CheckIsArchived(s *xorm.Session) (err error) {
return nil
}
// CreateOrUpdateList updates a list or creates it if it doesn't exist
func CreateOrUpdateList(s *xorm.Session, list *List, auth web.Auth) (err error) {
func checkListBeforeUpdateOrDelete(s *xorm.Session, list *List) error {
// Check if the namespace exists
if list.NamespaceID > 0 {
_, err = GetNamespaceByID(s, list.NamespaceID)
_, err := GetNamespaceByID(s, list.NamespaceID)
if err != nil {
return err
}
@ -565,7 +563,29 @@ func CreateOrUpdateList(s *xorm.Session, list *List, auth web.Auth) (err error)
}
}
if list.ID == 0 {
return nil
}
func CreateList(s *xorm.Session, list *List, auth web.Auth) (err error) {
err = list.CheckIsArchived(s)
if err != nil {
return err
}
doer, err := user.GetFromAuth(auth)
if err != nil {
return err
}
list.OwnerID = doer.ID
list.Owner = doer
list.ID = 0 // Otherwise only the first time a new list would be created
err = checkListBeforeUpdateOrDelete(s, list)
if err != nil {
return
}
_, err = s.Insert(list)
if err != nil {
return
@ -581,20 +601,45 @@ func CreateOrUpdateList(s *xorm.Session, list *List, auth web.Auth) (err error)
return err
}
}
} else {
// Create a new first bucket for this list
b := &Bucket{
ListID: list.ID,
Title: "Backlog",
}
err = b.Create(s, auth)
if err != nil {
return
}
return events.Dispatch(&ListCreatedEvent{
List: list,
Doer: doer,
})
}
func UpdateList(s *xorm.Session, list *List, auth web.Auth, updateListBackground bool) (err error) {
err = checkListBeforeUpdateOrDelete(s, list)
if err != nil {
return
}
// We need to specify the cols we want to update here to be able to un-archive lists
colsToUpdate := []string{
"title",
"is_archived",
"identifier",
"hex_color",
"background_file_id",
"position",
}
if list.Description != "" {
colsToUpdate = append(colsToUpdate, "description")
}
if updateListBackground {
colsToUpdate = append(colsToUpdate, "background_file_id")
}
wasFavorite, err := isFavorite(s, list.ID, auth, FavoriteKindList)
if err != nil {
return err
@ -618,6 +663,13 @@ func CreateOrUpdateList(s *xorm.Session, list *List, auth web.Auth) (err error)
if err != nil {
return err
}
err = events.Dispatch(&ListUpdatedEvent{
List: list,
Doer: auth,
})
if err != nil {
return err
}
l, err := GetListSimpleByID(s, list.ID)
@ -664,15 +716,7 @@ func (l *List) Update(s *xorm.Session, a web.Auth) (err error) {
return nil
}
err = CreateOrUpdateList(s, l, a)
if err != nil {
return err
}
return events.Dispatch(&ListUpdatedEvent{
List: l,
Doer: a,
})
return UpdateList(s, l, a, false)
}
func updateListLastUpdated(s *xorm.Session, list *List) error {
@ -705,39 +749,12 @@ func updateListByTaskID(s *xorm.Session, taskID int64) (err error) {
// @Failure 500 {object} models.Message "Internal error"
// @Router /namespaces/{namespaceID}/lists [put]
func (l *List) Create(s *xorm.Session, a web.Auth) (err error) {
err = l.CheckIsArchived(s)
if err != nil {
return err
}
doer, err := user.GetFromAuth(a)
if err != nil {
return err
}
l.OwnerID = doer.ID
l.Owner = doer
l.ID = 0 // Otherwise only the first time a new list would be created
err = CreateOrUpdateList(s, l, a)
err = CreateList(s, l, a)
if err != nil {
return
}
// Create a new first bucket for this list
b := &Bucket{
ListID: l.ID,
Title: "Backlog",
}
err = b.Create(s, a)
if err != nil {
return
}
return events.Dispatch(&ListCreatedEvent{
List: l,
Doer: doer,
})
return l.ReadOne(s, a)
}
// Delete implements the delete method of CRUDable

View file

@ -75,7 +75,7 @@ func (ld *ListDuplicate) Create(s *xorm.Session, doer web.Auth) (err error) {
ld.List.Identifier = "" // Reset the identifier to trigger regenerating a new one
// Set the owner to the current user
ld.List.OwnerID = doer.GetID()
if err := CreateOrUpdateList(s, ld.List, doer); err != nil {
if err := CreateList(s, ld.List, doer); err != nil {
// If there is no available unique list identifier, just reset it.
if IsErrListIdentifierIsNotUnique(err) {
ld.List.Identifier = ""

View file

@ -300,7 +300,7 @@ func RemoveListBackground(c echo.Context) error {
list.BackgroundFileID = 0
list.BackgroundInformation = nil
err = list.Update(s, auth)
err = models.UpdateList(s, list, auth, true)
if err != nil {
return err
}