diff --git a/models/attachment.go b/models/attachment.go index bb99ba467..6fb98a07d 100755 --- a/models/attachment.go +++ b/models/attachment.go @@ -82,6 +82,7 @@ type AttachmentsOptions struct { NeedRepoInfo bool Keyword string RecommendOnly bool + UserId int64 } func (a *Attachment) AfterUpdate() { diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 05a89964f..d3ae1624b 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -452,10 +452,11 @@ func PublicDataset(ctx *context.Context) { } func MyFavoriteDataset(ctx *context.Context) { - page := ctx.QueryInt("page") + UserId := ctx.User.ID 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))) @@ -467,31 +468,84 @@ func MyFavoriteDataset(ctx *context.Context) { }) return } - for i, _ := range datasetStars { - datasetIDs = append(datasetIDs, datasetStars[i].DatasetID) + for _, datasetStar := range datasetStars { + DatasetIsCollaborator := DatasetIsCollaborator(ctx, datasetStar.DatasetID) + dataset, err := models.GetDatasetByID(datasetStar.DatasetID) + if err != nil { + return + } + repo, err := models.GetRepositoryByID(dataset.RepoID) + if err != nil { + return + } + if repo.OwnerID == ctx.User.ID || DatasetIsCollaborator { + IsColDatasetIDs = append(IsColDatasetIDs, datasetStar.DatasetID) + } else { + NotColDatasetIDs = append(NotColDatasetIDs, datasetStar.DatasetID) + } } - datasets, count, err := models.Attachments(&models.AttachmentsOptions{ - ListOptions: models.ListOptions{ - Page: page, - PageSize: setting.UI.DatasetPagingNum, - }, + NotColDatasets, NotColcount, err := models.Attachments(&models.AttachmentsOptions{ Keyword: keyword, NeedDatasetIDs: true, - DatasetIDs: datasetIDs, + DatasetIDs: NotColDatasetIDs, NeedIsPrivate: true, IsPrivate: false, Type: cloudbrainType, JustNeedZipFile: true, NeedRepoInfo: true, RecommendOnly: ctx.QueryBool("recommend"), + UserId: UserId, + }) + if err != nil { + 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.Attachments(&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 + }) - data, err := json.Marshal(datasets) + page := ctx.QueryInt("page") + if page <= 0 { + page = 1 + } + pagesize := ctx.QueryInt("pagesize") + if pagesize <= 0 { + 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{ @@ -507,6 +561,29 @@ func MyFavoriteDataset(ctx *context.Context) { "count": strconv.FormatInt(count, 10), }) +} +func getPageDatasets(AttachmentInfos []*models.AttachmentInfo, page int, pagesize int) []*models.AttachmentInfo { + begin := (page - 1) * pagesize + end := (page) * pagesize + + if begin > len(AttachmentInfos)-1 { + return nil + } + if end > len(AttachmentInfos)-1 { + return AttachmentInfos[begin:] + } else { + return AttachmentInfos[begin:end] + } + +} +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) { @@ -533,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 +}