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.

390 lines
9.7 KiB

10 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 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. "github.com/go-gitea/gitea/models"
  11. "github.com/go-gitea/gitea/modules/base"
  12. "github.com/go-gitea/gitea/modules/context"
  13. "github.com/go-gitea/gitea/modules/setting"
  14. )
  15. const (
  16. DASHBOARD base.TplName = "user/dashboard/dashboard"
  17. ISSUES base.TplName = "user/dashboard/issues"
  18. PROFILE base.TplName = "user/profile"
  19. ORG_HOME base.TplName = "org/home"
  20. )
  21. // getDashboardContextUser finds out dashboard is viewing as which context user.
  22. func getDashboardContextUser(ctx *context.Context) *models.User {
  23. ctxUser := ctx.User
  24. orgName := ctx.Params(":org")
  25. if len(orgName) > 0 {
  26. // Organization.
  27. org, err := models.GetUserByName(orgName)
  28. if err != nil {
  29. if models.IsErrUserNotExist(err) {
  30. ctx.Handle(404, "GetUserByName", err)
  31. } else {
  32. ctx.Handle(500, "GetUserByName", err)
  33. }
  34. return nil
  35. }
  36. ctxUser = org
  37. }
  38. ctx.Data["ContextUser"] = ctxUser
  39. if err := ctx.User.GetOrganizations(true); err != nil {
  40. ctx.Handle(500, "GetOrganizations", err)
  41. return nil
  42. }
  43. ctx.Data["Orgs"] = ctx.User.Orgs
  44. return ctxUser
  45. }
  46. // retrieveFeeds loads feeds from database by given context user.
  47. // The user could be organization so it is not always the logged in user,
  48. // which is why we have to explicitly pass the context user ID.
  49. func retrieveFeeds(ctx *context.Context, ctxUser *models.User, userID, offset int64, isProfile bool) {
  50. actions, err := models.GetFeeds(ctxUser, userID, offset, isProfile)
  51. if err != nil {
  52. ctx.Handle(500, "GetFeeds", err)
  53. return
  54. }
  55. // Check access of private repositories.
  56. feeds := make([]*models.Action, 0, len(actions))
  57. unameAvatars := make(map[string]string)
  58. for _, act := range actions {
  59. // Cache results to reduce queries.
  60. _, ok := unameAvatars[act.ActUserName]
  61. if !ok {
  62. u, err := models.GetUserByName(act.ActUserName)
  63. if err != nil {
  64. if models.IsErrUserNotExist(err) {
  65. continue
  66. }
  67. ctx.Handle(500, "GetUserByName", err)
  68. return
  69. }
  70. unameAvatars[act.ActUserName] = u.RelAvatarLink()
  71. }
  72. act.ActAvatar = unameAvatars[act.ActUserName]
  73. feeds = append(feeds, act)
  74. }
  75. ctx.Data["Feeds"] = feeds
  76. }
  77. func Dashboard(ctx *context.Context) {
  78. ctxUser := getDashboardContextUser(ctx)
  79. if ctx.Written() {
  80. return
  81. }
  82. ctx.Data["Title"] = ctxUser.DisplayName() + " - " + ctx.Tr("dashboard")
  83. ctx.Data["PageIsDashboard"] = true
  84. ctx.Data["PageIsNews"] = true
  85. // Only user can have collaborative repositories.
  86. if !ctxUser.IsOrganization() {
  87. collaborateRepos, err := ctx.User.GetAccessibleRepositories(setting.UI.User.RepoPagingNum)
  88. if err != nil {
  89. ctx.Handle(500, "GetAccessibleRepositories", err)
  90. return
  91. } else if err = models.RepositoryList(collaborateRepos).LoadAttributes(); err != nil {
  92. ctx.Handle(500, "RepositoryList.LoadAttributes", err)
  93. return
  94. }
  95. ctx.Data["CollaborativeRepos"] = collaborateRepos
  96. }
  97. var err error
  98. var repos, mirrors []*models.Repository
  99. if ctxUser.IsOrganization() {
  100. repos, _, err = ctxUser.GetUserRepositories(ctx.User.ID, 1, setting.UI.User.RepoPagingNum)
  101. if err != nil {
  102. ctx.Handle(500, "GetUserRepositories", err)
  103. return
  104. }
  105. mirrors, err = ctxUser.GetUserMirrorRepositories(ctx.User.ID)
  106. if err != nil {
  107. ctx.Handle(500, "GetUserMirrorRepositories", err)
  108. return
  109. }
  110. } else {
  111. if err = ctxUser.GetRepositories(1, setting.UI.User.RepoPagingNum); err != nil {
  112. ctx.Handle(500, "GetRepositories", err)
  113. return
  114. }
  115. repos = ctxUser.Repos
  116. mirrors, err = ctxUser.GetMirrorRepositories()
  117. if err != nil {
  118. ctx.Handle(500, "GetMirrorRepositories", err)
  119. return
  120. }
  121. }
  122. ctx.Data["Repos"] = repos
  123. ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum
  124. if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
  125. ctx.Handle(500, "MirrorRepositoryList.LoadAttributes", err)
  126. return
  127. }
  128. ctx.Data["MirrorCount"] = len(mirrors)
  129. ctx.Data["Mirrors"] = mirrors
  130. retrieveFeeds(ctx, ctxUser, ctx.User.ID, 0, false)
  131. if ctx.Written() {
  132. return
  133. }
  134. ctx.HTML(200, DASHBOARD)
  135. }
  136. func Issues(ctx *context.Context) {
  137. isPullList := ctx.Params(":type") == "pulls"
  138. if isPullList {
  139. ctx.Data["Title"] = ctx.Tr("pull_requests")
  140. ctx.Data["PageIsPulls"] = true
  141. } else {
  142. ctx.Data["Title"] = ctx.Tr("issues")
  143. ctx.Data["PageIsIssues"] = true
  144. }
  145. ctxUser := getDashboardContextUser(ctx)
  146. if ctx.Written() {
  147. return
  148. }
  149. // Organization does not have view type and filter mode.
  150. var (
  151. viewType string
  152. sortType = ctx.Query("sort")
  153. filterMode = models.FilterModeAll
  154. assigneeID int64
  155. posterID int64
  156. )
  157. if ctxUser.IsOrganization() {
  158. viewType = "all"
  159. } else {
  160. viewType = ctx.Query("type")
  161. types := []string{"assigned", "created_by"}
  162. if !com.IsSliceContainsStr(types, viewType) {
  163. viewType = "all"
  164. }
  165. switch viewType {
  166. case "assigned":
  167. filterMode = models.FilterModeAssign
  168. assigneeID = ctxUser.ID
  169. case "created_by":
  170. filterMode = models.FilterModeCreate
  171. posterID = ctxUser.ID
  172. }
  173. }
  174. repoID := ctx.QueryInt64("repo")
  175. isShowClosed := ctx.Query("state") == "closed"
  176. // Get repositories.
  177. var err error
  178. var repos []*models.Repository
  179. if ctxUser.IsOrganization() {
  180. repos, _, err = ctxUser.GetUserRepositories(ctx.User.ID, 1, ctxUser.NumRepos)
  181. if err != nil {
  182. ctx.Handle(500, "GetRepositories", err)
  183. return
  184. }
  185. } else {
  186. if err := ctxUser.GetRepositories(1, ctx.User.NumRepos); err != nil {
  187. ctx.Handle(500, "GetRepositories", err)
  188. return
  189. }
  190. repos = ctxUser.Repos
  191. }
  192. allCount := 0
  193. repoIDs := make([]int64, 0, len(repos))
  194. showRepos := make([]*models.Repository, 0, len(repos))
  195. for _, repo := range repos {
  196. if (isPullList && repo.NumPulls == 0) ||
  197. (!isPullList &&
  198. (!repo.EnableIssues || repo.EnableExternalTracker || repo.NumIssues == 0)) {
  199. continue
  200. }
  201. repoIDs = append(repoIDs, repo.ID)
  202. if isPullList {
  203. allCount += repo.NumOpenPulls
  204. repo.NumOpenIssues = repo.NumOpenPulls
  205. repo.NumClosedIssues = repo.NumClosedPulls
  206. } else {
  207. allCount += repo.NumOpenIssues
  208. }
  209. if filterMode != models.FilterModeAll {
  210. // Calculate repository issue count with filter mode.
  211. numOpen, numClosed := repo.IssueStats(ctxUser.ID, filterMode, isPullList)
  212. repo.NumOpenIssues, repo.NumClosedIssues = int(numOpen), int(numClosed)
  213. }
  214. if repo.ID == repoID ||
  215. (isShowClosed && repo.NumClosedIssues > 0) ||
  216. (!isShowClosed && repo.NumOpenIssues > 0) {
  217. showRepos = append(showRepos, repo)
  218. }
  219. }
  220. ctx.Data["Repos"] = showRepos
  221. issueStats := models.GetUserIssueStats(repoID, ctxUser.ID, repoIDs, filterMode, isPullList)
  222. issueStats.AllCount = int64(allCount)
  223. page := ctx.QueryInt("page")
  224. if page <= 1 {
  225. page = 1
  226. }
  227. var total int
  228. if !isShowClosed {
  229. total = int(issueStats.OpenCount)
  230. } else {
  231. total = int(issueStats.ClosedCount)
  232. }
  233. ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  234. // Get issues.
  235. issues, err := models.Issues(&models.IssuesOptions{
  236. UserID: ctxUser.ID,
  237. AssigneeID: assigneeID,
  238. RepoID: repoID,
  239. PosterID: posterID,
  240. RepoIDs: repoIDs,
  241. Page: page,
  242. IsClosed: isShowClosed,
  243. IsPull: isPullList,
  244. SortType: sortType,
  245. })
  246. if err != nil {
  247. ctx.Handle(500, "Issues", err)
  248. return
  249. }
  250. // Get posters and repository.
  251. for i := range issues {
  252. issues[i].Repo, err = models.GetRepositoryByID(issues[i].RepoID)
  253. if err != nil {
  254. ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d]%v", issues[i].ID, err))
  255. return
  256. }
  257. if err = issues[i].Repo.GetOwner(); err != nil {
  258. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d]%v", issues[i].ID, err))
  259. return
  260. }
  261. }
  262. ctx.Data["Issues"] = issues
  263. ctx.Data["IssueStats"] = issueStats
  264. ctx.Data["ViewType"] = viewType
  265. ctx.Data["SortType"] = sortType
  266. ctx.Data["RepoID"] = repoID
  267. ctx.Data["IsShowClosed"] = isShowClosed
  268. if isShowClosed {
  269. ctx.Data["State"] = "closed"
  270. } else {
  271. ctx.Data["State"] = "open"
  272. }
  273. ctx.HTML(200, ISSUES)
  274. }
  275. func ShowSSHKeys(ctx *context.Context, uid int64) {
  276. keys, err := models.ListPublicKeys(uid)
  277. if err != nil {
  278. ctx.Handle(500, "ListPublicKeys", err)
  279. return
  280. }
  281. var buf bytes.Buffer
  282. for i := range keys {
  283. buf.WriteString(keys[i].OmitEmail())
  284. buf.WriteString("\n")
  285. }
  286. ctx.PlainText(200, buf.Bytes())
  287. }
  288. func showOrgProfile(ctx *context.Context) {
  289. ctx.SetParams(":org", ctx.Params(":username"))
  290. context.HandleOrgAssignment(ctx)
  291. if ctx.Written() {
  292. return
  293. }
  294. org := ctx.Org.Organization
  295. ctx.Data["Title"] = org.FullName
  296. page := ctx.QueryInt("page")
  297. if page <= 0 {
  298. page = 1
  299. }
  300. var (
  301. repos []*models.Repository
  302. count int64
  303. err error
  304. )
  305. if ctx.IsSigned && !ctx.User.IsAdmin {
  306. repos, count, err = org.GetUserRepositories(ctx.User.ID, page, setting.UI.User.RepoPagingNum)
  307. if err != nil {
  308. ctx.Handle(500, "GetUserRepositories", err)
  309. return
  310. }
  311. ctx.Data["Repos"] = repos
  312. } else {
  313. showPrivate := ctx.IsSigned && ctx.User.IsAdmin
  314. repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum)
  315. if err != nil {
  316. ctx.Handle(500, "GetRepositories", err)
  317. return
  318. }
  319. ctx.Data["Repos"] = repos
  320. count = models.CountUserRepositories(org.ID, showPrivate)
  321. }
  322. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  323. if err := org.GetMembers(); err != nil {
  324. ctx.Handle(500, "GetMembers", err)
  325. return
  326. }
  327. ctx.Data["Members"] = org.Members
  328. ctx.Data["Teams"] = org.Teams
  329. ctx.HTML(200, ORG_HOME)
  330. }
  331. func Email2User(ctx *context.Context) {
  332. u, err := models.GetUserByEmail(ctx.Query("email"))
  333. if err != nil {
  334. if models.IsErrUserNotExist(err) {
  335. ctx.Handle(404, "GetUserByEmail", err)
  336. } else {
  337. ctx.Handle(500, "GetUserByEmail", err)
  338. }
  339. return
  340. }
  341. ctx.Redirect(setting.AppSubUrl + "/user/" + u.Name)
  342. }