home.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 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. userRepoIDs = make([]int64, 0, len(repos))
  193. for _, repo := range repos {
  194. userRepoIDs = append(userRepoIDs, repo.ID)
  195. if filterMode != models.FILTER_MODE_YOUR_REPOS {
  196. continue
  197. }
  198. if isPullList {
  199. if isShowClosed && repo.NumClosedPulls == 0 ||
  200. !isShowClosed && repo.NumOpenPulls == 0 {
  201. continue
  202. }
  203. } else {
  204. if !repo.EnableIssues || repo.EnableExternalTracker ||
  205. isShowClosed && repo.NumClosedIssues == 0 ||
  206. !isShowClosed && repo.NumOpenIssues == 0 {
  207. continue
  208. }
  209. }
  210. showRepos = append(showRepos, repo)
  211. }
  212. // Filter repositories if the page shows issues.
  213. if !isPullList {
  214. userRepoIDs, err = models.FilterRepositoryWithIssues(userRepoIDs)
  215. if err != nil {
  216. ctx.Handle(500, "FilterRepositoryWithIssues", err)
  217. return
  218. }
  219. }
  220. issueOptions := &models.IssuesOptions{
  221. RepoID: repoID,
  222. Page: page,
  223. IsClosed: isShowClosed,
  224. IsPull: isPullList,
  225. SortType: sortType,
  226. }
  227. switch filterMode {
  228. case models.FILTER_MODE_YOUR_REPOS:
  229. // Get all issues from repositories from this user.
  230. if userRepoIDs == nil {
  231. issueOptions.RepoIDs = []int64{-1}
  232. } else {
  233. issueOptions.RepoIDs = userRepoIDs
  234. }
  235. case models.FILTER_MODE_ASSIGN:
  236. // Get all issues assigned to this user.
  237. issueOptions.AssigneeID = ctxUser.ID
  238. case models.FILTER_MODE_CREATE:
  239. // Get all issues created by this user.
  240. issueOptions.PosterID = ctxUser.ID
  241. }
  242. issues, err := models.Issues(issueOptions)
  243. if err != nil {
  244. ctx.Handle(500, "Issues", err)
  245. return
  246. }
  247. if repoID > 0 {
  248. repo, err := models.GetRepositoryByID(repoID)
  249. if err != nil {
  250. ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d] %v", repoID, err))
  251. return
  252. }
  253. if err = repo.GetOwner(); err != nil {
  254. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", repoID, err))
  255. return
  256. }
  257. // Check if user has access to given repository.
  258. if !repo.IsOwnedBy(ctxUser.ID) && !repo.HasAccess(ctxUser) {
  259. ctx.Handle(404, "Issues", fmt.Errorf("#%d", repoID))
  260. return
  261. }
  262. }
  263. for _, issue := range issues {
  264. if err = issue.Repo.GetOwner(); err != nil {
  265. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", issue.RepoID, err))
  266. return
  267. }
  268. }
  269. issueStats := models.GetUserIssueStats(repoID, ctxUser.ID, userRepoIDs, filterMode, isPullList)
  270. var total int
  271. if !isShowClosed {
  272. total = int(issueStats.OpenCount)
  273. } else {
  274. total = int(issueStats.ClosedCount)
  275. }
  276. ctx.Data["Issues"] = issues
  277. ctx.Data["Repos"] = showRepos
  278. ctx.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
  279. ctx.Data["IssueStats"] = issueStats
  280. ctx.Data["ViewType"] = string(filterMode)
  281. ctx.Data["SortType"] = sortType
  282. ctx.Data["RepoID"] = repoID
  283. ctx.Data["IsShowClosed"] = isShowClosed
  284. if isShowClosed {
  285. ctx.Data["State"] = "closed"
  286. } else {
  287. ctx.Data["State"] = "open"
  288. }
  289. ctx.HTML(200, ISSUES)
  290. }
  291. func ShowSSHKeys(ctx *context.Context, uid int64) {
  292. keys, err := models.ListPublicKeys(uid)
  293. if err != nil {
  294. ctx.Handle(500, "ListPublicKeys", err)
  295. return
  296. }
  297. var buf bytes.Buffer
  298. for i := range keys {
  299. buf.WriteString(keys[i].OmitEmail())
  300. buf.WriteString("\n")
  301. }
  302. ctx.PlainText(200, buf.Bytes())
  303. }
  304. func showOrgProfile(ctx *context.Context) {
  305. ctx.SetParams(":org", ctx.Params(":username"))
  306. context.HandleOrgAssignment(ctx)
  307. if ctx.Written() {
  308. return
  309. }
  310. org := ctx.Org.Organization
  311. ctx.Data["Title"] = org.FullName
  312. page := ctx.QueryInt("page")
  313. if page <= 0 {
  314. page = 1
  315. }
  316. var (
  317. repos []*models.Repository
  318. count int64
  319. err error
  320. )
  321. if ctx.IsSigned && !ctx.User.IsAdmin {
  322. repos, count, err = org.GetUserRepositories(ctx.User.ID, page, setting.UI.User.RepoPagingNum)
  323. if err != nil {
  324. ctx.Handle(500, "GetUserRepositories", err)
  325. return
  326. }
  327. ctx.Data["Repos"] = repos
  328. } else {
  329. showPrivate := ctx.IsSigned && ctx.User.IsAdmin
  330. repos, err = models.GetUserRepositories(&models.UserRepoOptions{
  331. UserID: org.ID,
  332. Private: showPrivate,
  333. Page: page,
  334. PageSize: setting.UI.User.RepoPagingNum,
  335. })
  336. if err != nil {
  337. ctx.Handle(500, "GetRepositories", err)
  338. return
  339. }
  340. ctx.Data["Repos"] = repos
  341. count = models.CountUserRepositories(org.ID, showPrivate)
  342. }
  343. ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
  344. if err := org.GetMembers(); err != nil {
  345. ctx.Handle(500, "GetMembers", err)
  346. return
  347. }
  348. ctx.Data["Members"] = org.Members
  349. ctx.Data["Teams"] = org.Teams
  350. ctx.HTML(200, ORG_HOME)
  351. }
  352. func Email2User(ctx *context.Context) {
  353. u, err := models.GetUserByEmail(ctx.Query("email"))
  354. if err != nil {
  355. if models.IsErrUserNotExist(err) {
  356. ctx.Handle(404, "GetUserByEmail", err)
  357. } else {
  358. ctx.Handle(500, "GetUserByEmail", err)
  359. }
  360. return
  361. }
  362. ctx.Redirect(setting.AppSubUrl + "/user/" + u.Name)
  363. }