#1384 add pagination for repositories

This commit is contained in:
Unknwon 2016-07-24 14:32:46 +08:00
parent 1f2e173a74
commit e74630ae3b
21 changed files with 360 additions and 192 deletions

View File

@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true) ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
##### Current tip version: 0.9.53 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions) ##### Current tip version: 0.9.54 (see [Releases](https://github.com/gogits/gogs/releases) for binary versions)
| Web | UI | Preview | | Web | UI | Preview |
|:-------------:|:-------:|:-------:| |:-------------:|:-------:|:-------:|

View File

@ -124,6 +124,7 @@ uname_holder = Username or email
password_holder = Password password_holder = Password
switch_dashboard_context = Switch Dashboard Context switch_dashboard_context = Switch Dashboard Context
my_repos = My Repositories my_repos = My Repositories
show_more_repos = Show more repositories...
collaborative_repos = Collaborative Repositories collaborative_repos = Collaborative Repositories
my_orgs = My Organizations my_orgs = My Organizations
my_mirrors = My Mirrors my_mirrors = My Mirrors

View File

@ -17,7 +17,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const APP_VER = "0.9.53.0724" const APP_VER = "0.9.54.0724"
func init() { func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())

View File

@ -121,23 +121,17 @@ func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
return repos, nil return repos, nil
} }
// GetAccessibleRepositories finds all repositories where a user has access but does not own. // GetAccessibleRepositories finds repositories which the user has access but does not own.
func (u *User) GetAccessibleRepositories() ([]*Repository, error) { // If limit is smaller than 1 means returns all found results.
accesses := make([]*Access, 0, 10) func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) {
if err := x.Find(&accesses, &Access{UserID: u.ID}); err != nil { sess := x.Where("owner_id !=? ", user.ID).Desc("updated_unix")
return nil, err if limit > 0 {
sess.Limit(limit)
repos = make([]*Repository, 0, limit)
} else {
repos = make([]*Repository, 0, 10)
} }
return repos, sess.Join("INNER", "access", "access.user_id = ? AND access.repo_id = repository.id", user.ID).Find(&repos)
if len(accesses) == 0 {
return []*Repository{}, nil
}
repoIDs := make([]int64, 0, len(accesses))
for _, access := range accesses {
repoIDs = append(repoIDs, access.RepoID)
}
repos := make([]*Repository, 0, len(repoIDs))
return repos, x.Where("owner_id != ?", u.ID).In("id", repoIDs).Desc("updated_unix").Find(&repos)
} }
func maxAccessMode(modes ...AccessMode) AccessMode { func maxAccessMode(modes ...AccessMode) AccessMode {

View File

@ -602,21 +602,22 @@ func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error
} }
// GetFeeds returns action list of given user in given context. // GetFeeds returns action list of given user in given context.
// userID is the user who's requesting, ctxUserID is the user/org that is requested. // actorID is the user who's requesting, ctxUserID is the user/org that is requested.
// userID can be -1, if isProfile is true or in order to skip the permission check. // actorID can be -1 when isProfile is true or to skip the permission check.
func GetFeeds(ctxUserID, userID, offset int64, isProfile bool) ([]*Action, error) { func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) {
actions := make([]*Action, 0, 20) actions := make([]*Action, 0, 20)
sess := x.Limit(20, int(offset)).Desc("id").Where("user_id=?", ctxUserID) sess := x.Limit(20, int(offset)).Desc("id").Where("user_id = ?", ctxUser.ID)
if isProfile { if isProfile {
sess.And("is_private=?", false).And("act_user_id=?", ctxUserID) sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID)
} else if ctxUserID != -1 { } else if actorID != -1 && ctxUser.IsOrganization() {
ctxUser := &User{ID: ctxUserID} // FIXME: only need to get IDs here, not all fields of repository.
if err := ctxUser.GetUserRepositories(userID); err != nil { repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)
return nil, err if err != nil {
return nil, fmt.Errorf("GetUserRepositories: %v", err)
} }
var repoIDs []int64 var repoIDs []int64
for _, repo := range ctxUser.Repos { for _, repo := range repos {
repoIDs = append(repoIDs, repo.ID) repoIDs = append(repoIDs, repo.ID)
} }

View File

@ -82,7 +82,7 @@ func (issue *Issue) loadAttributes(e Engine) (err error) {
return nil return nil
} }
func (issue *Issue) LoadAttributes() (err error) { func (issue *Issue) LoadAttributes() error {
return issue.loadAttributes(x) return issue.loadAttributes(x)
} }

View File

