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.

448 lines
10 KiB

10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 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. "bytes"
  7. "fmt"
  8. "sort"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/util"
  14. "github.com/Unknwon/com"
  15. "github.com/Unknwon/paginater"
  16. )
  17. const (
  18. tplDashboard base.TplName = "user/dashboard/dashboard"
  19. tplIssues base.TplName = "user/dashboard/issues"
  20. tplProfile base.TplName = "user/profile"
  21. tplOrgHome base.TplName = "org/home"
  22. )
  23. // getDashboardContextUser finds out dashboard is viewing as which context user.
  24. func getDashboardContextUser(ctx *context.Context) *models.User {
  25. ctxUser := ctx.User
  26. orgName := ctx.Params(":org")
  27. if len(orgName) > 0 {
  28. // Organization.
  29. org, err := models.GetUserByName(orgName)
  30. if err != nil {
  31. if models.IsErrUserNotExist(err) {
  32. ctx.Handle(404, "GetUserByName", err)
  33. } else {
  34. ctx.Handle(500, "GetUserByName", err)
  35. }
  36. return nil
  37. }
  38. ctxUser = org
  39. }
  40. ctx.Data["ContextUser"] = ctxUser
  41. if err := ctx.User.GetOrganizations(true); err != nil {
  42. ctx.Handle(500, "GetOrganizations", err)
  43. return nil
  44. }
  45. ctx.Data["Orgs"] = ctx.User.Orgs
  46. return ctxUser
  47. }
  48. // retrieveFeeds loads feeds for the specified user
  49. func retrieveFeeds(ctx *context.Context, options models.GetFeedsOptions) {
  50. actions, err := models.GetFeeds(options)
  51. if err != nil {
  52. ctx.Handle(500, "GetFeeds", err)
  53. return
  54. }
  55. userCache := map[int64]*models.User{options.RequestedUser.ID: options.RequestedUser}
  56. if ctx.User != nil {
  57. userCache[ctx.User.ID] = ctx.User
  58. }
  59. repoCache := map[int64]*models.Repository{}
  60. for _, act := range actions {
  61. // Cache results to reduce queries.
  62. u, ok := userCache[act.ActUserID]
  63. if !ok {
  64. u, err = models.GetUserByID(act.ActUserID)
  65. if err != nil {
  66. if models.IsErrUserNotExist(err) {
  67. continue
  68. }
  69. ctx.Handle(500, "GetUserByID", err)
  70. return
  71. }
  72. userCache[act.ActUserID] = u
  73. }
  74. act.ActUser = u
  75. repo, ok := repoCache[act.RepoID]
  76. if !ok {
  77. repo, err = models.GetRepositoryByID(act.RepoID)
  78. if err != nil {
  79. if models.IsErrRepoNotExist(err) {
  80. continue
  81. }
  82. ctx.Handle(500, "GetRepositoryByID", err)
  83. return
  84. }
  85. }
  86. act.Repo = repo
  87. repoOwner, ok := userCache[repo.OwnerID]
  88. if !ok {
  89. repoOwner, err = models.GetUserByID(repo.OwnerID)
  90. if err != nil {
  91. if models.IsErrUserNotExist(err) {
  92. continue
  93. }
  94. ctx.Handle(500, "GetUserByID", err)
  95. return
  96. }
  97. }
  98. repo.Owner = repoOwner
  99. }
  100. ctx.Data["Feeds"] = actions
  101. }
  102. // Dashboard render the dashborad page
  103. func Dashboard(ctx *context.Context) {
  104. ctxUser := getDashboardContextUser(ctx)
  105. if ctx.Written() {
  106. return
  107. }
  108. ctx.Data["Title"] = ctxUser.DisplayName() + " - " + ctx.Tr("dashboard")
  109. ctx.Data["PageIsDashboard"] = true
  110. ctx.Data["PageIsNews"] = true
  111. ctx.Data["SearchLimit"] = setting.UI.User.RepoPagingNum
  112. var err error
  113. var mirrors []*models.Repository
  114. if ctxUser.IsOrganization() {
  115. env, err := ctxUser.AccessibleReposEnv(ctx.User.ID)
  116. if err != nil {
  117. ctx.Handle(500, "AccessibleReposEnv", err)
  118. return
  119. }
  120. mirrors, err = env.MirrorRepos()
  121. if err != nil {
  122. ctx.Handle(500, "env.MirrorRepos", err)
  123. return
  124. }
  125. } else {
  126. mirrors, err = ctxUser.GetMirrorRepositories()
  127. if err != nil {
  128. ctx.Handle(500, "GetMirrorRepositories", err)
  129. return
  130. }
  131. }
  132. ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum
  133. if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
  134. ctx.Handle(500, "MirrorRepositoryList.LoadAttributes", err)
  135. return
  136. }
  137. ctx.Data["MirrorCount"] = len(mirrors)
  138. ctx.Data["Mirrors"] = mirrors
  139. retrieveFeeds(ctx, models.GetFeedsOptions{RequestedUser: ctxUser,
  140. IncludePrivate: true,
  141. OnlyPerformedBy: false,
  142. IncludeDeleted: false,
  143. })
  144. if ctx.Written() {
  145. return
  146. }
  147. ctx.HTML(200, tplDashboard)
  148. }
  149. // Issues render the user issues page
  150. func Issues(ctx *context.Context) {
  151. isPullList := ctx.Params(":type") == "pulls"
  152. if isPullList {
  153. ctx.Data["Title"] = ctx.Tr("pull_requests")
  154. ctx.Data["PageIsPulls"] = true
  155. } else {
  156. ctx.Data["Title"] = ctx.Tr("issues")
  157. ctx.Data["PageIsIssues"] = true
  158. }
  159. ctxUser := getDashboardContextUser(ctx)
  160. if ctx.Written() {
  161. return
  162. }
  163. // Organization does not have view type and filter mode.
  164. var (
  165. viewType string
  166. sortType = ctx.Query("sort")
  167. filterMode = models.FilterModeAll
  168. )
  169. if ctxUser.IsOrganization() {
  170. viewType = "all"
  171. } else {
  172. viewType = ctx.Query("type")
  173. switch viewType {
  174. case "assigned":
  175. filterMode = models.FilterModeAssign
  176. case "created_by":
  177. filterMode = models.FilterModeCreate
  178. case "all": // filterMode already set to All
  179. default:
  180. viewType = "all"
  181. }
  182. }
  183. page := ctx.QueryInt("page")
  184. if page <= 1 {
  185. page = 1
  186. }
  187. repoID := ctx.QueryInt64("repo")
  188. isShowClosed := ctx.Query("state") == "closed"
  189. // Get repositories.
  190. var err error
  191. var userRepoIDs []int64
  192. if ctxUser.IsOrganization() {
  193. env, err := ctxUser.AccessibleReposEnv(ctx.User.ID)
  194. if err != nil {
  195. ctx.Handle(500, "AccessibleReposEnv", err)
  196. return
  197. }
  198. userRepoIDs, err = env.RepoIDs(1, ctxUser.NumRepos)
  199. if err != nil {
  200. ctx.Handle(500, "env.RepoIDs", err)
  201. return
  202. }
  203. } else {
  204. userRepoIDs, err = ctxUser.GetAccessRepoIDs()
  205. if err != nil {
  206. ctx.Handle(500, "ctxUser.GetAccessRepoIDs", err)
  207. return
  208. }
  209. }
  210. if len(userRepoIDs) <= 0 {
  211. userRepoIDs = []int64{-1}
  212. }
  213. opts := &models.IssuesOptions{
  214. IsClosed: util.OptionalBoolOf(isShowClosed),
  215. IsPull: util.OptionalBoolOf(isPullList),
  216. SortType: sortType,
  217. }
  218. if repoID > 0 {
  219. opts.RepoIDs = []int64{repoID}
  220. }
  221. switch filterMode {
  222. case models.FilterModeAll:
  223. if repoID > 0 {
  224. if !com.IsSliceContainsInt64(userRepoIDs, repoID) {
  225. // force an empty result
  226. opts.RepoIDs = []int64{-1}
  227. }
  228. } else {
  229. opts.RepoIDs = userRepoIDs
  230. }
  231. case models.FilterModeAssign:
  232. opts.AssigneeID = ctxUser.ID
  233. case models.FilterModeCreate:
  234. opts.PosterID = ctxUser.ID
  235. case models.FilterModeMention:
  236. opts.MentionedID = ctxUser.ID
  237. }
  238. counts, err := models.CountIssuesByRepo(opts)
  239. if err != nil {
  240. ctx.Handle(500, "CountIssuesByRepo", err)
  241. return
  242. }
  243. opts.Page = page
  244. opts.PageSize = setting.UI.IssuePagingNum
  245. issues, err := models.Issues(opts)
  246. if err != nil {
  247. ctx.Handle(500, "Issues", err)
  248. return
  249. }
  250. showReposMap := make(map[int64]*models.Repository, len(counts))
  251. for repoID := range counts {
  252. repo, err := models.GetRepositoryByID(repoID)
  253. if err != nil {
  254. ctx.Handle(500, "GetRepositoryByID", err)
  255. return
  256. }
  257. showReposMap[repoID] = repo
  258. }
  259. if repoID > 0 {
  260. if _, ok := showReposMap[repoID]; !ok {
  261. repo, err := models.GetRepositoryByID(repoID)
  262. if err != nil {
  263. ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[%d]%v", repoID, err))
  264. return
  265. }
  266. showReposMap[repoID] = repo
  267. }
  268. repo := showReposMap[repoID]
  269. // Check if user has access to given repository.
  270. if !repo.IsOwnedBy(ctxUser.ID) && !repo.HasAccess(ctxUser) {
  271. ctx.Status(404)
  272. return
  273. }
  274. }
  275. showRepos := models.RepositoryListOfMap(showReposMap)
  276. sort.Sort(showRepos)
  277. if err = showRepos.LoadAttributes(); err != nil {
  278. ctx.Handle(500, "LoadAttributes", err)
  279. return
  280. }
  281. for _, issue := range issues {
  282. issue.Repo = showReposMap[issue.RepoID]
  283. }
  284. issueStats, err := models.GetUserIssueStats(models.UserIssueStatsOptions{
  285. UserID: ctxUser.ID,
  286. RepoID: repoID,
  287. UserRepoIDs: userRepoIDs,
  288. FilterMode: filterMode,
  289. IsPull: isPullList,
  290. IsClosed: isShowClosed,
  291. })
  292. if err != nil {
  293. ctx.Handle(500, "GetUserIssueStats", err)
  294. return
  295. }
  296. var total int
  297. if !isShowClosed {
  298. total = int(issueStats.OpenCount)
  299. } else {
  300. total = int(issueStats.ClosedCount)
  301. }
  302. ctx.Data["Issues"] = issues
  303. ctx.Data["Repos"] = showRepos
  304. ctx.Data["Counts"] = counts
  305. ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  306. ctx.Data["IssueStats"] = issueStats
  307. ctx.Data["ViewType"] = viewType
  308. ctx.Data["SortType"] = sortType
  309. ctx.Data["RepoID"] = repoID
  310. ctx.Data["IsShowClosed"] = isShowClosed
  311. if isShowClosed {
  312. ctx.Data["State"] = "closed"
  313. } else {
  314. ctx.Data["State"] = "open"
  315. }
  316. ctx.HTML(200, tplIssues)
  317. }
  318. // ShowSSHKeys output all the ssh keys of user by uid
  319. func ShowSSHKeys(ctx *context.Context, uid int64) {
  320. keys, err := models.ListPublicKeys(uid)
  321. if err != nil {
  322. ctx.Handle(500, "ListPublicKeys", err)
  323. return
  324. }
  325. var buf bytes.Buffer
  326. for i := range keys {
  327. buf.WriteString(keys[i].OmitEmail())
  328. buf.WriteString("\n")
  329. }
  330. ctx.PlainText(200, buf.Bytes())
  331. }
  332. func showOrgProfile(ctx *context.Context) {
  333. ctx.SetParams(":org", ctx.Params(":username"))
  334. context.HandleOrgAssignment(ctx)
  335. if ctx.Written() {
  336. return
  337. }
  338. org := ctx.Org.Organization
  339. ctx.Data["Title"] = org.DisplayName()
  340. page := ctx.QueryInt("page")
  341. if page <= 0 {
  342. page = 1
  343. }
  344. var (
  345. repos []*models.Repository
  346. count int64
  347. err error
  348. )
  349. if ctx.IsSigned && !ctx.User.IsAdmin {
  350. env, err := org.AccessibleReposEnv(ctx.User.ID)
  351. if err != nil {
  352. ctx.Handle(500, "AccessibleReposEnv", err)
  353. return
  354. }
  355. repos, err = env.Repos(page, setting.UI.User.RepoPagingNum)
  356. if err != nil {
  357. ctx.Handle(500, "env.Repos", err)
  358. return
  359. }
  360. count, err = env.CountRepos()
  361. if err != nil {
  362. ctx.Handle(500, "env.CountRepos", err)
  363. return
  364. }
  365. ctx.Data["Repos"] = repos
  366. } else {
  367. showPrivate := ctx.IsSigned && ctx.User.IsAdmin
  368. repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum, "")
  369. if err != nil {
  370. ctx.Handle(500, "GetRepositories", err)
  371. return
  372. }
  373. ctx.Data["Repos"] = repos
  374. count = models.CountUserRepositories(org.ID, showPrivate)
  375. }
  376. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  377. if err := org.GetMembers(); err != nil {
  378. ctx.Handle(500, "GetMembers", err)
  379. return
  380. }
  381. ctx.Data["Members"] = org.Members
  382. ctx.Data["Teams"] = org.Teams
  383. ctx.HTML(200, tplOrgHome)
  384. }
  385. // Email2User show user page via email
  386. func Email2User(ctx *context.Context) {
  387. u, err := models.GetUserByEmail(ctx.Query("email"))
  388. if err != nil {
  389. if models.IsErrUserNotExist(err) {
  390. ctx.Handle(404, "GetUserByEmail", err)
  391. } else {
  392. ctx.Handle(500, "GetUserByEmail", err)
  393. }
  394. return
  395. }
  396. ctx.Redirect(setting.AppSubURL + "/user/" + u.Name)
  397. }