You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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