Add mention, read/unread support of issue tracker

This commit is contained in:
Unknown 2014-05-07 16:51:14 -04:00
parent 6fb7229bea
commit 33d32585b1
11 changed files with 354 additions and 200 deletions

View File

@ -5,9 +5,12 @@
package models package models
import ( import (
"bytes"
"errors" "errors"
"strings" "strings"
"time" "time"
"github.com/gogits/gogs/modules/base"
) )
var ( var (
@ -25,6 +28,7 @@ type Issue struct {
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"`
@ -42,18 +46,6 @@ func (i *Issue) GetPoster() (err error) {
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()
@ -87,6 +79,18 @@ func GetIssueByIndex(rid, index int64) (*Issue, error) {
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)
@ -133,27 +137,115 @@ func GetIssues(uid, rid, pid, mid int64, page int, isClosed bool, labels, sortTy
return issues, err return issues, err
} }
// PairsContains returns true when pairs list contains given issue. // GetIssueCountByPoster returns number of issues of repository by poster.
func PairsContains(ius []*IssseUser, issueId int64) bool { func GetIssueCountByPoster(uid, rid int64, isClosed bool) int64 {
for i := range ius { count, _ := orm.Where("repo_id=?", rid).And("poster_id=?", uid).And("is_closed=?", isClosed).Count(new(Issue))
if ius[i].Iid == issueId { return count
return true
}
}
return false
} }
// GetIssueUserPairs returns all issue-user pairs by given repository and user. // IssueUser represents an issue-user relation.
func GetIssueUserPairs(rid, uid int64, isClosed bool) ([]*IssseUser, error) { type IssueUser struct {
ius := make([]*IssseUser, 0, 10) Id int64
err := orm.Find(&ius, &IssseUser{Rid: rid, Uid: uid, IsClosed: isClosed}) 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 []*IssueUser, issueId int64) int {
for i := range ius {
if ius[i].IssueId == issueId {
return i
}
}
return -1
}
// 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. // GetIssueUserPairsByRepoIds returns issue-user pairs by given repository IDs.
func GetUserIssueCount(uid, rid int64) int64 { func GetIssueUserPairsByRepoIds(rids []int64, isClosed bool, page int) ([]*IssueUser, error) {
count, _ := orm.Where("poster_id=?", uid).And("repo_id=?", rid).Count(new(Issue)) buf := bytes.NewBufferString("")
return count 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.
@ -172,7 +264,7 @@ const (
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)
@ -203,48 +295,23 @@ func GetIssueStats(rid, uid int64, isShowClosed bool, filterMode int) *IssueStat
*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) sess := orm.Where("repo_id=?", rid).And("uid=?", uid).And("is_mentioned=?", true)
tmpSess := sess
stats.OpenCount, _ = tmpSess.And("is_closed=?", false).Count(new(IssseUser))
*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
@ -252,28 +319,69 @@ func GetUserIssueStats(uid int64, filterMode int) *IssueStats {
// 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 Id int64
RepoId int64 `xorm:"INDEX"` Rid int64 `xorm:"INDEX"`
Names string Name string
Colors 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 Id int64
Rid int64 `xorm:"INDEX"`
Name string Name string
RepoId int64 `xorm:"INDEX"`
IsClosed bool
Content string Content string
IsClosed bool
NumIssues int NumIssues int
DueDate time.Time NumClosedIssues int
Created time.Time `xorm:"CREATED"` Completeness int // Percentage(1-100).
Deadline time.Time
ClosedDate time.Time
} }
// Issue types. // Issue types.
@ -300,7 +408,9 @@ type Comment struct {
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 {

View File

@ -34,7 +34,7 @@ var (
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() {

View File

@ -466,33 +466,25 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep
return initRepoCommit(tmpDir, user.NewGitSig()) return initRepoCommit(tmpDir, user.NewGitSig())
} }
// UserRepo reporesents a repository with user name. // GetRepositoriesWithUsers returns given number of repository objects with offset.
type UserRepo struct { // It also auto-gets corresponding users.
*Repository func GetRepositoriesWithUsers(num, offset int) ([]*Repository, error) {
UserName string repos := make([]*Repository, 0, num)
}
// GetRepos returns given number of repository objects with offset.
func GetRepos(num, offset int) ([]UserRepo, 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 _, repo := range repos {
for i := range repos { repo.Owner = &User{Id: repo.OwnerId}
urepos[i].Repository = &repos[i] has, err := orm.Get(repo.Owner)
u := new(User)
has, err := orm.Id(urepos[i].Repository.OwnerId).Get(u)
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.
@ -733,40 +725,40 @@ func GetCollaborators(repoName string) ([]string, error) {
// 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)"` Uid int64 `xorm:"UNIQUE(s)"`
UserId int64 `xorm:"UNIQUE(watch)"` 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. // GetWatchers returns all watchers of given repository.
func GetWatches(repoId int64) ([]Watch, error) { func GetWatchers(rid int64) ([]*Watch, error) {
watches := make([]Watch, 0, 10) watches := make([]*Watch, 0, 10)
err := orm.Find(&watches, &Watch{RepoId: repoId}) 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())
} }
@ -778,12 +770,12 @@ func NotifyWatchers(act *Action) 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())
} }
@ -792,11 +784,11 @@ func NotifyWatchers(act *Action) error {
} }
// IsWatching checks if user has watched given repository. // IsWatching checks if user has watched given repository.
func IsWatching(userId, repoId int64) bool { func IsWatching(uid, rid int64) bool {
has, _ := orm.Get(&Watch{0, repoId, userId}) has, _ := orm.Get(&Watch{0, rid, uid})
return has return has
} }
func ForkRepository(reposName string, userId int64) { func ForkRepository(repoName string, uid int64) {
} }

View File

@ -304,7 +304,7 @@ func DeleteUser(user *User) error {
} }
// 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
} }
@ -392,6 +392,19 @@ func GetUserEmailsByNames(names []string) []string {
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 {

View File

@ -114,20 +114,20 @@ func SendResetPasswdMail(r *middleware.Render, user *models.User) {
// 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)) tos := make([]string, 0, len(ws))
for i := range watches { for i := range ws {
uid := watches[i].UserId 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)
} }

View File

@ -152,7 +152,7 @@ func Repositories(ctx *middleware.Context) {
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

View File

@ -65,22 +65,27 @@ func Issues(ctx *middleware.Context) {
return return
} }
var pairs []*models.IssseUser
if filterMode == models.FM_MENTION {
// Get issue-user pairs. // Get issue-user pairs.
pairs, err = models.GetIssueUserPairs(ctx.Repo.Repository.Id, ctx.User.Id, isShowClosed) pairs, err := models.GetIssueUserPairs(ctx.Repo.Repository.Id, ctx.User.Id, isShowClosed)
if err != nil { if err != nil {
ctx.Handle(500, "issue.Issues(GetIssueUserPairs): %v", err) ctx.Handle(500, "issue.Issues(GetIssueUserPairs): %v", err)
return 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
@ -135,6 +140,24 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C
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.
@ -154,14 +177,13 @@ func CreateIssuePost(ctx *middleware.Context, params martini.Params, form auth.C
} }
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 {
@ -191,6 +213,12 @@ func ViewIssue(ctx *middleware.Context, params martini.Params) {
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 {
@ -294,7 +322,10 @@ func Comment(ctx *middleware.Context, params martini.Params) {
(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
} }
@ -311,6 +342,7 @@ func Comment(ctx *middleware.Context, params martini.Params) {
} }
} }
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"] {
@ -319,6 +351,21 @@ func Comment(ctx *middleware.Context, params martini.Params) {
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)
@ -344,14 +391,13 @@ func Comment(ctx *middleware.Context, params martini.Params) {
} }
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 {

View File

@ -13,6 +13,7 @@ import (
"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"
) )
@ -127,102 +128,94 @@ func Issues(ctx *middleware.Context) {
} }
_, _ = 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 if rid > 0 {
// closedIssueCount += repo.NumClosedIssues rids = []int64{rid}
}
// // Set repository information to issues. page, _ := base.StrTo(ctx.Query("page")).Int()
// for j := range issues {
// issues[j].Repo = &repos[i]
// }
// allIssues = append(allIssues, issues...)
// repos[i].NumOpenIssues = repo.NumIssues - repo.NumClosedIssues // Get all issues.
// if repos[i].NumOpenIssues > 0 { var ius []*models.IssueUser
// showRepos = append(showRepos, repos[i]) 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
}
// showIssues := make([]models.Issue, 0, len(allIssues)) issues := make([]*models.Issue, len(ius))
// ctx.Data["IsShowClosed"] = isShowClosed 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
}
}
// // Get posters and filter issues. issues[i].Repo, err = models.GetRepositoryById(issues[i].RepoId)
// for i := range allIssues { if err != nil {
// u, err := models.GetUserById(allIssues[i].PosterId) ctx.Handle(500, "user.Issues(GetRepositoryById)", err)
// if err != nil { return
// 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 { issues[i].Poster, err = models.GetUserById(issues[i].PosterId)
// continue if err != nil {
// } ctx.Handle(500, "user.Issues(GetUserById)", err)
return
// if isShowClosed == allIssues[i].IsClosed { }
// showIssues = append(showIssues, allIssues[i]) }
// }
// }
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

View File

@ -26,8 +26,8 @@
{{range .Repos}} {{range .Repos}}
<tr> <tr>
<td>{{.Id}}</td> <td>{{.Id}}</td>
<th>{{.UserName}}</th> <th>{{.Owner.Name}}</th>
<td><a href="/{{.UserName}}/{{.Name}}">{{.Name}}</a></td> <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>

View File

@ -21,7 +21,7 @@
</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">

View File

@ -21,19 +21,19 @@
<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-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}}&state=closed">{{.IssueStats.ClosedCount}} Closed</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>
@ -44,7 +44,7 @@
<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>