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.

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