home.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/context"
  13. "github.com/gogits/gogs/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. var (
  150. sortType = ctx.Query("sort")
  151. filterMode = models.FILTER_MODE_YOUR_REPOS
  152. )
  153. // Note: Organization does not have view type and filter mode.
  154. if !ctxUser.IsOrganization() {
  155. viewType := ctx.Query("type")
  156. types := []string{
  157. string(models.FILTER_MODE_YOUR_REPOS),
  158. string(models.FILTER_MODE_ASSIGN),
  159. string(models.FILTER_MODE_CREATE),
  160. }
  161. if !com.IsSliceContainsStr(types, viewType) {
  162. viewType = string(models.FILTER_MODE_YOUR_REPOS)
  163. }
  164. filterMode = models.FilterMode(viewType)
  165. }
  166. page := ctx.QueryInt("page")
  167. if page <= 1 {
  168. page = 1
  169. }
  170. repoID := ctx.QueryInt64("repo")
  171. isShowClosed := ctx.Query("state") == "closed"
  172. // Get repositories.
  173. var (
  174. err error
  175. repos []*models.Repository
  176. userRepoIDs []int64
  177. showRepos = make([]*models.Repository, 0, 10)
  178. )
  179. if filterMode == models.FILTER_MODE_YOUR_REPOS {
  180. if ctxUser.IsOrganization() {
  181. repos, _, err = ctxUser.GetUserRepositories(ctx.User.ID, 1, ctxUser.NumRepos)
  182. if err != nil {
  183. ctx.Handle(500, "GetRepositories", err)
  184. return
  185. }
  186. } else {
  187. if err := ctxUser.GetRepositories(1, ctx.User.NumRepos); err != nil {
  188. ctx.Handle(500, "GetRepositories", err)
  189. return
  190. }
  191. repos = ctxUser.Repos
  192. }
  193. userRepoIDs = make([]int64, 0, len(repos))
  194. for _, repo := range repos {
  195. if isPullList {
  196. if isShowClosed && repo.NumClosedPulls == 0 ||
  197. !isShowClosed && repo.NumOpenPulls == 0 {
  198. continue
  199. }
  200. } else {
  201. if !repo.EnableIssues || repo.EnableExternalTracker ||
  202. isShowClosed && repo.NumClosedIssues == 0 ||
  203. !isShowClosed && repo.NumOpenIssues == 0 {
  204. continue
  205. }
  206. }
  207. userRepoIDs = append(userRepoIDs, repo.ID)
  208. showRepos = append(showRepos, repo)
  209. }
  210. }
  211. issueOptions := &models.IssuesOptions{
  212. RepoID: repoID,
  213. Page: page,
  214. IsClosed: isShowClosed,
  215. IsPull: isPullList,
  216. SortType: sortType,
  217. }
  218. switch filterMode {
  219. case models.FILTER_MODE_YOUR_REPOS:
  220. // Get all issues from repositories from this user.
  221. issueOptions.RepoIDs = userRepoIDs
  222. case models.FILTER_MODE_ASSIGN:
  223. // Get all issues assigned to this user.
  224. issueOptions.AssigneeID = ctxUser.ID
  225. case models.FILTER_MODE_CREATE:
  226. // Get all issues created by this user.
  227. issueOptions.PosterID = ctxUser.ID
  228. }
  229. issues, err := models.Issues(issueOptions)
  230. if err != nil {
  231. ctx.Handle(500, "Issues", err)
  232. return
  233. }
  234. if repoID > 0 {
  235. repo, err := models.GetRepositoryByID(repoID)
  236. if err != nil {
  237. ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d] %v", repoID, err))
  238. return
  239. }
  240. if err = repo.GetOwner(); err != nil {
  241. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", repoID, err))
  242. return
  243. }
  244. // Check if user has access to given repository.
  245. if !repo.IsOwnedBy(ctxUser.ID) && !repo.HasAccess(ctxUser) {
  246. ctx.Handle(404, "Issues", fmt.Errorf("#%d", repoID))
  247. return
  248. }
  249. }
  250. for _, issue := range issues {
  251. if err = issue.Repo.GetOwner(); err != nil {
  252. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", issue.RepoID, err))
  253. return
  254. }
  255. }
  256. issueStats := models.GetUserIssueStats(repoID, ctxUser.ID, userRepoIDs, filterMode, isPullList)
  257. var total int
  258. if !isShowClosed {
  259. total = int(issueStats.OpenCount)
  260. } else {
  261. total = int(issueStats.ClosedCount)
  262. }
  263. ctx.Data["Issues"] = issues
  264. ctx.Data["Repos"] = showRepos
  265. ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  266. ctx.Data["IssueStats"] = issueStats
  267. ctx.Data["ViewType"] = string(filterMode)
  268. ctx.Data["SortType"] = sortType
  269. ctx.Data["RepoID"] = repoID
  270. ctx.Data["IsShowClosed"] = isShowClosed
  271. if isShowClosed {
  272. ctx.Data["State"] = "closed"
  273. } else {
  274. ctx.Data["State"] = "open"
  275. }
  276. ctx.HTML(200, ISSUES)
  277. }
  278. func ShowSSHKeys(ctx *context.Context, uid int64) {
  279. keys, err := models.ListPublicKeys(uid)
  280. if err != nil {
  281. ctx.Handle(500, "ListPublicKeys", err)
  282. return
  283. }
  284. var buf bytes.Buffer
  285. for i := range keys {
  286. buf.WriteString(keys[i].OmitEmail())
  287. buf.WriteString("\n")
  288. }
  289. ctx.PlainText(200, buf.Bytes())
  290. }
  291. func showOrgProfile(ctx *context.Context) {
  292. ctx.SetParams(":org", ctx.Params(":username"))
  293. context.HandleOrgAssignment(ctx)
  294. if ctx.Written() {
  295. return
  296. }
  297. org := ctx.Org.Organization
  298. ctx.Data["Title"] = org.FullName
  299. page := ctx.QueryInt("page")
  300. if page <= 0 {
  301. page = 1
  302. }
  303. var (
  304. repos []*models.Repository
  305. count int64
  306. err error
  307. )
  308. if ctx.IsSigned && !ctx.User.IsAdmin {
  309. repos, count, err = org.GetUserRepositories(ctx.User.ID, page, setting.UI.User.RepoPagingNum)
  310. if err != nil {
  311. ctx.Handle(500, "GetUserRepositories", err)
  312. return
  313. }
  314. ctx.Data["Repos"] = repos
  315. } else {
  316. showPrivate := ctx.IsSigned && ctx.User.IsAdmin
  317. repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum)
  318. if err != nil {
  319. ctx.Handle(500, "GetRepositories", err)
  320. return
  321. }
  322. ctx.Data["Repos"] = repos
  323. count = models.CountUserRepositories(org.ID, showPrivate)
  324. }
  325. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  326. if err := org.GetMembers(); err != nil {
  327. ctx.Handle(500, "GetMembers", err)
  328. return
  329. }
  330. ctx.Data["Members"] = org.Members
  331. ctx.Data["Teams"] = org.Teams
  332. ctx.HTML(200, ORG_HOME)
  333. }
  334. func Email2User(ctx *context.Context) {
  335. u, err := models.GetUserByEmail(ctx.Query("email"))
  336. if err != nil {
  337. if models.IsErrUserNotExist(err) {
  338. ctx.Handle(404, "GetUserByEmail", err)
  339. } else {
  340. ctx.Handle(500, "GetUserByEmail", err)
  341. }
  342. return
  343. }
  344. ctx.Redirect(setting.AppSubUrl + "/user/" + u.Name)
  345. }