home.go 9.6 KB

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