|
- // Copyright 2014 The Gogs Authors. All rights reserved.
- // Copyright 2018 The Gitea Authors. All rights reserved.
- // Use of this source code is governed by a MIT-style
- // license that can be found in the LICENSE file.
-
- package repo
-
- import (
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/base"
- "code.gitea.io/gitea/modules/context"
- "code.gitea.io/gitea/services/mailer"
- "fmt"
- "strconv"
- )
-
- const (
- tplFunds base.TplName = "repo/fund/list"
- )
-
- // Funds render repository branch page
- func Funds(ctx *context.Context) {
- ctx.Data["Title"] = "Funds"
- ctx.Data["PageIsFund"] = true
- page := ctx.QueryInt("page")
- funds, err := models.GetFunds(ctx.Repo.Repository.ID, page)
- if err != nil {
- ctx.ServerError("GetFunds", err)
- return
- }
- ctx.Data["Funds"] = funds
- ctx.Data["FundingToCollaboration"] = ctx.Repo.Repository.FundingToCollaboration
- ctx.HTML(200, tplFunds)
- }
-
- func Funding(ctx *context.Context) {
- //减少发送者点数
- //增加接收者点数
- //创建transfer记录
- var err error
- Qty, err := strconv.Atoi(ctx.Query("qty"))
- if err != nil {
- ctx.Flash.Error("请输入数字")
- }
- if Qty < 1 {
- ctx.Flash.Error("请至少发1个红包!")
- ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink+"/funds")
- return
- }
- var repoid int
- repoid, err = strconv.Atoi(ctx.Query("repoid"))
-
- repoPoint, err := models.GetRepoPointByUserID(ctx.User.ID)
- if err != nil {
- ctx.ServerError("GetUserByName", err)
- return
- }
- if ctx.User.Point-repoPoint < Qty {
- ctx.Flash.Error(fmt.Sprintf("余额不足,转出红包数目应该少于(个人账户余额减去个人名下仓库需要支出的红包数目!个人余额%d;名下仓库需要支出%d", ctx.User.Point, repoPoint))
- ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink+"/funds")
- return
- }
- why := ctx.Query("why")
- toUserName := ctx.Query("toid")
- err = models.TransferPoint(ctx.User.Name,
- why,
- toUserName,
- Qty)
- if err != nil {
- ctx.ServerError("Transfer", err)
- return
- }
- err = models.NewFund(ctx.User.Name,
- int64(repoid),
- int64(Qty))
- if err != nil {
- ctx.ServerError("Transfer", err)
- return
- }
- toUser, err := models.GetUserByName(toUserName)
- if err != nil {
- if models.IsErrUserNotExist(err) {
- ctx.NotFound("GetUserByName", nil)
- } else {
- ctx.ServerError("GetUserByName", err)
- }
- return
- }
-
- got, err := ctx.Repo.Repository.IsCollaborator(ctx.User.ID)
- if err != nil {
- ctx.ServerError("GetUserByName", err)
- return
- }
- if !ctx.Repo.IsOwner() && !got {
- if int64(Qty) >= ctx.Repo.Repository.FundingToCollaboration && ctx.Repo.Repository.FundingToCollaboration != 0 {
- userNeedAdd := &models.User{
- ID: ctx.User.ID,
- }
- err = ctx.Repo.Repository.AddCollaborator(userNeedAdd)
- if err != nil {
- ctx.ServerError("AddCollaborator", err)
- return
- }
- if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(userNeedAdd.ID, models.AccessModeRead); err != nil {
- ctx.ServerError("AddCollaborator/ChangeCollaborationAccessMode", err)
- return
- }
- }
- }
- //发通知邮件给接受积分的人
- mailer.SendRecievePointNotifyMail(
- ctx.User, //积分发送者
- toUser, //积分接受者
- Qty, //积分点数
- why) //原因
-
- ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink+"/funds")
- }
|