From cd298471e5f454bcd4d7ab3f2df08b4b6d5c47ee Mon Sep 17 00:00:00 2001 From: yuyuanshifu <747342561@qq.com> Date: Thu, 28 Jan 2021 15:25:50 +0800 Subject: [PATCH] blockchain index --- models/unit.go | 14 ++++++++++++++ routers/repo/blockchain.go | 38 +++++++++++++++++++++++++++++++++++--- routers/repo/issue.go | 9 ++++----- routers/routes/routes.go | 6 ++++++ 4 files changed, 59 insertions(+), 8 deletions(-) mode change 100644 => 100755 models/unit.go diff --git a/models/unit.go b/models/unit.go old mode 100644 new mode 100755 index 41712f238..3d1d03f18 --- a/models/unit.go +++ b/models/unit.go @@ -26,6 +26,7 @@ const ( UnitTypeExternalTracker // 7 ExternalTracker UnitTypeDatasets UnitType = 10 // 10 Dataset UnitTypeCloudBrain UnitType = 11 // 11 CloudBrain + UnitTypeBlockChain UnitType = 12 // 12 BlockChain ) // Value returns integer value for unit type @@ -53,6 +54,8 @@ func (u UnitType) String() string { return "UnitTypeDataset" case UnitTypeCloudBrain: return "UnitTypeCloudBrain" + case UnitTypeBlockChain: + return "UnitTypeBlockChain" } return fmt.Sprintf("Unknown UnitType %d", u) } @@ -76,6 +79,7 @@ var ( UnitTypeExternalTracker, UnitTypeDatasets, UnitTypeCloudBrain, + UnitTypeBlockChain, } // DefaultRepoUnits contains the default unit types @@ -87,6 +91,7 @@ var ( UnitTypeWiki, UnitTypeDatasets, UnitTypeCloudBrain, + UnitTypeBlockChain, } // NotAllowedDefaultRepoUnits contains units that can't be default @@ -268,6 +273,14 @@ var ( 6, } + UnitBlockChain = Unit{ + UnitTypeBlockChain, + "repo.blockchains", + "/blockchains", + "repo.blockchains.desc", + 7, + } + // Units contains all the units Units = map[UnitType]Unit{ UnitTypeCode: UnitCode, @@ -279,6 +292,7 @@ var ( UnitTypeExternalWiki: UnitExternalWiki, UnitTypeDatasets: UnitDataset, UnitTypeCloudBrain: UnitCloudBrain, + UnitTypeBlockChain: UnitBlockChain, } ) diff --git a/routers/repo/blockchain.go b/routers/repo/blockchain.go index cd9fdd550..04d19a746 100755 --- a/routers/repo/blockchain.go +++ b/routers/repo/blockchain.go @@ -1,13 +1,14 @@ package repo import ( - "encoding/json" - "strconv" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/blockchain" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" + "encoding/json" + "net/http" + "strconv" ) type BlockChainInitNotify struct { @@ -19,6 +20,37 @@ type BlockChainCommitNotify struct { CommitID string `json:"commitId"` TransactionHash string `json:"txHash"` } +const ( + tplBlockChainIndex base.TplName = "repo/blockchain/balance" +) + +// MustEnableDataset check if repository enable internal cb +func MustEnableBlockChain(ctx *context.Context) { + if !ctx.Repo.CanRead(models.UnitTypeBlockChain) { + ctx.NotFound("MustEnableBlockChain", nil) + return + } +} + +func BlockChainIndex(ctx *context.Context) { + MustEnableBlockChain(ctx) + repo := ctx.Repo.Repository + if repo.ContractAddress == "" || ctx.User.PublicKey == ""{ + log.Error("the repo(%d) or the user(%d) has not been initialized in block_chain", repo.RepoID, ctx.User.ID) + ctx.HTML(http.StatusInternalServerError, tplBlockChainIndex) + return + } + + res, err := blockchain.GetBalance(repo.ContractAddress, ctx.User.PublicKey) + if err != nil { + log.Error("GetBalance(%s) failed:%v", ctx.User.PublicKey, err) + ctx.HTML(http.StatusInternalServerError, tplBlockChainIndex) + return + } + + ctx.Data["balance"] = res.Data + ctx.HTML(200, tplBlockChainIndex) +} func HandleBlockChainInitNotify(ctx *context.Context) { var req BlockChainInitNotify diff --git a/routers/repo/issue.go b/routers/repo/issue.go index e23d10b92..555cd065f 100755 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -7,7 +7,6 @@ package repo import ( "bytes" - "code.gitea.io/gitea/modules/blockchain" "errors" "fmt" "io/ioutil" @@ -510,7 +509,7 @@ func NewIssue(ctx *context.Context) { ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWrite(models.UnitTypeIssues) - if ctx.Repo.Repository.ContractAddress == "" || ctx.User.PublicKey == ""{ + /*if ctx.Repo.Repository.ContractAddress == "" || ctx.User.PublicKey == ""{ log.Error("the repo(%d) or the user(%d) has not been initialized in block_chain", ctx.Repo.Repository.ID, ctx.User.ID) ctx.HTML(http.StatusInternalServerError, tplIssueNew) return @@ -523,7 +522,7 @@ func NewIssue(ctx *context.Context) { return } - ctx.Data["balance"] = res.Data + ctx.Data["balance"] = res.Data*/ ctx.HTML(200, tplIssueNew) } @@ -653,7 +652,7 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) { Ref: form.Ref, } - if repo.ContractAddress == "" || ctx.User.PublicKey == ""{ + /*if repo.ContractAddress == "" || ctx.User.PublicKey == ""{ log.Error("the repo(%d) or the user(%d) has not been initialized in block_chain", issue.RepoID, ctx.User.ID) ctx.HTML(http.StatusInternalServerError, tplIssueNew) return @@ -678,7 +677,7 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) { return } - issue.Amount = form.Rewards + issue.Amount = form.Rewards*/ if err := issue_service.NewIssue(repo, issue, labelIDs, attachments, assigneeIDs); err != nil { if models.IsErrUserDoesNotHaveAccessToRepo(err) { diff --git a/routers/routes/routes.go b/routers/routes/routes.go index cfb730490..67186ccd2 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -568,6 +568,8 @@ func RegisterRoutes(m *macaron.Macaron) { reqRepoDatasetWriter := context.RequireRepoWriter(models.UnitTypeDatasets) reqRepoCloudBrainReader := context.RequireRepoReader(models.UnitTypeCloudBrain) reqRepoCloudBrainWriter := context.RequireRepoWriter(models.UnitTypeCloudBrain) + reqRepoBlockChainReader := context.RequireRepoReader(models.UnitTypeBlockChain) + //reqRepoBlockChainWriter := context.RequireRepoWriter(models.UnitTypeBlockChain) // ***** START: Organization ***** m.Group("/org", func() { @@ -911,6 +913,10 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/create", reqRepoCloudBrainWriter, bindIgnErr(auth.CreateCloudBrainForm{}), repo.CloudBrainCreate) }, context.RepoRef()) + m.Group("/blockchain", func() { + m.Get("", reqRepoBlockChainReader, repo.BlockChainIndex) + }, context.RepoRef()) + m.Group("/wiki", func() { m.Get("/?:page", repo.Wiki) m.Get("/_pages", repo.WikiPages)