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.

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