| 
 | 1 | +// Copyright 2024 The Gitea Authors. All rights reserved.  | 
 | 2 | +// SPDX-License-Identifier: MIT  | 
 | 3 | + | 
 | 4 | +package repo  | 
 | 5 | + | 
 | 6 | +import (  | 
 | 7 | +	"net/http"  | 
 | 8 | +	"slices"  | 
 | 9 | +	"strings"  | 
 | 10 | + | 
 | 11 | +	repo_model "code.gitea.io/gitea/models/repo"  | 
 | 12 | +	user_model "code.gitea.io/gitea/models/user"  | 
 | 13 | +	"code.gitea.io/gitea/modules/setting"  | 
 | 14 | +	shared_user "code.gitea.io/gitea/routers/web/shared/user"  | 
 | 15 | +	"code.gitea.io/gitea/services/context"  | 
 | 16 | +)  | 
 | 17 | + | 
 | 18 | +type userSearchInfo struct {  | 
 | 19 | +	UserID     int64  `json:"user_id"`  | 
 | 20 | +	UserName   string `json:"username"`  | 
 | 21 | +	AvatarLink string `json:"avatar_link"`  | 
 | 22 | +	FullName   string `json:"full_name"`  | 
 | 23 | +}  | 
 | 24 | + | 
 | 25 | +type userSearchResponse struct {  | 
 | 26 | +	Results []*userSearchInfo `json:"results"`  | 
 | 27 | +}  | 
 | 28 | + | 
 | 29 | +// IssuePosters get posters for current repo's issues/pull requests  | 
 | 30 | +func IssuePosters(ctx *context.Context) {  | 
 | 31 | +	issuePosters(ctx, false)  | 
 | 32 | +}  | 
 | 33 | + | 
 | 34 | +func PullPosters(ctx *context.Context) {  | 
 | 35 | +	issuePosters(ctx, true)  | 
 | 36 | +}  | 
 | 37 | + | 
 | 38 | +func issuePosters(ctx *context.Context, isPullList bool) {  | 
 | 39 | +	repo := ctx.Repo.Repository  | 
 | 40 | +	search := strings.TrimSpace(ctx.FormString("q"))  | 
 | 41 | +	posters, err := repo_model.GetIssuePostersWithSearch(ctx, repo, isPullList, search, setting.UI.DefaultShowFullName)  | 
 | 42 | +	if err != nil {  | 
 | 43 | +		ctx.JSON(http.StatusInternalServerError, err)  | 
 | 44 | +		return  | 
 | 45 | +	}  | 
 | 46 | + | 
 | 47 | +	if search == "" && ctx.Doer != nil {  | 
 | 48 | +		// the returned posters slice only contains limited number of users,  | 
 | 49 | +		// to make the current user (doer) can quickly filter their own issues, always add doer to the posters slice  | 
 | 50 | +		if !slices.ContainsFunc(posters, func(user *user_model.User) bool { return user.ID == ctx.Doer.ID }) {  | 
 | 51 | +			posters = append(posters, ctx.Doer)  | 
 | 52 | +		}  | 
 | 53 | +	}  | 
 | 54 | + | 
 | 55 | +	posters = shared_user.MakeSelfOnTop(ctx.Doer, posters)  | 
 | 56 | + | 
 | 57 | +	resp := &userSearchResponse{}  | 
 | 58 | +	resp.Results = make([]*userSearchInfo, len(posters))  | 
 | 59 | +	for i, user := range posters {  | 
 | 60 | +		resp.Results[i] = &userSearchInfo{UserID: user.ID, UserName: user.Name, AvatarLink: user.AvatarLink(ctx)}  | 
 | 61 | +		if setting.UI.DefaultShowFullName {  | 
 | 62 | +			resp.Results[i].FullName = user.FullName  | 
 | 63 | +		}  | 
 | 64 | +	}  | 
 | 65 | +	ctx.JSON(http.StatusOK, resp)  | 
 | 66 | +}  | 
0 commit comments