home.go 10 KB

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