@@ -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) | |||
##### 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 | | |||
|:-------------:|:-------:|:-------:| |
@@ -124,6 +124,7 @@ uname_holder = Username or email | |||
password_holder = Password | |||
switch_dashboard_context = Switch Dashboard Context | |||
my_repos = My Repositories | |||
show_more_repos = Show more repositories... | |||
collaborative_repos = Collaborative Repositories | |||
my_orgs = My Organizations | |||
my_mirrors = My Mirrors |
@@ -17,7 +17,7 @@ import ( | |||
"github.com/gogits/gogs/modules/setting" | |||
) | |||
const APP_VER = "0.9.53.0724" | |||
const APP_VER = "0.9.54.0724" | |||
func init() { | |||
runtime.GOMAXPROCS(runtime.NumCPU()) |
@@ -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 { |
@@ -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) | |||
} | |||
@@ -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) | |||
} | |||
@@ -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)) |
@@ -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, |
@@ -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 | |||
} | |||
@@ -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) |
@@ -2563,9 +2563,10 @@ footer .ui.language .menu { | |||
border-radius: 3px; | |||
word-break: break-all; | |||
} | |||
.feeds .list .header { | |||
padding-top: 10px; | |||
padding-bottom: 5px; | |||
.feeds .list .header .ui.label { | |||
margin-top: -4px; | |||
padding: 4px 5px; | |||
font-weight: normal; | |||
} | |||
.feeds .list .header .plus.icon { | |||
margin-top: 5px; |
@@ -92,8 +92,11 @@ | |||
.list { | |||
.header { | |||
padding-top: 10px; | |||
padding-bottom: 5px; | |||
.ui.label { | |||
margin-top: -4px; | |||
padding: 4px 5px; | |||
font-weight: normal; | |||
} | |||
.plus.icon { | |||
margin-top: 5px; |
@@ -78,7 +78,7 @@ func Search(ctx *context.APIContext) { | |||
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories | |||
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 { | |||
ctx.Error(500, "GetRepositories", err) | |||
return |
@@ -45,7 +45,7 @@ func Home(ctx *context.Context) { | |||
} | |||
type RepoSearchOptions struct { | |||
Counter func() int64 | |||
Counter func(bool) int64 | |||
Ranger func(int, int) ([]*models.Repository, error) | |||
Private bool | |||
PageSize int | |||
@@ -55,7 +55,7 @@ type RepoSearchOptions struct { | |||
func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { | |||
page := ctx.QueryInt("page") | |||
if page <= 1 { | |||
if page <= 0 { | |||
page = 1 | |||
} | |||
@@ -72,7 +72,7 @@ func RenderRepoSearch(ctx *context.Context, opts *RepoSearchOptions) { | |||
ctx.Handle(500, "opts.Ranger", err) | |||
return | |||
} | |||
count = opts.Counter() | |||
count = opts.Counter(opts.Private) | |||
} else { | |||
repos, count, err = models.SearchRepositoryByName(&models.SearchRepoOptions{ | |||
Keyword: keyword, | |||
@@ -107,7 +107,7 @@ func ExploreRepos(ctx *context.Context) { | |||
ctx.Data["PageIsExploreRepositories"] = true | |||
RenderRepoSearch(ctx, &RepoSearchOptions{ | |||
Counter: models.CountPublicRepositories, | |||
Counter: models.CountRepositories, | |||
Ranger: models.GetRecentUpdatedRepositories, | |||
PageSize: setting.UI.ExplorePagingNum, | |||
OrderBy: "updated_unix DESC", |
@@ -55,8 +55,8 @@ func getDashboardContextUser(ctx *context.Context) *models.User { | |||
// 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. | |||
func retrieveFeeds(ctx *context.Context, ctxUserID, userID, offset int64, isProfile bool) { | |||
actions, err := models.GetFeeds(ctxUserID, userID, offset, isProfile) | |||
func retrieveFeeds(ctx *context.Context, ctxUser *models.User, userID, offset int64, isProfile bool) { | |||
actions, err := models.GetFeeds(ctxUser, userID, offset, isProfile) | |||
if err != nil { | |||
ctx.Handle(500, "GetFeeds", err) | |||
return | |||
@@ -98,54 +98,55 @@ func Dashboard(ctx *context.Context) { | |||
// Only user can have collaborative repositories. | |||
if !ctxUser.IsOrganization() { | |||
collaborateRepos, err := ctx.User.GetAccessibleRepositories() | |||
collaborateRepos, err := ctx.User.GetAccessibleRepositories(setting.UI.User.RepoPagingNum) | |||
if err != nil { | |||
ctx.Handle(500, "GetAccessibleRepositories", err) | |||
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 | |||
} | |||
var repos []*models.Repository | |||
var err error | |||
var repos, mirrors []*models.Repository | |||
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) | |||
return | |||
} | |||
repos = ctxUser.Repos | |||
} else { | |||
var err error | |||
repos, err = models.GetRepositories(ctxUser.ID, true) | |||
mirrors, err = ctxUser.GetUserMirrorRepositories(ctx.User.ID) | |||
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) | |||
return | |||
} | |||
repos = ctxUser.Repos | |||
mirrors, err = ctxUser.GetMirrorRepositories() | |||
if err != nil { | |||
ctx.Handle(500, "GetMirrorRepositories", err) | |||
return | |||
} | |||
} | |||
ctx.Data["Repos"] = repos | |||
ctx.Data["MaxShowRepoNum"] = setting.UI.User.RepoPagingNum | |||
// Get mirror repositories. | |||
mirrors := make([]*models.Repository, 0, 5) | |||
for _, repo := range repos { | |||
if repo.IsMirror { | |||
if err := repo.GetMirror(); err != nil { | |||
ctx.Handle(500, "GetMirror: "+repo.Name, err) | |||
return | |||
} | |||
mirrors = append(mirrors, repo) | |||
} | |||
if err := models.MirrorRepositoryList(mirrors).LoadAttributes(); err != nil { | |||
ctx.Handle(500, "MirrorRepositoryList.LoadAttributes", err) | |||
return | |||
} | |||
ctx.Data["MirrorCount"] = len(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() { | |||
return | |||
} | |||
@@ -198,18 +199,21 @@ func Issues(ctx *context.Context) { | |||
isShowClosed := ctx.Query("state") == "closed" | |||
// Get repositories. | |||
var err error | |||
var repos []*models.Repository | |||
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) | |||
return | |||
} | |||
} else { | |||
if err := ctxUser.GetRepositories(); err != nil { | |||
if err := ctxUser.GetRepositories(1, ctx.User.NumRepos); err != nil { | |||
ctx.Handle(500, "GetRepositories", err) | |||
return | |||
} | |||
repos = ctxUser.Repos | |||
} | |||
repos := ctxUser.Repos | |||
allCount := 0 | |||
repoIDs := make([]int64, 0, len(repos)) | |||
@@ -331,29 +335,34 @@ func showOrgProfile(ctx *context.Context) { | |||
org := ctx.Org.Organization | |||
ctx.Data["Title"] = org.FullName | |||
if ctx.IsSigned { | |||
if ctx.User.IsAdmin { | |||
repos, err := models.GetRepositories(org.ID, true) | |||
if err != nil { | |||
ctx.Handle(500, "GetRepositoriesAsAdmin", err) | |||
return | |||
} | |||
ctx.Data["Repos"] = repos | |||
} else { | |||
if err := org.GetUserRepositories(ctx.User.ID); err != nil { | |||
ctx.Handle(500, "GetUserRepositories", err) | |||
return | |||
} | |||
ctx.Data["Repos"] = org.Repos | |||
page := ctx.QueryInt("page") | |||
if page <= 0 { | |||
page = 1 | |||
} | |||
var ( | |||
repos []*models.Repository | |||
count int64 | |||
err error | |||
) | |||
if ctx.IsSigned && !ctx.User.IsAdmin { | |||
repos, count, err = org.GetUserRepositories(ctx.User.ID, page, setting.UI.User.RepoPagingNum) | |||
if err != nil { | |||
ctx.Handle(500, "GetUserRepositories", err) | |||
return | |||
} | |||
ctx.Data["Repos"] = repos | |||
} 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 { | |||
ctx.Handle(500, "GetRepositories", err) | |||
return | |||
} | |||
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 { | |||
ctx.Handle(500, "GetMembers", err) |
@@ -9,6 +9,8 @@ import ( | |||
"path" | |||
"strings" | |||
"github.com/Unknwon/paginater" | |||
"github.com/gogits/gogs/models" | |||
"github.com/gogits/gogs/modules/base" | |||
"github.com/gogits/gogs/modules/context" | |||
@@ -55,27 +57,27 @@ func Profile(ctx *context.Context) { | |||
isShowKeys = true | |||
} | |||
u := GetUserByName(ctx, strings.TrimSuffix(uname, ".keys")) | |||
ctxUser := GetUserByName(ctx, strings.TrimSuffix(uname, ".keys")) | |||
if ctx.Written() { | |||
return | |||
} | |||
// Show SSH keys. | |||
if isShowKeys { | |||
ShowSSHKeys(ctx, u.ID) | |||
ShowSSHKeys(ctx, ctxUser.ID) | |||
return | |||
} | |||
if u.IsOrganization() { | |||
if ctxUser.IsOrganization() { | |||
showOrgProfile(ctx) | |||
return | |||
} | |||
ctx.Data["Title"] = u.DisplayName() | |||
ctx.Data["Title"] = ctxUser.DisplayName() | |||
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 { | |||
ctx.Handle(500, "GetOrgsByUserIDDesc", err) | |||
return | |||
@@ -87,17 +89,22 @@ func Profile(ctx *context.Context) { | |||
ctx.Data["TabName"] = tab | |||
switch tab { | |||
case "activity": | |||
retrieveFeeds(ctx, u.ID, -1, 0, true) | |||
retrieveFeeds(ctx, ctxUser, -1, 0, true) | |||
if ctx.Written() { | |||
return | |||
} | |||
default: | |||
var err error | |||
ctx.Data["Repos"], err = models.GetRepositories(u.ID, ctx.IsSigned && ctx.User.ID == u.ID) | |||
page := ctx.QueryInt("page") | |||
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 { | |||
ctx.Handle(500, "GetRepositories", err) | |||
return | |||
} | |||
ctx.Data["Page"] = paginater.New(ctxUser.NumRepos, setting.UI.User.RepoPagingNum, page, 5) | |||
} | |||
ctx.HTML(200, PROFILE) |
@@ -1 +1 @@ | |||
0.9.53.0724 | |||
0.9.54.0724 |
@@ -32,6 +32,7 @@ | |||
<div class="ui divider"></div> | |||
{{end}} | |||
{{template "explore/repo_list" .}} | |||
{{template "explore/page" .}} | |||
</div> | |||
<div class="ui five wide column"> |
@@ -37,12 +37,17 @@ | |||
</a> | |||
</li> | |||
{{end}} | |||
{{if gt .ContextUser.NumRepos .MaxShowRepoNum}} | |||
<li> | |||
<a href="{{.ContextUser.HomeLink}}">{{.i18n.Tr "home.show_more_repos"}}</a> | |||
</li> | |||
{{end}} | |||
</ul> | |||
</div> | |||
{{if not .ContextUser.IsOrganization}} | |||
<h4 class="ui top attached header"> | |||
{{.i18n.Tr "home.collaborative_repos"}} <span class="ui grey label">{{.CollaborateCount}}</span> | |||
{{.i18n.Tr "home.collaborative_repos"}} | |||
</h4> | |||
<div class="ui attached table segment"> | |||
<ul id="collaborative-repo-list"> |
@@ -86,6 +86,7 @@ | |||
</div> | |||
{{if ne .TabName "activity"}} | |||
{{template "explore/repo_list" .}} | |||
{{template "explore/page" .}} | |||
{{else}} | |||
<br> | |||
<div class="feeds"> |