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.

275 lines
6.7 KiB

  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "fmt"
  7. "path"
  8. "strings"
  9. "github.com/Unknwon/paginater"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/util"
  15. "code.gitea.io/gitea/routers/repo"
  16. )
  17. const (
  18. tplFollowers base.TplName = "user/meta/followers"
  19. tplStars base.TplName = "user/meta/stars"
  20. )
  21. // GetUserByName get user by name
  22. func GetUserByName(ctx *context.Context, name string) *models.User {
  23. user, err := models.GetUserByName(name)
  24. if err != nil {
  25. if models.IsErrUserNotExist(err) {
  26. ctx.NotFound("GetUserByName", nil)
  27. } else {
  28. ctx.ServerError("GetUserByName", err)
  29. }
  30. return nil
  31. }
  32. return user
  33. }
  34. // GetUserByParams returns user whose name is presented in URL paramenter.
  35. func GetUserByParams(ctx *context.Context) *models.User {
  36. return GetUserByName(ctx, ctx.Params(":username"))
  37. }
  38. // Profile render user's profile page
  39. func Profile(ctx *context.Context) {
  40. uname := ctx.Params(":username")
  41. // Special handle for FireFox requests favicon.ico.
  42. if uname == "favicon.ico" {
  43. ctx.ServeFile(path.Join(setting.StaticRootPath, "public/img/favicon.png"))
  44. return
  45. } else if strings.HasSuffix(uname, ".png") {
  46. ctx.Error(404)
  47. return
  48. }
  49. isShowKeys := false
  50. if strings.HasSuffix(uname, ".keys") {
  51. isShowKeys = true
  52. }
  53. ctxUser := GetUserByName(ctx, strings.TrimSuffix(uname, ".keys"))
  54. if ctx.Written() {
  55. return
  56. }
  57. // Show SSH keys.
  58. if isShowKeys {
  59. ShowSSHKeys(ctx, ctxUser.ID)
  60. return
  61. }
  62. if ctxUser.IsOrganization() {
  63. showOrgProfile(ctx)
  64. return
  65. }
  66. // Show OpenID URIs
  67. openIDs, err := models.GetUserOpenIDs(ctxUser.ID)
  68. if err != nil {
  69. ctx.ServerError("GetUserOpenIDs", err)
  70. return
  71. }
  72. ctx.Data["Title"] = ctxUser.DisplayName()
  73. ctx.Data["PageIsUserProfile"] = true
  74. ctx.Data["Owner"] = ctxUser
  75. ctx.Data["OpenIDs"] = openIDs
  76. showPrivate := ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == ctxUser.ID)
  77. orgs, err := models.GetOrgsByUserID(ctxUser.ID, showPrivate)
  78. if err != nil {
  79. ctx.ServerError("GetOrgsByUserIDDesc", err)
  80. return
  81. }
  82. ctx.Data["Orgs"] = orgs
  83. tab := ctx.Query("tab")
  84. ctx.Data["TabName"] = tab
  85. page := ctx.QueryInt("page")
  86. if page <= 0 {
  87. page = 1
  88. }
  89. var (
  90. repos []*models.Repository
  91. count int64
  92. orderBy models.SearchOrderBy
  93. )
  94. ctx.Data["SortType"] = ctx.Query("sort")
  95. switch ctx.Query("sort") {
  96. case "newest":
  97. orderBy = models.SearchOrderByNewest
  98. case "oldest":
  99. orderBy = models.SearchOrderByOldest
  100. case "recentupdate":
  101. orderBy = models.SearchOrderByRecentUpdated
  102. case "leastupdate":
  103. orderBy = models.SearchOrderByLeastUpdated
  104. case "reversealphabetically":
  105. orderBy = models.SearchOrderByAlphabeticallyReverse
  106. case "alphabetically":
  107. orderBy = models.SearchOrderByAlphabetically
  108. default:
  109. ctx.Data["SortType"] = "recentupdate"
  110. orderBy = models.SearchOrderByRecentUpdated
  111. }
  112. keyword := strings.Trim(ctx.Query("q"), " ")
  113. ctx.Data["Keyword"] = keyword
  114. switch tab {
  115. case "activity":
  116. retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser,
  117. IncludePrivate: showPrivate,
  118. OnlyPerformedBy: true,
  119. IncludeDeleted: false,
  120. })
  121. if ctx.Written() {
  122. return
  123. }
  124. case "stars":
  125. ctx.Data["PageIsProfileStarList"] = true
  126. if len(keyword) == 0 {
  127. repos, err = ctxUser.GetStarredRepos(showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
  128. if err != nil {
  129. ctx.ServerError("GetStarredRepos", err)
  130. return
  131. }
  132. count, err = ctxUser.GetStarredRepoCount(showPrivate)
  133. if err != nil {
  134. ctx.ServerError("GetStarredRepoCount", err)
  135. return
  136. }
  137. } else {
  138. repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
  139. Keyword: keyword,
  140. OwnerID: ctxUser.ID,
  141. OrderBy: orderBy,
  142. Private: showPrivate,
  143. Page: page,
  144. PageSize: setting.UI.User.RepoPagingNum,
  145. Starred: true,
  146. Collaborate: util.OptionalBoolFalse,
  147. })
  148. if err != nil {
  149. ctx.ServerError("SearchRepositoryByName", err)
  150. return
  151. }
  152. }
  153. ctx.Data["Repos"] = repos
  154. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  155. ctx.Data["Total"] = count
  156. default:
  157. if len(keyword) == 0 {
  158. var total int
  159. repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
  160. if err != nil {
  161. ctx.ServerError("GetRepositories", err)
  162. return
  163. }
  164. ctx.Data["Repos"] = repos
  165. if showPrivate {
  166. total = ctxUser.NumRepos
  167. } else {
  168. count, err := models.GetPublicRepositoryCount(ctxUser)
  169. if err != nil {
  170. ctx.ServerError("GetPublicRepositoryCount", err)
  171. return
  172. }
  173. total = int(count)
  174. }
  175. ctx.Data["Page"] = paginater.New(total, setting.UI.User.RepoPagingNum, page, 5)
  176. ctx.Data["Total"] = total
  177. } else {
  178. repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
  179. Keyword: keyword,
  180. OwnerID: ctxUser.ID,
  181. OrderBy: orderBy,
  182. Private: showPrivate,
  183. Page: page,
  184. IsProfile: true,
  185. PageSize: setting.UI.User.RepoPagingNum,
  186. })
  187. if err != nil {
  188. ctx.ServerError("SearchRepositoryByName", err)
  189. return
  190. }
  191. ctx.Data["Repos"] = repos
  192. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  193. ctx.Data["Total"] = count
  194. }
  195. }
  196. ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
  197. ctx.HTML(200, tplProfile)
  198. }
  199. // Followers render user's followers page
  200. func Followers(ctx *context.Context) {
  201. u := GetUserByParams(ctx)
  202. if ctx.Written() {
  203. return
  204. }
  205. ctx.Data["Title"] = u.DisplayName()
  206. ctx.Data["CardsTitle"] = ctx.Tr("user.followers")
  207. ctx.Data["PageIsFollowers"] = true
  208. ctx.Data["Owner"] = u
  209. repo.RenderUserCards(ctx, u.NumFollowers, u.GetFollowers, tplFollowers)
  210. }
  211. // Following render user's followering page
  212. func Following(ctx *context.Context) {
  213. u := GetUserByParams(ctx)
  214. if ctx.Written() {
  215. return
  216. }
  217. ctx.Data["Title"] = u.DisplayName()
  218. ctx.Data["CardsTitle"] = ctx.Tr("user.following")
  219. ctx.Data["PageIsFollowing"] = true
  220. ctx.Data["Owner"] = u
  221. repo.RenderUserCards(ctx, u.NumFollowing, u.GetFollowing, tplFollowers)
  222. }
  223. // Action response for follow/unfollow user request
  224. func Action(ctx *context.Context) {
  225. u := GetUserByParams(ctx)
  226. if ctx.Written() {
  227. return
  228. }
  229. var err error
  230. switch ctx.Params(":action") {
  231. case "follow":
  232. err = models.FollowUser(ctx.User.ID, u.ID)
  233. case "unfollow":
  234. err = models.UnfollowUser(ctx.User.ID, u.ID)
  235. }
  236. if err != nil {
  237. ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
  238. return
  239. }
  240. ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink())
  241. }