diff --git a/models/models.go b/models/models.go index 67892399a..2fab3d0dc 100755 --- a/models/models.go +++ b/models/models.go @@ -129,6 +129,7 @@ func init() { new(Cloudbrain), new(FileChunk), new(BlockChain), + new(RecommendOrg), ) gonicNames := []string{"SSL", "UID"} diff --git a/models/recommend_org.go b/models/recommend_org.go new file mode 100755 index 000000000..092ea8338 --- /dev/null +++ b/models/recommend_org.go @@ -0,0 +1,48 @@ +package models + +import ( + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/timeutil" +) + +type RecommendOrg struct { + ID int64 `xorm:"pk autoincr"` + Order int64 `xorm:"INDEX NOT NULL unique"` + OrgID int64 `xorm:"INDEX NOT NULL unique"` + CreatedUnix timeutil.TimeStamp `xorm:"created"` + + Org *User `xorm:"-"` +} + +type RecommendOrgList []*RecommendOrg + +func getRecommendOrgs(e Engine) (RecommendOrgList, error) { + orgs := make(RecommendOrgList, 0, 10) + err := e.Asc("order"). + Find(&orgs) + return orgs, err +} + +func GetRecommendOrgs() (RecommendOrgList, error) { + return getRecommendOrgs(x) +} + +func delRecommendOrgs(e Engine) error { + sql := "delete from recommend_org" + _, err := e.Exec(sql) + return err +} + +func UpdateRecommendOrgs(orgs RecommendOrgList) error { + if err := delRecommendOrgs(x); err != nil { + log.Error("delRecommendOrgs failed:%v", err.Error()) + return err + } + + if _, err := x.Insert(&orgs); err != nil { + log.Error("Insert failed:%v", err.Error()) + return err + } + + return nil +} diff --git a/models/repo_list.go b/models/repo_list.go index cae6c9cc8..57b6ebbd6 100755 --- a/models/repo_list.go +++ b/models/repo_list.go @@ -199,7 +199,7 @@ const ( SearchOrderByForksReverse SearchOrderBy = "num_forks DESC" SearchOrderByDownloadTimes SearchOrderBy = "download_times DESC" SearchOrderByHot SearchOrderBy = "(num_watches + num_stars + num_forks + clone_cnt) DESC" - SearchOrderByActive SearchOrderBy = "(num_issues + num_pulls) DESC" + SearchOrderByActive SearchOrderBy = "(num_issues + num_pulls + num_commit) DESC" ) // SearchRepositoryCondition creates a query condition according search repository options diff --git a/modules/context/auth.go b/modules/context/auth.go index 6aee002b5..3f53e6fce 100755 --- a/modules/context/auth.go +++ b/modules/context/auth.go @@ -26,6 +26,7 @@ type ToggleOptions struct { AdminRequired bool DisableCSRF bool BasicAuthRequired bool + OperationRequired bool } // Toggle returns toggle options as middleware @@ -142,6 +143,15 @@ func Toggle(options *ToggleOptions) macaron.Handler { return } } + + if options.OperationRequired { + //todo: add isOperator judgement + if !ctx.User.IsAdmin { + ctx.Error(403) + return + } + ctx.Data["PageIsOperation"] = true + } } } diff --git a/routers/home.go b/routers/home.go index d2daff8a5..087e4f57c 100755 --- a/routers/home.go +++ b/routers/home.go @@ -139,7 +139,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { case "hot": orderBy = models.SearchOrderByHot case "active": - orderBy = models.SearchOrderByHot + orderBy = models.SearchOrderByActive default: ctx.Data["SortType"] = "recentupdate" diff --git a/routers/operation/orgs.go b/routers/operation/orgs.go new file mode 100755 index 000000000..755c22172 --- /dev/null +++ b/routers/operation/orgs.go @@ -0,0 +1,64 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Copyright 2020 The Gitea Authors. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package operation + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/routers" +) + +const ( + tplOrgs base.TplName = "admin/org/list" +) + +type UpdateRecommendOrgs struct { + OrgInfos string `binding:"required"` +} + +type OrgInfo struct { + OrgID int64 `json:"org_id"` + Order int64 `json:"order"` +} + +type OrgInfos struct { + OrgInfo []OrgInfo `json:"org_infos"` +} + +// Organizations show all the organizations recommended +func Organizations(ctx *context.Context) { + ctx.Data["PageIsAdmin"] = true + ctx.Data["PageIsAdminOrganizations"] = true + + routers.RenderUserSearch(ctx, &models.SearchUserOptions{ + Type: models.UserTypeOrganization, + ListOptions: models.ListOptions{ + PageSize: setting.UI.Admin.OrgPagingNum, + }, + Visible: []structs.VisibleType{structs.VisibleTypePublic, structs.VisibleTypeLimited, structs.VisibleTypePrivate}, + }, tplOrgs) +} + +// UpdateRecommendOrganizations update the organizations recommended +func UpdateRecommendOrganizations(ctx *context.Context, req OrgInfos) { + orgs := make(models.RecommendOrgList, 0, 10) + for _, org := range req.OrgInfo { + orgs = append(orgs, &models.RecommendOrg{ + OrgID: org.OrgID, + Order: org.Order, + }) + } + + if err := models.UpdateRecommendOrgs(orgs); err != nil { + log.Error("UpdateRecommendOrgs failed:%v", err.Error(), ctx.Data["MsgID"]) + ctx.ServerError("UpdateRecommendOrgs failed", err) + return + } +} diff --git a/routers/private/hook.go b/routers/private/hook.go index 881ce3378..b05a8a33c 100755 --- a/routers/private/hook.go +++ b/routers/private/hook.go @@ -520,7 +520,7 @@ func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) { } } } - + if err := updateRepoCommitCnt(ctx, repo); err != nil { log.Error("updateRepoCommitCnt failed:%v", err.Error(), ctx.Data["MsgID"]) } diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 84e2c7487..763a751cb 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -6,6 +6,7 @@ package routes import ( "bytes" + "code.gitea.io/gitea/routers/operation" "encoding/gob" "net/http" "path" @@ -539,6 +540,15 @@ func RegisterRoutes(m *macaron.Macaron) { }, adminReq) // ***** END: Admin ***** + //operationReq := context.Toggle(&context.ToggleOptions{SignInRequired: true, OperationRequired: true}) + + // ***** START: Operation ***** + m.Group("/operation", func() { + m.Get("/config/recommend_org", operation.Organizations) + m.Post("/config/recommend_org", bindIgnErr(operation.OrgInfos{}), operation.UpdateRecommendOrganizations) + }) + // ***** END: Operation ***** + m.Group("", func() { m.Get("/:username", user.Profile) }, ignSignIn)