From 8380ef6c291bc6773490a78b3ef5678cd975ee33 Mon Sep 17 00:00:00 2001 From: liuzx Date: Thu, 9 Jun 2022 11:44:48 +0800 Subject: [PATCH 01/13] update --- models/attachment.go | 123 ++++++++++++++++++++++++++++++++++++++++ routers/repo/dataset.go | 4 +- 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/models/attachment.go b/models/attachment.go index ea8f1645f..2e9abfcda 100755 --- a/models/attachment.go +++ b/models/attachment.go @@ -81,6 +81,7 @@ type AttachmentsOptions struct { NeedRepoInfo bool Keyword string RecommendOnly bool + UserId int64 } func (a *Attachment) AfterUpdate() { @@ -653,3 +654,125 @@ func Attachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) { return attachments, count, nil } + +func MyFvAttachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) { + sess := x.NewSession() + defer sess.Close() + + var cond = builder.NewCond() + if opts.NeedDatasetIDs { + cond = cond.And( + builder.In("attachment.dataset_id", opts.DatasetIDs), + ) + } + + if opts.UploaderID > 0 { + cond = cond.And( + builder.Eq{"attachment.uploader_id": opts.UploaderID}, + ) + } + + if (opts.Type) >= 0 { + cond = cond.And( + builder.Eq{"attachment.type": opts.Type}, + ) + } + + if opts.NeedIsPrivate { + cond = cond.And( + builder.Eq{"attachment.is_private": opts.IsPrivate}, + ) + for _, DatasetID := range opts.DatasetIDs { + dataset, err := GetDatasetByID(DatasetID) + if err != nil { + return nil, 0, fmt.Errorf("GetDatasetByID failed error: %v", err) + } + repo, err := GetRepositoryByID(dataset.RepoID) + if err != nil { + return nil, 0, fmt.Errorf("GetRepositoryByID failed error: %v", err) + } + isCollaborator, _ := repo.IsCollaborator(opts.UserId) + if !isCollaborator { + cond = cond.And( + builder.Eq{"attachment.dataset_id": DatasetID}, + builder.Eq{"attachment.is_private": false}, + ) + } else { + cond = cond.And( + builder.Eq{"attachment.dataset_id": DatasetID}, + ) + } + } + } + if opts.RecommendOnly { + cond = cond.And(builder.In("attachment.id", builder.Select("attachment.id"). + From("attachment"). + Join("INNER", "dataset", "attachment.dataset_id = dataset.id and dataset.recommend=true"))) + } + + if opts.JustNeedZipFile { + var DecompressState []int32 + DecompressState = append(DecompressState, DecompressStateDone, DecompressStateIng, DecompressStateFailed) + cond = cond.And( + builder.In("attachment.decompress_state", DecompressState), + ) + } + + var count int64 + var err error + if len(opts.Keyword) == 0 { + count, err = sess.Where(cond).Count(new(Attachment)) + } else { + lowerKeyWord := strings.ToLower(opts.Keyword) + + cond = cond.And(builder.Or(builder.Like{"LOWER(attachment.name)", lowerKeyWord}, builder.Like{"LOWER(attachment.description)", lowerKeyWord})) + count, err = sess.Table(&Attachment{}).Where(cond).Count(new(AttachmentInfo)) + + } + + if err != nil { + return nil, 0, fmt.Errorf("Count: %v", err) + } + + if opts.Page >= 0 && opts.PageSize > 0 { + var start int + if opts.Page == 0 { + start = 0 + } else { + start = (opts.Page - 1) * opts.PageSize + } + sess.Limit(opts.PageSize, start) + } + + sess.OrderBy("attachment.created_unix DESC") + attachments := make([]*AttachmentInfo, 0, setting.UI.DatasetPagingNum) + if err := sess.Table(&Attachment{}).Where(cond). + Find(&attachments); err != nil { + return nil, 0, fmt.Errorf("Find: %v", err) + } + + if opts.NeedRepoInfo { + for _, attachment := range attachments { + dataset, err := GetDatasetByID(attachment.DatasetID) + if err != nil { + return nil, 0, fmt.Errorf("GetDatasetByID failed error: %v", err) + } + attachment.Recommend = dataset.Recommend + repo, err := GetRepositoryByID(dataset.RepoID) + if err == nil { + attachment.Repo = repo + } else { + return nil, 0, fmt.Errorf("GetRepositoryByID failed error: %v", err) + } + user, err := GetUserByID(attachment.UploaderID) + if err == nil { + attachment.RelAvatarLink = user.RelAvatarLink() + attachment.UserName = user.Name + } else { + return nil, 0, fmt.Errorf("GetUserByID failed error: %v", err) + } + } + } + + return attachments, count, nil +} diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 0e57fe1a0..d26f099e1 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -448,6 +448,7 @@ func PublicDataset(ctx *context.Context) { } func MyFavoriteDataset(ctx *context.Context) { + UserId := ctx.User.ID page := ctx.QueryInt("page") cloudbrainType := ctx.QueryInt("type") keyword := strings.Trim(ctx.Query("q"), " ") @@ -467,7 +468,7 @@ func MyFavoriteDataset(ctx *context.Context) { datasetIDs = append(datasetIDs, datasetStars[i].DatasetID) } - datasets, count, err := models.Attachments(&models.AttachmentsOptions{ + datasets, count, err := models.MyFvAttachments(&models.AttachmentsOptions{ ListOptions: models.ListOptions{ Page: page, PageSize: setting.UI.DatasetPagingNum, @@ -481,6 +482,7 @@ func MyFavoriteDataset(ctx *context.Context) { JustNeedZipFile: true, NeedRepoInfo: true, RecommendOnly: ctx.QueryBool("recommend"), + UserId: UserId, }) if err != nil { ctx.ServerError("datasets", err) From 93546b50656902f281b712475f589e94a39092af Mon Sep 17 00:00:00 2001 From: liuzx Date: Fri, 10 Jun 2022 11:48:30 +0800 Subject: [PATCH 02/13] fix-2208 --- models/attachment.go | 21 ------------------- routers/repo/dataset.go | 45 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index 2e9abfcda..dbc48f807 100755 --- a/models/attachment.go +++ b/models/attachment.go @@ -682,27 +682,6 @@ func MyFvAttachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) cond = cond.And( builder.Eq{"attachment.is_private": opts.IsPrivate}, ) - for _, DatasetID := range opts.DatasetIDs { - dataset, err := GetDatasetByID(DatasetID) - if err != nil { - return nil, 0, fmt.Errorf("GetDatasetByID failed error: %v", err) - } - repo, err := GetRepositoryByID(dataset.RepoID) - if err != nil { - return nil, 0, fmt.Errorf("GetRepositoryByID failed error: %v", err) - } - isCollaborator, _ := repo.IsCollaborator(opts.UserId) - if !isCollaborator { - cond = cond.And( - builder.Eq{"attachment.dataset_id": DatasetID}, - builder.Eq{"attachment.is_private": false}, - ) - } else { - cond = cond.And( - builder.Eq{"attachment.dataset_id": DatasetID}, - ) - } - } } if opts.RecommendOnly { cond = cond.And(builder.In("attachment.id", builder.Select("attachment.id"). diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index d26f099e1..b360dccee 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -452,7 +452,8 @@ func MyFavoriteDataset(ctx *context.Context) { page := ctx.QueryInt("page") cloudbrainType := ctx.QueryInt("type") keyword := strings.Trim(ctx.Query("q"), " ") - var datasetIDs []int64 + var NotColDatasetIDs []int64 + var IsColDatasetIDs []int64 datasetStars, err := models.GetDatasetStarByUser(ctx.User) if err != nil { ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("GetDatasetStarByUser failed", err))) @@ -464,18 +465,31 @@ func MyFavoriteDataset(ctx *context.Context) { }) return } - for i, _ := range datasetStars { - datasetIDs = append(datasetIDs, datasetStars[i].DatasetID) + for _, datasetStar := range datasetStars { + dataset, err := models.GetDatasetByID(datasetStar.DatasetID) + if err != nil { + return + } + repo, err := models.GetRepositoryByID(dataset.RepoID) + if err != nil { + return + } + isCollaborator, _ := repo.IsCollaborator(ctx.User.ID) + if !isCollaborator { + NotColDatasetIDs = append(NotColDatasetIDs, datasetStar.DatasetID) + } else { + IsColDatasetIDs = append(IsColDatasetIDs, datasetStar.DatasetID) + } } - datasets, count, err := models.MyFvAttachments(&models.AttachmentsOptions{ + NotColDatasets, NotColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ ListOptions: models.ListOptions{ Page: page, PageSize: setting.UI.DatasetPagingNum, }, Keyword: keyword, NeedDatasetIDs: true, - DatasetIDs: datasetIDs, + DatasetIDs: NotColDatasetIDs, NeedIsPrivate: true, IsPrivate: false, Type: cloudbrainType, @@ -489,6 +503,27 @@ func MyFavoriteDataset(ctx *context.Context) { return } + IsColDatasets, IsColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ + ListOptions: models.ListOptions{ + Page: page, + PageSize: setting.UI.DatasetPagingNum, + }, + Keyword: keyword, + NeedDatasetIDs: true, + DatasetIDs: NotColDatasetIDs, + NeedIsPrivate: false, + Type: cloudbrainType, + JustNeedZipFile: true, + NeedRepoInfo: true, + RecommendOnly: ctx.QueryBool("recommend"), + UserId: UserId, + }) + if err != nil { + ctx.ServerError("datasets", err) + return + } + datasets := IsColDatasets + NotColDatasets + count := NotColcount + IsColcount data, err := json.Marshal(datasets) if err != nil { log.Error("json.Marshal failed:", err.Error()) From 8f705a6fc6182e697c9e425b49969a153c62fe69 Mon Sep 17 00:00:00 2001 From: liuzx Date: Fri, 10 Jun 2022 14:29:36 +0800 Subject: [PATCH 03/13] fix-2208 --- routers/repo/dataset.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index b360dccee..f78cd1a2b 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -522,7 +522,7 @@ func MyFavoriteDataset(ctx *context.Context) { ctx.ServerError("datasets", err) return } - datasets := IsColDatasets + NotColDatasets + datasets := merge(IsColDatasets, NotColDatasets) count := NotColcount + IsColcount data, err := json.Marshal(datasets) if err != nil { @@ -541,6 +541,19 @@ func MyFavoriteDataset(ctx *context.Context) { }) } +func merge(a, b interface{}) interface{} { + + jb, err := json.Marshal(b) + if err != nil { + fmt.Println("Marshal error b:", err) + } + err = json.Unmarshal(jb, &a) + if err != nil { + fmt.Println("Unmarshal error b-a:", err) + } + + return a +} func GetDatasetStatus(ctx *context.Context) { From d0906406f9267291e4068a26f3e7ef022b7fb7a5 Mon Sep 17 00:00:00 2001 From: liuzx Date: Fri, 10 Jun 2022 17:17:11 +0800 Subject: [PATCH 04/13] update --- routers/repo/dataset.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 75fa35979..813c3081b 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -506,7 +506,7 @@ func MyFavoriteDataset(ctx *context.Context) { ctx.ServerError("datasets", err) return } - + //If is collaborator, there is no need to determine whether the dataset is private or public IsColDatasets, IsColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ ListOptions: models.ListOptions{ Page: page, @@ -528,6 +528,10 @@ func MyFavoriteDataset(ctx *context.Context) { } datasets := merge(IsColDatasets, NotColDatasets) count := NotColcount + IsColcount + + pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, getTotalPage(count, setting.UI.IssuePagingNum)) + pager.SetDefaultParams(ctx) + data, err := json.Marshal(datasets) if err != nil { log.Error("json.Marshal failed:", err.Error()) From 0a47ae4d8c50e07311e6b27956a9c9e2f73b0da2 Mon Sep 17 00:00:00 2001 From: liuzx Date: Fri, 10 Jun 2022 17:26:19 +0800 Subject: [PATCH 05/13] update --- models/attachment.go | 20 ++++++++++---------- routers/repo/dataset.go | 17 +++++++++-------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index a23cd3796..e8d2142a2 100755 --- a/models/attachment.go +++ b/models/attachment.go @@ -723,18 +723,18 @@ func MyFvAttachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) return nil, 0, fmt.Errorf("Count: %v", err) } - if opts.Page >= 0 && opts.PageSize > 0 { - var start int - if opts.Page == 0 { - start = 0 - } else { - start = (opts.Page - 1) * opts.PageSize - } - sess.Limit(opts.PageSize, start) - } + // if opts.Page >= 0 && opts.PageSize > 0 { + // var start int + // if opts.Page == 0 { + // start = 0 + // } else { + // start = (opts.Page - 1) * opts.PageSize + // } + // sess.Limit(opts.PageSize, start) + // } sess.OrderBy("attachment.created_unix DESC") - attachments := make([]*AttachmentInfo, 0, setting.UI.DatasetPagingNum) + attachments := make([]*AttachmentInfo, 0) if err := sess.Table(&Attachment{}).Where(cond). Find(&attachments); err != nil { return nil, 0, fmt.Errorf("Find: %v", err) diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 813c3081b..7f2aa572b 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -487,10 +487,6 @@ func MyFavoriteDataset(ctx *context.Context) { } NotColDatasets, NotColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ - ListOptions: models.ListOptions{ - Page: page, - PageSize: setting.UI.DatasetPagingNum, - }, Keyword: keyword, NeedDatasetIDs: true, DatasetIDs: NotColDatasetIDs, @@ -508,10 +504,6 @@ func MyFavoriteDataset(ctx *context.Context) { } //If is collaborator, there is no need to determine whether the dataset is private or public IsColDatasets, IsColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ - ListOptions: models.ListOptions{ - Page: page, - PageSize: setting.UI.DatasetPagingNum, - }, Keyword: keyword, NeedDatasetIDs: true, DatasetIDs: NotColDatasetIDs, @@ -562,6 +554,15 @@ func merge(a, b interface{}) interface{} { return a } +func getTotalPage(total int64, pageSize int) int { + + another := 0 + if int(total)%pageSize != 0 { + another = 1 + } + return int(total)/pageSize + another + +} func GetDatasetStatus(ctx *context.Context) { From beb3646f2af0b3fa798701e33b04cf38d599084d Mon Sep 17 00:00:00 2001 From: liuzx Date: Fri, 10 Jun 2022 18:03:50 +0800 Subject: [PATCH 06/13] fix-2208 --- models/attachment.go | 10 ---------- routers/repo/dataset.go | 38 +++++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index e8d2142a2..2bc405b01 100755 --- a/models/attachment.go +++ b/models/attachment.go @@ -723,16 +723,6 @@ func MyFvAttachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) return nil, 0, fmt.Errorf("Count: %v", err) } - // if opts.Page >= 0 && opts.PageSize > 0 { - // var start int - // if opts.Page == 0 { - // start = 0 - // } else { - // start = (opts.Page - 1) * opts.PageSize - // } - // sess.Limit(opts.PageSize, start) - // } - sess.OrderBy("attachment.created_unix DESC") attachments := make([]*AttachmentInfo, 0) if err := sess.Table(&Attachment{}).Where(cond). diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 7f2aa572b..464ae002e 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -521,19 +521,17 @@ func MyFavoriteDataset(ctx *context.Context) { datasets := merge(IsColDatasets, NotColDatasets) count := NotColcount + IsColcount - pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, getTotalPage(count, setting.UI.IssuePagingNum)) - pager.SetDefaultParams(ctx) - - data, err := json.Marshal(datasets) - if err != nil { - log.Error("json.Marshal failed:", err.Error()) - ctx.JSON(200, map[string]string{ - "result_code": "-1", - "error_msg": err.Error(), - "data": "", - }) - return + // page := ctx.QueryInt("page") + if page <= 0 { + page = 1 } + pagesize := ctx.QueryInt("pagesize") + if pagesize <= 0 { + pagesize = 5 + } + pageDatasetsInfo := getPageDatasets(datasets, page, pagesize) + + data, err := json.Marshal(pageDatasetsInfo) ctx.JSON(200, map[string]string{ "result_code": "0", "data": string(data), @@ -541,7 +539,21 @@ func MyFavoriteDataset(ctx *context.Context) { }) } -func merge(a, b interface{}) interface{} { +func getPageDatasets(datasets []*models.AttachmentInfo, page int, pagesize int) []*models.AttachmentInfo { + begin := (page - 1) * pagesize + end := (page) * pagesize + + if begin > len(datasets)-1 { + return nil + } + if end > len(datasets)-1 { + return datasets[begin:] + } else { + return datasets[begin:end] + } + +} +func merge(a, b []*models.AttachmentInfo) []*models.AttachmentInfo { jb, err := json.Marshal(b) if err != nil { From d9e043b47eb0822113db4218f28d6c40f88b9780 Mon Sep 17 00:00:00 2001 From: liuzx Date: Mon, 13 Jun 2022 11:15:31 +0800 Subject: [PATCH 07/13] fix-bug --- routers/repo/dataset.go | 50 +++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 464ae002e..9695e9221 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -453,11 +453,10 @@ func PublicDataset(ctx *context.Context) { func MyFavoriteDataset(ctx *context.Context) { UserId := ctx.User.ID - page := ctx.QueryInt("page") cloudbrainType := ctx.QueryInt("type") keyword := strings.Trim(ctx.Query("q"), " ") var NotColDatasetIDs []int64 - var IsColDatasetIDs []int64 + // var IsColDatasetIDs []int64 datasetStars, err := models.GetDatasetStarByUser(ctx.User) if err != nil { ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("GetDatasetStarByUser failed", err))) @@ -481,9 +480,10 @@ func MyFavoriteDataset(ctx *context.Context) { isCollaborator, _ := repo.IsCollaborator(ctx.User.ID) if !isCollaborator { NotColDatasetIDs = append(NotColDatasetIDs, datasetStar.DatasetID) - } else { - IsColDatasetIDs = append(IsColDatasetIDs, datasetStar.DatasetID) } + // } else { + // IsColDatasetIDs = append(IsColDatasetIDs, datasetStar.DatasetID) + // } } NotColDatasets, NotColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ @@ -503,25 +503,31 @@ func MyFavoriteDataset(ctx *context.Context) { return } //If is collaborator, there is no need to determine whether the dataset is private or public - IsColDatasets, IsColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ - Keyword: keyword, - NeedDatasetIDs: true, - DatasetIDs: NotColDatasetIDs, - NeedIsPrivate: false, - Type: cloudbrainType, - JustNeedZipFile: true, - NeedRepoInfo: true, - RecommendOnly: ctx.QueryBool("recommend"), - UserId: UserId, - }) - if err != nil { - ctx.ServerError("datasets", err) - return - } - datasets := merge(IsColDatasets, NotColDatasets) - count := NotColcount + IsColcount + // IsColDatasets, IsColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ + // Keyword: keyword, + // NeedDatasetIDs: true, + // DatasetIDs: IsColDatasetIDs, + // NeedIsPrivate: false, + // Type: cloudbrainType, + // JustNeedZipFile: true, + // NeedRepoInfo: true, + // RecommendOnly: ctx.QueryBool("recommend"), + // UserId: UserId, + // }) + // if err != nil { + // ctx.ServerError("datasets", err) + // return + // } + datasets := NotColDatasets + // datasets := merge(IsColDatasets, NotColDatasets) + // count := NotColcount + IsColcount + count := NotColcount + + // sort.Slice(datasets, func(i, j int) bool { + // return datasets[i].Attachment.CreatedUnix > datasets[j].Attachment.CreatedUnix + // }) - // page := ctx.QueryInt("page") + page := ctx.QueryInt("page") if page <= 0 { page = 1 } From ad9a4a5968ba39b0233effb71695f31a50163530 Mon Sep 17 00:00:00 2001 From: liuzx Date: Mon, 13 Jun 2022 11:31:57 +0800 Subject: [PATCH 08/13] fix-bug --- routers/repo/dataset.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 9695e9221..51ae7d497 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -538,6 +538,15 @@ func MyFavoriteDataset(ctx *context.Context) { pageDatasetsInfo := getPageDatasets(datasets, page, pagesize) data, err := json.Marshal(pageDatasetsInfo) + if err != nil { + log.Error("json.Marshal failed:", err.Error()) + ctx.JSON(200, map[string]string{ + "result_code": "-1", + "error_msg": err.Error(), + "data": "", + }) + return + } ctx.JSON(200, map[string]string{ "result_code": "0", "data": string(data), From eac8b469f9b1150af740d2181581b79e968cf233 Mon Sep 17 00:00:00 2001 From: liuzx Date: Tue, 14 Jun 2022 15:33:38 +0800 Subject: [PATCH 09/13] fix-2208 --- routers/repo/dataset.go | 87 +++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 51ae7d497..f19f82e93 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -456,7 +456,7 @@ func MyFavoriteDataset(ctx *context.Context) { cloudbrainType := ctx.QueryInt("type") keyword := strings.Trim(ctx.Query("q"), " ") var NotColDatasetIDs []int64 - // var IsColDatasetIDs []int64 + var IsColDatasetIDs []int64 datasetStars, err := models.GetDatasetStarByUser(ctx.User) if err != nil { ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("GetDatasetStarByUser failed", err))) @@ -480,10 +480,9 @@ func MyFavoriteDataset(ctx *context.Context) { isCollaborator, _ := repo.IsCollaborator(ctx.User.ID) if !isCollaborator { NotColDatasetIDs = append(NotColDatasetIDs, datasetStar.DatasetID) + } else { + IsColDatasetIDs = append(IsColDatasetIDs, datasetStar.DatasetID) } - // } else { - // IsColDatasetIDs = append(IsColDatasetIDs, datasetStar.DatasetID) - // } } NotColDatasets, NotColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ @@ -503,29 +502,30 @@ func MyFavoriteDataset(ctx *context.Context) { return } //If is collaborator, there is no need to determine whether the dataset is private or public - // IsColDatasets, IsColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ - // Keyword: keyword, - // NeedDatasetIDs: true, - // DatasetIDs: IsColDatasetIDs, - // NeedIsPrivate: false, - // Type: cloudbrainType, - // JustNeedZipFile: true, - // NeedRepoInfo: true, - // RecommendOnly: ctx.QueryBool("recommend"), - // UserId: UserId, - // }) - // if err != nil { - // ctx.ServerError("datasets", err) - // return - // } - datasets := NotColDatasets - // datasets := merge(IsColDatasets, NotColDatasets) - // count := NotColcount + IsColcount - count := NotColcount - - // sort.Slice(datasets, func(i, j int) bool { - // return datasets[i].Attachment.CreatedUnix > datasets[j].Attachment.CreatedUnix - // }) + IsColDatasets, IsColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ + Keyword: keyword, + NeedDatasetIDs: true, + DatasetIDs: IsColDatasetIDs, + NeedIsPrivate: false, + Type: cloudbrainType, + JustNeedZipFile: true, + NeedRepoInfo: true, + RecommendOnly: ctx.QueryBool("recommend"), + UserId: UserId, + }) + if err != nil { + ctx.ServerError("datasets", err) + return + } + for _, NotColDataset := range NotColDatasets { + IsColDatasets = append(IsColDatasets, NotColDataset) + } + datasets := IsColDatasets + count := NotColcount + IsColcount + //排序 + sort.Slice(datasets, func(i, j int) bool { + return datasets[i].Attachment.CreatedUnix > datasets[j].Attachment.CreatedUnix + }) page := ctx.QueryInt("page") if page <= 0 { @@ -536,8 +536,16 @@ func MyFavoriteDataset(ctx *context.Context) { pagesize = 5 } pageDatasetsInfo := getPageDatasets(datasets, page, pagesize) - + if pageDatasetsInfo == nil { + ctx.JSON(200, map[string]string{ + "result_code": "0", + "data": "[]", + "count": strconv.FormatInt(count, 10), + }) + return + } data, err := json.Marshal(pageDatasetsInfo) + log.Info("data:", data) if err != nil { log.Error("json.Marshal failed:", err.Error()) ctx.JSON(200, map[string]string{ @@ -554,32 +562,19 @@ func MyFavoriteDataset(ctx *context.Context) { }) } -func getPageDatasets(datasets []*models.AttachmentInfo, page int, pagesize int) []*models.AttachmentInfo { +func getPageDatasets(AttachmentInfos []*models.AttachmentInfo, page int, pagesize int) []*models.AttachmentInfo { begin := (page - 1) * pagesize end := (page) * pagesize - if begin > len(datasets)-1 { + if begin > len(AttachmentInfos)-1 { return nil } - if end > len(datasets)-1 { - return datasets[begin:] + if end > len(AttachmentInfos)-1 { + return AttachmentInfos[begin:] } else { - return datasets[begin:end] - } - -} -func merge(a, b []*models.AttachmentInfo) []*models.AttachmentInfo { - - jb, err := json.Marshal(b) - if err != nil { - fmt.Println("Marshal error b:", err) - } - err = json.Unmarshal(jb, &a) - if err != nil { - fmt.Println("Unmarshal error b-a:", err) + return AttachmentInfos[begin:end] } - return a } func getTotalPage(total int64, pageSize int) int { From 9a81532b4ccd6629b1cc7f48bbd439d1556b4da4 Mon Sep 17 00:00:00 2001 From: liuzx Date: Tue, 14 Jun 2022 15:50:15 +0800 Subject: [PATCH 10/13] fix-2208 --- routers/repo/dataset.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index f19f82e93..465afb374 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -478,10 +478,11 @@ func MyFavoriteDataset(ctx *context.Context) { return } isCollaborator, _ := repo.IsCollaborator(ctx.User.ID) - if !isCollaborator { - NotColDatasetIDs = append(NotColDatasetIDs, datasetStar.DatasetID) - } else { + + if repo.OwnerID == ctx.User.ID || isCollaborator { IsColDatasetIDs = append(IsColDatasetIDs, datasetStar.DatasetID) + } else { + NotColDatasetIDs = append(NotColDatasetIDs, datasetStar.DatasetID) } } From 620ff2ca2bf337d0424a7ed71410be87d0a9568c Mon Sep 17 00:00:00 2001 From: liuzx Date: Wed, 15 Jun 2022 09:38:48 +0800 Subject: [PATCH 11/13] fix-2208 --- routers/repo/dataset.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 465afb374..26d88f38d 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -479,7 +479,7 @@ func MyFavoriteDataset(ctx *context.Context) { } isCollaborator, _ := repo.IsCollaborator(ctx.User.ID) - if repo.OwnerID == ctx.User.ID || isCollaborator { + if repo.OwnerID == ctx.User.ID || isCollaborator || repo.Owner.IsUserPartOfOrg(ctx.User.ID) { IsColDatasetIDs = append(IsColDatasetIDs, datasetStar.DatasetID) } else { NotColDatasetIDs = append(NotColDatasetIDs, datasetStar.DatasetID) From 1eef408ad0cf9baaf5a40898beccfca32df84700 Mon Sep 17 00:00:00 2001 From: liuzx Date: Wed, 15 Jun 2022 11:18:05 +0800 Subject: [PATCH 12/13] update --- routers/repo/dataset.go | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index baf000a5b..4c1baf740 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -469,6 +469,7 @@ func MyFavoriteDataset(ctx *context.Context) { return } for _, datasetStar := range datasetStars { + DatasetIsCollaborator := DatasetIsCollaborator(ctx, datasetStar.DatasetID) dataset, err := models.GetDatasetByID(datasetStar.DatasetID) if err != nil { return @@ -477,9 +478,7 @@ func MyFavoriteDataset(ctx *context.Context) { if err != nil { return } - isCollaborator, _ := repo.IsCollaborator(ctx.User.ID) - - if repo.OwnerID == ctx.User.ID || isCollaborator || repo.Owner.IsUserPartOfOrg(ctx.User.ID) { + if repo.OwnerID == ctx.User.ID || DatasetIsCollaborator { IsColDatasetIDs = append(IsColDatasetIDs, datasetStar.DatasetID) } else { NotColDatasetIDs = append(NotColDatasetIDs, datasetStar.DatasetID) @@ -611,3 +610,30 @@ func GetDatasetStatus(ctx *context.Context) { "AttachmentStatus": fmt.Sprint(attachment.DecompressState), }) } +func DatasetIsCollaborator(ctx *context.Context, DatasetID int64) bool { + dataset, err := models.GetDatasetByID(DatasetID) + if err != nil { + log.Info("query dataset error") + } else { + repo, err := models.GetRepositoryByID(dataset.RepoID) + if err != nil { + log.Info("query repo error.") + } else { + repo.GetOwner() + if ctx.User != nil { + if repo.Owner.IsOrganization() { + if repo.Owner.IsUserPartOfOrg(ctx.User.ID) { + log.Info("org user may visit the attach.") + return true + } + } + isCollaborator, _ := repo.IsCollaborator(ctx.User.ID) + if isCollaborator { + log.Info("Collaborator user may visit the attach.") + return true + } + } + } + } + return false +} From 5bcc7b3ff591a43a38871b63344a6851bed9453c Mon Sep 17 00:00:00 2001 From: liuzx Date: Wed, 15 Jun 2022 15:28:09 +0800 Subject: [PATCH 13/13] update --- models/attachment.go | 91 ----------------------------------------- routers/repo/dataset.go | 4 +- 2 files changed, 2 insertions(+), 93 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index 2bc405b01..6fb98a07d 100755 --- a/models/attachment.go +++ b/models/attachment.go @@ -664,94 +664,3 @@ func Attachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) { return attachments, count, nil } - -func MyFvAttachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) { - sess := x.NewSession() - defer sess.Close() - - var cond = builder.NewCond() - if opts.NeedDatasetIDs { - cond = cond.And( - builder.In("attachment.dataset_id", opts.DatasetIDs), - ) - } - - if opts.UploaderID > 0 { - cond = cond.And( - builder.Eq{"attachment.uploader_id": opts.UploaderID}, - ) - } - - if (opts.Type) >= 0 { - cond = cond.And( - builder.Eq{"attachment.type": opts.Type}, - ) - } - - if opts.NeedIsPrivate { - cond = cond.And( - builder.Eq{"attachment.is_private": opts.IsPrivate}, - ) - } - if opts.RecommendOnly { - cond = cond.And(builder.In("attachment.id", builder.Select("attachment.id"). - From("attachment"). - Join("INNER", "dataset", "attachment.dataset_id = dataset.id and dataset.recommend=true"))) - } - - if opts.JustNeedZipFile { - var DecompressState []int32 - DecompressState = append(DecompressState, DecompressStateDone, DecompressStateIng, DecompressStateFailed) - cond = cond.And( - builder.In("attachment.decompress_state", DecompressState), - ) - } - - var count int64 - var err error - if len(opts.Keyword) == 0 { - count, err = sess.Where(cond).Count(new(Attachment)) - } else { - lowerKeyWord := strings.ToLower(opts.Keyword) - - cond = cond.And(builder.Or(builder.Like{"LOWER(attachment.name)", lowerKeyWord}, builder.Like{"LOWER(attachment.description)", lowerKeyWord})) - count, err = sess.Table(&Attachment{}).Where(cond).Count(new(AttachmentInfo)) - - } - - if err != nil { - return nil, 0, fmt.Errorf("Count: %v", err) - } - - sess.OrderBy("attachment.created_unix DESC") - attachments := make([]*AttachmentInfo, 0) - if err := sess.Table(&Attachment{}).Where(cond). - Find(&attachments); err != nil { - return nil, 0, fmt.Errorf("Find: %v", err) - } - - if opts.NeedRepoInfo { - for _, attachment := range attachments { - dataset, err := GetDatasetByID(attachment.DatasetID) - if err != nil { - return nil, 0, fmt.Errorf("GetDatasetByID failed error: %v", err) - } - attachment.Recommend = dataset.Recommend - repo, err := GetRepositoryByID(dataset.RepoID) - if err == nil { - attachment.Repo = repo - } else { - return nil, 0, fmt.Errorf("GetRepositoryByID failed error: %v", err) - } - user, err := GetUserByID(attachment.UploaderID) - if err == nil { - attachment.RelAvatarLink = user.RelAvatarLink() - attachment.UserName = user.Name - } else { - return nil, 0, fmt.Errorf("GetUserByID failed error: %v", err) - } - } - } - - return attachments, count, nil -} diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 4c1baf740..d3ae1624b 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -485,7 +485,7 @@ func MyFavoriteDataset(ctx *context.Context) { } } - NotColDatasets, NotColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ + NotColDatasets, NotColcount, err := models.Attachments(&models.AttachmentsOptions{ Keyword: keyword, NeedDatasetIDs: true, DatasetIDs: NotColDatasetIDs, @@ -502,7 +502,7 @@ func MyFavoriteDataset(ctx *context.Context) { return } //If is collaborator, there is no need to determine whether the dataset is private or public - IsColDatasets, IsColcount, err := models.MyFvAttachments(&models.AttachmentsOptions{ + IsColDatasets, IsColcount, err := models.Attachments(&models.AttachmentsOptions{ Keyword: keyword, NeedDatasetIDs: true, DatasetIDs: IsColDatasetIDs,