diff options
author | Unknown <joe2010xtmf@163.com> | 2014-05-07 16:51:14 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-05-07 16:51:14 -0400 |
commit | 33d32585b1b7232c764e8cb8629a33076902c882 (patch) | |
tree | 4450575c9a0a71c8a7d51e9b4eafee321226d8c2 /models | |
parent | 6fb7229beaadb53f59cb85d80976f99708a7434d (diff) | |
download | gitea-33d32585b1b7232c764e8cb8629a33076902c882.tar.gz gitea-33d32585b1b7232c764e8cb8629a33076902c882.zip |
Add mention, read/unread support of issue tracker
Diffstat (limited to 'models')
-rw-r--r-- | models/issue.go | 252 | ||||
-rw-r--r-- | models/models.go | 2 | ||||
-rw-r--r-- | models/repo.go | 58 | ||||
-rw-r--r-- | models/user.go | 15 |
4 files changed, 221 insertions, 106 deletions
diff --git a/models/issue.go b/models/issue.go index eb88190cc8..b93989435a 100644 --- a/models/issue.go +++ b/models/issue.go @@ -5,9 +5,12 @@ package models import ( + "bytes" "errors" "strings" "time" + + "github.com/gogits/gogs/modules/base" ) var ( @@ -25,6 +28,7 @@ type Issue struct { Poster *User `xorm:"-"` MilestoneId int64 AssigneeId int64 + IsRead bool `xorm:"-"` IsPull bool // Indicates whether is a pull request or not. IsClosed bool Labels string `xorm:"TEXT"` @@ -42,18 +46,6 @@ func (i *Issue) GetPoster() (err error) { return err } -// IssseUser represents an issue-user relation. -type IssseUser struct { - Id int64 - Iid int64 // Issue ID. - Rid int64 // Repository ID. - Uid int64 // User ID. - IsRead bool - IsAssigned bool - IsMentioned bool - IsClosed bool -} - // CreateIssue creates new issue for repository. func NewIssue(issue *Issue) (err error) { sess := orm.NewSession() @@ -87,6 +79,18 @@ func GetIssueByIndex(rid, index int64) (*Issue, error) { return issue, nil } +// GetIssueById returns an issue by ID. +func GetIssueById(id int64) (*Issue, error) { + issue := &Issue{Id: id} + has, err := orm.Get(issue) + if err != nil { + return nil, err + } else if !has { + return nil, ErrIssueNotExist + } + return issue, nil +} + // GetIssues returns a list of issues by given conditions. func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labels, sortType string) ([]Issue, error) { sess := orm.Limit(20, (page-1)*20) @@ -133,27 +137,115 @@ func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labels, sortTy return issues, err } +// GetIssueCountByPoster returns number of issues of repository by poster. +func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 { + count, _ := orm.Where("repo_id=?", rid).And("poster_id=?", uid).And("is_closed=?", isClosed).Count(new(Issue)) + return count +} + +// IssueUser represents an issue-user relation. +type IssueUser struct { + Id int64 + Uid int64 // User ID. + IssueId int64 + RepoId int64 + IsRead bool + IsAssigned bool + IsMentioned bool + IsPoster bool + IsClosed bool +} + +// NewIssueUserPairs adds new issue-user pairs for new issue of repository. +func NewIssueUserPairs(rid, iid, oid, uid, aid int64) (err error) { + iu := &IssueUser{IssueId: iid, RepoId: rid} + + ws, err := GetWatchers(rid) + if err != nil { + return err + } + + // TODO: check collaborators. + // Add owner. + ids := []int64{oid} + for _, id := range ids { + if IsWatching(id, rid) { + continue + } + + // In case owner is not watching. + ws = append(ws, &Watch{Uid: id}) + } + + for _, w := range ws { + if w.Uid == 0 { + continue + } + + iu.Uid = w.Uid + iu.IsPoster = iu.Uid == uid + iu.IsAssigned = iu.Uid == aid + if _, err = orm.Insert(iu); err != nil { + return err + } + } + return nil +} + // PairsContains returns true when pairs list contains given issue. -func PairsContains(ius []*IssseUser, issueId int64) bool { +func PairsContains(ius []*IssueUser, issueId int64) int { for i := range ius { - if ius[i].Iid == issueId { - return true + if ius[i].IssueId == issueId { + return i } } - return false + return -1 } -// GetIssueUserPairs returns all issue-user pairs by given repository and user. -func GetIssueUserPairs(rid, uid int64, isClosed bool) ([]*IssseUser, error) { - ius := make([]*IssseUser, 0, 10) - err := orm.Find(&ius, &IssseUser{Rid: rid, Uid: uid, IsClosed: isClosed}) +// GetIssueUserPairs returns issue-user pairs by given repository and user. +func GetIssueUserPairs(rid, uid int64, isClosed bool) ([]*IssueUser, error) { + ius := make([]*IssueUser, 0, 10) + err := orm.Where("is_closed=?", isClosed).Find(&ius, &IssueUser{RepoId: rid, Uid: uid}) return ius, err } -// GetUserIssueCount returns the number of issues that were created by given user in repository. -func GetUserIssueCount(uid, rid int64) int64 { - count, _ := orm.Where("poster_id=?", uid).And("repo_id=?", rid).Count(new(Issue)) - return count +// GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs. +func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) { + buf := bytes.NewBufferString("") + for _, rid := range rids { + buf.WriteString("repo_id=") + buf.WriteString(base.ToStr(rid)) + buf.WriteString(" OR ") + } + cond := strings.TrimSuffix(buf.String(), " OR ") + + ius := make([]*IssueUser, 0, 10) + sess := orm.Limit(20, (page-1)*20).Where("is_closed=?", isClosed) + if len(cond) > 0 { + sess.And(cond) + } + err := sess.Find(&ius) + return ius, err +} + +// GetIssueUserPairsByMode returns issue-user pairs by given repository and user. +func GetIssueUserPairsByMode(uid, rid int64, isClosed bool, page, filterMode int) ([]*IssueUser, error) { + ius := make([]*IssueUser, 0, 10) + sess := orm.Limit(20, (page-1)*20).Where("uid=?", uid).And("is_closed=?", isClosed) + if rid > 0 { + sess.And("repo_id=?", rid) + } + + switch filterMode { + case FM_ASSIGN: + sess.And("is_assigned=?", true) + case FM_CREATE: + sess.And("is_poster=?", true) + default: + return ius, nil + } + err := sess.Find(&ius) + return ius, err } // IssueStats represents issue statistic information. @@ -172,7 +264,7 @@ const ( FM_MENTION ) -// GetIssueStats returns issue statistic information by given condition. +// GetIssueStats returns issue statistic information by given conditions. func GetIssueStats(rid, uid int64, isShowClosed bool, filterMode int) *IssueStats { stats := &IssueStats{} issue := new(Issue) @@ -203,48 +295,23 @@ func GetIssueStats(rid, uid int64, isShowClosed bool, filterMode int) *IssueStat *tmpSess = *sess stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue) } else { - sess := orm.Where("rid=?", rid).And("uid=?", uid).And("is_mentioned=?", true) - tmpSess := sess - stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(new(IssseUser)) + sess := orm.Where("repo_id=?", rid).And("uid=?", uid).And("is_mentioned=?", true) *tmpSess = *sess - stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(new(IssseUser)) + stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(new(IssueUser)) + *tmpSess = *sess + stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(new(IssueUser)) } nofilter: stats.AssignCount, _ = orm.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("assignee_id=?", uid).Count(issue) stats.CreateCount, _ = orm.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("poster_id=?", uid).Count(issue) - stats.MentionCount, _ = orm.Where("rid=?", rid).And("uid=?", uid).And("is_closed=?", isShowClosed).And("is_mentioned=?", true).Count(new(IssseUser)) + stats.MentionCount, _ = orm.Where("repo_id=?", rid).And("uid=?", uid).And("is_closed=?", isShowClosed).And("is_mentioned=?", true).Count(new(IssueUser)) return stats } -// GetUserIssueStats returns issue statistic information for dashboard by given condition. +// GetUserIssueStats returns issue statistic information for dashboard by given conditions. func GetUserIssueStats(uid int64, filterMode int) *IssueStats { stats := &IssueStats{} issue := new(Issue) - iu := new(IssseUser) - - sess := orm.Where("uid=?", uid) - tmpSess := sess - if filterMode == 0 { - stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(iu) - *tmpSess = *sess - stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(iu) - } - - switch filterMode { - case FM_ASSIGN: - sess.And("is_assigned=?", true) - *tmpSess = *sess - stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(iu) - *tmpSess = *sess - stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(iu) - case FM_CREATE: - sess.Where("poster_id=?", uid) - *tmpSess = *sess - stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(issue) - *tmpSess = *sess - stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue) - } - stats.AssignCount, _ = orm.Where("assignee_id=?", uid).And("is_closed=?", false).Count(issue) stats.CreateCount, _ = orm.Where("poster_id=?", uid).And("is_closed=?", false).Count(issue) return stats @@ -252,28 +319,69 @@ func GetUserIssueStats(uid int64, filterMode int) *IssueStats { // UpdateIssue updates information of issue. func UpdateIssue(issue *Issue) error { - _, err := orm.AllCols().Update(issue) + _, err := orm.Id(issue.Id).AllCols().Update(issue) return err } -// Label represents a list of labels of repository for issues. +// UpdateIssueUserByStatus updates issue-user pairs by issue status. +func UpdateIssueUserPairsByStatus(iid int64, isClosed bool) error { + rawSql := "UPDATE `issue_user` SET is_closed = ? WHERE issue_id = ?" + _, err := orm.Exec(rawSql, isClosed, iid) + return err +} + +// UpdateIssueUserPairByRead updates issue-user pair for reading. +func UpdateIssueUserPairByRead(uid, iid int64) error { + rawSql := "UPDATE `issue_user` SET is_read = ? WHERE uid = ? AND issue_id = ?" + _, err := orm.Exec(rawSql, true, uid, iid) + return err +} + +// UpdateIssueUserPairsByMentions updates issue-user pairs by mentioning. +func UpdateIssueUserPairsByMentions(uids []int64, iid int64) error { + for _, uid := range uids { + iu := &IssueUser{Uid: uid, IssueId: iid} + has, err := orm.Get(iu) + if err != nil { + return err + } + + iu.IsMentioned = true + if has { + _, err = orm.Id(iu.Id).AllCols().Update(iu) + } else { + _, err = orm.Insert(iu) + } + if err != nil { + return err + } + } + return nil +} + +// Label represents a label of repository for issues. type Label struct { - Id int64 - RepoId int64 `xorm:"INDEX"` - Names string - Colors string + Id int64 + Rid int64 `xorm:"INDEX"` + Name string + Color string + NumIssues int + NumClosedIssues int + NumOpenIssues int `xorm:"-"` } // Milestone represents a milestone of repository. type Milestone struct { - Id int64 - Name string - RepoId int64 `xorm:"INDEX"` - IsClosed bool - Content string - NumIssues int - DueDate time.Time - Created time.Time `xorm:"CREATED"` + Id int64 + Rid int64 `xorm:"INDEX"` + Name string + Content string + IsClosed bool + NumIssues int + NumClosedIssues int + Completeness int // Percentage(1-100). + Deadline time.Time + ClosedDate time.Time } // Issue types. @@ -300,7 +408,9 @@ type Comment struct { func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string) error { sess := orm.NewSession() defer sess.Close() - sess.Begin() + if err := sess.Begin(); err != nil { + return err + } if _, err := sess.Insert(&Comment{PosterId: userId, Type: cmtType, IssueId: issueId, CommitId: commitId, Line: line, Content: content}); err != nil { diff --git a/models/models.go b/models/models.go index d7f3629927..02141b7a8f 100644 --- a/models/models.go +++ b/models/models.go @@ -34,7 +34,7 @@ var ( func init() { tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch), new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow), - new(Mirror), new(Release), new(LoginSource), new(Webhook), new(IssseUser)) + new(Mirror), new(Release), new(LoginSource), new(Webhook), new(IssueUser)) } func LoadModelsConfig() { diff --git a/models/repo.go b/models/repo.go index 079e100a13..640b6e723d 100644 --- a/models/repo.go +++ b/models/repo.go @@ -466,33 +466,25 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep return initRepoCommit(tmpDir, user.NewGitSig()) } -// UserRepo reporesents a repository with user name. -type UserRepo struct { - *Repository - UserName string -} - -// GetRepos returns given number of repository objects with offset. -func GetRepos(num, offset int) ([]UserRepo, error) { - repos := make([]Repository, 0, num) +// GetRepositoriesWithUsers returns given number of repository objects with offset. +// It also auto-gets corresponding users. +func GetRepositoriesWithUsers(num, offset int) ([]*Repository, error) { + repos := make([]*Repository, 0, num) if err := orm.Limit(num, offset).Asc("id").Find(&repos); err != nil { return nil, err } - urepos := make([]UserRepo, len(repos)) - for i := range repos { - urepos[i].Repository = &repos[i] - u := new(User) - has, err := orm.Id(urepos[i].Repository.OwnerId).Get(u) + for _, repo := range repos { + repo.Owner = &User{Id: repo.OwnerId} + has, err := orm.Get(repo.Owner) if err != nil { return nil, err } else if !has { return nil, ErrUserNotExist } - urepos[i].UserName = u.Name } - return urepos, nil + return repos, nil } // RepoPath returns repository path by given user and repository name. @@ -733,40 +725,40 @@ func GetCollaborators(repoName string) ([]string, error) { // Watch is connection request for receiving repository notifycation. type Watch struct { Id int64 - RepoId int64 `xorm:"UNIQUE(watch)"` - UserId int64 `xorm:"UNIQUE(watch)"` + Uid int64 `xorm:"UNIQUE(s)"` + RepoId int64 `xorm:"UNIQUE(s)"` } // Watch or unwatch repository. -func WatchRepo(userId, repoId int64, watch bool) (err error) { +func WatchRepo(uid, rid int64, watch bool) (err error) { if watch { - if _, err = orm.Insert(&Watch{RepoId: repoId, UserId: userId}); err != nil { + if _, err = orm.Insert(&Watch{RepoId: rid, Uid: uid}); err != nil { return err } rawSql := "UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?" - _, err = orm.Exec(rawSql, repoId) + _, err = orm.Exec(rawSql, rid) } else { - if _, err = orm.Delete(&Watch{0, repoId, userId}); err != nil { + if _, err = orm.Delete(&Watch{0, rid, uid}); err != nil { return err } rawSql := "UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?" - _, err = orm.Exec(rawSql, repoId) + _, err = orm.Exec(rawSql, rid) } return err } -// GetWatches returns all watches of given repository. -func GetWatches(repoId int64) ([]Watch, error) { - watches := make([]Watch, 0, 10) - err := orm.Find(&watches, &Watch{RepoId: repoId}) +// GetWatchers returns all watchers of given repository. +func GetWatchers(rid int64) ([]*Watch, error) { + watches := make([]*Watch, 0, 10) + err := orm.Find(&watches, &Watch{RepoId: rid}) return watches, err } // NotifyWatchers creates batch of actions for every watcher. func NotifyWatchers(act *Action) error { // Add feeds for user self and all watchers. - watches, err := GetWatches(act.RepoId) + watches, err := GetWatchers(act.RepoId) if err != nil { return errors.New("repo.NotifyWatchers(get watches): " + err.Error()) } @@ -778,12 +770,12 @@ func NotifyWatchers(act *Action) error { } for i := range watches { - if act.ActUserId == watches[i].UserId { + if act.ActUserId == watches[i].Uid { continue } act.Id = 0 - act.UserId = watches[i].UserId + act.UserId = watches[i].Uid if _, err = orm.InsertOne(act); err != nil { return errors.New("repo.NotifyWatchers(create action): " + err.Error()) } @@ -792,11 +784,11 @@ func NotifyWatchers(act *Action) error { } // IsWatching checks if user has watched given repository. -func IsWatching(userId, repoId int64) bool { - has, _ := orm.Get(&Watch{0, repoId, userId}) +func IsWatching(uid, rid int64) bool { + has, _ := orm.Get(&Watch{0, rid, uid}) return has } -func ForkRepository(reposName string, userId int64) { +func ForkRepository(repoName string, uid int64) { } diff --git a/models/user.go b/models/user.go index 720065c3f1..9ffdf8e6ae 100644 --- a/models/user.go +++ b/models/user.go @@ -304,7 +304,7 @@ func DeleteUser(user *User) error { } // Delete all watches. - if _, err = orm.Delete(&Watch{UserId: user.Id}); err != nil { + if _, err = orm.Delete(&Watch{Uid: user.Id}); err != nil { return err } @@ -392,6 +392,19 @@ func GetUserEmailsByNames(names []string) []string { return mails } +// GetUserIdsByNames returns a slice of ids corresponds to names. +func GetUserIdsByNames(names []string) []int64 { + ids := make([]int64, 0, len(names)) + for _, name := range names { + u, err := GetUserByName(name) + if err != nil { + continue + } + ids = append(ids, u.Id) + } + return ids +} + // GetUserByEmail returns the user object by given e-mail if exists. func GetUserByEmail(email string) (*User, error) { if len(email) == 0 { |