home.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/middleware"
  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. func getDashboardContextUser(ctx *middleware.Context) *models.User {
  22. ctxUser := ctx.User
  23. orgName := ctx.Params(":org")
  24. if len(orgName) > 0 {
  25. // Organization.
  26. org, err := models.GetUserByName(orgName)
  27. if err != nil {
  28. if models.IsErrUserNotExist(err) {
  29. ctx.Handle(404, "GetUserByName", err)
  30. } else {
  31. ctx.Handle(500, "GetUserByName", err)
  32. }
  33. return nil
  34. }
  35. ctxUser = org
  36. }
  37. ctx.Data["ContextUser"] = ctxUser
  38. if err := ctx.User.GetOrganizations(true); err != nil {
  39. ctx.Handle(500, "GetOrganizations", err)
  40. return nil
  41. }
  42. ctx.Data["Orgs"] = ctx.User.Orgs
  43. return ctxUser
  44. }
  45. func retrieveFeeds(ctx *middleware.Context, uid, offset int64, isProfile bool) {
  46. actions, err := models.GetFeeds(uid, offset, isProfile)
  47. if err != nil {
  48. ctx.Handle(500, "GetFeeds", err)
  49. return
  50. }
  51. // Check access of private repositories.
  52. feeds := make([]*models.Action, 0, len(actions))
  53. unameAvatars := make(map[string]string)
  54. for _, act := range actions {
  55. // Cache results to reduce queries.
  56. _, ok := unameAvatars[act.ActUserName]
  57. if !ok {
  58. u, err := models.GetUserByName(act.ActUserName)
  59. if err != nil {
  60. if models.IsErrUserNotExist(err) {
  61. continue
  62. }
  63. ctx.Handle(500, "GetUserByName", err)
  64. return
  65. }
  66. unameAvatars[act.ActUserName] = u.AvatarLink()
  67. }
  68. act.ActAvatar = unameAvatars[act.ActUserName]
  69. feeds = append(feeds, act)
  70. }
  71. ctx.Data["Feeds"] = feeds
  72. }
  73. func Dashboard(ctx *middleware.Context) {
  74. ctx.Data["Title"] = ctx.Tr("dashboard")
  75. ctx.Data["PageIsDashboard"] = true
  76. ctx.Data["PageIsNews"] = true
  77. ctxUser := getDashboardContextUser(ctx)
  78. if ctx.Written() {
  79. return
  80. }
  81. if !ctxUser.IsOrganization() {
  82. collaborateRepos, err := ctx.User.GetAccessibleRepositories()
  83. if err != nil {
  84. ctx.Handle(500, "GetAccessibleRepositories", err)
  85. return
  86. }
  87. for i := range collaborateRepos {
  88. if err = collaborateRepos[i].GetOwner(); err != nil {
  89. ctx.Handle(500, "GetOwner: "+collaborateRepos[i].Name, err)
  90. return
  91. }
  92. }
  93. ctx.Data["CollaborateCount"] = len(collaborateRepos)
  94. ctx.Data["CollaborativeRepos"] = collaborateRepos
  95. }
  96. repos, err := models.GetRepositories(ctxUser.Id, true)
  97. if err != nil {
  98. ctx.Handle(500, "GetRepositories", err)
  99. return
  100. }
  101. ctx.Data["Repos"] = repos
  102. // Get mirror repositories.
  103. mirrors := make([]*models.Repository, 0, 5)
  104. for _, repo := range repos {
  105. if repo.IsMirror {
  106. if err = repo.GetMirror(); err != nil {
  107. ctx.Handle(500, "GetMirror: "+repo.Name, err)
  108. return
  109. }
  110. mirrors = append(mirrors, repo)
  111. }
  112. }
  113. ctx.Data["MirrorCount"] = len(mirrors)
  114. ctx.Data["Mirrors"] = mirrors
  115. retrieveFeeds(ctx, ctxUser.Id, 0, false)
  116. if ctx.Written() {
  117. return
  118. }
  119. ctx.HTML(200, DASHBOARD)
  120. }
  121. func Issues(ctx *middleware.Context) {
  122. isPullList := ctx.Params(":type") == "pulls"
  123. if isPullList {
  124. ctx.Data["Title"] = ctx.Tr("pull_requests")
  125. ctx.Data["PageIsPulls"] = true
  126. } else {
  127. ctx.Data["Title"] = ctx.Tr("issues")
  128. ctx.Data["PageIsIssues"] = true
  129. }
  130. ctxUser := getDashboardContextUser(ctx)
  131. if ctx.Written() {
  132. return
  133. }
  134. // Organization does not have view type and filter mode.
  135. var (
  136. viewType string
  137. sortType = ctx.Query("sort")
  138. filterMode = models.FM_ALL
  139. assigneeID int64
  140. posterID int64
  141. )
  142. if ctxUser.IsOrganization() {
  143. viewType = "all"
  144. } else {
  145. viewType = ctx.Query("type")
  146. types := []string{"assigned", "created_by"}
  147. if !com.IsSliceContainsStr(types, viewType) {
  148. viewType = "all"
  149. }
  150. switch viewType {
  151. case "assigned":
  152. filterMode = models.FM_ASSIGN
  153. assigneeID = ctxUser.Id
  154. case "created_by":
  155. filterMode = models.FM_CREATE
  156. posterID = ctxUser.Id
  157. }
  158. }
  159. repoID := ctx.QueryInt64("repo")
  160. isShowClosed := ctx.Query("state") == "closed"
  161. // Get repositories.
  162. repos, err := models.GetRepositories(ctxUser.Id, true)
  163. if err != nil {
  164. ctx.Handle(500, "GetRepositories", err)
  165. return
  166. }
  167. allCount := 0
  168. repoIDs := make([]int64, 0, len(repos))
  169. showRepos := make([]*models.Repository, 0, len(repos))
  170. for _, repo := range repos {
  171. if (isPullList && repo.NumPulls == 0) ||
  172. (!isPullList && repo.NumIssues == 0) {
  173. continue
  174. }
  175. repoIDs = append(repoIDs, repo.ID)
  176. if isPullList {
  177. allCount += repo.NumOpenPulls
  178. repo.NumOpenIssues = repo.NumOpenPulls
  179. repo.NumClosedIssues = repo.NumClosedPulls
  180. } else {
  181. allCount += repo.NumOpenIssues
  182. }
  183. if filterMode != models.FM_ALL {
  184. // Calculate repository issue count with filter mode.
  185. numOpen, numClosed := repo.IssueStats(ctxUser.Id, filterMode, isPullList)
  186. repo.NumOpenIssues, repo.NumClosedIssues = int(numOpen), int(numClosed)
  187. }
  188. if repo.ID == repoID ||
  189. (isShowClosed && repo.NumClosedIssues > 0) ||
  190. (!isShowClosed && repo.NumOpenIssues > 0) {
  191. showRepos = append(showRepos, repo)
  192. }
  193. }
  194. ctx.Data["Repos"] = showRepos
  195. issueStats := models.GetUserIssueStats(repoID, ctxUser.Id, repoIDs, filterMode, isPullList)
  196. issueStats.AllCount = int64(allCount)
  197. page := ctx.QueryInt("page")
  198. if page <= 1 {
  199. page = 1
  200. }
  201. var total int
  202. if !isShowClosed {
  203. total = int(issueStats.OpenCount)
  204. } else {
  205. total = int(issueStats.ClosedCount)
  206. }
  207. ctx.Data["Page"] = paginater.New(total, setting.IssuePagingNum, page, 5)
  208. // Get issues.
  209. issues, err := models.Issues(&models.IssuesOptions{
  210. UserID: ctxUser.Id,
  211. AssigneeID: assigneeID,
  212. RepoID: repoID,
  213. PosterID: posterID,
  214. RepoIDs: repoIDs,
  215. Page: page,
  216. IsClosed: isShowClosed,
  217. IsPull: isPullList,
  218. SortType: sortType,
  219. })
  220. if err != nil {
  221. ctx.Handle(500, "Issues: %v", err)
  222. return
  223. }
  224. // Get posters and repository.
  225. for i := range issues {
  226. issues[i].Repo, err = models.GetRepositoryByID(issues[i].RepoID)
  227. if err != nil {
  228. ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d]%v", issues[i].ID, err))
  229. return
  230. }
  231. if err = issues[i].Repo.GetOwner(); err != nil {
  232. ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d]%v", issues[i].ID, err))
  233. return
  234. }
  235. if err = issues[i].GetPoster(); err != nil {
  236. ctx.Handle(500, "GetPoster", fmt.Errorf("[#%d]%v", issues[i].ID, err))
  237. return
  238. }
  239. }
  240. ctx.Data["Issues"] = issues
  241. ctx.Data["IssueStats"] = issueStats
  242. ctx.Data["ViewType"] = viewType
  243. ctx.Data["SortType"] = sortType
  244. ctx.Data["RepoID"] = repoID
  245. ctx.Data["IsShowClosed"] = isShowClosed
  246. if isShowClosed {
  247. ctx.Data["State"] = "closed"
  248. } else {
  249. ctx.Data["State"] = "open"
  250. }
  251. ctx.HTML(200, ISSUES)
  252. }
  253. func ShowSSHKeys(ctx *middleware.Context, uid int64) {
  254. keys, err := models.ListPublicKeys(uid)
  255. if err != nil {
  256. ctx.Handle(500, "ListPublicKeys", err)
  257. return
  258. }
  259. var buf bytes.Buffer
  260. for i := range keys {
  261. buf.WriteString(keys[i].OmitEmail())
  262. buf.WriteString("\n")
  263. }
  264. ctx.PlainText(200, buf.Bytes())
  265. }
  266. func showOrgProfile(ctx *middleware.Context) {
  267. ctx.SetParams(":org", ctx.Params(":username"))
  268. middleware.HandleOrgAssignment(ctx)
  269. if ctx.Written() {
  270. return
  271. }
  272. org := ctx.Org.Organization
  273. ctx.Data["Title"] = org.FullName
  274. repos, err := models.GetRepositories(org.Id, ctx.IsSigned && (ctx.User.IsAdmin || org.IsOrgMember(ctx.User.Id)))
  275. if err != nil {
  276. ctx.Handle(500, "GetRepositories", err)
  277. return
  278. }
  279. ctx.Data["Repos"] = repos
  280. if err = org.GetMembers(); err != nil {
  281. ctx.Handle(500, "GetMembers", err)
  282. return
  283. }
  284. ctx.Data["Members"] = org.Members
  285. if err = org.GetTeams(); err != nil {
  286. ctx.Handle(500, "GetTeams", err)
  287. return
  288. }
  289. ctx.Data["Teams"] = org.Teams
  290. ctx.HTML(200, ORG_HOME)
  291. }
  292. func Email2User(ctx *middleware.Context) {
  293. u, err := models.GetUserByEmail(ctx.Query("email"))
  294. if err != nil {
  295. if models.IsErrUserNotExist(err) {
  296. ctx.Handle(404, "GetUserByEmail", err)
  297. } else {
  298. ctx.Handle(500, "GetUserByEmail", err)
  299. }
  300. return
  301. }
  302. ctx.Redirect(setting.AppSubUrl + "/user/" + u.Name)
  303. }