summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2016-07-24 14:32:46 +0800
committerUnknwon <u@gogs.io>2016-07-24 14:32:46 +0800
commite74630ae3b635a43a1bdafcf8b80d2f87b3536b6 (patch)
tree63c5fb32b6ca4417c3dfd9c05f532d5598aa4117 /models
parent1f2e173a745da8e4b57f96b5561a3c10054d3b76 (diff)
downloadgitea-e74630ae3b635a43a1bdafcf8b80d2f87b3536b6.tar.gz
gitea-e74630ae3b635a43a1bdafcf8b80d2f87b3536b6.zip
#1384 add pagination for repositories
Diffstat (limited to 'models')
-rw-r--r--models/access.go28
-rw-r--r--models/action.go21
-rw-r--r--models/issue.go2
-rw-r--r--models/models.go6
-rw-r--r--models/org.go149
-rw-r--r--models/repo.go165
-rw-r--r--models/user.go11
7 files changed, 261 insertions, 121 deletions
diff --git a/models/access.go b/models/access.go
index 32e9b6ea8b..b3be9b5bb6 100644
--- a/models/access.go
+++ b/models/access.go
@@ -121,23 +121,17 @@ func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
return repos, nil
}
-// GetAccessibleRepositories finds all repositories where a user has access but does not own.
-func (u *User) GetAccessibleRepositories() ([]*Repository, error) {
- accesses := make([]*Access, 0, 10)
- if err := x.Find(&accesses, &Access{UserID: u.ID}); err != nil {
- return nil, err
- }
-
- 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)
+// GetAccessibleRepositories finds repositories which the user has access but does not own.
+// If limit is smaller than 1 means returns all found results.
+func (user *User) GetAccessibleRepositories(limit int) (repos []*Repository, _ error) {
+ sess := x.Where("owner_id !=? ", user.ID).Desc("updated_unix")
+ 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)
}
func maxAccessMode(modes ...AccessMode) AccessMode {
diff --git a/models/action.go b/models/action.go
index 23b526ee95..6e2234fda5 100644
--- a/models/action.go
+++ b/models/action.go
@@ -602,21 +602,22 @@ func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error
}
// 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.
-// userID can be -1, if isProfile is true or in order to skip the permission check.
-func GetFeeds(ctxUserID, userID, offset int64, isProfile bool) ([]*Action, error) {
+// actorID is the user who's requesting, ctxUserID is the user/org that is requested.
+// actorID can be -1 when isProfile is true or to skip the permission check.
+func GetFeeds(ctxUser *User, actorID, offset int64, isProfile bool) ([]*Action, error) {
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 {
- sess.And("is_private=?", false).And("act_user_id=?", ctxUserID)
- } else if ctxUserID != -1 {
- ctxUser := &User{ID: ctxUserID}
- if err := ctxUser.GetUserRepositories(userID); err != nil {
- return nil, err
+ sess.And("is_private = ?", false).And("act_user_id = ?", ctxUser.ID)
+ } else if actorID != -1 && ctxUser.IsOrganization() {
+ // FIXME: only need to get IDs here, not all fields of repository.
+ repos, _, err := ctxUser.GetUserRepositories(actorID, 1, ctxUser.NumRepos)
+ if err != nil {
+ return nil, fmt.Errorf("GetUserRepositories: %v", err)
}
var repoIDs []int64
- for _, repo := range ctxUser.Repos {
+ for _, repo := range repos {
repoIDs = append(repoIDs, repo.ID)
}
diff --git a/models/issue.go b/models/issue.go
index b8183b94e8..3fd97fbe7d 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -82,7 +82,7 @@ func (issue *Issue) loadAttributes(e Engine) (err error) {
return nil
}
-func (issue *Issue) LoadAttributes() (err error) {
+func (issue *Issue) LoadAttributes() error {
return issue.loadAttributes(x)
}
diff --git a/models/models.go b/models/models.go
index 90fa3f6465..758e1e8270 100644
--- a/models/models.go
+++ b/models/models.go
@@ -98,8 +98,8 @@ func LoadConfigs() {
func getEngine() (*xorm.Engine, error) {
cnnstr := ""
var Param string = "?"
- if strings.Contains(DbCfg.Name,Param) {
- Param="&"
+ if strings.Contains(DbCfg.Name, Param) {
+ Param = "&"
}
switch DbCfg.Type {
case "mysql":
@@ -206,7 +206,7 @@ func GetStatistic() (stats Statistic) {
stats.Counter.User = CountUsers()
stats.Counter.Org = CountOrganizations()
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.Star, _ = x.Count(new(Star))
stats.Counter.Action, _ = x.Count(new(Action))
diff --git a/models/org.go b/models/org.go
index c85e88d618..32aa4fd25e 100644
--- a/models/org.go
+++ b/models/org.go
@@ -10,8 +10,10 @@ import (
"os"
"strings"
- "github.com/Unknwon/com"
"github.com/go-xorm/xorm"
+
+ "github.com/gogits/gogs/modules/base"
+ "github.com/gogits/gogs/modules/log"
)
var (
@@ -313,9 +315,9 @@ func GetOrgUsersByOrgID(orgID int64) ([]*OrgUser, error) {
}
// 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)
- 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 {
return err
} else if !has {
@@ -328,8 +330,8 @@ func ChangeOrgUserStatus(orgId, uid int64, public bool) error {
}
// AddOrgUser adds new user to given organization.
-func AddOrgUser(orgId, uid int64) error {
- if IsOrganizationMember(orgId, uid) {
+func AddOrgUser(orgID, uid int64) error {
+ if IsOrganizationMember(orgID, uid) {
return nil
}
@@ -341,13 +343,13 @@ func AddOrgUser(orgId, uid int64) error {
ou := &OrgUser{
Uid: uid,
- OrgID: orgId,
+ OrgID: orgID,
}
if _, err := sess.Insert(ou); err != nil {
sess.Rollback()
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()
return err
}
@@ -356,35 +358,39 @@ func AddOrgUser(orgId, uid int64) error {
}
// RemoveOrgUser removes user from given organization.
-func RemoveOrgUser(orgId, uid int64) error {
+func RemoveOrgUser(orgID, userID int64) error {
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 {
return fmt.Errorf("get org-user: %v", err)
} else if !has {
return nil
}
- u, err := GetUserByID(uid)
+ user, err := GetUserByID(userID)
+ if err != nil {
+ return fmt.Errorf("GetUserByID [%d]: %v", userID, err)
+ }
+ org, err := GetUserByID(orgID)
if err != nil {
- return fmt.Errorf("GetUserById: %v", err)
+ return fmt.Errorf("GetUserByID [%d]: %v", orgID, err)
}
- org, err := GetUserByID(orgId)
+
+ // 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("get organization: %v", err)
- } else if err = org.GetRepositories(); err != nil {
- return fmt.Errorf("GetRepositories: %v", err)
+ return fmt.Errorf("GetUserRepositories [%d]: %v", user.ID, err)
}
// 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()
if err != nil {
return err
}
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 {
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
}
- // Delete all repository accesses.
- access := &Access{UserID: u.ID}
- for _, repo := range org.Repos {
- access.RepoID = repo.ID
- if _, err = sess.Delete(access); err != nil {
- return err
- } else if err = watchRepo(sess, u.ID, repo.ID, false); err != nil {
+ // Delete all repository accesses and unwatch them.
+ repoIDs := make([]int64, len(repos))
+ for i := range repos {
+ repoIDs = append(repoIDs, repos[i].ID)
+ if err = watchRepo(sess, user.ID, repos[i].ID, false); err != nil {
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.
- teams, err := getUserTeams(sess, org.ID, u.ID)
+ teams, err := getUserTeams(sess, org.ID, user.ID)
if err != nil {
return err
}
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
}
}
@@ -438,38 +445,86 @@ func RemoveOrgRepo(orgID, repoID int64) error {
return removeOrgRepo(x, orgID, repoID)
}
-// GetUserRepositories gets all repositories of an organization,
-// that the user with the given userID has access to.
-func (org *User) GetUserRepositories(userID int64) (err error) {
+// GetUserTeamIDs returns of all team IDs of the organization that user is memeber of.
+// It returns [-1] if user is not memeber of any teams.
+func (org *User) GetUserTeamIDs(userID int64) ([]int64, error) {
teams := make([]*Team, 0, org.NumTeams)
- if err = x.Sql(`SELECT team.id FROM team
-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 {
- return fmt.Errorf("get teams: %v", err)
+ if err := x.Sql(`SELECT team.id FROM team
+ 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 {
+ return nil, err
}
- teamIDs := make([]string, len(teams))
+ teamIDs := make([]int64, len(teams))
for i := range teams {
- teamIDs[i] = com.ToStr(teams[i].ID)
+ teamIDs[i] = teams[i].ID
}
if len(teamIDs) == 0 {
// 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
-INNER JOIN team_repo ON team_repo.repo_id = repository.id
-WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
-GROUP BY repository.id`, strings.Join(teamIDs, ",")), org.ID, false).Find(&repos); err != nil {
- return fmt.Errorf("get repositories: %v", err)
+ INNER JOIN team_repo
+ ON team_repo.repo_id = repository.id
+ WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
+ 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)
+ }
+
+ results, err := x.Query(fmt.Sprintf(`SELECT repository.id FROM repository
+ INNER JOIN team_repo
+ ON team_repo.repo_id = repository.id
+ 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)
}
- org.Repos = repos
- // FIXME: should I change this value inside method,
- // or only in location of caller where it's really needed?
- org.NumRepos = len(org.Repos)
- return nil
+ 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,
diff --git a/models/repo.go b/models/repo.go
index 1c395e583c..02b4e9fbc7 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -1018,11 +1018,14 @@ func CreateRepository(u *User, opts CreateRepoOptions) (_ *Repository, err error
return repo, sess.Commit()
}
-func countRepositories(showPrivate bool) int64 {
- sess := x.NewSession()
+func countRepositories(userID int64, private bool) int64 {
+ sess := x.Where("id > 0")
- if !showPrivate {
- sess.Where("is_private=?", false)
+ if userID > 0 {
+ sess.And("owner_id = ?", userID)
+ }
+ if !private {
+ sess.And("is_private=?", false)
}
count, err := sess.Count(new(Repository))
@@ -1033,13 +1036,17 @@ func countRepositories(showPrivate bool) int64 {
}
// CountRepositories returns number of repositories.
-func CountRepositories() int64 {
- return countRepositories(true)
+// Argument private only takes effect when it is false,
+// set it true to count all repositories.
+func CountRepositories(private bool) int64 {
+ return countRepositories(-1, private)
}
-// CountPublicRepositories returns number of public repositories.
-func CountPublicRepositories() int64 {
- return countRepositories(false)
+// CountUserRepositories returns number of repositories user owns.
+// Argument private only takes effect when it is 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) {
@@ -1448,16 +1455,26 @@ func GetRepositoryByID(id int64) (*Repository, error) {
return getRepositoryByID(x, id)
}
-// GetRepositories returns a list of repositories of given user.
-func GetRepositories(uid int64, private bool) ([]*Repository, error) {
- repos := make([]*Repository, 0, 10)
- sess := x.Desc("updated_unix")
-
+// GetUserRepositories returns a list of repositories of given user.
+func GetUserRepositories(userID int64, private bool, page, pageSize int) ([]*Repository, error) {
+ sess := x.Where("owner_id = ?", userID).Desc("updated_unix")
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.
@@ -1849,6 +1866,74 @@ func CheckRepoStats() {
// ***** 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)"`
}
-func isWatching(e Engine, uid, repoId int64) bool {
- has, _ := e.Get(&Watch{0, uid, repoId})
+func isWatching(e Engine, userID, repoID int64) bool {
+ has, _ := e.Get(&Watch{0, userID, repoID})
return has
}
// IsWatching checks if user has watched given repository.
-func IsWatching(uid, repoId int64) bool {
- return isWatching(x, uid, repoId)
+func IsWatching(userID, repoID int64) bool {
+ 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 isWatching(e, uid, repoId) {
+ if isWatching(e, userID, repoID) {
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
}
- _, 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 {
- if !isWatching(e, uid, repoId) {
+ if !isWatching(e, userID, repoID) {
return nil
}
- if _, err = e.Delete(&Watch{0, uid, repoId}); err != nil {
+ if _, err = e.Delete(&Watch{0, userID, repoID}); err != nil {
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
}
// Watch or unwatch repository.
-func WatchRepo(uid, repoId int64, watch bool) (err error) {
- return watchRepo(x, uid, repoId, watch)
+func WatchRepo(userID, repoID int64, watch bool) (err error) {
+ return watchRepo(x, userID, repoID, watch)
}
func getWatchers(e Engine, repoID int64) ([]*Watch, error) {
@@ -1967,34 +2052,34 @@ type Star struct {
}
// 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 IsStaring(uid, repoId) {
+ if IsStaring(userID, repoID) {
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
- } 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
}
- _, 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 {
- if !IsStaring(uid, repoId) {
+ if !IsStaring(userID, repoID) {
return nil
}
- if _, err = x.Delete(&Star{0, uid, repoId}); err != nil {
+ if _, err = x.Delete(&Star{0, userID, repoID}); err != nil {
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
}
- _, 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
}
// IsStaring checks if user has starred given repository.
-func IsStaring(uid, repoId int64) bool {
- has, _ := x.Get(&Star{0, uid, repoId})
+func IsStaring(userID, repoID int64) bool {
+ has, _ := x.Get(&Star{0, userID, repoID})
return has
}
diff --git a/models/user.go b/models/user.go
index 5b1fd4ae51..a22162dc2d 100644
--- a/models/user.go
+++ b/models/user.go
@@ -402,12 +402,17 @@ func (u *User) GetOrganizationCount() (int64, error) {
return u.getOrganizationCount(x)
}
-// GetRepositories returns all repositories that user owns, including private repositories.
-func (u *User) GetRepositories() (err error) {
- u.Repos, err = GetRepositories(u.ID, true)
+// GetRepositories returns repositories that user owns, including private repositories.
+func (u *User) GetRepositories(page, pageSize int) (err error) {
+ u.Repos, err = GetUserRepositories(u.ID, true, page, pageSize)
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.
func (u *User) GetOwnedOrganizations() (err error) {
u.OwnedOrgs, err = GetOwnedOrgsByUserID(u.ID)