Skip to content

Refactor commentTags functionality #17558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,32 @@ const (
CommentTagOwner
)

// WithTag enable a specific tag on the CommentTag.
func (ct CommentTag) WithTag(tag CommentTag) CommentTag {
ct |= (1 << tag)
return ct
}

func stringToCommentTag(tag string) CommentTag {
switch tag {
case "Poster":
return CommentTagPoster
case "Writer":
return CommentTagWriter
case "Owner":
return CommentTagOwner
default:
return CommentTagNone
}
}

// HasTag returns if a certain tag is enabled on the CommentTag.
func (ct CommentTag) HasTag(compareTag string) bool {
checkCommentTag := stringToCommentTag(compareTag)
bitValue := ct & (1 << checkCommentTag)
return (bitValue > 0)
}

// Comment represents a comment in commit and issue page.
type Comment struct {
ID int64 `xorm:"pk autoincr"`
Expand Down
42 changes: 25 additions & 17 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,32 +1020,40 @@ func commentTag(repo *models.Repository, poster *models.User, issue *models.Issu
if err != nil {
return models.CommentTagNone, err
}
if perm.IsOwner() {
if !poster.IsAdmin {
return models.CommentTagOwner, nil
}

ok, err := models.IsUserRealRepoAdmin(repo, poster)
if err != nil {
return models.CommentTagNone, err
}
// By default the poster has no tags.
pCommentTag := models.CommentTagNone

if ok {
return models.CommentTagOwner, nil
}
// Check if the poster is owner of the repo.
if perm.IsOwner() {
// If the poster isn't a admin, enable the owner Tag.
if !poster.IsAdmin {
pCommentTag = pCommentTag.WithTag(models.CommentTagOwner)
} else {

if ok, err = repo.IsCollaborator(poster.ID); ok && err == nil {
return models.CommentTagWriter, nil
// Otherwise check if poster is the real repo admin.
ok, err := models.IsUserRealRepoAdmin(repo, poster)
if err != nil {
return models.CommentTagNone, err
}
if ok {
pCommentTag = pCommentTag.WithTag(models.CommentTagOwner)
}
}
}

return models.CommentTagNone, err
// Is the poster can write code to the repo, enable Writer tag.
// Only enable this if the poster doesn't have the owner tag already.
if !pCommentTag.HasTag("Owner") && perm.CanWrite(models.UnitTypeCode) {
pCommentTag = pCommentTag.WithTag(models.CommentTagWriter)
}

if perm.CanWrite(models.UnitTypeCode) {
return models.CommentTagWriter, nil
// If the poster is the actual poster of the issue, enable Poster tag.
if issue.IsPoster(poster.ID) {
pCommentTag = pCommentTag.WithTag(models.CommentTagPoster)
}

return models.CommentTagNone, nil
return pCommentTag, nil
}

func getBranchData(ctx *context.Context, issue *models.Issue) {
Expand Down
13 changes: 8 additions & 5 deletions templates/repo/issue/view_content.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@
<div class="comment-header-right actions df ac">
{{if not $.Repository.IsArchived}}
{{if gt .Issue.ShowTag 0}}
<div class="ui basic label">
{{if eq .Issue.ShowTag 2}}
{{if (.Issue.ShowTag.HasTag "Writer")}}
<div class="ui basic label">
{{$.i18n.Tr "repo.issues.collaborator"}}
{{else if eq .Issue.ShowTag 3}}
</div>
{{end}}
{{if (.Issue.ShowTag.HasTag "Owner")}}
<div class="ui basic label">
{{$.i18n.Tr "repo.issues.owner"}}
{{end}}
</div>
</div>
{{end}}
{{end}}
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/issues/%d/reactions" $.RepoLink .Issue.Index)}}
{{template "repo/issue/view_content/context_menu" Dict "ctx" $ "item" .Issue "delete" false "issue" true "diff" false "IsCommentPoster" $.IsIssuePoster}}
Expand Down
48 changes: 24 additions & 24 deletions templates/repo/issue/view_content/comments.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,19 @@
</div>
<div class="comment-header-right actions df ac">
{{if not $.Repository.IsArchived}}
{{if or (and (eq .PosterID .Issue.PosterID) (eq .Issue.OriginalAuthorID 0)) (and (eq .Issue.OriginalAuthorID .OriginalAuthorID) (not (eq .OriginalAuthorID 0))) }}
{{if (.ShowTag.HasTag "Poster")}}
<div class="ui basic label">
{{$.i18n.Tr "repo.issues.poster"}}
</div>
{{end}}
{{if gt .ShowTag 0}}
{{if (.ShowTag.HasTag "Writer")}}
<div class="ui basic label">
{{if eq .ShowTag 2}}
{{$.i18n.Tr "repo.issues.collaborator"}}
{{else if eq .ShowTag 3}}
{{$.i18n.Tr "repo.issues.owner"}}
{{end}}
{{$.i18n.Tr "repo.issues.collaborator"}}
</div>
{{end}}
{{if (.ShowTag.HasTag "Owner")}}
<div class="ui basic label">
{{$.i18n.Tr "repo.issues.owner"}}
</div>
{{end}}
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}}
Expand Down Expand Up @@ -549,24 +550,23 @@
</span>
</div>
<div class="comment-header-right actions df ac">
{{if not $.Repository.IsArchived}}
{{if or (and (eq .PosterID $.Issue.PosterID) (eq $.Issue.OriginalAuthorID 0)) (eq $.Issue.OriginalAuthorID .OriginalAuthorID) }}
<div class="ui basic label">
{{$.i18n.Tr "repo.issues.poster"}}
</div>
{{end}}
{{if gt .ShowTag 0}}
<div class="ui basic label">
{{if eq .ShowTag 2}}
{{$.i18n.Tr "repo.issues.collaborator"}}
{{else if eq .ShowTag 3}}
{{$.i18n.Tr "repo.issues.owner"}}
{{end}}
</div>
{{end}}
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}}
{{template "repo/issue/view_content/context_menu" Dict "ctx" $ "item" . "delete" true "issue" true "diff" true "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}}
{{if (.ShowTag.HasTag "Poster")}}
<div class="ui basic label">
{{$.i18n.Tr "repo.issues.poster"}}
</div>
{{end}}
{{if (.ShowTag.HasTag "Writer")}}
<div class="ui basic label">
{{$.i18n.Tr "repo.issues.collaborator"}}
</div>
{{end}}
{{if (.ShowTag.HasTag "Owner")}}
<div class="ui basic label">
{{$.i18n.Tr "repo.issues.owner"}}
</div>
{{end}}
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.RepoLink .ID)}}
{{template "repo/issue/view_content/context_menu" Dict "ctx" $ "item" . "delete" true "issue" true "diff" true "IsCommentPoster" (and $.IsSigned (eq $.SignedUserID .PosterID))}}
</div>
</div>
<div class="text comment-content">
Expand Down