Browse Source

config recommend orgs

pull/364/head
lewis 3 years ago
parent
commit
f6a0af2044
8 changed files with 136 additions and 3 deletions
  1. +1
    -0
      models/models.go
  2. +48
    -0
      models/recommend_org.go
  3. +1
    -1
      models/repo_list.go
  4. +10
    -0
      modules/context/auth.go
  5. +1
    -1
      routers/home.go
  6. +64
    -0
      routers/operation/orgs.go
  7. +1
    -1
      routers/private/hook.go
  8. +10
    -0
      routers/routes/routes.go

+ 1
- 0
models/models.go View File

@@ -129,6 +129,7 @@ func init() {
new(Cloudbrain),
new(FileChunk),
new(BlockChain),
new(RecommendOrg),
)

gonicNames := []string{"SSL", "UID"}


+ 48
- 0
models/recommend_org.go View File

@@ -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
}

+ 1
- 1
models/repo_list.go View File

@@ -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


+ 10
- 0
modules/context/auth.go View File

@@ -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
}
}
}



+ 1
- 1
routers/home.go View File

@@ -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"


+ 64
- 0
routers/operation/orgs.go View File

@@ -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
}
}

+ 1
- 1
routers/private/hook.go View File

@@ -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"])
}


+ 10
- 0
routers/routes/routes.go View File

@@ -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)


Loading…
Cancel
Save