Fix not able to make saved filters favorite

This commit is contained in:
kolaente 2021-04-03 16:49:20 +02:00
parent 9b7eef985e
commit 0b8173c1c3
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
9 changed files with 131 additions and 24 deletions

1
.gitignore vendored
View file

@ -23,3 +23,4 @@ files/
vikunja-dump*
vendor/
os-packages/
mage_output_file.go

View file

@ -0,0 +1,43 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package migration
import (
"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"
)
type savedFilters20210403145503 struct {
IsFavorite bool `xorm:"default false" json:"is_favorite"`
}
func (savedFilters20210403145503) TableName() string {
return "saved_filters"
}
func init() {
migrations = append(migrations, &xormigrate.Migration{
ID: "20210403145503",
Description: "",
Migrate: func(tx *xorm.Engine) error {
return tx.Sync2(savedFilters20210403145503{})
},
Rollback: func(tx *xorm.Engine) error {
return nil
},
})
}

View file

@ -586,6 +586,25 @@ func CreateOrUpdateList(s *xorm.Session, list *List, auth web.Auth) (err error)
// @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{id} [post]
func (l *List) Update(s *xorm.Session, a web.Auth) (err error) {
fid := getSavedFilterIDFromListID(l.ID)
if fid > 0 {
f, err := getSavedFilterSimpleByID(s, fid)
if err != nil {
return err
}
f.IsFavorite = l.IsFavorite
f.Title = l.Title
f.Description = l.Description
err = f.Update(s, a)
if err != nil {
return err
}
*l = *f.toList()
return nil
}
err = CreateOrUpdateList(s, l, a)
if err != nil {
return err

View file

@ -115,6 +115,17 @@ func (l *List) CanUpdate(s *xorm.Session, a web.Auth) (canUpdate bool, err error
if l.ID == FavoritesPseudoList.ID {
return false, nil
}
fid := getSavedFilterIDFromListID(l.ID)
if fid > 0 {
sf, err := getSavedFilterSimpleByID(s, fid)
if err != nil {
return false, err
}
return sf.CanUpdate(s, a)
}
canUpdate, err = l.CanWrite(s, a)
// If the list is archived and the user tries to un-archive it, let the request through
if IsErrListIsArchived(err) && !l.IsArchived {

View file

@ -434,14 +434,10 @@ func getSavedFilters(s *xorm.Session, doer *user.User) (savedFiltersNamespace *N
}
for _, filter := range savedFilters {
savedFiltersNamespace.Lists = append(savedFiltersNamespace.Lists, &List{
ID: getListIDFromSavedFilterID(filter.ID),
Title: filter.Title,
Description: filter.Description,
Created: filter.Created,
Updated: filter.Updated,
Owner: doer,
})
filterList := filter.toList()
filterList.NamespaceID = savedFiltersNamespace.ID
filterList.Owner = doer
savedFiltersNamespace.Lists = append(savedFiltersNamespace.Lists, filterList)
}
return
@ -521,6 +517,19 @@ func (n *Namespace) ReadAll(s *xorm.Session, a web.Auth, search string, page int
lists = append(lists, sharedListsNamespace.Lists...)
}
/////////////////
// Saved Filters
savedFiltersNamespace, err := getSavedFilters(s, doer)
if err != nil {
return nil, 0, 0, err
}
if savedFiltersNamespace != nil {
namespaces[savedFiltersNamespace.ID] = savedFiltersNamespace
lists = append(lists, savedFiltersNamespace.Lists...)
}
/////////////////
// Favorite lists
@ -533,18 +542,6 @@ func (n *Namespace) ReadAll(s *xorm.Session, a web.Auth, search string, page int
namespaces[favoritesNamespace.ID] = favoritesNamespace
}
/////////////////
// Saved Filters
savedFiltersNamespace, err := getSavedFilters(s, doer)
if err != nil {
return nil, 0, 0, err
}
if savedFiltersNamespace != nil {
namespaces[savedFiltersNamespace.ID] = savedFiltersNamespace
}
//////////////////////
// Put it all together
@ -554,8 +551,8 @@ func (n *Namespace) ReadAll(s *xorm.Session, a web.Auth, search string, page int
}
for _, list := range lists {
if list.NamespaceID == SharedListsPseudoNamespace.ID {
// Shared lists are already in the namespace
if list.NamespaceID == SharedListsPseudoNamespace.ID || list.NamespaceID == SavedFiltersPseudoNamespace.ID {
// Shared lists and filtered lists are already in the namespace
continue
}
namespaces[list.NamespaceID].Lists = append(namespaces[list.NamespaceID].Lists, list)

View file

@ -29,7 +29,7 @@ type SavedFilter struct {
// The unique numeric id of this saved filter
ID int64 `xorm:"autoincr not null unique pk" json:"id" param:"filter"`
// The actual filters this filter contains
Filters *TaskCollection `xorm:"JSON not null" json:"filters"`
Filters *TaskCollection `xorm:"JSON not null" json:"filters" valid:"required"`
// The title of the filter.
Title string `xorm:"varchar(250) not null" json:"title" valid:"required,runelength(1|250)" minLength:"1" maxLength:"250"`
// The description of the filter
@ -39,6 +39,9 @@ type SavedFilter struct {
// The user who owns this filter
Owner *user.User `xorm:"-" json:"owner" valid:"-"`
// True if the filter is a favorite. Favorite filters show up in a separate namespace together with favorite lists.
IsFavorite bool `xorm:"default false" json:"is_favorite"`
// A timestamp when this filter was created. You cannot change this value.
Created time.Time `xorm:"created not null" json:"created"`
// A timestamp when this filter was last updated. You cannot change this value.
@ -90,6 +93,18 @@ func getSavedFiltersForUser(s *xorm.Session, auth web.Auth) (filters []*SavedFil
return
}
func (sf *SavedFilter) toList() *List {
return &List{
ID: getListIDFromSavedFilterID(sf.ID),
Title: sf.Title,
Description: sf.Description,
IsFavorite: sf.IsFavorite,
Created: sf.Created,
Updated: sf.Updated,
Owner: sf.Owner,
}
}
// Create creates a new saved filter
// @Summary Creates a new saved filter
// @Description Creates a new saved filter
@ -154,12 +169,22 @@ func (sf *SavedFilter) ReadOne(s *xorm.Session, a web.Auth) error {
// @Failure 500 {object} models.Message "Internal error"
// @Router /filters/{id} [post]
func (sf *SavedFilter) Update(s *xorm.Session, a web.Auth) error {
_, err := s.
origFilter, err := getSavedFilterSimpleByID(s, sf.ID)
if err != nil {
return err
}
if sf.Filters == nil {
sf.Filters = origFilter.Filters
}
_, err = s.
Where("id = ?", sf.ID).
Cols(
"title",
"description",
"filters",
"is_favorite",
).
Update(sf)
return err

View file

@ -7770,6 +7770,10 @@ var doc = `{
"description": "The unique numeric id of this saved filter",
"type": "integer"
},
"is_favorite": {
"description": "True if the filter is a favorite. Favorite filters show up in a separate namespace together with favorite lists.",
"type": "boolean"
},
"owner": {
"description": "The user who owns this filter",
"$ref": "#/definitions/user.User"

View file

@ -7753,6 +7753,10 @@
"description": "The unique numeric id of this saved filter",
"type": "integer"
},
"is_favorite": {
"description": "True if the filter is a favorite. Favorite filters show up in a separate namespace together with favorite lists.",
"type": "boolean"
},
"owner": {
"description": "The user who owns this filter",
"$ref": "#/definitions/user.User"

View file

@ -524,6 +524,9 @@ definitions:
id:
description: The unique numeric id of this saved filter
type: integer
is_favorite:
description: True if the filter is a favorite. Favorite filters show up in a separate namespace together with favorite lists.
type: boolean
owner:
$ref: '#/definitions/user.User'
description: The user who owns this filter