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.

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