@ -98,8 +98,8 @@ func LoadConfigs() {
func getEngine() (*xorm.Engine, error) { func getEngine() (*xorm.Engine, error) {
cnnstr := "" cnnstr := ""
var Param string = "?" var Param string = "?"
if strings.Contains(DbCfg.Name,Param) { if strings.Contains(DbCfg.Name, Param) {
Param="&" Param = "&"
} }
switch DbCfg.Type { switch DbCfg.Type {
case "mysql": case "mysql":
@ -206,7 +206,7 @@ func GetStatistic() (stats Statistic) {
stats.Counter.User = CountUsers() stats.Counter.User = CountUsers()
stats.Counter.Org = CountOrganizations() stats.Counter.Org = CountOrganizations()
stats.Counter.PublicKey, _ = x.Count(new(PublicKey)) stats.Counter.PublicKey, _ = x.Count(new(PublicKey))
stats.Counter.Repo = CountRepositories() stats.Counter.Repo = CountRepositories(true)
stats.Counter.Watch, _ = x.Count(new(Watch)) stats.Counter.Watch, _ = x.Count(new(Watch))
stats.Counter.Star, _ = x.Count(new(Star)) stats.Counter.Star, _ = x.Count(new(Star))
stats.Counter.Action, _ = x.Count(new(Action)) stats.Counter.Action, _ = x.Count(new(Action))

View File

@ -10,8 +10,10 @@ import (
"os" "os"
"strings" "strings"
"github.com/Unknwon/com"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/log"
) )
var ( var (
@ -313,9 +315,9 @@ func GetOrgUsersByOrgID(orgID int64) ([]*OrgUser, error) {
} }
// ChangeOrgUserStatus changes public or private membership status. // ChangeOrgUserStatus changes public or private membership status.
func ChangeOrgUserStatus(orgId, uid int64, public bool) error { func ChangeOrgUserStatus(orgID, uid int64, public bool) error {
ou := new(OrgUser) ou := new(OrgUser)
has, err := x.Where("uid=?", uid).And("org_id=?", orgId).Get(ou) has, err := x.Where("uid=?", uid).And("org_id=?", orgID).Get(ou)
if err != nil { if err != nil {
return err return err
} else if !has { } else if !has {
@ -328,8 +330,8 @@ func ChangeOrgUserStatus(orgId, uid int64, public bool) error {
} }
// AddOrgUser adds new user to given organization. // AddOrgUser adds new user to given organization.
func AddOrgUser(orgId, uid int64) error { func AddOrgUser(orgID, uid int64) error {
if IsOrganizationMember(orgId, uid) { if IsOrganizationMember(orgID, uid) {
return nil return nil
} }
@ -341,13 +343,13 @@ func AddOrgUser(orgId, uid int64) error {
ou := &OrgUser{ ou := &OrgUser{
Uid: uid, Uid: uid,
OrgID: orgId, OrgID: orgID,
} }
if _, err := sess.Insert(ou); err != nil { if _, err := sess.Insert(ou); err != nil {
sess.Rollback() sess.Rollback()
return err return err
} else if _, err = sess.Exec("UPDATE `user` SET num_members = num_members + 1 WHERE id = ?", orgId); err != nil { } else if _, err = sess.Exec("UPDATE `user` SET num_members = num_members + 1 WHERE id = ?", orgID); err != nil {
sess.Rollback() sess.Rollback()
return err return err
} }
@ -356,35 +358,39 @@ func AddOrgUser(orgId, uid int64) error {
} }
// RemoveOrgUser removes user from given organization. // RemoveOrgUser removes user from given organization.
func RemoveOrgUser(orgId, uid int64) error { func RemoveOrgUser(orgID, userID int64) error {
ou := new(OrgUser) ou := new(OrgUser)
has, err := x.Where("uid=?", uid).And("org_id=?", orgId).Get(ou) has, err := x.Where("uid=?", userID).And("org_id=?", orgID).Get(ou)
if err != nil { if err != nil {
return fmt.Errorf("get org-user: %v", err) return fmt.Errorf("get org-user: %v", err)
} else if !has { } else if !has {
return nil return nil
} }
u, err := GetUserByID(uid) user, err := GetUserByID(userID)
if err != nil { if err != nil {
return fmt.Errorf("GetUserById: %v", err) return fmt.Errorf("GetUserByID [%d]: %v", userID, err)
} }
org, err := GetUserByID(orgId) org, err := GetUserByID(orgID)
if err != nil { if err != nil {
return fmt.Errorf("get organization: %v", err) return fmt.Errorf("GetUserByID [%d]: %v", orgID, err)
} else if err = org.GetRepositories(); err != nil { }
return fmt.Errorf("GetRepositories: %v", err)
// FIXME: only need to get IDs here, not all fields of repository.
repos, _, err := org.GetUserRepositories(user.ID, 1, org.NumRepos)
if err != nil {
return fmt.Errorf("GetUserRepositories [%d]: %v", user.ID, err)
} }
// Check if the user to delete is the last member in owner team. // Check if the user to delete is the last member in owner team.
if IsOrganizationOwner(orgId, uid) { if IsOrganizationOwner(orgID, userID) {
t, err := org.GetOwnerTeam() t, err := org.GetOwnerTeam()
if err != nil { if err != nil {
return err return err
} }
if t.NumMembers == 1 { if t.NumMembers == 1 {
return ErrLastOrgOwner{UID: uid} return ErrLastOrgOwner{UID: userID}
} }
} }
@ -396,28 +402,29 @@ func RemoveOrgUser(orgId, uid int64) error {
if _, err := sess.Id(ou.ID).Delete(ou); err != nil { if _, err := sess.Id(ou.ID).Delete(ou); err != nil {
return err return err
} else if _, err = sess.Exec("UPDATE `user` SET num_members=num_members-1 WHERE id=?", orgId); err != nil { } else if _, err = sess.Exec("UPDATE `user` SET num_members=num_members-1 WHERE id=?", orgID); err != nil {
return err return err
} }
// Delete all repository accesses. // Delete all repository accesses and unwatch them.
access := &Access{UserID: u.ID} repoIDs := make([]int64, len(repos))
for _, repo := range org.Repos { for i := range repos {
access.RepoID = repo.ID repoIDs = append(repoIDs, repos[i].ID)
if _, err = sess.Delete(access); err != nil { if err = watchRepo(sess, user.ID, repos[i].ID, false); err != nil {
return err
} else if err = watchRepo(sess, u.ID, repo.ID, false); err != nil {
return err return err
} }
} }
if _, err = sess.Where("user_id = ?", user.ID).In("repo_id", repoIDs).Delete(new(Access)); err != nil {
return err
}
// Delete member in his/her teams. // Delete member in his/her teams.
teams, err := getUserTeams(sess, org.ID, u.ID) teams, err := getUserTeams(sess, org.ID, user.ID)
if err != nil { if err != nil {
return err return err
} }
for _, t := range teams { for _, t := range teams {
if err = removeTeamMember(sess, org.ID, t.ID, u.ID); err != nil { if err = removeTeamMember(sess, org.ID, t.ID, user.ID); err != nil {
return err return err
} }
} }
@ -438,38 +445,86 @@ func RemoveOrgRepo(orgID, repoID int64) error {
return removeOrgRepo(x, orgID, repoID) return removeOrgRepo(x, orgID, repoID)
} }
// GetUserRepositories gets all repositories of an organization, // GetUserTeamIDs returns of all team IDs of the organization that user is memeber of.
// that the user with the given userID has access to. // It returns [-1] if user is not memeber of any teams.
func (org *User) GetUserRepositories(userID int64) (err error) { func (org *User) GetUserTeamIDs(userID int64) ([]int64, error) {
teams := make([]*Team, 0, org.NumTeams) teams := make([]*Team, 0, org.NumTeams)
if err = x.Sql(`SELECT team.id FROM team if err := x.Sql(`SELECT team.id FROM team
INNER JOIN team_user ON team_user.team_id = team.id INNER JOIN team_user ON team_user.team_id = team.id
WHERE team_user.org_id = ? AND team_user.uid = ?`, org.ID, userID).Find(&teams); err != nil { WHERE team_user.org_id = ? AND team_user.uid = ?`, org.ID, userID).Find(&teams); err != nil {
return fmt.Errorf("get teams: %v", err) return nil, err
} }
teamIDs := make([]string, len(teams)) teamIDs := make([]int64, len(teams))
for i := range teams { for i := range teams {
teamIDs[i] = com.ToStr(teams[i].ID) teamIDs[i] = teams[i].ID
} }
if len(teamIDs) == 0 { if len(teamIDs) == 0 {
// user has no team but "IN ()" is invalid SQL // user has no team but "IN ()" is invalid SQL
teamIDs = append(teamIDs, "-1") // there is no repo with id=-1 teamIDs = append(teamIDs, -1) // there is no repo with id=-1
}
return teamIDs, nil
}
// GetUserRepositories returns repositories of the organization
// that the user with the given userID has access to.
func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repository, int64, error) {
teamIDs, err := org.GetUserTeamIDs(userID)
if err != nil {
return nil, 0, fmt.Errorf("GetUserTeamIDs: %v", err)
} }
repos := make([]*Repository, 0, 5) if page <= 0 {
page = 1
}
repos := make([]*Repository, 0, pageSize)
// FIXME: use XORM chain operations instead of raw SQL.
if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository
INNER JOIN team_repo ON team_repo.repo_id = repository.id INNER JOIN team_repo
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s) ON team_repo.repo_id = repository.id
GROUP BY repository.id`, strings.Join(teamIDs, ",")), org.ID, false).Find(&repos); err != nil { WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
return fmt.Errorf("get repositories: %v", err) GROUP BY repository.id
ORDER BY updated_unix DESC
LIMIT %d OFFSET %d`,
strings.Join(base.Int64sToStrings(teamIDs), ","), pageSize, (page-1)*pageSize),
org.ID, false).Find(&repos); err != nil {
return nil, 0, fmt.Errorf("get repositories: %v", err)
} }
org.Repos = repos
// FIXME: should I change this value inside method, results, err := x.Query(fmt.Sprintf(`SELECT repository.id FROM repository
// or only in location of caller where it's really needed? INNER JOIN team_repo
org.NumRepos = len(org.Repos) ON team_repo.repo_id = repository.id
return nil WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
GROUP BY repository.id
ORDER BY updated_unix DESC`,
strings.Join(base.Int64sToStrings(teamIDs), ",")),
org.ID, false)
if err != nil {
log.Error(4, "count user repositories in organization: %v", err)
}
return repos, int64(len(results)), nil
}
// GetUserRepositories returns mirror repositories of the organization
// that the user with the given userID has access to.
func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
teamIDs, err := org.GetUserTeamIDs(userID)
if err != nil {
return nil, fmt.Errorf("GetUserTeamIDs: %v", err)
}
repos := make([]*Repository, 0, 10)
if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository
INNER JOIN team_repo
ON team_repo.repo_id = repository.id AND repository.is_mirror = ?
WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
GROUP BY repository.id`,
strings.Join(base.Int64sToStrings(teamIDs), ",")),
true, org.ID, false).Find(&repos); err != nil {
return nil, fmt.Errorf("get repositories: %v", err)
}
return repos, nil
} }
// GetTeams returns all teams that belong to organization, // GetTeams returns all teams that belong to organization,

View File

@ -1018,11 +1018,14 @@ func CreateRepository(u *User, opts CreateRepoOptions) (_ *Repository, err error
return repo, sess.Commit() return repo, sess.Commit()
} }
func countRepositories(showPrivate bool) int64 { func countRepositories(userID int64, private bool) int64 {
sess := x.NewSession() sess := x.Where("id > 0")
if !showPrivate { if userID > 0 {
sess.Where("is_private=?", false) sess.And("owner_id = ?", userID)
}
if !private {
sess.And("is_private=?", false)
} }
count, err := sess.Count(new(Repository)) count, err := sess.Count(new(Repository))
@ -1033,13 +1036,17 @@ func countRepositories(showPrivate bool) int64 {
} }
// CountRepositories returns number of repositories. // CountRepositories returns number of repositories.
func CountRepositories() int64 { // Argument private only takes effect when it is false,
return countRepositories(true) // set it true to count all repositories.
func CountRepositories(private bool) int64 {
return countRepositories(-1, private)
} }
// CountPublicRepositories returns number of public repositories. // CountUserRepositories returns number of repositories user owns.
func CountPublicRepositories() int64 { // Argument private only takes effect when it is false,
return countRepositories(false) // set it true to count all repositories.
func CountUserRepositories(userID int64, private bool) int64 {
return countRepositories(userID, private)
} }
func Repositories(page, pageSize int) (_ []*Repository, err error) { func Repositories(page, pageSize int) (_ []*Repository, err error) {
@ -1448,16 +1455,26 @@ func GetRepositoryByID(id int64) (*Repository, error) {
return getRepositoryByID(x, id) return getRepositoryByID(x, id)
} }
// GetRepositories returns a list of repositories of given user. // GetUserRepositories returns a list of repositories of given user.
func GetRepositories(uid int64, private bool) ([]*Repository, error) { func GetUserRepositories(userID int64, private bool, page, pageSize int) ([]*Repository, error) {
repos := make([]*Repository, 0, 10) sess := x.Where("owner_id = ?", userID).Desc("updated_unix")
sess := x.Desc("updated_unix")
if !private { if !private {
sess.Where("is_private=?", false) sess.And("is_private=?", false)
} }
return repos, sess.Find(&repos, &Repository{OwnerID: uid}) if page <= 0 {
page = 1
}
sess.Limit(pageSize, (page-1)*pageSize)
repos := make([]*Repository, 0, pageSize)
return repos, sess.Find(&repos)
}
// GetUserRepositories returns a list of mirror repositories of given user.
func GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
repos := make([]*Repository, 0, 10)
return repos, x.Where("owner_id = ?", userID).And("is_mirror = ?", true).Find(&repos)
} }
// GetRecentUpdatedRepositories returns the list of repositories that are recently updated. // GetRecentUpdatedRepositories returns the list of repositories that are recently updated.
@ -1849,6 +1866,74 @@ func CheckRepoStats() {
// ***** END: Repository.NumForks ***** // ***** END: Repository.NumForks *****
} }
type RepositoryList []*Repository
func (repos RepositoryList) loadAttributes(e Engine) error {
if len(repos) == 0 {
return nil
}
// Load owners.
set := make(map[int64]*User)
for i := range repos {
set[repos[i].OwnerID] = nil
}
userIDs := make([]int64, 0, len(set))
for userID := range set {
userIDs = append(userIDs, userID)
}
users := make([]*User, 0, len(userIDs))
if err := e.Where("id > 0").In("id", userIDs).Find(&users); err != nil {
return fmt.Errorf("find users: %v", err)
}
for i := range users {
set[users[i].ID] = users[i]
}
for i := range repos {
repos[i].Owner = set[repos[i].OwnerID]
}
return nil
}
func (repos RepositoryList) LoadAttributes() error {
return repos.loadAttributes(x)
}
type MirrorRepositoryList []*Repository
func (repos MirrorRepositoryList) loadAttributes(e Engine) error {
if len(repos) == 0 {
return nil
}
// Load mirrors.
repoIDs := make([]int64, 0, len(repos))
for i := range repos {
if !repos[i].IsMirror {
continue
}
repoIDs = append(repoIDs, repos[i].ID)
}
mirrors := make([]*Mirror, 0, len(repoIDs))
if err := e.Where("id > 0").In("repo_id", repoIDs).Find(&mirrors); err != nil {
return fmt.Errorf("find mirrors: %v", err)
}
set := make(map[int64]*Mirror)
for i := range mirrors {
set[mirrors[i].RepoID] = mirrors[i]
}
for i := range repos {
repos[i].Mirror = set[repos[i].ID]
}
return nil
}
func (repos MirrorRepositoryList) LoadAttributes() error {
return repos.loadAttributes(x)
}
// __ __ __ .__ // __ __ __ .__
// / \ / \_____ _/ |_ ____ | |__ // / \ / \_____ _/ |_ ____ | |__
// \ \/\/ /\__ \\ __\/ ___\| | \ // \ \/\/ /\__ \\ __\/ ___\| | \
@ -1863,40 +1948,40 @@ type Watch struct {
RepoID int64 `xorm:"UNIQUE(watch)"` RepoID int64 `xorm:"UNIQUE(watch)"`
} }
func isWatching(e Engine, uid, repoId int64) bool { func isWatching(e Engine, userID, repoID int64) bool {
has, _ := e.Get(&Watch{0, uid, repoId}) has, _ := e.Get(&Watch{0, userID, repoID})
return has return has
} }
// IsWatching checks if user has watched given repository. // IsWatching checks if user has watched given repository.
func IsWatching(uid, repoId int64) bool { func IsWatching(userID, repoID int64) bool {
return isWatching(x, uid, repoId) return isWatching(x, userID, repoID)
} }
func watchRepo(e Engine, uid, repoId int64, watch bool) (err error) { func watchRepo(e Engine, userID, repoID int64, watch bool) (err error) {
if watch { if watch {
if isWatching(e, uid, repoId) { if isWatching(e, userID, repoID) {
return nil return nil
} }
if _, err = e.Insert(&Watch{RepoID: repoId, UserID: uid}); err != nil { if _, err = e.Insert(&Watch{RepoID: repoID, UserID: userID}); err != nil {
return err return err
} }
_, err = e.Exec("UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?", repoId) _, err = e.Exec("UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?", repoID)
} else { } else {
if !isWatching(e, uid, repoId) { if !isWatching(e, userID, repoID) {
return nil return nil
} }
if _, err = e.Delete(&Watch{0, uid, repoId}); err != nil { if _, err = e.Delete(&Watch{0, userID, repoID}); err != nil {
return err return err
} }
_, err = e.Exec("UPDATE `repository` SET num_watches=num_watches-1 WHERE id=?", repoId) _, err = e.Exec("UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?", repoID)
} }
return err return err
} }
// Watch or unwatch repository. // Watch or unwatch repository.
func WatchRepo(uid, repoId int64, watch bool) (err error) { func WatchRepo(userID, repoID int64, watch bool) (err error) {
return watchRepo(x, uid, repoId, watch) return watchRepo(x, userID, repoID, watch)
} }
func getWatchers(e Engine, repoID int64) ([]*Watch, error) { func getWatchers(e Engine, repoID int64) ([]*Watch, error) {
@ -1967,34 +2052,34 @@ type Star struct {
} }
// Star or unstar repository. // Star or unstar repository.
func StarRepo(uid, repoId int64, star bool) (err error) { func StarRepo(userID, repoID int64, star bool) (err error) {
if star { if star {
if IsStaring(uid, repoId) { if IsStaring(userID, repoID) {
return nil return nil
} }
if _, err = x.Insert(&Star{UID: uid, RepoID: repoId}); err != nil { if _, err = x.Insert(&Star{UID: userID, RepoID: repoID}); err != nil {
return err return err
} else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoId); err != nil { } else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars + 1 WHERE id = ?", repoID); err != nil {
return err return err
} }
_, err = x.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", uid) _, err = x.Exec("UPDATE `user` SET num_stars = num_stars + 1 WHERE id = ?", userID)
} else { } else {
if !IsStaring(uid, repoId) { if !IsStaring(userID, repoID) {
return nil return nil
} }
if _, err = x.Delete(&Star{0, uid, repoId}); err != nil { if _, err = x.Delete(&Star{0, userID, repoID}); err != nil {
return err return err
} else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoId); err != nil { } else if _, err = x.Exec("UPDATE `repository` SET num_stars = num_stars - 1 WHERE id = ?", repoID); err != nil {
return err return err
} }
_, err = x.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", uid) _, err = x.Exec("UPDATE `user` SET num_stars = num_stars - 1 WHERE id = ?", userID)
} }
return err return err
} }
// IsStaring checks if user has starred given repository. // IsStaring checks if user has starred given repository.
func IsStaring(uid, repoId int64) bool { func IsStaring(userID, repoID int64) bool {
has, _ := x.Get(&Star{0, uid, repoId}) has, _ := x.Get(&Star{0, userID, repoID})
return has return has
} }

View File

@ -402,12 +402,17 @@ func (u *User) GetOrganizationCount() (int64, error) {
return u.getOrganizationCount(x) return u.getOrganizationCount(x)
} }
// GetRepositories returns all repositories that user owns, including private repositories. // GetRepositories returns repositories that user owns, including private repositories.
func (u *User) GetRepositories() (err error) { func (u *User) GetRepositories(page, pageSize int) (err error) {
u.Repos, err = GetRepositories(u.ID, true) u.Repos, err = GetUserRepositories(u.ID, true, page, pageSize)
return err return err
} }
// GetRepositories returns mirror repositories that user owns, including private repositories.
func (u *User) GetMirrorRepositories() ([]*Repository, error) {
return GetUserMirrorRepositories(u.ID)
}
// GetOwnedOrganizations returns all organizations that user owns. // GetOwnedOrganizations returns all organizations that user owns.
func (u *User) GetOwnedOrganizations() (err error) { func (u *User) GetOwnedOrganizations() (err error) {
u.OwnedOrgs, err = GetOwnedOrgsByUserID(u.ID) u.OwnedOrgs, err = GetOwnedOrgsByUserID(u.ID)

File diff suppressed because one or more lines are too long

View File

@ -2563,9 +2563,10 @@ footer .ui.language .menu {
border-radius: 3px; border-radius: 3px;
word-break: break-all; word-break: break-all;
} }
.feeds .list .header { .feeds .list .header .ui.label {
padding-top: 10px; margin-top: -4px;
padding-bottom: 5px; padding: 4px 5px;
font-weight: normal;
} }
.feeds .list .header .plus.icon { .feeds .list .header .plus.icon {
margin-top: 5px; margin-top: 5px;

View File

@ -92,8 +92,11 @@
.list { .list {
.header { .header {
padding-top: 10px; .ui.label {
padding-bottom: 5px; margin-top: -4px;
padding: 4px 5px;
font-weight: normal;
}
.plus.icon { .plus.icon {
margin-top: 5px; margin-top: 5px;

View File

@ -78,7 +78,7 @@ func Search(ctx *context.APIContext) {
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
func ListMyRepos(ctx *context.APIContext) { func ListMyRepos(ctx *context.APIContext) {
ownRepos, err := models.GetRepositories(ctx.User.ID, true) ownRepos, err := models.GetUserRepositories(ctx.User.ID, true, 1, ctx.User.NumRepos)
if err != nil { if err != nil {
ctx.Error(500, "GetRepositories", err) ctx.Error(500, "GetRepositories", err)
return return

View File

@ -45,7 +45,7 @@ func Home(ctx *context.Context) {
} }
type RepoSearchOptions struct { type RepoSearchOptions struct {
Counter func() int64 Counter func(bool) int64
Ranger func(int, int) ([]*models.Repository, error) Ranger func(int, int) ([]*models.Repository, error)
Private bool Private bool
PageSize int PageSize int
@ -55,7 +55,7 @@ type RepoSearchOptions struct {
func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
page := ctx.QueryInt("page") page := ctx.QueryInt("page")
if page <= 1 { if page <= 0 {
page = 1 page = 1
} }
@ -72,7 +72,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) {
ctx.Handle(500, "opts.Ranger", err) ctx.Handle(500, "opts.Ranger", err)
return return
} }
count = opts.Counter() count = opts.Counter(opts.Private)
} else { } else {
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{
Keyword: keyword, Keyword: keyword,
@ -107,7 +107,7 @@ func ExploreRepos(ctx *context.Context) {
ctx.Data["PageIsExploreRepositories"] = true ctx.Data["PageIsExploreRepositories"] = true
RenderRepoSearch(ctx, &RepoSearchOptions{ RenderRepoSearch(ctx, &RepoSearchOptions{
Counter: models.CountPublicRepositories, Counter: models.CountRepositories,
Ranger: models.GetRecentUpdatedRepositories, Ranger: models.GetRecentUpdatedRepositories,
PageSize: setting.UI.ExplorePagingNum, PageSize: setting.UI.ExplorePagingNum,
OrderBy: "updated_unix DESC", OrderBy: "updated_unix DESC",

View File

@ -55,8 +55,8 @@ func getDashboardContextUser(ctx *context.Context) *models.User {
// retrieveFeeds loads feeds from database by given context user. // retrieveFeeds loads feeds from database by given context user.
// The user could be organization so it is not always the logged in 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. // which is why we have to explicitly pass the context user ID.
func retrieveFeeds(ctx *context.Context, ctxUserID, userID, offset int64, isProfile bool) { func retrieveFeeds(ctx *context.Context, ctxUser *models.User, userID, offset int64, isProfile bool) {
actions, err := models.GetFeeds(ctxUserID, userID, offset, isProfile) actions, err := models.GetFeeds(ctxUser, userID, offset, isProfile)
if err != nil { if err != nil {
ctx.Handle(500, "GetFeeds", err) ctx.Handle(500, "GetFeeds", err)
return return
@ -98,54 +98,55 @@ func Dashboard(ctx *context.Context) {
// Only user can have collaborative repositories. // Only user can have collaborative repositories.
if !ctxUser.IsOrganization() { if !ctxUser.IsOrganization() {
collaborateRepos, err := ctx.User.GetAccessibleRepositories() collaborateRepos, err := ctx.User.GetAccessibleRepositories(setting.UI.User.RepoPagingNum)
if err != nil { if err != nil {
ctx.Handle(500, "GetAccessibleRepositories", err) ctx.Handle(500, "GetAccessibleRepositories", err)
return return
} else if err = models.RepositoryList(collaborateRepos).LoadAttributes(); err != nil {
ctx.Handle(500, "RepositoryList.LoadAttributes", err)
return
} }
for i := range collaborateRepos {
if err = collaborateRepos[i].GetOwner(); err != nil {
ctx.Handle(500, "GetOwner: "+collaborateRepos[i].Name, err)
return
}
}
ctx.Data["CollaborateCount"] = len(collaborateRepos)
ctx.Data["CollaborativeRepos"] = collaborateRepos ctx.Data["CollaborativeRepos"] = collaborateRepos
} }
var repos []*models.Repository var err error
var repos, mirrors []*models.Repository
if ctxUser.IsOrganization() { if ctxUser.IsOrganization() {
if err := ctxUser.GetUserRepositories(ctx.User.ID); err != nil { repos, _, err = ctxUser.GetUserRepositories(ctx.User.ID, 1, setting.UI.User.RepoPagingNum)
if err != nil {
ctx.Handle(500, "GetUserRepositories", err) ctx.Handle(500, "GetUserRepositories", err)
return return
} }
repos = ctxUser.Repos
} else { mirrors, err = ctxUser.GetUserMirrorRepositories(ctx.User.ID)
var err error
repos, err = models.GetRepositories(ctxUser.ID, true)
if err != nil { if err != nil {
ctx.Handle(500, "GetUserMirrorRepositories", err)
return
}
} else {
if err = ctxUser.GetRepositories(1, setting.UI.User.RepoPagingNum); err != nil {
ctx.Handle(500, "GetRepositories", err) ctx.Handle(500, "GetRepositories", err)
return return
} }
repos = ctxUser.Repos
mirrors, err = ctxUser.GetMirrorRepositories()
if err != nil {
ctx.Handle(500, "GetMirrorRepositories", err)
return
}
} }
ctx.Data["Repos"] = repos ctx.Data["Repos"] = repos
ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum
// Get mirror repositories. if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil {
mirrors := make([]*models.Repository, 0, 5) ctx.Handle(500, "MirrorRepositoryList.LoadAttributes", err)
for _, repo := range repos { return
if repo.IsMirror {
if err := repo.GetMirror(); err != nil {
ctx.Handle(500, "GetMirror: "+repo.Name, err)
return
}
mirrors = append(mirrors, repo)
}
} }
ctx.Data["MirrorCount"] = len(mirrors) ctx.Data["MirrorCount"] = len(mirrors)
ctx.Data["Mirrors"] = mirrors ctx.Data["Mirrors"] = mirrors
retrieveFeeds(ctx, ctxUser.ID, ctx.User.ID, 0, false) retrieveFeeds(ctx, ctxUser, ctx.User.ID, 0, false)
if ctx.Written() { if ctx.Written() {
return return
} }
@ -198,18 +199,21 @@ func Issues(ctx *context.Context) {
isShowClosed := ctx.Query("state") == "closed" isShowClosed := ctx.Query("state") == "closed"
// Get repositories. // Get repositories.
var err error
var repos []*models.Repository
if ctxUser.IsOrganization() { if ctxUser.IsOrganization() {
if err := ctxUser.GetUserRepositories(ctx.User.ID); err != nil { repos, _, err = ctxUser.GetUserRepositories(ctx.User.ID, 1, ctx.User.NumRepos)
if err != nil {
ctx.Handle(500, "GetRepositories", err) ctx.Handle(500, "GetRepositories", err)
return return
} }
} else { } else {
if err := ctxUser.GetRepositories(); err != nil { if err := ctxUser.GetRepositories(1, ctx.User.NumRepos); err != nil {
ctx.Handle(500, "GetRepositories", err) ctx.Handle(500, "GetRepositories", err)
return return
} }
repos = ctxUser.Repos
} }
repos := ctxUser.Repos
allCount := 0 allCount := 0
repoIDs := make([]int64, 0, len(repos)) repoIDs := make([]int64, 0, len(repos))
@ -331,29 +335,34 @@ func showOrgProfile(ctx *context.Context) {
org := ctx.Org.Organization org := ctx.Org.Organization
ctx.Data["Title"] = org.FullName ctx.Data["Title"] = org.FullName
if ctx.IsSigned { page := ctx.QueryInt("page")
if ctx.User.IsAdmin { if page <= 0 {
repos, err := models.GetRepositories(org.ID, true) page = 1
if err != nil { }
ctx.Handle(500, "GetRepositoriesAsAdmin", err)
return var (
} repos []*models.Repository
ctx.Data["Repos"] = repos count int64
} else { err error
if err := org.GetUserRepositories(ctx.User.ID); err != nil { )
ctx.Handle(500, "GetUserRepositories", err) if ctx.IsSigned && !ctx.User.IsAdmin {
return repos, count, err = org.GetUserRepositories(ctx.User.ID, page, setting.UI.User.RepoPagingNum)
} if err != nil {
ctx.Data["Repos"] = org.Repos ctx.Handle(500, "GetUserRepositories", err)
return
} }
ctx.Data["Repos"] = repos
} else { } else {
repos, err := models.GetRepositories(org.ID, false) showPrivate := ctx.IsSigned && ctx.User.IsAdmin
repos, err = models.GetUserRepositories(org.ID, showPrivate, page, setting.UI.User.RepoPagingNum)
if err != nil { if err != nil {
ctx.Handle(500, "GetRepositories", err) ctx.Handle(500, "GetRepositories", err)
return return
} }
ctx.Data["Repos"] = repos ctx.Data["Repos"] = repos
count = models.CountUserRepositories(org.ID, showPrivate)
} }
ctx.Data["Page"] = paginater.New(int(count), setting.UI.User.RepoPagingNum, page, 5)
if err := org.GetMembers(); err != nil { if err := org.GetMembers(); err != nil {
ctx.Handle(500, "GetMembers", err) ctx.Handle(500, "GetMembers", err)

View File

@ -9,6 +9,8 @@ import (
"path" "path"
"strings" "strings"
"github.com/Unknwon/paginater"
"github.com/gogits/gogs/models" "github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/context"
@ -55,27 +57,27 @@ func Profile(ctx *context.Context) {
isShowKeys = true isShowKeys = true
} }
u := GetUserByName(ctx, strings.TrimSuffix(uname, ".keys")) ctxUser := GetUserByName(ctx, strings.TrimSuffix(uname, ".keys"))
if ctx.Written() { if ctx.Written() {
return return
} }
// Show SSH keys. // Show SSH keys.
if isShowKeys { if isShowKeys {
ShowSSHKeys(ctx, u.ID) ShowSSHKeys(ctx, ctxUser.ID)
return return
} }
if u.IsOrganization() { if ctxUser.IsOrganization() {
showOrgProfile(ctx) showOrgProfile(ctx)
return return
} }
ctx.Data["Title"] = u.DisplayName() ctx.Data["Title"] = ctxUser.DisplayName()
ctx.Data["PageIsUserProfile"] = true ctx.Data["PageIsUserProfile"] = true
ctx.Data["Owner"] = u ctx.Data["Owner"] = ctxUser
orgs, err := models.GetOrgsByUserID(u.ID, ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == u.ID)) orgs, err := models.GetOrgsByUserID(ctxUser.ID, ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == ctxUser.ID))
if err != nil { if err != nil {
ctx.Handle(500, "GetOrgsByUserIDDesc", err) ctx.Handle(500, "GetOrgsByUserIDDesc", err)
return return
@ -87,17 +89,22 @@ func Profile(ctx *context.Context) {
ctx.Data["TabName"] = tab ctx.Data["TabName"] = tab
switch tab { switch tab {
case "activity": case "activity":
retrieveFeeds(ctx, u.ID, -1, 0, true) retrieveFeeds(ctx, ctxUser, -1, 0, true)
if ctx.Written() { if ctx.Written() {
return return
} }
default: default:
var err error page := ctx.QueryInt("page")
ctx.Data["Repos"], err = models.GetRepositories(u.ID, ctx.IsSigned && ctx.User.ID == u.ID) if page <= 0 {
page = 1
}
ctx.Data["Repos"], err = models.GetUserRepositories(ctxUser.ID, ctx.IsSigned && ctx.User.ID == ctxUser.ID, page, setting.UI.User.RepoPagingNum)
if err != nil { if err != nil {
ctx.Handle(500, "GetRepositories", err) ctx.Handle(500, "GetRepositories", err)
return return
} }
ctx.Data["Page"] = paginater.New(ctxUser.NumRepos, setting.UI.User.RepoPagingNum, page, 5)
} }
ctx.HTML(200, PROFILE) ctx.HTML(200, PROFILE)

View File

@ -1 +1 @@
0.9.53.0724 0.9.54.0724

View File

@ -32,6 +32,7 @@
<div class="ui divider"></div> <div class="ui divider"></div>
{{end}} {{end}}
{{template "explore/repo_list" .}} {{template "explore/repo_list" .}}
{{template "explore/page" .}}
</div> </div>
<div class="ui five wide column"> <div class="ui five wide column">

View File

@ -37,12 +37,17 @@
</a> </a>
</li> </li>
{{end}} {{end}}
{{if gt .ContextUser.NumRepos .MaxShowRepoNum}}
<li>
<a href="{{.ContextUser.HomeLink}}">{{.i18n.Tr "home.show_more_repos"}}</a>
</li>
{{end}}
</ul> </ul>
</div> </div>
{{if not .ContextUser.IsOrganization}} {{if not .ContextUser.IsOrganization}}
<h4 class="ui top attached header"> <h4 class="ui top attached header">
{{.i18n.Tr "home.collaborative_repos"}} <span class="ui grey label">{{.CollaborateCount}}</span> {{.i18n.Tr "home.collaborative_repos"}}
</h4> </h4>
<div class="ui attached table segment"> <div class="ui attached table segment">
<ul id="collaborative-repo-list"> <ul id="collaborative-repo-list">

View File

@ -86,6 +86,7 @@
</div> </div>
{{if ne .TabName "activity"}} {{if ne .TabName "activity"}}
{{template "explore/repo_list" .}} {{template "explore/repo_list" .}}
{{template "explore/page" .}}
{{else}} {{else}}
<br> <br>
<div class="feeds"> <div class="feeds">