Move setting list favorites

This commit is contained in:
kolaente 2021-07-09 22:27:12 +02:00
parent 29a6b395e4
commit 84184b16a5
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 54 additions and 11 deletions

View File

@ -64,8 +64,8 @@ type List struct {
// Holds extra information about the background set since some background providers require attribution or similar. If not null, the background can be accessed at /lists/{listID}/background // Holds extra information about the background set since some background providers require attribution or similar. If not null, the background can be accessed at /lists/{listID}/background
BackgroundInformation interface{} `xorm:"-" json:"background_information"` BackgroundInformation interface{} `xorm:"-" json:"background_information"`
// True if a list is a favorite. Favorite lists show up in a separate namespace. // True if a list is a favorite. Favorite lists show up in a separate namespace. This value depends on the user making the call to the api.
IsFavorite bool `xorm:"default false" json:"is_favorite"` IsFavorite bool `xorm:"-" json:"is_favorite"`
// The subscription status for the user reading this list. You can only read this property, use the subscription endpoints to modify it. // The subscription status for the user reading this list. You can only read this property, use the subscription endpoints to modify it.
// Will only returned when retreiving one list. // Will only returned when retreiving one list.
@ -155,7 +155,7 @@ func GetListsByNamespaceID(s *xorm.Session, nID int64, doer *user.User) (lists [
} }
// get more list details // get more list details
err = addListDetails(s, lists) err = addListDetails(s, lists, doer)
return lists, err return lists, err
} }
@ -183,7 +183,7 @@ func (l *List) ReadAll(s *xorm.Session, a web.Auth, search string, page int, per
return nil, 0, 0, err return nil, 0, 0, err
} }
lists := []*List{list} lists := []*List{list}
err = addListDetails(s, lists) err = addListDetails(s, lists, a)
return lists, 0, 0, err return lists, 0, 0, err
} }
@ -201,7 +201,7 @@ func (l *List) ReadAll(s *xorm.Session, a web.Auth, search string, page int, per
} }
// Add more list details // Add more list details
err = addListDetails(s, lists) err = addListDetails(s, lists, a)
return lists, resultCount, totalItems, err return lists, resultCount, totalItems, err
} }
@ -266,6 +266,11 @@ func (l *List) ReadOne(s *xorm.Session, a web.Auth) (err error) {
} }
} }
l.IsFavorite, err = isFavorite(s, l.ID, a, FavoriteKindList)
if err != nil {
return
}
l.Subscription, err = GetSubscription(s, SubscriptionEntityList, l.ID, a) l.Subscription, err = GetSubscription(s, SubscriptionEntityList, l.ID, a)
return return
} }
@ -421,7 +426,7 @@ func getRawListsForUser(s *xorm.Session, opts *listOptions) (lists []*List, resu
} }
// addListDetails adds owner user objects and list tasks to all lists in the slice // addListDetails adds owner user objects and list tasks to all lists in the slice
func addListDetails(s *xorm.Session, lists []*List) (err error) { func addListDetails(s *xorm.Session, lists []*List, a web.Auth) (err error) {
if len(lists) == 0 { if len(lists) == 0 {
return return
} }
@ -441,7 +446,9 @@ func addListDetails(s *xorm.Session, lists []*List) (err error) {
} }
var fileIDs []int64 var fileIDs []int64
var listIDs []int64
for _, l := range lists { for _, l := range lists {
listIDs = append(listIDs, l.ID)
if o, exists := owners[l.OwnerID]; exists { if o, exists := owners[l.OwnerID]; exists {
l.Owner = o l.Owner = o
} }
@ -451,6 +458,15 @@ func addListDetails(s *xorm.Session, lists []*List) (err error) {
fileIDs = append(fileIDs, l.BackgroundFileID) fileIDs = append(fileIDs, l.BackgroundFileID)
} }
favs, err := getFavorites(s, listIDs, a, FavoriteKindList)
if err != nil {
return err
}
for _, list := range lists {
list.IsFavorite = favs[list.ID]
}
if len(fileIDs) == 0 { if len(fileIDs) == 0 {
return return
} }
@ -536,6 +552,11 @@ func CreateOrUpdateList(s *xorm.Session, list *List, auth web.Auth) (err error)
if list.ID == 0 { if list.ID == 0 {
_, err = s.Insert(list) _, err = s.Insert(list)
if list.IsFavorite {
if err := addToFavorites(s, list.ID, auth, FavoriteKindList); err != nil {
return err
}
}
} else { } else {
// We need to specify the cols we want to update here to be able to un-archive lists // We need to specify the cols we want to update here to be able to un-archive lists
colsToUpdate := []string{ colsToUpdate := []string{
@ -543,13 +564,28 @@ func CreateOrUpdateList(s *xorm.Session, list *List, auth web.Auth) (err error)
"is_archived", "is_archived",
"identifier", "identifier",
"hex_color", "hex_color",
"is_favorite",
"background_file_id", "background_file_id",
} }
if list.Description != "" { if list.Description != "" {
colsToUpdate = append(colsToUpdate, "description") colsToUpdate = append(colsToUpdate, "description")
} }
wasFavorite, err := isFavorite(s, list.ID, auth, FavoriteKindList)
if err != nil {
return err
}
if list.IsFavorite && !wasFavorite {
if err := addToFavorites(s, list.ID, auth, FavoriteKindList); err != nil {
return err
}
}
if !list.IsFavorite && wasFavorite {
if err := removeFromFavorite(s, list.ID, auth, FavoriteKindList); err != nil {
return err
}
}
_, err = s. _, err = s.
ID(list.ID). ID(list.ID).
Cols(colsToUpdate...). Cols(colsToUpdate...).
@ -568,7 +604,6 @@ func CreateOrUpdateList(s *xorm.Session, list *List, auth web.Auth) (err error)
*list = *l *list = *l
err = list.ReadOne(s, auth) err = list.ReadOne(s, auth)
return return
} }
// Update implements the update method of CRUDable // Update implements the update method of CRUDable
@ -593,7 +628,6 @@ func (l *List) Update(s *xorm.Session, a web.Auth) (err error) {
return err return err
} }
f.IsFavorite = l.IsFavorite
f.Title = l.Title f.Title = l.Title
f.Description = l.Description f.Description = l.Description
err = f.Update(s, a) err = f.Update(s, a)

View File

@ -96,7 +96,7 @@ type Task struct {
Attachments []*TaskAttachment `xorm:"-" json:"attachments"` Attachments []*TaskAttachment `xorm:"-" json:"attachments"`
// True if a task is a favorite task. Favorite tasks show up in a separate "Important" list. This value depends on the user making the call to the api. // True if a task is a favorite task. Favorite tasks show up in a separate "Important" list. This value depends on the user making the call to the api.
IsFavorite bool `xorm:"default false" json:"is_favorite"` IsFavorite bool `xorm:"-" json:"is_favorite"`
// The subscription status for the user reading this task. You can only read this property, use the subscription endpoints to modify it. // The subscription status for the user reading this task. You can only read this property, use the subscription endpoints to modify it.
// Will only returned when retreiving one task. // Will only returned when retreiving one task.
@ -393,7 +393,16 @@ func getRawTasksForLists(s *xorm.Session, lists []*List, a web.Auth, opts *taskO
userListIDs = append(userListIDs, l.ID) userListIDs = append(userListIDs, l.ID)
} }
listCond = builder.Or(listIDCond, builder.And(builder.Eq{"is_favorite": true}, builder.In("list_id", userListIDs))) favs, err := getFavorites(s, userListIDs, a, FavoriteKindList)
if err != nil {
return nil, 0, 0, err
}
var favoriteListIDs []int64
for id, _ := range favs {
favoriteListIDs = append(favoriteListIDs, id)
}
listCond = builder.Or(listIDCond, builder.In("list_id", favoriteListIDs))
} }
if len(reminderFilters) > 0 { if len(reminderFilters) > 0 {