gitea源码

issue_poster.go 1.8KB

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