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.

288 lines
7.1 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. topicOnly := ctx.QueryBool("topic")
  90. var (
  91. repos []*models.Repository
  92. count int64
  93. orderBy models.SearchOrderBy
  94. )
  95. ctx.Data["SortType"] = ctx.Query("sort")
  96. switch ctx.Query("sort") {
  97. case "newest":
  98. orderBy = models.SearchOrderByNewest
  99. case "oldest":
  100. orderBy = models.SearchOrderByOldest
  101. case "recentupdate":
  102. orderBy = models.SearchOrderByRecentUpdated
  103. case "leastupdate":
  104. orderBy = models.SearchOrderByLeastUpdated
  105. case "reversealphabetically":
  106. orderBy = models.SearchOrderByAlphabeticallyReverse
  107. case "alphabetically":
  108. orderBy = models.SearchOrderByAlphabetically
  109. case "moststars":
  110. orderBy = models.SearchOrderByStarsReverse
  111. case "feweststars":
  112. orderBy = models.SearchOrderByStars
  113. case "mostforks":
  114. orderBy = models.SearchOrderByForksReverse
  115. case "fewestforks":
  116. orderBy = models.SearchOrderByForks
  117. default:
  118. ctx.Data["SortType"] = "recentupdate"
  119. orderBy = models.SearchOrderByRecentUpdated
  120. }
  121. keyword := strings.Trim(ctx.Query("q"), " ")
  122. ctx.Data["Keyword"] = keyword
  123. switch tab {
  124. case "activity":
  125. retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser,
  126. IncludePrivate: showPrivate,
  127. OnlyPerformedBy: true,
  128. IncludeDeleted: false,
  129. })
  130. if ctx.Written() {
  131. return
  132. }
  133. case "stars":
  134. ctx.Data["PageIsProfileStarList"] = true
  135. if len(keyword) == 0 {
  136. repos, err = ctxUser.GetStarredRepos(showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
  137. if err != nil {
  138. ctx.ServerError("GetStarredRepos", err)
  139. return
  140. }
  141. count, err = ctxUser.GetStarredRepoCount(showPrivate)
  142. if err != nil {
  143. ctx.ServerError("GetStarredRepoCount", err)
  144. return
  145. }
  146. } else {
  147. repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
  148. Keyword: keyword,
  149. OwnerID: ctxUser.ID,
  150. OrderBy: orderBy,
  151. Private: showPrivate,
  152. Page: page,
  153. PageSize: setting.UI.User.RepoPagingNum,
  154. Starred: true,
  155. Collaborate: util.OptionalBoolFalse,
  156. TopicOnly: topicOnly,
  157. })
  158. if err != nil {
  159. ctx.ServerError("SearchRepositoryByName", err)
  160. return
  161. }
  162. }
  163. ctx.Data["Repos"] = repos
  164. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  165. ctx.Data["Total"] = count
  166. default:
  167. if len(keyword) == 0 {
  168. var total int
  169. repos, err = models.GetUserRepositories(ctxUser.ID, showPrivate, page, setting.UI.User.RepoPagingNum, orderBy.String())
  170. if err != nil {
  171. ctx.ServerError("GetRepositories", err)
  172. return
  173. }
  174. ctx.Data["Repos"] = repos
  175. if showPrivate {
  176. total = ctxUser.NumRepos
  177. } else {
  178. count, err := models.GetPublicRepositoryCount(ctxUser)
  179. if err != nil {
  180. ctx.ServerError("GetPublicRepositoryCount", err)
  181. return
  182. }
  183. total = int(count)
  184. }
  185. ctx.Data["Page"] = paginater.New(total, setting.UI.User.RepoPagingNum, page, 5)
  186. ctx.Data["Total"] = total
  187. } else {
  188. repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
  189. Keyword: keyword,
  190. OwnerID: ctxUser.ID,
  191. OrderBy: orderBy,
  192. Private: showPrivate,
  193. Page: page,
  194. IsProfile: true,
  195. PageSize: setting.UI.User.RepoPagingNum,
  196. Collaborate: util.OptionalBoolFalse,
  197. TopicOnly: topicOnly,
  198. })
  199. if err != nil {
  200. ctx.ServerError("SearchRepositoryByName", err)
  201. return
  202. }
  203. ctx.Data["Repos"] = repos
  204. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  205. ctx.Data["Total"] = count
  206. }
  207. }
  208. ctx.Data["ShowUserEmail"] = setting.UI.ShowUserEmail
  209. ctx.HTML(200, tplProfile)
  210. }
  211. // Followers render user's followers page
  212. func Followers(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.followers")
  219. ctx.Data["PageIsFollowers"] = true
  220. ctx.Data["Owner"] = u
  221. repo.RenderUserCards(ctx, u.NumFollowers, u.GetFollowers, tplFollowers)
  222. }
  223. // Following render user's followering page
  224. func Following(ctx *context.Context) {
  225. u := GetUserByParams(ctx)
  226. if ctx.Written() {
  227. return
  228. }
  229. ctx.Data["Title"] = u.DisplayName()
  230. ctx.Data["CardsTitle"] = ctx.Tr("user.following")
  231. ctx.Data["PageIsFollowing"] = true
  232. ctx.Data["Owner"] = u
  233. repo.RenderUserCards(ctx, u.NumFollowing, u.GetFollowing, tplFollowers)
  234. }
  235. // Action response for follow/unfollow user request
  236. func Action(ctx *context.Context) {
  237. u := GetUserByParams(ctx)
  238. if ctx.Written() {
  239. return
  240. }
  241. var err error
  242. switch ctx.Params(":action") {
  243. case "follow":
  244. err = models.FollowUser(ctx.User.ID, u.ID)
  245. case "unfollow":
  246. err = models.UnfollowUser(ctx.User.ID, u.ID)
  247. }
  248. if err != nil {
  249. ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
  250. return
  251. }
  252. ctx.RedirectToFirst(ctx.Query("redirect_to"), u.HomeLink())
  253. }