package models | package models | ||||
import ( | import ( | ||||
"bytes" | |||||
"errors" | "errors" | ||||
"strings" | "strings" | ||||
"time" | "time" | ||||
"github.com/gogits/gogs/modules/base" | |||||
) | ) | ||||
var ( | var ( | ||||
Poster *User `xorm:"-"` | Poster *User `xorm:"-"` | ||||
MilestoneId int64 | MilestoneId int64 | ||||
AssigneeId int64 | AssigneeId int64 | ||||
IsRead bool `xorm:"-"` | |||||
IsPull bool // Indicates whether is a pull request or not. | IsPull bool // Indicates whether is a pull request or not. | ||||
IsClosed bool | IsClosed bool | ||||
Labels string `xorm:"TEXT"` | Labels string `xorm:"TEXT"` | ||||
return err | 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. | // CreateIssue creates new issue for repository. | ||||
func NewIssue(issue *Issue) (err error) { | func NewIssue(issue *Issue) (err error) { | ||||
sess := orm.NewSession() | sess := orm.NewSession() | ||||
return issue, nil | 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. | // 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) { | func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labels, sortType string) ([]Issue, error) { | ||||
sess := orm.Limit(20, (page-1)*20) | sess := orm.Limit(20, (page-1)*20) | ||||
return issues, err | 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. | // 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 { | 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 | 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. | // IssueStats represents issue statistic information. | ||||
FM_MENTION | 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 { | func GetIssueStats(rid, uid int64, isShowClosed bool, filterMode int) *IssueStats { | ||||
stats := &IssueStats{} | stats := &IssueStats{} | ||||
issue := new(Issue) | issue := new(Issue) | ||||
*tmpSess = *sess | *tmpSess = *sess | ||||
stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue) | stats.ClosedCount, _ = tmpSess.And("is_closed=?", true).Count(issue) | ||||
} else { | } 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 | *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: | nofilter: | ||||
stats.AssignCount, _ = orm.Where("repo_id=?", rid).And("is_closed=?", isShowClosed).And("assignee_id=?", uid).Count(issue) | 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.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 | 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 { | func GetUserIssueStats(uid int64, filterMode int) *IssueStats { | ||||
stats := &IssueStats{} | stats := &IssueStats{} | ||||
issue := new(Issue) | 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.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) | stats.CreateCount, _ = orm.Where("poster_id=?", uid).And("is_closed=?", false).Count(issue) | ||||
return stats | return stats | ||||
// UpdateIssue updates information of issue. | // UpdateIssue updates information of issue. | ||||
func UpdateIssue(issue *Issue) error { | func UpdateIssue(issue *Issue) error { | ||||
_, err := orm.AllCols().Update(issue) | |||||
_, err := orm.Id(issue.Id).AllCols().Update(issue) | |||||
return err | 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 { | 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. | // Milestone represents a milestone of repository. | ||||
type Milestone struct { | 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. | // Issue types. | ||||
func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string) error { | func CreateComment(userId, repoId, issueId, commitId, line int64, cmtType int, content string) error { | ||||
sess := orm.NewSession() | sess := orm.NewSession() | ||||
defer sess.Close() | defer sess.Close() | ||||
sess.Begin() | |||||
if err := sess.Begin(); err != nil { | |||||
return err | |||||
} | |||||
if _, err := sess.Insert(&Comment{PosterId: userId, Type: cmtType, IssueId: issueId, | if _, err := sess.Insert(&Comment{PosterId: userId, Type: cmtType, IssueId: issueId, | ||||
CommitId: commitId, Line: line, Content: content}); err != nil { | CommitId: commitId, Line: line, Content: content}); err != nil { |
func init() { | func init() { | ||||
tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch), | tables = append(tables, new(User), new(PublicKey), new(Repository), new(Watch), | ||||
new(Action), new(Access), new(Issue), new(Comment), new(Oauth2), new(Follow), | 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() { | func LoadModelsConfig() { |
return initRepoCommit(tmpDir, user.NewGitSig()) | 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 { | if err := orm.Limit(num, offset).Asc("id").Find(&repos); err != nil { | ||||
return nil, err | 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 { | if err != nil { | ||||
return nil, err | return nil, err | ||||
} else if !has { | } else if !has { | ||||
return nil, ErrUserNotExist | return nil, ErrUserNotExist | ||||
} | } | ||||
urepos[i].UserName = u.Name | |||||
} | } | ||||
return urepos, nil | |||||
return repos, nil | |||||
} | } | ||||
// RepoPath returns repository path by given user and repository name. | // RepoPath returns repository path by given user and repository name. | ||||
// Watch is connection request for receiving repository notifycation. | // Watch is connection request for receiving repository notifycation. | ||||
type Watch struct { | type Watch struct { | ||||
Id int64 | 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. | // 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 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 | return err | ||||
} | } | ||||
rawSql := "UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?" | rawSql := "UPDATE `repository` SET num_watches = num_watches + 1 WHERE id = ?" | ||||
_, err = orm.Exec(rawSql, repoId) | |||||
_, err = orm.Exec(rawSql, rid) | |||||
} else { | } else { | ||||
if _, err = orm.Delete(&Watch{0, repoId, userId}); err != nil { | |||||
if _, err = orm.Delete(&Watch{0, rid, uid}); err != nil { | |||||
return err | return err | ||||
} | } | ||||
rawSql := "UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?" | rawSql := "UPDATE `repository` SET num_watches = num_watches - 1 WHERE id = ?" | ||||
_, err = orm.Exec(rawSql, repoId) | |||||
_, err = orm.Exec(rawSql, rid) | |||||
} | } | ||||
return err | 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 | return watches, err | ||||
} | } | ||||
// NotifyWatchers creates batch of actions for every watcher. | // NotifyWatchers creates batch of actions for every watcher. | ||||
func NotifyWatchers(act *Action) error { | func NotifyWatchers(act *Action) error { | ||||
// Add feeds for user self and all watchers. | // Add feeds for user self and all watchers. | ||||
watches, err := GetWatches(act.RepoId) | |||||
watches, err := GetWatchers(act.RepoId) | |||||
if err != nil { | if err != nil { | ||||
return errors.New("repo.NotifyWatchers(get watches): " + err.Error()) | return errors.New("repo.NotifyWatchers(get watches): " + err.Error()) | ||||
} | } | ||||
} | } | ||||
for i := range watches { | for i := range watches { | ||||
if act.ActUserId == watches[i].UserId { | |||||
if act.ActUserId == watches[i].Uid { | |||||
continue | continue | ||||
} | } | ||||
act.Id = 0 | act.Id = 0 | ||||
act.UserId = watches[i].UserId | |||||
act.UserId = watches[i].Uid | |||||
if _, err = orm.InsertOne(act); err != nil { | if _, err = orm.InsertOne(act); err != nil { | ||||
return errors.New("repo.NotifyWatchers(create action): " + err.Error()) | return errors.New("repo.NotifyWatchers(create action): " + err.Error()) | ||||
} | } | ||||
} | } | ||||
// IsWatching checks if user has watched given repository. | // 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 | return has | ||||
} | } | ||||
func ForkRepository(reposName string, userId int64) { | |||||
func ForkRepository(repoName string, uid int64) { | |||||
} | } |
} | } | ||||
// Delete all watches. | // 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 | return err | ||||
} | } | ||||
return mails | 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. | // GetUserByEmail returns the user object by given e-mail if exists. | ||||
func GetUserByEmail(email string) (*User, error) { | func GetUserByEmail(email string) (*User, error) { | ||||
if len(email) == 0 { | if len(email) == 0 { |
// SendIssueNotifyMail sends mail notification of all watchers of repository. | // SendIssueNotifyMail sends mail notification of all watchers of repository. | ||||
func SendIssueNotifyMail(user, owner *models.User, repo *models.Repository, issue *models.Issue) ([]string, error) { | func SendIssueNotifyMail(user, owner *models.User, repo *models.Repository, issue *models.Issue) ([]string, error) { | ||||
watches, err := models.GetWatches(repo.Id) | |||||
ws, err := models.GetWatchers(repo.Id) | |||||
if err != nil { | if err != nil { | ||||
return nil, errors.New("mail.NotifyWatchers(get watches): " + err.Error()) | |||||
return nil, errors.New("mail.NotifyWatchers(GetWatchers): " + err.Error()) | |||||
} | } | ||||
tos := make([]string, 0, len(watches)) | |||||
for i := range watches { | |||||
uid := watches[i].UserId | |||||
tos := make([]string, 0, len(ws)) | |||||
for i := range ws { | |||||
uid := ws[i].Uid | |||||
if user.Id == uid { | if user.Id == uid { | ||||
continue | continue | ||||
} | } | ||||
u, err := models.GetUserById(uid) | u, err := models.GetUserById(uid) | ||||
if err != nil { | if err != nil { | ||||
return nil, errors.New("mail.NotifyWatchers(get user): " + err.Error()) | |||||
return nil, errors.New("mail.NotifyWatchers(GetUserById): " + err.Error()) | |||||
} | } | ||||
tos = append(tos, u.Email) | tos = append(tos, u.Email) | ||||
} | } |
ctx.Data["PageIsRepos"] = true | ctx.Data["PageIsRepos"] = true | ||||
var err error | var err error | ||||
ctx.Data["Repos"], err = models.GetRepos(200, 0) | |||||
ctx.Data["Repos"], err = models.GetRepositoriesWithUsers(200, 0) | |||||
if err != nil { | if err != nil { | ||||
ctx.Handle(500, "admin.Repositories", err) | ctx.Handle(500, "admin.Repositories", err) | ||||
return | return |
return | return | ||||
} | } | ||||
var pairs []*models.IssseUser | |||||
if filterMode == models.FM_MENTION { | |||||
// Get issue-user pairs. | |||||
pairs, err = models.GetIssueUserPairs(ctx.Repo.Repository.Id, ctx.User.Id, isShowClosed) | |||||
if err != nil { | |||||
ctx.Handle(500, "issue.Issues(GetIssueUserPairs): %v", err) | |||||
return | |||||
} | |||||
// Get issue-user pairs. | |||||
pairs, err := models.GetIssueUserPairs(ctx.Repo.Repository.Id, ctx.User.Id, isShowClosed) | |||||
if err != nil { | |||||
ctx.Handle(500, "issue.Issues(GetIssueUserPairs): %v", err) | |||||
return | |||||
} | } | ||||
// Get posters. | // Get posters. | ||||
for i := range issues { | for i := range issues { | ||||
if filterMode == models.FM_MENTION && !models.PairsContains(pairs, issues[i].Id) { | |||||
idx := models.PairsContains(pairs, issues[i].Id) | |||||
if filterMode == models.FM_MENTION && (idx == -1 || !pairs[idx].IsMentioned) { | |||||
continue | continue | ||||
} | } | ||||
if idx > -1 { | |||||
issues[i].IsRead = pairs[idx].IsRead | |||||
} else { | |||||
issues[i].IsRead = true | |||||
} | |||||
if err = issues[i].GetPoster(); err != nil { | if err = issues[i].GetPoster(); err != nil { | ||||
ctx.Handle(500, "issue.Issues(GetPoster): %v", err) | ctx.Handle(500, "issue.Issues(GetPoster): %v", err) | ||||
return | return | ||||
if err := models.NewIssue(issue); err != nil { | if err := models.NewIssue(issue); err != nil { | ||||
ctx.Handle(500, "issue.CreateIssue(NewIssue)", err) | ctx.Handle(500, "issue.CreateIssue(NewIssue)", err) | ||||
return | return | ||||
} else if err := models.NewIssueUserPairs(issue.RepoId, issue.Id, | |||||
ctx.Repo.Owner.Id, ctx.User.Id, form.AssigneeId); err != nil { | |||||
ctx.Handle(500, "issue.CreateIssue(NewIssueUserPairs)", err) | |||||
return | |||||
} | |||||
// Update mentions. | |||||
ms := base.MentionPattern.FindAllString(issue.Content, -1) | |||||
if len(ms) > 0 { | |||||
for i := range ms { | |||||
ms[i] = ms[i][1:] | |||||
} | |||||
ids := models.GetUserIdsByNames(ms) | |||||
if err := models.UpdateIssueUserPairsByMentions(ids, issue.Id); err != nil { | |||||
ctx.Handle(500, "issue.CreateIssue(UpdateIssueUserPairsByMentions)", err) | |||||
return | |||||
} | |||||
} | } | ||||
// Notify watchers. | // Notify watchers. | ||||
} | } | ||||
tos = append(tos, ctx.User.LowerName) | tos = append(tos, ctx.User.LowerName) | ||||
ms := base.MentionPattern.FindAllString(issue.Content, -1) | |||||
newTos := make([]string, 0, len(ms)) | newTos := make([]string, 0, len(ms)) | ||||
for _, m := range ms { | for _, m := range ms { | ||||
if com.IsSliceContainsStr(tos, m[1:]) { | |||||
if com.IsSliceContainsStr(tos, m) { | |||||
continue | continue | ||||
} | } | ||||
newTos = append(newTos, m[1:]) | |||||
newTos = append(newTos, m) | |||||
} | } | ||||
if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, | if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, | ||||
ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { | ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { | ||||
return | return | ||||
} | } | ||||
// Update issue-user. | |||||
if err = models.UpdateIssueUserPairByRead(ctx.User.Id, issue.Id); err != nil { | |||||
ctx.Handle(500, "issue.ViewIssue(UpdateIssueUserPairByRead): %v", err) | |||||
return | |||||
} | |||||
// Get poster. | // Get poster. | ||||
u, err := models.GetUserById(issue.PosterId) | u, err := models.GetUserById(issue.PosterId) | ||||
if err != nil { | if err != nil { | ||||
(strings.Contains(newStatus, "Close") && !issue.IsClosed) { | (strings.Contains(newStatus, "Close") && !issue.IsClosed) { | ||||
issue.IsClosed = !issue.IsClosed | issue.IsClosed = !issue.IsClosed | ||||
if err = models.UpdateIssue(issue); err != nil { | if err = models.UpdateIssue(issue); err != nil { | ||||
ctx.Handle(200, "issue.Comment(update issue status)", err) | |||||
ctx.Handle(500, "issue.Comment(UpdateIssue)", err) | |||||
return | |||||
} else if err = models.UpdateIssueUserPairsByStatus(issue.Id, issue.IsClosed); err != nil { | |||||
ctx.Handle(500, "issue.Comment(UpdateIssueUserPairsByStatus)", err) | |||||
return | return | ||||
} | } | ||||
} | } | ||||
} | } | ||||
var ms []string | |||||
content := ctx.Query("content") | content := ctx.Query("content") | ||||
if len(content) > 0 { | if len(content) > 0 { | ||||
switch params["action"] { | switch params["action"] { | ||||
ctx.Handle(500, "issue.Comment(create comment)", err) | ctx.Handle(500, "issue.Comment(create comment)", err) | ||||
return | return | ||||
} | } | ||||
// Update mentions. | |||||
ms = base.MentionPattern.FindAllString(issue.Content, -1) | |||||
if len(ms) > 0 { | |||||
for i := range ms { | |||||
ms[i] = ms[i][1:] | |||||
} | |||||
ids := models.GetUserIdsByNames(ms) | |||||
if err := models.UpdateIssueUserPairsByMentions(ids, issue.Id); err != nil { | |||||
ctx.Handle(500, "issue.CreateIssue(UpdateIssueUserPairsByMentions)", err) | |||||
return | |||||
} | |||||
} | |||||
log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id) | log.Trace("%s Comment created: %d", ctx.Req.RequestURI, issue.Id) | ||||
default: | default: | ||||
ctx.Handle(404, "issue.Comment", err) | ctx.Handle(404, "issue.Comment", err) | ||||
} | } | ||||
tos = append(tos, ctx.User.LowerName) | tos = append(tos, ctx.User.LowerName) | ||||
ms := base.MentionPattern.FindAllString(issue.Content, -1) | |||||
newTos := make([]string, 0, len(ms)) | newTos := make([]string, 0, len(ms)) | ||||
for _, m := range ms { | for _, m := range ms { | ||||
if com.IsSliceContainsStr(tos, m[1:]) { | |||||
if com.IsSliceContainsStr(tos, m) { | |||||
continue | continue | ||||
} | } | ||||
newTos = append(newTos, m[1:]) | |||||
newTos = append(newTos, m) | |||||
} | } | ||||
if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, | if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, | ||||
ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { | ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { |
"github.com/gogits/gogs/models" | "github.com/gogits/gogs/models" | ||||
"github.com/gogits/gogs/modules/auth" | "github.com/gogits/gogs/modules/auth" | ||||
"github.com/gogits/gogs/modules/base" | "github.com/gogits/gogs/modules/base" | ||||
"github.com/gogits/gogs/modules/log" | |||||
"github.com/gogits/gogs/modules/middleware" | "github.com/gogits/gogs/modules/middleware" | ||||
) | ) | ||||
} | } | ||||
_, _ = assigneeId, posterId | _, _ = assigneeId, posterId | ||||
// page, _ := base.StrTo(ctx.Query("page")).Int() | |||||
// repoId, _ := base.StrTo(ctx.Query("repoid")).Int64() | |||||
// ctx.Data["RepoId"] = repoId | |||||
// var posterId int64 = 0 | |||||
// if ctx.Query("type") == "created_by" { | |||||
// posterId = ctx.User.Id | |||||
// ctx.Data["ViewType"] = "created_by" | |||||
// } | |||||
rid, _ := base.StrTo(ctx.Query("repoid")).Int64() | rid, _ := base.StrTo(ctx.Query("repoid")).Int64() | ||||
issueStats := models.GetUserIssueStats(ctx.User.Id, filterMode) | issueStats := models.GetUserIssueStats(ctx.User.Id, filterMode) | ||||
// Get all repositories. | // Get all repositories. | ||||
repos, err := models.GetRepositories(ctx.User, true) | repos, err := models.GetRepositories(ctx.User, true) | ||||
if err != nil { | if err != nil { | ||||
ctx.Handle(500, "user.Issues(get repositories)", err) | |||||
ctx.Handle(500, "user.Issues(GetRepositories)", err) | |||||
return | return | ||||
} | } | ||||
rids := make([]int64, 0, len(repos)) | |||||
showRepos := make([]*models.Repository, 0, len(repos)) | showRepos := make([]*models.Repository, 0, len(repos)) | ||||
// Get all issues. | |||||
// allIssues := make([]models.Issue, 0, 5*len(repos)) | |||||
for _, repo := range repos { | for _, repo := range repos { | ||||
if repo.NumIssues == 0 { | if repo.NumIssues == 0 { | ||||
continue | continue | ||||
} | } | ||||
rids = append(rids, repo.Id) | |||||
repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues | repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues | ||||
issueStats.AllCount += int64(repo.NumOpenIssues) | issueStats.AllCount += int64(repo.NumOpenIssues) | ||||
// switch filterMode{ | |||||
// case models.FM_ASSIGN: | |||||
// } | |||||
if isShowClosed { | if isShowClosed { | ||||
if repo.NumClosedIssues > 0 { | if repo.NumClosedIssues > 0 { | ||||
if filterMode == models.FM_CREATE { | |||||
repo.NumClosedIssues = int(models.GetIssueCountByPoster(ctx.User.Id, repo.Id, isShowClosed)) | |||||
} | |||||
showRepos = append(showRepos, repo) | showRepos = append(showRepos, repo) | ||||
} | } | ||||
} else { | } else { | ||||
if repo.NumOpenIssues > 0 { | if repo.NumOpenIssues > 0 { | ||||
if filterMode == models.FM_CREATE { | |||||
repo.NumOpenIssues = int(models.GetIssueCountByPoster(ctx.User.Id, repo.Id, isShowClosed)) | |||||
} | |||||
showRepos = append(showRepos, repo) | showRepos = append(showRepos, repo) | ||||
} | } | ||||
} | } | ||||
} | |||||
// issues, err := models.GetIssues(0, repo.Id, posterId, 0, page, isShowClosed, "", "") | |||||
// if err != nil { | |||||
// ctx.Handle(200, "user.Issues(get issues)", err) | |||||
// return | |||||
// } | |||||
} | |||||
// allIssueCount += repo.NumIssues | |||||
// closedIssueCount += repo.NumClosedIssues | |||||
// // Set repository information to issues. | |||||
// for j := range issues { | |||||
// issues[j].Repo = &repos[i] | |||||
// } | |||||
// allIssues = append(allIssues, issues...) | |||||
// repos[i].NumOpenIssues = repo.NumIssues - repo.NumClosedIssues | |||||
// if repos[i].NumOpenIssues > 0 { | |||||
// showRepos = append(showRepos, repos[i]) | |||||
// } | |||||
// } | |||||
// showIssues := make([]models.Issue, 0, len(allIssues)) | |||||
// ctx.Data["IsShowClosed"] = isShowClosed | |||||
// // Get posters and filter issues. | |||||
// for i := range allIssues { | |||||
// u, err := models.GetUserById(allIssues[i].PosterId) | |||||
// if err != nil { | |||||
// ctx.Handle(200, "user.Issues(get poster): %v", err) | |||||
// return | |||||
// } | |||||
// allIssues[i].Poster = u | |||||
// if u.Id == ctx.User.Id { | |||||
// createdByCount++ | |||||
// } | |||||
// if repoId > 0 && repoId != allIssues[i].Repo.Id { | |||||
// continue | |||||
// } | |||||
// if isShowClosed == allIssues[i].IsClosed { | |||||
// showIssues = append(showIssues, allIssues[i]) | |||||
// } | |||||
// } | |||||
if rid > 0 { | |||||
rids = []int64{rid} | |||||
} | |||||
page, _ := base.StrTo(ctx.Query("page")).Int() | |||||
// Get all issues. | |||||
var ius []*models.IssueUser | |||||
switch viewType { | |||||
case "assigned": | |||||
fallthrough | |||||
case "created_by": | |||||
ius, err = models.GetIssueUserPairsByMode(ctx.User.Id, rid, isShowClosed, page, filterMode) | |||||
default: | |||||
ius, err = models.GetIssueUserPairsByRepoIds(rids, isShowClosed, page) | |||||
} | |||||
if err != nil { | |||||
ctx.Handle(500, "user.Issues(GetAllIssueUserPairs)", err) | |||||
return | |||||
} | |||||
issues := make([]*models.Issue, len(ius)) | |||||
for i := range ius { | |||||
issues[i], err = models.GetIssueById(ius[i].IssueId) | |||||
if err != nil { | |||||
if err == models.ErrIssueNotExist { | |||||
log.Error("user.Issues(#%d): issue not exist", ius[i].IssueId) | |||||
continue | |||||
} else { | |||||
ctx.Handle(500, "user.Issues(GetIssue)", err) | |||||
return | |||||
} | |||||
} | |||||
issues[i].Repo, err = models.GetRepositoryById(issues[i].RepoId) | |||||
if err != nil { | |||||
ctx.Handle(500, "user.Issues(GetRepositoryById)", err) | |||||
return | |||||
} | |||||
issues[i].Poster, err = models.GetUserById(issues[i].PosterId) | |||||
if err != nil { | |||||
ctx.Handle(500, "user.Issues(GetUserById)", err) | |||||
return | |||||
} | |||||
} | |||||
ctx.Data["RepoId"] = rid | ctx.Data["RepoId"] = rid | ||||
ctx.Data["Repos"] = showRepos | ctx.Data["Repos"] = showRepos | ||||
ctx.Data["Issues"] = issues | |||||
ctx.Data["ViewType"] = viewType | ctx.Data["ViewType"] = viewType | ||||
ctx.Data["IssueStats"] = issueStats | ctx.Data["IssueStats"] = issueStats | ||||
ctx.Data["IsShowClosed"] = isShowClosed | ctx.Data["IsShowClosed"] = isShowClosed |
{{range .Repos}} | {{range .Repos}} | ||||
<tr> | <tr> | ||||
<td>{{.Id}}</td> | <td>{{.Id}}</td> | ||||
<th>{{.UserName}}</th> | |||||
<td><a href="/{{.UserName}}/{{.Name}}">{{.Name}}</a></td> | |||||
<th>{{.Owner.Name}}</th> | |||||
<td><a href="/{{.Owner.Name}}/{{.Name}}">{{.Name}}</a></td> | |||||
<td><i class="fa fa{{if .IsPrivate}}-check{{end}}-square-o"></i></td> | <td><i class="fa fa{{if .IsPrivate}}-check{{end}}-square-o"></i></td> | ||||
<td>{{.NumWatches}}</td> | <td>{{.NumWatches}}</td> | ||||
<td>{{.NumIssues}}</td> | <td>{{.NumIssues}}</td> |
</div> | </div> | ||||
<div class="issues list-group"> | <div class="issues list-group"> | ||||
{{range .Issues}}{{if .Poster}} | {{range .Issues}}{{if .Poster}} | ||||
<div class="list-group-item issue-item" id="issue-{{.Id}}"> | |||||
<div class="list-group-item issue-item{{if not .IsRead}} unread{{end}}" id="issue-{{.Id}}"> | |||||
<span class="number pull-right">#{{.Index}}</span> | <span class="number pull-right">#{{.Index}}</span> | ||||
<h5 class="title"><a href="{{$.RepoLink}}/issues/{{.Index}}">{{.Name}}</a></h5> | <h5 class="title"><a href="{{$.RepoLink}}/issues/{{.Index}}">{{.Name}}</a></h5> | ||||
<p class="info"> | <p class="info"> |
<li><a href="/issues?type=created_by&repoid={{.RepoId}}&state={{.State}}"{{if eq .ViewType "created_by"}} class="active"{{end}}>Created by you <strong class="pull-right">{{.IssueStats.CreateCount}}</strong></a></li> | <li><a href="/issues?type=created_by&repoid={{.RepoId}}&state={{.State}}"{{if eq .ViewType "created_by"}} class="active"{{end}}>Created by you <strong class="pull-right">{{.IssueStats.CreateCount}}</strong></a></li> | ||||
<li><hr/></li> | <li><hr/></li> | ||||
{{range .Repos}} | {{range .Repos}} | ||||
<li><a href="/issues?type={{$.ViewType}}{{if eq $.RepoId .Id}}{{else}}&repoid={{.Id}}{{end}}" class="sm{{if eq $.RepoId .Id}} active{{end}}">{{$.SignedUser.Name}}/{{.Name}} <strong class="pull-right">{{.NumOpenIssues}}</strong></a></li> | |||||
<li><a href="/issues?type={{$.ViewType}}{{if eq $.RepoId .Id}}{{else}}&repoid={{.Id}}{{end}}&state={{$.State}}" class="sm{{if eq $.RepoId .Id}} active{{end}}">{{$.SignedUser.Name}}/{{.Name}} <strong class="pull-right">{{if $.IsShowClosed}}{{.NumClosedIssues}}{{else}}{{.NumOpenIssues}}{{end}}</strong></a></li> | |||||
{{end}} | {{end}} | ||||
</ul> | </ul> | ||||
</div> | </div> | ||||
<div class="col-md-9"> | <div class="col-md-9"> | ||||
<div class="filter-option"> | <div class="filter-option"> | ||||
<div class="btn-group"> | <div class="btn-group"> | ||||
<a class="btn btn-default issue-open{{if not .IsShowClosed}} active{{end}}" href="/issues?type={{.ViewType}}">{{..IssueStats.OpenCount}} Open</a> | |||||
<a class="btn btn-default issue-close{{if .IsShowClosed}} active{{end}}" href="/issues?type={{.ViewType}}&state=closed">{{.IssueStats.ClosedCount}} Closed</a> | |||||
<a class="btn btn-default issue-open{{if not .IsShowClosed}} active{{end}}" href="/issues?type={{.ViewType}}&repoid={{.RepoId}}">Open</a> | |||||
<a class="btn btn-default issue-close{{if .IsShowClosed}} active{{end}}" href="/issues?type={{.ViewType}}&repoid={{.RepoId}}&state=closed">Closed</a> | |||||
</div> | </div> | ||||
</div> | </div> | ||||
<div class="issues list-group"> | <div class="issues list-group"> | ||||
{{range .Issues}} | |||||
{{range .Issues}}{{if .}} | |||||
<div class="list-group-item issue-item" id="issue-{{.Id}}"> | <div class="list-group-item issue-item" id="issue-{{.Id}}"> | ||||
<span class="number pull-right">#{{.Index}}</span> | <span class="number pull-right">#{{.Index}}</span> | ||||
<h5 class="title"><a href="/{{$.SignedUser.Name}}/{{.Repo.Name}}/issues/{{.Index}}">{{.Name}}</a></h5> | <h5 class="title"><a href="/{{$.SignedUser.Name}}/{{.Repo.Name}}/issues/{{.Index}}">{{.Name}}</a></h5> | ||||
<span class="comment"><i class="fa fa-comments"></i> {{.NumComments}}</span> | <span class="comment"><i class="fa fa-comments"></i> {{.NumComments}}</span> | ||||
</p> | </p> | ||||
</div> | </div> | ||||
{{end}} | |||||
{{end}}{{end}} | |||||
</div> | </div> | ||||
</div> | </div> | ||||
</div> | </div> |