home.go 9.4 KB

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