aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorBwko <bouwko@gmail.com>2017-08-23 03:30:54 +0200
committerLunny Xiao <xiaolunwen@gmail.com>2017-08-23 09:30:54 +0800
commit1a5fe4326f1166eac447cb598db34f7ae348d610 (patch)
tree0ca4412138469051eda63a981b32353e2139698e /models
parentfaf4b503b24d33a2a0f455d26bb782345ab8e0c9 (diff)
downloadgitea-1a5fe4326f1166eac447cb598db34f7ae348d610.tar.gz
gitea-1a5fe4326f1166eac447cb598db34f7ae348d610.zip
Add collaborative repositories to the dashboard (#2205)
* Add collaborative repositories to the dashboard Remove some unused code from the Dashboard func * fix some bug and some refactor * fix tests
Diffstat (limited to 'models')
-rw-r--r--models/action.go30
-rw-r--r--models/repo_list.go56
-rw-r--r--models/repo_list_test.go2
3 files changed, 50 insertions, 38 deletions
diff --git a/models/action.go b/models/action.go
index 852bb66d28..595be92f76 100644
--- a/models/action.go
+++ b/models/action.go
@@ -15,6 +15,7 @@ import (
"unicode"
"github.com/Unknwon/com"
+ "github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
"code.gitea.io/git"
@@ -712,10 +713,13 @@ type GetFeedsOptions struct {
IncludePrivate bool // include private actions
OnlyPerformedBy bool // only actions performed by requested user
IncludeDeleted bool // include deleted actions
+ Collaborate bool // Include collaborative repositories
}
// GetFeeds returns actions according to the provided options
func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
+ cond := builder.NewCond()
+
var repoIDs []int64
if opts.RequestedUser.IsOrganization() {
env, err := opts.RequestedUser.AccessibleReposEnv(opts.RequestingUserID)
@@ -725,26 +729,28 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
if repoIDs, err = env.RepoIDs(1, opts.RequestedUser.NumRepos); err != nil {
return nil, fmt.Errorf("GetUserRepositories: %v", err)
}
+
+ cond = cond.And(builder.In("repo_id", repoIDs))
+ }
+
+ if opts.Collaborate {
+ cond = builder.Eq{"user_id": opts.RequestedUser.ID}.Or(
+ builder.Expr(`repo_id IN (SELECT repo_id FROM "access" WHERE access.user_id = ?)`, opts.RequestedUser.ID))
+ } else {
+ cond = builder.Eq{"user_id": opts.RequestedUser.ID}
}
- actions := make([]*Action, 0, 20)
- sess := x.Limit(20).
- Desc("id").
- Where("user_id = ?", opts.RequestedUser.ID)
if opts.OnlyPerformedBy {
- sess.And("act_user_id = ?", opts.RequestedUser.ID)
+ cond = cond.And(builder.Eq{"act_user_id": opts.RequestedUser.ID})
}
if !opts.IncludePrivate {
- sess.And("is_private = ?", false)
- }
- if opts.RequestedUser.IsOrganization() {
- sess.In("repo_id", repoIDs)
+ cond = cond.And(builder.Eq{"is_private": false})
}
if !opts.IncludeDeleted {
- sess.And("is_deleted = ?", false)
-
+ cond = cond.And(builder.Eq{"is_deleted": false})
}
- return actions, sess.Find(&actions)
+ actions := make([]*Action, 0, 20)
+ return actions, x.Limit(20).Desc("id").Where(cond).Find(&actions)
}
diff --git a/models/repo_list.go b/models/repo_list.go
index 2158cfe676..905408ae6b 100644
--- a/models/repo_list.go
+++ b/models/repo_list.go
@@ -9,7 +9,6 @@ import (
"strings"
"github.com/go-xorm/builder"
- "github.com/go-xorm/xorm"
)
// RepositoryList contains a list of repositories
@@ -98,13 +97,14 @@ type SearchRepoOptions struct {
// Owner in we search search
//
// in: query
- OwnerID int64 `json:"uid"`
- Searcher *User `json:"-"` //ID of the person who's seeking
- OrderBy string `json:"-"`
- Private bool `json:"-"` // Include private repositories in results
- Starred bool `json:"-"`
- Page int `json:"-"`
- IsProfile bool `json:"-"`
+ OwnerID int64 `json:"uid"`
+ Searcher *User `json:"-"` //ID of the person who's seeking
+ OrderBy string `json:"-"`
+ Private bool `json:"-"` // Include private repositories in results
+ Collaborate bool `json:"-"` // Include collaborative repositories
+ Starred bool `json:"-"`
+ Page int `json:"-"`
+ IsProfile bool `json:"-"`
// Limit of result
//
// maximum: setting.ExplorePagingNum
@@ -115,25 +115,21 @@ type SearchRepoOptions struct {
// SearchRepositoryByName takes keyword and part of repository name to search,
// it returns results in given range and number of total results.
func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, count int64, err error) {
- var (
- sess *xorm.Session
- cond = builder.NewCond()
- )
-
- opts.Keyword = strings.ToLower(opts.Keyword)
-
+ var cond = builder.NewCond()
if opts.Page <= 0 {
opts.Page = 1
}
- repos = make([]*Repository, 0, opts.PageSize)
-
if opts.Starred && opts.OwnerID > 0 {
cond = builder.Eq{
"star.uid": opts.OwnerID,
}
}
- cond = cond.And(builder.Like{"lower_name", opts.Keyword})
+
+ opts.Keyword = strings.ToLower(opts.Keyword)
+ if opts.Keyword != "" {
+ cond = cond.And(builder.Like{"lower_name", opts.Keyword})
+ }
// Append conditions
if !opts.Starred && opts.OwnerID > 0 {
@@ -157,27 +153,33 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
ownerIds = append(ownerIds, org.ID)
}
- cond = cond.Or(builder.And(builder.Like{"lower_name", opts.Keyword}, builder.In("owner_id", ownerIds)))
+ searcherReposCond := builder.In("owner_id", ownerIds)
+ if opts.Collaborate {
+ searcherReposCond = searcherReposCond.Or(builder.Expr(`id IN (SELECT repo_id FROM "access" WHERE access.user_id = ? AND owner_id != ?)`,
+ opts.Searcher.ID, opts.Searcher.ID))
+ }
+ cond = cond.And(searcherReposCond)
}
if len(opts.OrderBy) == 0 {
opts.OrderBy = "name ASC"
}
+ sess := x.NewSession()
+ defer sess.Close()
+
if opts.Starred && opts.OwnerID > 0 {
- sess = x.
- Join("INNER", "star", "star.repo_id = repository.id").
- Where(cond)
- count, err = x.
+ count, err = sess.
Join("INNER", "star", "star.repo_id = repository.id").
Where(cond).
Count(new(Repository))
if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
}
+
+ sess.Join("INNER", "star", "star.repo_id = repository.id")
} else {
- sess = x.Where(cond)
- count, err = x.
+ count, err = sess.
Where(cond).
Count(new(Repository))
if err != nil {
@@ -185,7 +187,9 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
}
}
+ repos = make([]*Repository, 0, opts.PageSize)
if err = sess.
+ Where(cond).
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
OrderBy(opts.OrderBy).
Find(&repos); err != nil {
@@ -193,7 +197,7 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (repos RepositoryList, coun
}
if !opts.IsProfile {
- if err = repos.loadAttributes(x); err != nil {
+ if err = repos.loadAttributes(sess); err != nil {
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
}
}
diff --git a/models/repo_list_test.go b/models/repo_list_test.go
index 1fbfa935e9..9c8a526a43 100644
--- a/models/repo_list_test.go
+++ b/models/repo_list_test.go
@@ -42,6 +42,7 @@ func TestSearchRepositoryByName(t *testing.T) {
Keyword: "repo_13",
Page: 1,
PageSize: 10,
+ Private: true,
Searcher: &User{ID: 14},
})
@@ -54,6 +55,7 @@ func TestSearchRepositoryByName(t *testing.T) {
Keyword: "test_repo",
Page: 1,
PageSize: 10,
+ Private: true,
Searcher: &User{ID: 14},
})