You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

profile.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. showPrivate := ctx.IsSigned && (ctx.Doer.IsAdmin || ctx.Doer.ID == ctx.ContextUser.ID)
  68. prepareUserProfileTabData(ctx, showPrivate, profileDbRepo, profileGitRepo, profileReadmeBlob)
  69. // call PrepareContextForProfileBigAvatar later to avoid re-querying the NumFollowers & NumFollowing
  70. shared_user.PrepareContextForProfileBigAvatar(ctx)
  71. ctx.HTML(http.StatusOK, tplProfile)
  72. }
  73. func prepareUserProfileTabData(ctx *context.Context, showPrivate bool, profileDbRepo *repo_model.Repository, profileGitRepo *git.Repository, profileReadme *git.Blob) {
  74. // if there is a profile readme, default to "overview" page, otherwise, default to "repositories" page
  75. // if there is not a profile readme, the overview tab should be treated as the repositories tab
  76. tab := ctx.FormString("tab")
  77. if tab == "" || tab == "overview" {
  78. if profileReadme != nil {
  79. tab = "overview"
  80. } else {
  81. tab = "repositories"
  82. }
  83. }
  84. ctx.Data["TabName"] = tab
  85. ctx.Data["HasProfileReadme"] = profileReadme != nil
  86. page := ctx.FormInt("page")
  87. if page <= 0 {
  88. page = 1
  89. }
  90. pagingNum := setting.UI.User.RepoPagingNum
  91. topicOnly := ctx.FormBool("topic")
  92. var (
  93. repos []*repo_model.Repository
  94. count int64
  95. total int
  96. orderBy db.SearchOrderBy
  97. )
  98. ctx.Data["SortType"] = ctx.FormString("sort")
  99. switch ctx.FormString("sort") {
  100. case "newest":
  101. orderBy = db.SearchOrderByNewest
  102. case "oldest":
  103. orderBy = db.SearchOrderByOldest
  104. case "recentupdate":
  105. orderBy = db.SearchOrderByRecentUpdated
  106. case "leastupdate":
  107. orderBy = db.SearchOrderByLeastUpdated
  108. case "reversealphabetically":
  109. orderBy = db.SearchOrderByAlphabeticallyReverse
  110. case "alphabetically":
  111. orderBy = db.SearchOrderByAlphabetically
  112. case "moststars":
  113. orderBy = db.SearchOrderByStarsReverse
  114. case "feweststars":
  115. orderBy = db.SearchOrderByStars
  116. case "mostforks":
  117. orderBy = db.SearchOrderByForksReverse
  118. case "fewestforks":
  119. orderBy = db.SearchOrderByForks
  120. default:
  121. ctx.Data["SortType"] = "recentupdate"
  122. orderBy = db.SearchOrderByRecentUpdated
  123. }
  124. keyword := ctx.FormTrim("q")
  125. ctx.Data["Keyword"] = keyword
  126. language := ctx.FormTrim("language")
  127. ctx.Data["Language"] = language
  128. followers, numFollowers, err := user_model.GetUserFollowers(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{
  129. PageSize: pagingNum,
  130. Page: page,
  131. })
  132. if err != nil {
  133. ctx.ServerError("GetUserFollowers", err)
  134. return
  135. }
  136. ctx.Data["NumFollowers"] = numFollowers
  137. following, numFollowing, err := user_model.GetUserFollowing(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{
  138. PageSize: pagingNum,
  139. Page: page,
  140. })
  141. if err != nil {
  142. ctx.ServerError("GetUserFollowing", err)
  143. return
  144. }
  145. ctx.Data["NumFollowing"] = numFollowing
  146. archived := ctx.FormOptionalBool("archived")
  147. ctx.Data["IsArchived"] = archived
  148. fork := ctx.FormOptionalBool("fork")
  149. ctx.Data["IsFork"] = fork
  150. mirror := ctx.FormOptionalBool("mirror")
  151. ctx.Data["IsMirror"] = mirror
  152. template := ctx.FormOptionalBool("template")
  153. ctx.Data["IsTemplate"] = template
  154. private := ctx.FormOptionalBool("private")
  155. ctx.Data["IsPrivate"] = private
  156. switch tab {
  157. case "followers":
  158. ctx.Data["Cards"] = followers
  159. total = int(numFollowers)
  160. case "following":
  161. ctx.Data["Cards"] = following
  162. total = int(numFollowing)
  163. case "activity":
  164. date := ctx.FormString("date")
  165. pagingNum = setting.UI.FeedPagingNum
  166. items, count, err := activities_model.GetFeeds(ctx, activities_model.GetFeedsOptions{
  167. RequestedUser: ctx.ContextUser,
  168. Actor: ctx.Doer,
  169. IncludePrivate: showPrivate,
  170. OnlyPerformedBy: true,
  171. IncludeDeleted: false,
  172. Date: date,
  173. ListOptions: db.ListOptions{
  174. PageSize: pagingNum,
  175. Page: page,
  176. },
  177. })
  178. if err != nil {
  179. ctx.ServerError("GetFeeds", err)
  180. return
  181. }
  182. ctx.Data["Feeds"] = items
  183. ctx.Data["Date"] = date
  184. total = int(count)
  185. case "stars":
  186. ctx.Data["PageIsProfileStarList"] = true
  187. repos, count, err = repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
  188. ListOptions: db.ListOptions{
  189. PageSize: pagingNum,
  190. Page: page,
  191. },
  192. Actor: ctx.Doer,
  193. Keyword: keyword,
  194. OrderBy: orderBy,
  195. Private: ctx.IsSigned,
  196. StarredByID: ctx.ContextUser.ID,
  197. Collaborate: optional.Some(false),
  198. TopicOnly: topicOnly,
  199. Language: language,
  200. IncludeDescription: setting.UI.SearchRepoDescription,
  201. Archived: archived,
  202. Fork: fork,
  203. Mirror: mirror,
  204. Template: template,
  205. IsPrivate: private,
  206. })
  207. if err != nil {
  208. ctx.ServerError("SearchRepository", err)
  209. return
  210. }
  211. total = int(count)
  212. case "watching":
  213. repos, count, err = repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
  214. ListOptions: db.ListOptions{
  215. PageSize: pagingNum,
  216. Page: page,
  217. },
  218. Actor: ctx.Doer,
  219. Keyword: keyword,
  220. OrderBy: orderBy,
  221. Private: ctx.IsSigned,
  222. WatchedByID: ctx.ContextUser.ID,
  223. Collaborate: optional.Some(false),
  224. TopicOnly: topicOnly,
  225. Language: language,
  226. IncludeDescription: setting.UI.SearchRepoDescription,
  227. Archived: archived,
  228. Fork: fork,
  229. Mirror: mirror,
  230. Template: template,
  231. IsPrivate: private,
  232. })
  233. if err != nil {
  234. ctx.ServerError("SearchRepository", err)
  235. return
  236. }
  237. total = int(count)
  238. case "overview":
  239. if bytes, err := profileReadme.GetBlobContent(setting.UI.MaxDisplayFileSize); err != nil {
  240. log.Error("failed to GetBlobContent: %v", err)
  241. } else {
  242. if profileContent, err := markdown.RenderString(&markup.RenderContext{
  243. Ctx: ctx,
  244. GitRepo: profileGitRepo,
  245. Links: markup.Links{
  246. // Give the repo link to the markdown render for the full link of media element.
  247. // the media link usually be like /[user]/[repoName]/media/branch/[branchName],
  248. // Eg. /Tom/.profile/media/branch/main
  249. // The branch shown on the profile page is the default branch, this need to be in sync with doc, see:
  250. // https://docs.gitea.com/usage/profile-readme
  251. Base: profileDbRepo.Link(),
  252. BranchPath: path.Join("branch", util.PathEscapeSegments(profileDbRepo.DefaultBranch)),
  253. },
  254. Metas: map[string]string{"mode": "document"},
  255. }, bytes); err != nil {
  256. log.Error("failed to RenderString: %v", err)
  257. } else {
  258. ctx.Data["ProfileReadme"] = profileContent
  259. }
  260. }
  261. default: // default to "repositories"
  262. repos, count, err = repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
  263. ListOptions: db.ListOptions{
  264. PageSize: pagingNum,
  265. Page: page,
  266. },
  267. Actor: ctx.Doer,
  268. Keyword: keyword,
  269. OwnerID: ctx.ContextUser.ID,
  270. OrderBy: orderBy,
  271. Private: ctx.IsSigned,
  272. Collaborate: optional.Some(false),
  273. TopicOnly: topicOnly,
  274. Language: language,
  275. IncludeDescription: setting.UI.SearchRepoDescription,
  276. Archived: archived,
  277. Fork: fork,
  278. Mirror: mirror,
  279. Template: template,
  280. IsPrivate: private,
  281. })
  282. if err != nil {
  283. ctx.ServerError("SearchRepository", err)
  284. return
  285. }
  286. total = int(count)
  287. }
  288. ctx.Data["Repos"] = repos
  289. ctx.Data["Total"] = total
  290. err = shared_user.LoadHeaderCount(ctx)
  291. if err != nil {
  292. ctx.ServerError("LoadHeaderCount", err)
  293. return
  294. }
  295. pager := context.NewPagination(total, pagingNum, page, 5)
  296. pager.SetDefaultParams(ctx)
  297. pager.AddParam(ctx, "tab", "TabName")
  298. if tab != "followers" && tab != "following" && tab != "activity" && tab != "projects" {
  299. pager.AddParam(ctx, "language", "Language")
  300. }
  301. if tab == "activity" {
  302. pager.AddParam(ctx, "date", "Date")
  303. }
  304. ctx.Data["Page"] = pager
  305. }
  306. // Action response for follow/unfollow user request
  307. func Action(ctx *context.Context) {
  308. var err error
  309. switch ctx.FormString("action") {
  310. case "follow":
  311. err = user_model.FollowUser(ctx, ctx.Doer.ID, ctx.ContextUser.ID)
  312. case "unfollow":
  313. err = user_model.UnfollowUser(ctx, ctx.Doer.ID, ctx.ContextUser.ID)
  314. }
  315. if err != nil {
  316. log.Error("Failed to apply action %q: %v", ctx.FormString("action"), err)
  317. ctx.Error(http.StatusBadRequest, fmt.Sprintf("Action %q failed", ctx.FormString("action")))
  318. return
  319. }
  320. if ctx.ContextUser.IsIndividual() {
  321. shared_user.PrepareContextForProfileBigAvatar(ctx)
  322. ctx.HTML(http.StatusOK, tplProfileBigAvatar)
  323. return
  324. } else if ctx.ContextUser.IsOrganization() {
  325. ctx.Data["Org"] = ctx.ContextUser
  326. ctx.Data["IsFollowing"] = ctx.Doer != nil && user_model.IsFollowing(ctx, ctx.Doer.ID, ctx.ContextUser.ID)
  327. ctx.HTML(http.StatusOK, tplFollowUnfollow)
  328. return
  329. }
  330. log.Error("Failed to apply action %q: unsupport context user type: %s", ctx.FormString("action"), ctx.ContextUser.Type)
  331. ctx.Error(http.StatusBadRequest, fmt.Sprintf("Action %q failed", ctx.FormString("action")))
  332. }