mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Add notify watcher action
This commit is contained in:
		@@ -19,6 +19,7 @@ const (
 | 
			
		||||
	OP_STAR_REPO
 | 
			
		||||
	OP_FOLLOW_REPO
 | 
			
		||||
	OP_COMMIT_REPO
 | 
			
		||||
	OP_CREATE_ISSUE
 | 
			
		||||
	OP_PULL_REQUEST
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -67,34 +68,10 @@ func CommitRepoAction(userId int64, userName string,
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Add feeds for user self and all watchers.
 | 
			
		||||
	watches, err := GetWatches(repoId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("action.CommitRepoAction(get watches): %d/%s", userId, repoName)
 | 
			
		||||
	if err = NotifyWatchers(userId, repoId, OP_COMMIT_REPO, userName, repoName, refName, string(bs)); err != nil {
 | 
			
		||||
		log.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName)
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	watches = append(watches, Watch{UserId: userId})
 | 
			
		||||
 | 
			
		||||
	for i := range watches {
 | 
			
		||||
		if userId == watches[i].UserId && i > 0 {
 | 
			
		||||
			continue // Do not add twice in case author watches his/her repository.
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		_, err = orm.InsertOne(&Action{
 | 
			
		||||
			UserId:      watches[i].UserId,
 | 
			
		||||
			ActUserId:   userId,
 | 
			
		||||
			ActUserName: userName,
 | 
			
		||||
			OpType:      OP_COMMIT_REPO,
 | 
			
		||||
			Content:     string(bs),
 | 
			
		||||
			RepoId:      repoId,
 | 
			
		||||
			RepoName:    repoName,
 | 
			
		||||
			RefName:     refName,
 | 
			
		||||
		})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Error("action.CommitRepoAction(notify watches): %d/%s", userId, repoName)
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Update repository last update time.
 | 
			
		||||
	repo, err := GetRepositoryByName(userId, repoName)
 | 
			
		||||
 
 | 
			
		||||
@@ -23,6 +23,7 @@ type Issue struct {
 | 
			
		||||
	Name        string
 | 
			
		||||
	RepoId      int64 `xorm:"index"`
 | 
			
		||||
	PosterId    int64
 | 
			
		||||
	Poster      *User `xorm:"-"`
 | 
			
		||||
	MilestoneId int64
 | 
			
		||||
	AssigneeId  int64
 | 
			
		||||
	IsPull      bool // Indicates whether is a pull request or not.
 | 
			
		||||
 
 | 
			
		||||
@@ -262,27 +262,27 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	// hook/post-update
 | 
			
		||||
	pu, err := os.OpenFile(filepath.Join(repoPath, "hooks", "post-update"), os.O_CREATE|os.O_WRONLY, 0777)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer pu.Close()
 | 
			
		||||
	// TODO: Windows .bat
 | 
			
		||||
	if _, err = pu.WriteString(fmt.Sprintf("#!/usr/bin/env bash\n%s update\n", appPath)); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
		// hook/post-update
 | 
			
		||||
		pu, err := os.OpenFile(filepath.Join(repoPath, "hooks", "post-update"), os.O_CREATE|os.O_WRONLY, 0777)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		defer pu.Close()
 | 
			
		||||
		// TODO: Windows .bat
 | 
			
		||||
		if _, err = pu.WriteString(fmt.Sprintf("#!/usr/bin/env bash\n%s update\n", appPath)); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	// hook/post-update
 | 
			
		||||
	pu2, err := os.OpenFile(filepath.Join(repoPath, "hooks", "post-receive"), os.O_CREATE|os.O_WRONLY, 0777)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer pu2.Close()
 | 
			
		||||
	// TODO: Windows .bat
 | 
			
		||||
	if _, err = pu2.WriteString("#!/usr/bin/env bash\ngit update-server-info\n"); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
		// hook/post-update
 | 
			
		||||
		pu2, err := os.OpenFile(filepath.Join(repoPath, "hooks", "post-receive"), os.O_CREATE|os.O_WRONLY, 0777)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		defer pu2.Close()
 | 
			
		||||
		// TODO: Windows .bat
 | 
			
		||||
		if _, err = pu2.WriteString("#!/usr/bin/env bash\ngit update-server-info\n"); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	*/
 | 
			
		||||
 | 
			
		||||
	// Initialize repository according to user's choice.
 | 
			
		||||
@@ -506,6 +506,37 @@ func GetWatches(repoId int64) ([]Watch, error) {
 | 
			
		||||
	return watches, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NotifyWatchers creates batch of actions for every watcher.
 | 
			
		||||
func NotifyWatchers(userId, repoId int64, opType int, userName, repoName, refName, content string) error {
 | 
			
		||||
	// Add feeds for user self and all watchers.
 | 
			
		||||
	watches, err := GetWatches(repoId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return errors.New("repo.NotifyWatchers(get watches): " + err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	watches = append(watches, Watch{UserId: userId})
 | 
			
		||||
 | 
			
		||||
	for i := range watches {
 | 
			
		||||
		if userId == watches[i].UserId && i > 0 {
 | 
			
		||||
			continue // Do not add twice in case author watches his/her repository.
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		_, err = orm.InsertOne(&Action{
 | 
			
		||||
			UserId:      watches[i].UserId,
 | 
			
		||||
			ActUserId:   userId,
 | 
			
		||||
			ActUserName: userName,
 | 
			
		||||
			OpType:      opType,
 | 
			
		||||
			Content:     content,
 | 
			
		||||
			RepoId:      repoId,
 | 
			
		||||
			RepoName:    repoName,
 | 
			
		||||
			RefName:     refName,
 | 
			
		||||
		})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return errors.New("repo.NotifyWatchers(create action): " + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IsWatching checks if user has watched given repository.
 | 
			
		||||
func IsWatching(userId, repoId int64) bool {
 | 
			
		||||
	has, _ := orm.Get(&Watch{0, repoId, userId})
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user