Change NamespacesWithLists struct

This commit is contained in:
kolaente 2021-08-29 22:33:16 +02:00
parent 279722d943
commit fcb64d5d8b
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
11 changed files with 80 additions and 46 deletions

View File

@ -26,7 +26,7 @@ import (
"github.com/laurent22/ical-go"
)
func GetCaldavTodosForTasks(list *models.List, listTasks []*models.Task) string {
func GetCaldavTodosForTasks(list *models.ListWithTasksAndBuckets, listTasks []*models.Task) string {
// Make caldav todos from Vikunja todos
var caldavtodos []*Todo

View File

@ -127,14 +127,26 @@ func exportListsAndTasks(s *xorm.Session, u *user.User, wr *zip.Writer) (err err
}
namespaceIDs := []int64{}
namespaces := []*NamespaceWithLists{}
namespaces := []*NamespaceWithListsAndTasks{}
for _, n := range namspaces.([]*NamespaceWithLists) {
if n.ID < 1 {
// Don't include filters
continue
}
nn := &NamespaceWithListsAndTasks{
Namespace: n.Namespace,
Lists: []*ListWithTasksAndBuckets{},
}
for _, l := range n.Lists {
nn.Lists = append(nn.Lists, &ListWithTasksAndBuckets{
List: *l,
})
}
namespaceIDs = append(namespaceIDs, n.ID)
namespaces = append(namespaces, n)
namespaces = append(namespaces, nn)
}
if len(namespaceIDs) == 0 {
@ -155,11 +167,15 @@ func exportListsAndTasks(s *xorm.Session, u *user.User, wr *zip.Writer) (err err
return err
}
listMap := make(map[int64]*List)
listMap := make(map[int64]*ListWithTasksAndBuckets)
listIDs := []int64{}
for _, n := range namespaces {
for _, l := range n.Lists {
listMap[l.ID] = l
listMap[l.ID] = &ListWithTasksAndBuckets{
List: l.List,
BackgroundFileID: l.BackgroundFileID,
Tasks: []*Task{},
}
listIDs = append(listIDs, l.ID)
}
}

View File

@ -51,12 +51,6 @@ type List struct {
// The user who created this list.
Owner *user.User `xorm:"-" json:"owner" valid:"-"`
// An array of tasks which belong to the list.
// Deprecated: you should use the dedicated task list endpoint because it has support for pagination and filtering
Tasks []*Task `xorm:"-" json:"-"`
// Only used for migration.
Buckets []*Bucket `xorm:"-" json:"-"`
// Whether or not a list is archived.
IsArchived bool `xorm:"not null default false" json:"is_archived" query:"is_archived"`
@ -85,6 +79,15 @@ type List struct {
web.Rights `xorm:"-" json:"-"`
}
type ListWithTasksAndBuckets struct {
List
// An array of tasks which belong to the list.
Tasks []*Task `xorm:"-" json:"tasks"`
// Only used for migration.
Buckets []*Bucket `xorm:"-" json:"buckets"`
BackgroundFileID int64 `xorm:"null" json:"background_file_id"`
}
// TableName returns a better name for the lists table
func (l *List) TableName() string {
return "lists"

View File

@ -187,6 +187,11 @@ type NamespaceWithLists struct {
Lists []*List `xorm:"-" json:"lists"`
}
type NamespaceWithListsAndTasks struct {
Namespace
Lists []*ListWithTasksAndBuckets `xorm:"-" json:"lists"`
}
func makeNamespaceSlice(namespaces map[int64]*NamespaceWithLists, userMap map[int64]*user.User, subscriptions map[int64]*Subscription) []*NamespaceWithLists {
all := make([]*NamespaceWithLists, 0, len(namespaces))
for _, n := range namespaces {

View File

@ -31,7 +31,7 @@ import (
// InsertFromStructure takes a fully nested Vikunja data structure and a user and then creates everything for this user
// (Namespaces, tasks, etc. Even attachments and relations.)
func InsertFromStructure(str []*models.NamespaceWithLists, user *user.User) (err error) {
func InsertFromStructure(str []*models.NamespaceWithListsAndTasks, user *user.User) (err error) {
s := db.NewSession()
defer s.Close()
@ -45,7 +45,7 @@ func InsertFromStructure(str []*models.NamespaceWithLists, user *user.User) (err
return s.Commit()
}
func insertFromStructure(s *xorm.Session, str []*models.NamespaceWithLists, user *user.User) (err error) {
func insertFromStructure(s *xorm.Session, str []*models.NamespaceWithListsAndTasks, user *user.User) (err error) {
log.Debugf("[creating structure] Creating %d namespaces", len(str))

View File

@ -243,15 +243,15 @@ func getMicrosoftTodoData(token string) (microsoftTodoData []*list, err error) {
return
}
func convertMicrosoftTodoData(todoData []*list) (vikunjsStructure []*models.NamespaceWithLists, err error) {
func convertMicrosoftTodoData(todoData []*list) (vikunjsStructure []*models.NamespaceWithListsAndTasks, err error) {
// One namespace with all lists
vikunjsStructure = []*models.NamespaceWithLists{
vikunjsStructure = []*models.NamespaceWithListsAndTasks{
{
Namespace: models.Namespace{
Title: "Migrated from Microsoft Todo",
},
Lists: []*models.List{},
Lists: []*models.ListWithTasksAndBuckets{},
},
}
@ -262,8 +262,10 @@ func convertMicrosoftTodoData(todoData []*list) (vikunjsStructure []*models.Name
log.Debugf("[Microsoft Todo Migration] Converting list %s", l.ID)
// Lists only with title
list := &models.List{
Title: l.DisplayName,
list := &models.ListWithTasksAndBuckets{
List: models.List{
Title: l.DisplayName,
},
}
log.Debugf("[Microsoft Todo Migration] Converting %d tasks", len(l.Tasks))

View File

@ -252,16 +252,16 @@ func parseDate(dateString string) (date time.Time, err error) {
return date, err
}
func convertTodoistToVikunja(sync *sync, doneItems map[int64]*doneItem) (fullVikunjaHierachie []*models.NamespaceWithLists, err error) {
func convertTodoistToVikunja(sync *sync, doneItems map[int64]*doneItem) (fullVikunjaHierachie []*models.NamespaceWithListsAndTasks, err error) {
newNamespace := &models.NamespaceWithLists{
newNamespace := &models.NamespaceWithListsAndTasks{
Namespace: models.Namespace{
Title: "Migrated from todoist",
},
}
// A map for all vikunja lists with the project id they're coming from as key
lists := make(map[int64]*models.List, len(sync.Projects))
lists := make(map[int64]*models.ListWithTasksAndBuckets, len(sync.Projects))
// A map for all vikunja tasks with the todoist task id as key to find them easily and add more data
tasks := make(map[int64]*models.Task, len(sync.Items))
@ -270,10 +270,12 @@ func convertTodoistToVikunja(sync *sync, doneItems map[int64]*doneItem) (fullVik
labels := make(map[int64]*models.Label, len(sync.Labels))
for _, p := range sync.Projects {
list := &models.List{
Title: p.Name,
HexColor: todoistColors[p.Color],
IsArchived: p.IsArchived == 1,
list := &models.ListWithTasksAndBuckets{
List: models.List{
Title: p.Name,
HexColor: todoistColors[p.Color],
IsArchived: p.IsArchived == 1,
},
}
lists[p.ID] = list
@ -449,7 +451,7 @@ func convertTodoistToVikunja(sync *sync, doneItems map[int64]*doneItem) (fullVik
tasks[r.ItemID].Reminders = append(tasks[r.ItemID].Reminders, date.In(config.GetTimeZone()))
}
return []*models.NamespaceWithLists{
return []*models.NamespaceWithListsAndTasks{
newNamespace,
}, err
}

View File

@ -144,16 +144,16 @@ func getTrelloData(token string) (trelloData []*trello.Board, err error) {
// Converts all previously obtained data from trello into the vikunja format.
// `trelloData` should contain all boards with their lists and cards respectively.
func convertTrelloDataToVikunja(trelloData []*trello.Board) (fullVikunjaHierachie []*models.NamespaceWithLists, err error) {
func convertTrelloDataToVikunja(trelloData []*trello.Board) (fullVikunjaHierachie []*models.NamespaceWithListsAndTasks, err error) {
log.Debugf("[Trello Migration] ")
fullVikunjaHierachie = []*models.NamespaceWithLists{
fullVikunjaHierachie = []*models.NamespaceWithListsAndTasks{
{
Namespace: models.Namespace{
Title: "Imported from Trello",
},
Lists: []*models.List{},
Lists: []*models.ListWithTasksAndBuckets{},
},
}
@ -162,10 +162,12 @@ func convertTrelloDataToVikunja(trelloData []*trello.Board) (fullVikunjaHierachi
log.Debugf("[Trello Migration] Converting %d boards to vikunja lists", len(trelloData))
for _, board := range trelloData {
list := &models.List{
Title: board.Name,
Description: board.Desc,
IsArchived: board.Closed,
list := &models.ListWithTasksAndBuckets{
List: models.List{
Title: board.Name,
Description: board.Desc,
IsArchived: board.Closed,
},
}
// Background

View File

@ -142,11 +142,13 @@ type wunderlistContents struct {
subtasks []*subtask
}
func convertListForFolder(listID int, list *list, content *wunderlistContents) (*models.List, error) {
func convertListForFolder(listID int, list *list, content *wunderlistContents) (*models.ListWithTasksAndBuckets, error) {
l := &models.List{
Title: list.Title,
Created: list.CreatedAt,
l := &models.ListWithTasksAndBuckets{
List: models.List{
Title: list.Title,
Created: list.CreatedAt,
},
}
// Find all tasks belonging to this list and put them in
@ -239,7 +241,7 @@ func convertListForFolder(listID int, list *list, content *wunderlistContents) (
return l, nil
}
func convertWunderlistToVikunja(content *wunderlistContents) (fullVikunjaHierachie []*models.NamespaceWithLists, err error) {
func convertWunderlistToVikunja(content *wunderlistContents) (fullVikunjaHierachie []*models.NamespaceWithListsAndTasks, err error) {
// Make a map from the list with the key being list id for easier handling
listMap := make(map[int]*list, len(content.lists))
@ -249,7 +251,7 @@ func convertWunderlistToVikunja(content *wunderlistContents) (fullVikunjaHierach
// First, we look through all folders and create namespaces for them.
for _, folder := range content.folders {
namespace := &models.NamespaceWithLists{
namespace := &models.NamespaceWithListsAndTasks{
Namespace: models.Namespace{
Title: folder.Title,
Created: folder.CreatedAt,
@ -276,7 +278,7 @@ func convertWunderlistToVikunja(content *wunderlistContents) (fullVikunjaHierach
// At the end, loop over all lists which don't belong to a namespace and put them in a default namespace
if len(listMap) > 0 {
newNamespace := &models.NamespaceWithLists{
newNamespace := &models.NamespaceWithListsAndTasks{
Namespace: models.Namespace{
Title: "Migrated from wunderlist",
},

View File

@ -56,7 +56,7 @@ func ListHandler(c echo.Context) error {
}
storage := &VikunjaCaldavListStorage{
list: &models.List{ID: listID},
list: &models.ListWithTasksAndBuckets{List: models.List{ID: listID}},
user: u,
}
@ -102,7 +102,7 @@ func TaskHandler(c echo.Context) error {
taskUID := strings.TrimSuffix(c.Param("task"), ".ics")
storage := &VikunjaCaldavListStorage{
list: &models.List{ID: listID},
list: &models.ListWithTasksAndBuckets{List: models.List{ID: listID}},
task: &models.Task{UID: taskUID},
user: u,
}

View File

@ -39,7 +39,7 @@ const ListBasePath = DavBasePath + `lists`
// VikunjaCaldavListStorage represents a list storage
type VikunjaCaldavListStorage struct {
// Used when handling a list
list *models.List
list *models.ListWithTasksAndBuckets
// Used when handling a single task, like updating
task *models.Task
// The current user
@ -109,7 +109,9 @@ func (vcls *VikunjaCaldavListStorage) GetResources(rpath string, withChildren bo
var resources []data.Resource
for _, l := range lists {
rr := VikunjaListResourceAdapter{
list: l,
list: &models.ListWithTasksAndBuckets{
List: *l,
},
isCollection: true,
}
r := data.NewResource(ListBasePath+"/"+strconv.FormatInt(l.ID, 10), &rr)
@ -368,7 +370,7 @@ func (vcls *VikunjaCaldavListStorage) DeleteResource(rpath string) error {
// VikunjaListResourceAdapter holds the actual resource
type VikunjaListResourceAdapter struct {
list *models.List
list *models.ListWithTasksAndBuckets
listTasks []*models.Task
task *models.Task
@ -415,7 +417,7 @@ func (vlra *VikunjaListResourceAdapter) GetContent() string {
}
if vlra.task != nil {
list := models.List{Tasks: []*models.Task{vlra.task}}
list := models.ListWithTasksAndBuckets{Tasks: []*models.Task{vlra.task}}
return caldav.GetCaldavTodosForTasks(&list, list.Tasks)
}