code.gitea.io/gitea@v1.21.7/routers/web/repo/issue_watch.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"net/http"
     8  	"strconv"
     9  
    10  	issues_model "code.gitea.io/gitea/models/issues"
    11  	"code.gitea.io/gitea/modules/context"
    12  	"code.gitea.io/gitea/modules/log"
    13  )
    14  
    15  // IssueWatch sets issue watching
    16  func IssueWatch(ctx *context.Context) {
    17  	issue := GetActionIssue(ctx)
    18  	if ctx.Written() {
    19  		return
    20  	}
    21  
    22  	if !ctx.IsSigned || (ctx.Doer.ID != issue.PosterID && !ctx.Repo.CanReadIssuesOrPulls(issue.IsPull)) {
    23  		if log.IsTrace() {
    24  			if ctx.IsSigned {
    25  				issueType := "issues"
    26  				if issue.IsPull {
    27  					issueType = "pulls"
    28  				}
    29  				log.Trace("Permission Denied: User %-v not the Poster (ID: %d) and cannot read %s in Repo %-v.\n"+
    30  					"User in Repo has Permissions: %-+v",
    31  					ctx.Doer,
    32  					issue.PosterID,
    33  					issueType,
    34  					ctx.Repo.Repository,
    35  					ctx.Repo.Permission)
    36  			} else {
    37  				log.Trace("Permission Denied: Not logged in")
    38  			}
    39  		}
    40  		ctx.Error(http.StatusForbidden)
    41  		return
    42  	}
    43  
    44  	watch, err := strconv.ParseBool(ctx.Req.PostForm.Get("watch"))
    45  	if err != nil {
    46  		ctx.ServerError("watch is not bool", err)
    47  		return
    48  	}
    49  
    50  	if err := issues_model.CreateOrUpdateIssueWatch(ctx, ctx.Doer.ID, issue.ID, watch); err != nil {
    51  		ctx.ServerError("CreateOrUpdateIssueWatch", err)
    52  		return
    53  	}
    54  
    55  	ctx.Redirect(issue.Link())
    56  }