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.

444 lines
11 KiB

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