mirror of https://github.com/gogits/gogs.git
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.
425 lines
10 KiB
425 lines
10 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package user
|
||
|
|
||
|
import (
|
||
10 years ago
|
"bytes"
|
||
11 years ago
|
"fmt"
|
||
|
|
||
11 years ago
|
"github.com/Unknwon/com"
|
||
9 years ago
|
"github.com/Unknwon/paginater"
|
||
11 years ago
|
|
||
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/models/errors"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
const (
|
||
8 years ago
|
DASHBOARD = "user/dashboard/dashboard"
|
||
|
NEWS_FEED = "user/dashboard/feeds"
|
||
|
ISSUES = "user/dashboard/issues"
|
||
|
PROFILE = "user/profile"
|
||
|
ORG_HOME = "org/home"
|
||
11 years ago
|
)
|
||
|
|
||
9 years ago
|
// getDashboardContextUser finds out dashboard is viewing as which context user.
|
||
8 years ago
|
func getDashboardContextUser(c *context.Context) *models.User {
|
||
|
ctxUser := c.User
|
||
|
orgName := c.Params(":org")
|
||
11 years ago
|
if len(orgName) > 0 {
|
||
|
// Organization.
|
||
|
org, err := models.GetUserByName(orgName)
|
||
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
|
||
9 years ago
|
return nil
|
||
11 years ago
|
}
|
||
|
ctxUser = org
|
||
9 years ago
|
}
|
||
8 years ago
|
c.Data["ContextUser"] = ctxUser
|
||
9 years ago
|
|
||
8 years ago
|
if err := c.User.GetOrganizations(true); err != nil {
|
||
|
c.Handle(500, "GetOrganizations", err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
8 years ago
|
c.Data["Orgs"] = c.User.Orgs
|
||
9 years ago
|
|
||
|
return ctxUser
|
||
|
}
|
||
|
|
||
9 years ago
|
// retrieveFeeds loads feeds from database by given context user.
|
||
|
// The user could be organization so it is not always the logged in user,
|
||
|
// which is why we have to explicitly pass the context user ID.
|
||
8 years ago
|
func retrieveFeeds(c *context.Context, ctxUser *models.User, userID int64, isProfile bool) {
|
||
|
actions, err := models.GetFeeds(ctxUser, userID, c.QueryInt64("after_id"), isProfile)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetFeeds", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
// Check access of private repositories.
|
||
|
feeds := make([]*models.Action, 0, len(actions))
|
||
|
unameAvatars := make(map[string]string)
|
||
|
for _, act := range actions {
|
||
|
// Cache results to reduce queries.
|
||
|
_, ok := unameAvatars[act.ActUserName]
|
||
|
if !ok {
|
||
|
u, err := models.GetUserByName(act.ActUserName)
|
||
|
if err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
9 years ago
|
continue
|
||
|
}
|
||
8 years ago
|
c.Handle(500, "GetUserByName", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
unameAvatars[act.ActUserName] = u.RelAvatarLink()
|
||
9 years ago
|
}
|
||
|
|
||
|
act.ActAvatar = unameAvatars[act.ActUserName]
|
||
|
feeds = append(feeds, act)
|
||
|
}
|
||
8 years ago
|
c.Data["Feeds"] = feeds
|
||
8 years ago
|
if len(feeds) > 0 {
|
||
|
afterID := feeds[len(feeds)-1].ID
|
||
8 years ago
|
c.Data["AfterID"] = afterID
|
||
|
c.Header().Set("X-AJAX-URL", fmt.Sprintf("%s?after_id=%d", c.Data["Link"], afterID))
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Dashboard(c *context.Context) {
|
||
|
ctxUser := getDashboardContextUser(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
retrieveFeeds(c, ctxUser, c.User.ID, false)
|
||
|
if c.Written() {
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.Req.Header.Get("X-AJAX") == "true" {
|
||
|
c.HTML(200, NEWS_FEED)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["Title"] = ctxUser.DisplayName() + " - " + c.Tr("dashboard")
|
||
|
c.Data["PageIsDashboard"] = true
|
||
|
c.Data["PageIsNews"] = true
|
||
9 years ago
|
|
||
|
// Only user can have collaborative repositories.
|
||
9 years ago
|
if !ctxUser.IsOrganization() {
|
||
8 years ago
|
collaborateRepos, err := c.User.GetAccessibleRepositories(setting.UI.User.RepoPagingNum)
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetAccessibleRepositories", err)
|
||
11 years ago
|
return
|
||
9 years ago
|
} else if err = models.RepositoryList(collaborateRepos).LoadAttributes(); err != nil {
|
||
8 years ago
|
c.Handle(500, "RepositoryList.LoadAttributes", err)
|
||
9 years ago
|
return
|
||
11 years ago
|
}
|
||
8 years ago
|
c.Data["CollaborativeRepos"] = collaborateRepos
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
9 years ago
|
var err error
|
||
|
var repos, mirrors []*models.Repository
|
||
8 years ago
|
var repoCount int64
|
||
9 years ago
|
if ctxUser.IsOrganization() {
|
||
8 years ago
|
repos, repoCount, err = ctxUser.GetUserRepositories(c.User.ID, 1, setting.UI.User.RepoPagingNum)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetUserRepositories", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
mirrors, err = ctxUser.GetUserMirrorRepositories(c.User.ID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetUserMirrorRepositories", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
} else {
|
||
|
if err = ctxUser.GetRepositories(1, setting.UI.User.RepoPagingNum); err != nil {
|
||
8 years ago
|
c.Handle(500, "GetRepositories", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
repos = ctxUser.Repos
|
||
8 years ago
|
repoCount = int64(ctxUser.NumRepos)
|
||
9 years ago
|
|
||
|
mirrors, err = ctxUser.GetMirrorRepositories()
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetMirrorRepositories", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
11 years ago
|
}
|
||
8 years ago
|
c.Data["Repos"] = repos
|
||
|
c.Data["RepoCount"] = repoCount
|
||
|
c.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum
|
||
11 years ago
|
|
||
9 years ago
|
if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
|
||
8 years ago
|
c.Handle(500, "MirrorRepositoryList.LoadAttributes", err)
|
||
9 years ago
|
return
|
||
11 years ago
|
}
|
||
8 years ago
|
c.Data["MirrorCount"] = len(mirrors)
|
||
|
c.Data["Mirrors"] = mirrors
|
||
11 years ago
|
|
||
8 years ago
|
c.HTML(200, DASHBOARD)
|
||
11 years ago
|
}
|
||
|
|
||
8 years ago
|
func Issues(c *context.Context) {
|
||
|
isPullList := c.Params(":type") == "pulls"
|
||
9 years ago
|
if isPullList {
|
||
8 years ago
|
c.Data["Title"] = c.Tr("pull_requests")
|
||
|
c.Data["PageIsPulls"] = true
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Data["Title"] = c.Tr("issues")
|
||
|
c.Data["PageIsIssues"] = true
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
ctxUser := getDashboardContextUser(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
var (
|
||
8 years ago
|
sortType = c.Query("sort")
|
||
8 years ago
|
filterMode = models.FILTER_MODE_YOUR_REPOS
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
// Note: Organization does not have view type and filter mode.
|
||
|
if !ctxUser.IsOrganization() {
|
||
8 years ago
|
viewType := c.Query("type")
|
||
8 years ago
|
types := []string{
|
||
|
string(models.FILTER_MODE_YOUR_REPOS),
|
||
|
string(models.FILTER_MODE_ASSIGN),
|
||
|
string(models.FILTER_MODE_CREATE),
|
||
|
}
|
||
|
if !com.IsSliceContainsStr(types, viewType) {
|
||
|
viewType = string(models.FILTER_MODE_YOUR_REPOS)
|
||
9 years ago
|
}
|
||
8 years ago
|
filterMode = models.FilterMode(viewType)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
page := c.QueryInt("page")
|
||
8 years ago
|
if page <= 1 {
|
||
|
page = 1
|
||
|
}
|
||
|
|
||
8 years ago
|
repoID := c.QueryInt64("repo")
|
||
|
isShowClosed := c.Query("state") == "closed"
|
||
9 years ago
|
|
||
|
// Get repositories.
|
||
8 years ago
|
var (
|
||
|
err error
|
||
|
repos []*models.Repository
|
||
|
userRepoIDs []int64
|
||
|
showRepos = make([]*models.Repository, 0, 10)
|
||
|
)
|
||
8 years ago
|
if ctxUser.IsOrganization() {
|
||
8 years ago
|
repos, _, err = ctxUser.GetUserRepositories(c.User.ID, 1, ctxUser.NumRepos)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetRepositories", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
} else {
|
||
8 years ago
|
if err := ctxUser.GetRepositories(1, c.User.NumRepos); err != nil {
|
||
|
c.Handle(500, "GetRepositories", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
repos = ctxUser.Repos
|
||
|
}
|
||
|
|
||
|
userRepoIDs = make([]int64, 0, len(repos))
|
||
|
for _, repo := range repos {
|
||
8 years ago
|
userRepoIDs = append(userRepoIDs, repo.ID)
|
||
|
|
||
|
if filterMode != models.FILTER_MODE_YOUR_REPOS {
|
||
|
continue
|
||
|
}
|
||
|
|
||
8 years ago
|
if isPullList {
|
||
|
if isShowClosed && repo.NumClosedPulls == 0 ||
|
||
|
!isShowClosed && repo.NumOpenPulls == 0 {
|
||
|
continue
|
||
8 years ago
|
}
|
||
|
} else {
|
||
8 years ago
|
if !repo.EnableIssues || repo.EnableExternalTracker ||
|
||
|
isShowClosed && repo.NumClosedIssues == 0 ||
|
||
|
!isShowClosed && repo.NumOpenIssues == 0 {
|
||
|
continue
|
||
8 years ago
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
showRepos = append(showRepos, repo)
|
||
|
}
|
||
|
|
||
|
// Filter repositories if the page shows issues.
|
||
|
if !isPullList {
|
||
|
userRepoIDs, err = models.FilterRepositoryWithIssues(userRepoIDs)
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "FilterRepositoryWithIssues", err)
|
||
8 years ago
|
return
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
issueOptions := &models.IssuesOptions{
|
||
|
RepoID: repoID,
|
||
|
Page: page,
|
||
|
IsClosed: isShowClosed,
|
||
|
IsPull: isPullList,
|
||
|
SortType: sortType,
|
||
|
}
|
||
8 years ago
|
switch filterMode {
|
||
8 years ago
|
case models.FILTER_MODE_YOUR_REPOS:
|
||
8 years ago
|
// Get all issues from repositories from this user.
|
||
8 years ago
|
if userRepoIDs == nil {
|
||
|
issueOptions.RepoIDs = []int64{-1}
|
||
|
} else {
|
||
|
issueOptions.RepoIDs = userRepoIDs
|
||
|
}
|
||
8 years ago
|
|
||
|
case models.FILTER_MODE_ASSIGN:
|
||
8 years ago
|
// Get all issues assigned to this user.
|
||
8 years ago
|
issueOptions.AssigneeID = ctxUser.ID
|
||
|
|
||
|
case models.FILTER_MODE_CREATE:
|
||
8 years ago
|
// Get all issues created by this user.
|
||
8 years ago
|
issueOptions.PosterID = ctxUser.ID
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
issues, err := models.Issues(issueOptions)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "Issues", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if repoID > 0 {
|
||
|
repo, err := models.GetRepositoryByID(repoID)
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d] %v", repoID, err))
|
||
8 years ago
|
return
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if err = repo.GetOwner(); err != nil {
|
||
8 years ago
|
c.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", repoID, err))
|
||
8 years ago
|
return
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// Check if user has access to given repository.
|
||
8 years ago
|
if !repo.IsOwnedBy(ctxUser.ID) && !repo.HasAccess(ctxUser.ID) {
|
||
8 years ago
|
c.Handle(404, "Issues", fmt.Errorf("#%d", repoID))
|
||
8 years ago
|
return
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
for _, issue := range issues {
|
||
|
if err = issue.Repo.GetOwner(); err != nil {
|
||
8 years ago
|
c.Handle(500, "GetOwner", fmt.Errorf("[#%d] %v", issue.RepoID, err))
|
||
8 years ago
|
return
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
issueStats := models.GetUserIssueStats(repoID, ctxUser.ID, userRepoIDs, filterMode, isPullList)
|
||
|
|
||
9 years ago
|
var total int
|
||
|
if !isShowClosed {
|
||
|
total = int(issueStats.OpenCount)
|
||
|
} else {
|
||
|
total = int(issueStats.ClosedCount)
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
c.Data["Issues"] = issues
|
||
|
c.Data["Repos"] = showRepos
|
||
|
c.Data["Page"] = paginater.New(total, setting.UI.IssuePagingNum, page, 5)
|
||
|
c.Data["IssueStats"] = issueStats
|
||
|
c.Data["ViewType"] = string(filterMode)
|
||
|
c.Data["SortType"] = sortType
|
||
|
c.Data["RepoID"] = repoID
|
||
|
c.Data["IsShowClosed"] = isShowClosed
|
||
8 years ago
|
|
||
9 years ago
|
if isShowClosed {
|
||
8 years ago
|
c.Data["State"] = "closed"
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Data["State"] = "open"
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.HTML(200, ISSUES)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func ShowSSHKeys(c *context.Context, uid int64) {
|
||
10 years ago
|
keys, err := models.ListPublicKeys(uid)
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "ListPublicKeys", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
|
var buf bytes.Buffer
|
||
|
for i := range keys {
|
||
|
buf.WriteString(keys[i].OmitEmail())
|
||
10 years ago
|
buf.WriteString("\n")
|
||
10 years ago
|
}
|
||
8 years ago
|
c.PlainText(200, buf.Bytes())
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func showOrgProfile(c *context.Context) {
|
||
|
c.SetParams(":org", c.Params(":username"))
|
||
|
context.HandleOrgAssignment(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
org := c.Org.Organization
|
||
|
c.Data["Title"] = org.FullName
|
||
9 years ago
|
|
||
8 years ago
|
page := c.QueryInt("page")
|
||
9 years ago
|
if page <= 0 {
|
||
|
page = 1
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
repos []*models.Repository
|
||
|
count int64
|
||
|
err error
|
||
|
)
|
||
8 years ago
|
if c.IsLogged && !c.User.IsAdmin {
|
||
|
repos, count, err = org.GetUserRepositories(c.User.ID, page, setting.UI.User.RepoPagingNum)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetUserRepositories", err)
|
||
9 years ago
|
return
|
||
9 years ago
|
}
|
||
8 years ago
|
c.Data["Repos"] = repos
|
||
9 years ago
|
} else {
|
||
8 years ago
|
showPrivate := c.IsLogged && c.User.IsAdmin
|
||
8 years ago
|
repos, err = models.GetUserRepositories(&models.UserRepoOptions{
|
||
|
UserID: org.ID,
|
||
|
Private: showPrivate,
|
||
|
Page: page,
|
||
|
PageSize: setting.UI.User.RepoPagingNum,
|
||
|
})
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetRepositories", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Repos"] = repos
|
||
9 years ago
|
count = models.CountUserRepositories(org.ID, showPrivate)
|
||
9 years ago
|
}
|
||
8 years ago
|
c.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
|
||
9 years ago
|
|
||
9 years ago
|
if err := org.GetMembers(); err != nil {
|
||
8 years ago
|
c.Handle(500, "GetMembers", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Members"] = org.Members
|
||
9 years ago
|
|
||
8 years ago
|
c.Data["Teams"] = org.Teams
|
||
9 years ago
|
|
||
8 years ago
|
c.HTML(200, ORG_HOME)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Email2User(c *context.Context) {
|
||
|
u, err := models.GetUserByEmail(c.Query("email"))
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetUserByEmail", errors.IsUserNotExist, err)
|
||
11 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Redirect(setting.AppSubURL + "/user/" + u.Name)
|
||
11 years ago
|
}
|