選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

profile.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package user
  5. import (
  6. "fmt"
  7. "net/http"
  8. "path"
  9. "strings"
  10. activities_model "code.gitea.io/gitea/models/activities"
  11. "code.gitea.io/gitea/models/db"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/modules/base"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/markup"
  18. "code.gitea.io/gitea/modules/markup/markdown"
  19. "code.gitea.io/gitea/modules/optional"
  20. "code.gitea.io/gitea/modules/setting"
  21. "code.gitea.io/gitea/modules/util"
  22. "code.gitea.io/gitea/routers/web/feed"
  23. "code.gitea.io/gitea/routers/web/org"
  24. shared_user "code.gitea.io/gitea/routers/web/shared/user"
  25. "code.gitea.io/gitea/services/context"
  26. )
  27. const (
  28. tplProfileBigAvatar base.TplName = "shared/user/profile_big_avatar"
  29. tplFollowUnfollow base.TplName = "org/follow_unfollow"
  30. )
  31. // OwnerProfile render profile page for a user or a organization (aka, repo owner)
  32. func OwnerProfile(ctx *context.Context) {
  33. if strings.Contains(ctx.Req.Header.Get("Accept"), "application/rss+xml") {
  34. feed.ShowUserFeedRSS(ctx)
  35. return
  36. }
  37. if strings.Contains(ctx.Req.Header.Get("Accept"), "application/atom+xml") {
  38. feed.ShowUserFeedAtom(ctx)
  39. return
  40. }
  41. if ctx.ContextUser.IsOrganization() {
  42. org.Home(ctx)
  43. } else {
  44. userProfile(ctx)
  45. }
  46. }
  47. func userProfile(ctx *context.Context) {
  48. // check view permissions
  49. if !user_model.IsUserVisibleToViewer(ctx, ctx.ContextUser, ctx.Doer) {
  50. ctx.NotFound("user", fmt.Errorf(ctx.ContextUser.Name))
  51. return
  52. }
  53. ctx.Data["Title"] = ctx.ContextUser.DisplayName()
  54. ctx.Data["PageIsUserProfile"] = true
  55. // prepare heatmap data
  56. if setting.Service.EnableUserHeatmap {
  57. data, err := activities_model.GetUserHeatmapDataByUser(ctx, ctx.ContextUser, ctx.Doer)
  58. if err != nil {
  59. ctx.ServerError("GetUserHeatmapDataByUser", err)
  60. return
  61. }
  62. ctx.Data["HeatmapData"] = data
  63. ctx.Data["HeatmapTotalContributions"] = activities_model.GetTotalContributionsInHeatmap(data)
  64. }
  65. profileDbRepo, profileGitRepo, profileReadmeBlob, profileClose := shared_user.FindUserProfileReadme(ctx, ctx.Doer)
  66. defer profileClose()
  67. prepareUserProfileTabData(ctx, profileDbRepo, profileGitRepo, profileReadmeBlob)
  68. // call PrepareContextForProfileBigAvatar later to avoid re-querying the NumFollowers & NumFollowing
  69. shared_user.PrepareContextForProfileBigAvatar(ctx)
  70. ctx.HTML(http.StatusOK, tplProfile)
  71. }
  72. func prepareUserProfileTabData(ctx *context.Context, profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadme *git.Blob) {
  73. // if there is a profile readme, default to "overview" page, otherwise, default to "repositories" page
  74. // if there is not a profile readme, the overview tab should be treated as the repositories tab
  75. tab := ctx.FormString("tab")
  76. if tab == "" || tab == "overview" {
  77. if profileReadme != nil {
  78. tab = "overview"
  79. } else {
  80. tab = "repositories"
  81. }
  82. }
  83. ctx.Data["TabName"] = tab
  84. ctx.Data["HasProfileReadme"] = profileReadme != nil
  85. page := ctx.FormInt("page")
  86. if page <= 0 {
  87. page = 1
  88. }
  89. pagingNum := setting.UI.User.RepoPagingNum
  90. topicOnly := ctx.FormBool("topic")
  91. var (
  92. repos []*repo_model.Repository
  93. count int64
  94. total int
  95. orderBy db.SearchOrderBy
  96. )
  97. ctx.Data["SortType"] = ctx.FormString("sort")
  98. switch ctx.FormString("sort") {
  99. case "newest":
  100. orderBy = db.SearchOrderByNewest
  101. case "oldest":
  102. orderBy = db.SearchOrderByOldest
  103. case "recentupdate":
  104. orderBy = db.SearchOrderByRecentUpdated
  105. case "leastupdate":
  106. orderBy = db.SearchOrderByLeastUpdated
  107. case "reversealphabetically":
  108. orderBy = db.SearchOrderByAlphabeticallyReverse
  109. case "alphabetically":
  110. orderBy = db.SearchOrderByAlphabetically
  111. case "moststars":
  112. orderBy = db.SearchOrderByStarsReverse
  113. case "feweststars":
  114. orderBy = db.SearchOrderByStars
  115. case "mostforks":
  116. orderBy = db.SearchOrderByForksReverse
  117. case "fewestforks":
  118. orderBy = db.SearchOrderByForks
  119. default:
  120. ctx.Data["SortType"] = "recentupdate"
  121. orderBy = db.SearchOrderByRecentUpdated
  122. }
  123. keyword := ctx.FormTrim("q")
  124. ctx.Data["Keyword"] = keyword
  125. language := ctx.FormTrim("language")
  126. ctx.Data["Language"] = language
  127. followers, numFollowers, err := user_model.GetUserFollowers(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{
  128. PageSize: pagingNum,
  129. Page: page,
  130. })
  131. if err != nil {
  132. ctx.ServerError("GetUserFollowers", err)
  133. return
  134. }
  135. ctx.Data["NumFollowers"] = numFollowers
  136. following, numFollowing, err := user_model.GetUserFollowing(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{
  137. PageSize: pagingNum,
  138. Page: page,
  139. })
  140. if err != nil {
  141. ctx.ServerError("GetUserFollowing", err)
  142. return
  143. }
  144. ctx.Data["NumFollowing"] = numFollowing
  145. archived := ctx.FormOptionalBool("archived")
  146. ctx.Data["IsArchived"] = archived
  147. fork := ctx.FormOptionalBool("fork")
  148. ctx.Data["IsFork"] = fork
  149. mirror := ctx.FormOptionalBool("mirror")
  150. ctx.Data["IsMirror"] = mirror
  151. template := ctx.FormOptionalBool("template")
  152. ctx.Data["IsTemplate"] = template
  153. private := ctx.FormOptionalBool("private")
  154. ctx.Data["IsPrivate"] = private
  155. switch tab {
  156. case "followers":
  157. ctx.Data["Cards"] = followers
  158. total = int(numFollowers)
  159. case "following":
  160. ctx.Data["Cards"] = following
  161. total = int(numFollowing)
  162. case "activity":
  163. date := ctx.FormString("date")
  164. pagingNum = setting.UI.FeedPagingNum
  165. items, count, err := activities_model.GetFeeds(ctx, activities_model.GetFeedsOptions{
  166. RequestedUser: ctx.ContextUser,
  167. Actor: ctx.Doer,
  168. IncludePrivate: true,
  169. OnlyPerformedBy: true,
  170. IncludeDeleted: false,
  171. Date: date,
  172. ListOptions: db.ListOptions{
  173. PageSize: pagingNum,
  174. Page: page,
  175. },
  176. })
  177. if err != nil {
  178. ctx.ServerError("GetFeeds", err)
  179. return
  180. }
  181. ctx.Data["Feeds"] = items
  182. ctx.Data["Date"] = date
  183. total = int(count)
  184. case "stars":
  185. ctx.Data["PageIsProfileStarList"] = true
  186. repos, count, err = repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
  187. ListOptions: db.ListOptions{
  188. PageSize: pagingNum,
  189. Page: page,
  190. },
  191. Actor: ctx.Doer,
  192. Keyword: keyword,
  193. OrderBy: orderBy,
  194. Private: ctx.IsSigned,
  195. StarredByID: ctx.ContextUser.ID,
  196. Collaborate: optional.Some(false),
  197. TopicOnly: topicOnly,
  198. Language: language,
  199. IncludeDescription: setting.UI.SearchRepoDescription,
  200. Archived: archived,
  201. Fork: fork,
  202. Mirror: mirror,
  203. Template: template,
  204. IsPrivate: private,
  205. })
  206. if err != nil {
  207. ctx.ServerError("SearchRepository", err)
  208. return
  209. }
  210. total = int(count)
  211. case "watching":
  212. repos, count, err = repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
  213. ListOptions: db.ListOptions{
  214. PageSize: pagingNum,
  215. Page: page,
  216. },
  217. Actor: ctx.Doer,
  218. Keyword: keyword,
  219. OrderBy: orderBy,
  220. Private: ctx.IsSigned,
  221. WatchedByID: ctx.ContextUser.ID,
  222. Collaborate: optional.Some(false),
  223. TopicOnly: topicOnly,
  224. Language: language,
  225. IncludeDescription: setting.UI.SearchRepoDescription,
  226. Archived: archived,
  227. Fork: fork,
  228. Mirror: mirror,
  229. Template: template,
  230. IsPrivate: private,
  231. })
  232. if err != nil {
  233. ctx.ServerError("SearchRepository", err)
  234. return
  235. }
  236. total = int(count)
  237. case "overview":
  238. if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
  239. log.Error("failed to GetBlobContent: %v", err)
  240. } else {
  241. if profileContent, err := markdown.RenderString(&markup.RenderContext{
  242. Ctx: ctx,
  243. GitRepo: profileGitRepo,
  244. Links: markup.Links{
  245. // Give the repo link to the markdown render for the full link of media element.
  246. // the media link usually be like /[user]/[repoName]/media/branch/[branchName],
  247. // Eg. /Tom/.profile/media/branch/main
  248. // The branch shown on the profile page is the default branch, this need to be in sync with doc, see:
  249. // https://docs.gitea.com/usage/profile-readme
  250. Base: profileDbRepo.Link(),
  251. BranchPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)),
  252. },
  253. Metas: map[string]string{"mode": "document"},
  254. }, bytes); err != nil {
  255. log.Error("failed to RenderString: %v", err)
  256. } else {
  257. ctx.Data["ProfileReadme"] = profileContent
  258. }
  259. }
  260. default: // default to "repositories"
  261. repos, count, err = repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
  262. ListOptions: db.ListOptions{
  263. PageSize: pagingNum,
  264. Page: page,
  265. },
  266. Actor: ctx.Doer,
  267. Keyword: keyword,
  268. OwnerID: ctx.ContextUser.ID,
  269. OrderBy: orderBy,
  270. Private: ctx.IsSigned,
  271. Collaborate: optional.Some(false),
  272. TopicOnly: topicOnly,
  273. Language: language,
  274. IncludeDescription: setting.UI.SearchRepoDescription,
  275. Archived: archived,
  276. Fork: fork,
  277. Mirror: mirror,
  278. Template: template,
  279. IsPrivate: private,
  280. })
  281. if err != nil {
  282. ctx.ServerError("SearchRepository", err)
  283. return
  284. }
  285. total = int(count)
  286. }
  287. ctx.Data["Repos"] = repos
  288. ctx.Data["Total"] = total
  289. err = shared_user.LoadHeaderCount(ctx)
  290. if err != nil {
  291. ctx.ServerError("LoadHeaderCount", err)
  292. return
  293. }
  294. pager := context.NewPagination(total, pagingNum, page, 5)
  295. pager.SetDefaultParams(ctx)
  296. pager.AddParamString("tab", tab)
  297. if tab != "followers" && tab != "following" && tab != "activity" && tab != "projects" {
  298. pager.AddParamString("language", language)
  299. }
  300. if tab == "activity" {
  301. if ctx.Data["Date"] != nil {
  302. pager.AddParamString("date", fmt.Sprint(ctx.Data["Date"]))
  303. }
  304. }
  305. ctx.Data["Page"] = pager
  306. }
  307. // Action response for follow/unfollow user request
  308. func Action(ctx *context.Context) {
  309. var err error
  310. switch ctx.FormString("action") {
  311. case "follow":
  312. err = user_model.FollowUser(ctx, ctx.Doer, ctx.ContextUser)
  313. case "unfollow":
  314. err = user_model.UnfollowUser(ctx, ctx.Doer.ID, ctx.ContextUser.ID)
  315. }
  316. if err != nil {
  317. log.Error("Failed to apply action %q: %v", ctx.FormString("action"), err)
  318. ctx.Error(http.StatusBadRequest, fmt.Sprintf("Action %q failed", ctx.FormString("action")))
  319. return
  320. }
  321. if ctx.ContextUser.IsIndividual() {
  322. shared_user.PrepareContextForProfileBigAvatar(ctx)
  323. ctx.HTML(http.StatusOK, tplProfileBigAvatar)
  324. return
  325. } else if ctx.ContextUser.IsOrganization() {
  326. ctx.Data["Org"] = ctx.ContextUser
  327. ctx.Data["IsFollowing"] = ctx.Doer != nil && user_model.IsFollowing(ctx, ctx.Doer.ID, ctx.ContextUser.ID)
  328. ctx.HTML(http.StatusOK, tplFollowUnfollow)
  329. return
  330. }
  331. log.Error("Failed to apply action %q: unsupport context user type: %s", ctx.FormString("action"), ctx.ContextUser.Type)
  332. ctx.Error(http.StatusBadRequest, fmt.Sprintf("Action %q failed", ctx.FormString("action")))
  333. }