code.gitea.io/gitea@v1.19.3/modules/notification/indexer/indexer.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package indexer
     5  
     6  import (
     7  	"context"
     8  
     9  	issues_model "code.gitea.io/gitea/models/issues"
    10  	repo_model "code.gitea.io/gitea/models/repo"
    11  	user_model "code.gitea.io/gitea/models/user"
    12  	"code.gitea.io/gitea/modules/git"
    13  	code_indexer "code.gitea.io/gitea/modules/indexer/code"
    14  	issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
    15  	stats_indexer "code.gitea.io/gitea/modules/indexer/stats"
    16  	"code.gitea.io/gitea/modules/log"
    17  	"code.gitea.io/gitea/modules/notification/base"
    18  	"code.gitea.io/gitea/modules/repository"
    19  	"code.gitea.io/gitea/modules/setting"
    20  )
    21  
    22  type indexerNotifier struct {
    23  	base.NullNotifier
    24  }
    25  
    26  var _ base.Notifier = &indexerNotifier{}
    27  
    28  // NewNotifier create a new indexerNotifier notifier
    29  func NewNotifier() base.Notifier {
    30  	return &indexerNotifier{}
    31  }
    32  
    33  func (r *indexerNotifier) NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
    34  	issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
    35  ) {
    36  	if comment.Type == issues_model.CommentTypeComment {
    37  		if issue.Comments == nil {
    38  			if err := issue.LoadDiscussComments(ctx); err != nil {
    39  				log.Error("LoadDiscussComments failed: %v", err)
    40  				return
    41  			}
    42  		} else {
    43  			issue.Comments = append(issue.Comments, comment)
    44  		}
    45  
    46  		issue_indexer.UpdateIssueIndexer(issue)
    47  	}
    48  }
    49  
    50  func (r *indexerNotifier) NotifyNewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) {
    51  	issue_indexer.UpdateIssueIndexer(issue)
    52  }
    53  
    54  func (r *indexerNotifier) NotifyNewPullRequest(ctx context.Context, pr *issues_model.PullRequest, mentions []*user_model.User) {
    55  	issue_indexer.UpdateIssueIndexer(pr.Issue)
    56  }
    57  
    58  func (r *indexerNotifier) NotifyUpdateComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment, oldContent string) {
    59  	if c.Type == issues_model.CommentTypeComment {
    60  		var found bool
    61  		if c.Issue.Comments != nil {
    62  			for i := 0; i < len(c.Issue.Comments); i++ {
    63  				if c.Issue.Comments[i].ID == c.ID {
    64  					c.Issue.Comments[i] = c
    65  					found = true
    66  					break
    67  				}
    68  			}
    69  		}
    70  
    71  		if !found {
    72  			if err := c.Issue.LoadDiscussComments(ctx); err != nil {
    73  				log.Error("LoadDiscussComments failed: %v", err)
    74  				return
    75  			}
    76  		}
    77  
    78  		issue_indexer.UpdateIssueIndexer(c.Issue)
    79  	}
    80  }
    81  
    82  func (r *indexerNotifier) NotifyDeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) {
    83  	if comment.Type == issues_model.CommentTypeComment {
    84  		if err := comment.LoadIssue(ctx); err != nil {
    85  			log.Error("LoadIssue: %v", err)
    86  			return
    87  		}
    88  
    89  		var found bool
    90  		if comment.Issue.Comments != nil {
    91  			for i := 0; i < len(comment.Issue.Comments); i++ {
    92  				if comment.Issue.Comments[i].ID == comment.ID {
    93  					comment.Issue.Comments = append(comment.Issue.Comments[:i], comment.Issue.Comments[i+1:]...)
    94  					found = true
    95  					break
    96  				}
    97  			}
    98  		}
    99  
   100  		if !found {
   101  			if err := comment.Issue.LoadDiscussComments(ctx); err != nil {
   102  				log.Error("LoadDiscussComments failed: %v", err)
   103  				return
   104  			}
   105  		}
   106  		// reload comments to delete the old comment
   107  		issue_indexer.UpdateIssueIndexer(comment.Issue)
   108  	}
   109  }
   110  
   111  func (r *indexerNotifier) NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) {
   112  	issue_indexer.DeleteRepoIssueIndexer(ctx, repo)
   113  	if setting.Indexer.RepoIndexerEnabled {
   114  		code_indexer.UpdateRepoIndexer(repo)
   115  	}
   116  }
   117  
   118  func (r *indexerNotifier) NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
   119  	issue_indexer.UpdateRepoIndexer(ctx, repo)
   120  	if setting.Indexer.RepoIndexerEnabled && !repo.IsEmpty {
   121  		code_indexer.UpdateRepoIndexer(repo)
   122  	}
   123  	if err := stats_indexer.UpdateRepoIndexer(repo); err != nil {
   124  		log.Error("stats_indexer.UpdateRepoIndexer(%d) failed: %v", repo.ID, err)
   125  	}
   126  }
   127  
   128  func (r *indexerNotifier) NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
   129  	if setting.Indexer.RepoIndexerEnabled && opts.RefFullName == git.BranchPrefix+repo.DefaultBranch {
   130  		code_indexer.UpdateRepoIndexer(repo)
   131  	}
   132  	if err := stats_indexer.UpdateRepoIndexer(repo); err != nil {
   133  		log.Error("stats_indexer.UpdateRepoIndexer(%d) failed: %v", repo.ID, err)
   134  	}
   135  }
   136  
   137  func (r *indexerNotifier) NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
   138  	if setting.Indexer.RepoIndexerEnabled && opts.RefFullName == git.BranchPrefix+repo.DefaultBranch {
   139  		code_indexer.UpdateRepoIndexer(repo)
   140  	}
   141  	if err := stats_indexer.UpdateRepoIndexer(repo); err != nil {
   142  		log.Error("stats_indexer.UpdateRepoIndexer(%d) failed: %v", repo.ID, err)
   143  	}
   144  }
   145  
   146  func (r *indexerNotifier) NotifyIssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) {
   147  	issue_indexer.UpdateIssueIndexer(issue)
   148  }
   149  
   150  func (r *indexerNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldTitle string) {
   151  	issue_indexer.UpdateIssueIndexer(issue)
   152  }
   153  
   154  func (r *indexerNotifier) NotifyIssueChangeRef(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldRef string) {
   155  	issue_indexer.UpdateIssueIndexer(issue)
   156  }