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.

425 lines
9.7 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. userRepoIDs, err = ctxUser.GetAccessRepoIDs()
  183. if err != nil {
  184. ctx.ServerError("ctxUser.GetAccessRepoIDs", err)
  185. return
  186. }
  187. }
  188. if len(userRepoIDs) <= 0 {
  189. userRepoIDs = []int64{-1}
  190. }
  191. opts := &models.IssuesOptions{
  192. IsClosed: util.OptionalBoolOf(isShowClosed),
  193. IsPull: util.OptionalBoolOf(isPullList),
  194. SortType: sortType,
  195. }
  196. if repoID > 0 {
  197. opts.RepoIDs = []int64{repoID}
  198. }
  199. switch filterMode {
  200. case models.FilterModeAll:
  201. if repoID > 0 {
  202. if !com.IsSliceContainsInt64(userRepoIDs, repoID) {
  203. // force an empty result
  204. opts.RepoIDs = []int64{-1}
  205. }
  206. } else {
  207. opts.RepoIDs = userRepoIDs
  208. }
  209. case models.FilterModeAssign:
  210. opts.AssigneeID = ctxUser.ID
  211. case models.FilterModeCreate:
  212. opts.PosterID = ctxUser.ID
  213. case models.FilterModeMention:
  214. opts.MentionedID = ctxUser.ID
  215. }
  216. counts, err := models.CountIssuesByRepo(opts)
  217. if err != nil {
  218. ctx.ServerError("CountIssuesByRepo", err)
  219. return
  220. }
  221. opts.Page = page
  222. opts.PageSize = setting.UI.IssuePagingNum
  223. issues, err := models.Issues(opts)
  224. if err != nil {
  225. ctx.ServerError("Issues", err)
  226. return
  227. }
  228. showReposMap := make(map[int64]*models.Repository, len(counts))
  229. for repoID := range counts {
  230. repo, err := models.GetRepositoryByID(repoID)
  231. if err != nil {
  232. ctx.ServerError("GetRepositoryByID", err)
  233. return
  234. }
  235. showReposMap[repoID] = repo
  236. }
  237. if repoID > 0 {
  238. if _, ok := showReposMap[repoID]; !ok {
  239. repo, err := models.GetRepositoryByID(repoID)
  240. if err != nil {
  241. ctx.ServerError("GetRepositoryByID", fmt.Errorf("[%d]%v", repoID, err))
  242. return
  243. }
  244. showReposMap[repoID] = repo
  245. }
  246. repo := showReposMap[repoID]
  247. // Check if user has access to given repository.
  248. if !repo.IsOwnedBy(ctxUser.ID) && !repo.HasAccess(ctxUser) {
  249. ctx.Status(404)
  250. return
  251. }
  252. }
  253. showRepos := models.RepositoryListOfMap(showReposMap)
  254. sort.Sort(showRepos)
  255. if err = showRepos.LoadAttributes(); err != nil {
  256. ctx.ServerError("LoadAttributes", err)
  257. return
  258. }
  259. for _, issue := range issues {
  260. issue.Repo = showReposMap[issue.RepoID]
  261. }
  262. issueStats, err := models.GetUserIssueStats(models.UserIssueStatsOptions{
  263. UserID: ctxUser.ID,
  264. RepoID: repoID,
  265. UserRepoIDs: userRepoIDs,
  266. FilterMode: filterMode,
  267. IsPull: isPullList,
  268. IsClosed: isShowClosed,
  269. })
  270. if err != nil {
  271. ctx.ServerError("GetUserIssueStats", err)
  272. return
  273. }
  274. var total int
  275. if !isShowClosed {
  276. total = int(issueStats.OpenCount)
  277. } else {
  278. total = int(issueStats.ClosedCount)
  279. }
  280. ctx.Data["Issues"] = issues
  281. ctx.Data["Repos"] = showRepos
  282. ctx.Data["Counts"] = counts
  283. ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  284. ctx.Data["IssueStats"] = issueStats
  285. ctx.Data["ViewType"] = viewType
  286. ctx.Data["SortType"] = sortType
  287. ctx.Data["RepoID"] = repoID
  288. ctx.Data["IsShowClosed"] = isShowClosed
  289. if isShowClosed {
  290. ctx.Data["State"] = "closed"
  291. } else {
  292. ctx.Data["State"] = "open"
  293. }
  294. ctx.HTML(200, tplIssues)
  295. }
  296. // ShowSSHKeys output all the ssh keys of user by uid
  297. func ShowSSHKeys(ctx *context.Context, uid int64) {
  298. keys, err := models.ListPublicKeys(uid)
  299. if err != nil {
  300. ctx.ServerError("ListPublicKeys", err)
  301. return
  302. }
  303. var buf bytes.Buffer
  304. for i := range keys {
  305. buf.WriteString(keys[i].OmitEmail())
  306. buf.WriteString("\n")
  307. }
  308. ctx.PlainText(200, buf.Bytes())
  309. }
  310. func showOrgProfile(ctx *context.Context) {
  311. ctx.SetParams(":org", ctx.Params(":username"))
  312. context.HandleOrgAssignment(ctx)
  313. if ctx.Written() {
  314. return
  315. }
  316. org := ctx.Org.Organization
  317. ctx.Data["Title"] = org.DisplayName()
  318. page := ctx.QueryInt("page")
  319. if page <= 0 {
  320. page = 1
  321. }
  322. var (
  323. repos []*models.Repository
  324. count int64
  325. err error
  326. )
  327. if ctx.IsSigned && !ctx.User.IsAdmin {
  328. env, err := org.AccessibleReposEnv(ctx.User.ID)
  329. if err != nil {
  330. ctx.ServerError("AccessibleReposEnv", err)
  331. return
  332. }
  333. repos, err = env.Repos(page, setting.UI.User.RepoPagingNum)
  334. if err != nil {
  335. ctx.ServerError("env.Repos", err)
  336. return
  337. }
  338. count, err = env.CountRepos()
  339. if err != nil {
  340. ctx.ServerError("env.CountRepos", err)
  341. return
  342. }
  343. ctx.Data["Repos"] = repos
  344. } else {
  345. showPrivate := ctx.IsSigned && ctx.User.IsAdmin
  346. repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum, "")
  347. if err != nil {
  348. ctx.ServerError("GetRepositories", err)
  349. return
  350. }
  351. ctx.Data["Repos"] = repos
  352. count = models.CountUserRepositories(org.ID, showPrivate)
  353. }
  354. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  355. if err := org.GetMembers(); err != nil {
  356. ctx.ServerError("GetMembers", err)
  357. return
  358. }
  359. ctx.Data["Members"] = org.Members
  360. ctx.Data["Teams"] = org.Teams
  361. ctx.HTML(200, tplOrgHome)
  362. }
  363. // Email2User show user page via email
  364. func Email2User(ctx *context.Context) {
  365. u, err := models.GetUserByEmail(ctx.Query("email"))
  366. if err != nil {
  367. if models.IsErrUserNotExist(err) {
  368. ctx.NotFound("GetUserByEmail", err)
  369. } else {
  370. ctx.ServerError("GetUserByEmail", err)
  371. }
  372. return
  373. }
  374. ctx.Redirect(setting.AppSubURL + "/user/" + u.Name)
  375. }