Skip to content

Commit 4142397

Browse files
authored
Fix mentionable users when writing issue comments (#32715)
Fix #32702
1 parent 838653d commit 4142397

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

routers/web/repo/issue.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,12 @@ func attachmentsHTML(ctx *context.Context, attachments []*repo_model.Attachment,
637637
return attachHTML
638638
}
639639

640-
// get all teams that current user can mention
641-
func handleTeamMentions(ctx *context.Context) {
640+
// handleMentionableAssigneesAndTeams gets all teams that current user can mention, and fills the assignee users to the context data
641+
func handleMentionableAssigneesAndTeams(ctx *context.Context, assignees []*user_model.User) {
642+
// TODO: need to figure out how many places this is really used, and rename it to "MentionableAssignees"
643+
// at the moment it is used on the issue list page, for the markdown editor mention
644+
ctx.Data["Assignees"] = assignees
645+
642646
if ctx.Doer == nil || !ctx.Repo.Owner.IsOrganization() {
643647
return
644648
}

routers/web/repo/issue_list.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,7 @@ func issues(ctx *context.Context, milestoneID, projectID int64, isPullOption opt
715715
ctx.ServerError("GetRepoAssignees", err)
716716
return
717717
}
718-
ctx.Data["Assignees"] = shared_user.MakeSelfOnTop(ctx.Doer, assigneeUsers)
719-
720-
handleTeamMentions(ctx)
718+
handleMentionableAssigneesAndTeams(ctx, shared_user.MakeSelfOnTop(ctx.Doer, assigneeUsers))
721719
if ctx.Written() {
722720
return
723721
}

routers/web/repo/issue_page_meta.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (d *IssuePageMetaData) retrieveAssigneesDataForIssueWriter(ctx *context.Con
148148
d.AssigneesData.SelectedAssigneeIDs = strings.Join(ids, ",")
149149
}
150150
// FIXME: this is a tricky part which writes ctx.Data["Mentionable*"]
151-
handleTeamMentions(ctx)
151+
handleMentionableAssigneesAndTeams(ctx, d.AssigneesData.CandidateAssignees)
152152
}
153153

154154
func (d *IssuePageMetaData) retrieveProjectsDataForIssueWriter(ctx *context.Context) {

routers/web/repo/pull.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
840840
ctx.ServerError("GetRepoAssignees", err)
841841
return
842842
}
843-
ctx.Data["Assignees"] = shared_user.MakeSelfOnTop(ctx.Doer, assigneeUsers)
844-
845-
handleTeamMentions(ctx)
843+
handleMentionableAssigneesAndTeams(ctx, shared_user.MakeSelfOnTop(ctx.Doer, assigneeUsers))
846844
if ctx.Written() {
847845
return
848846
}

routers/web/shared/user/helper.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44
package user
55

66
import (
7-
"sort"
7+
"slices"
88

99
"code.gitea.io/gitea/models/user"
1010
)
1111

1212
func MakeSelfOnTop(doer *user.User, users []*user.User) []*user.User {
1313
if doer != nil {
14-
sort.Slice(users, func(i, j int) bool {
15-
if users[i].ID == users[j].ID {
16-
return false
17-
}
18-
return users[i].ID == doer.ID // if users[i] is self, put it before others, so less=true
14+
idx := slices.IndexFunc(users, func(u *user.User) bool {
15+
return u.ID == doer.ID
1916
})
17+
if idx > 0 {
18+
newUsers := make([]*user.User, len(users))
19+
newUsers[0] = users[idx]
20+
copy(newUsers[1:], users[:idx])
21+
copy(newUsers[idx+1:], users[idx+1:])
22+
return newUsers
23+
}
2024
}
2125
return users
2226
}

0 commit comments

Comments
 (0)