code.gitea.io/gitea@v1.22.3/models/repo/watch.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"context"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	user_model "code.gitea.io/gitea/models/user"
    11  	"code.gitea.io/gitea/modules/setting"
    12  	"code.gitea.io/gitea/modules/timeutil"
    13  )
    14  
    15  // WatchMode specifies what kind of watch the user has on a repository
    16  type WatchMode int8
    17  
    18  const (
    19  	// WatchModeNone don't watch
    20  	WatchModeNone WatchMode = iota // 0
    21  	// WatchModeNormal watch repository (from other sources)
    22  	WatchModeNormal // 1
    23  	// WatchModeDont explicit don't auto-watch
    24  	WatchModeDont // 2
    25  	// WatchModeAuto watch repository (from AutoWatchOnChanges)
    26  	WatchModeAuto // 3
    27  )
    28  
    29  // Watch is connection request for receiving repository notification.
    30  type Watch struct {
    31  	ID          int64              `xorm:"pk autoincr"`
    32  	UserID      int64              `xorm:"UNIQUE(watch)"`
    33  	RepoID      int64              `xorm:"UNIQUE(watch)"`
    34  	Mode        WatchMode          `xorm:"SMALLINT NOT NULL DEFAULT 1"`
    35  	CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
    36  	UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
    37  }
    38  
    39  func init() {
    40  	db.RegisterModel(new(Watch))
    41  }
    42  
    43  // GetWatch gets what kind of subscription a user has on a given repository; returns dummy record if none found
    44  func GetWatch(ctx context.Context, userID, repoID int64) (Watch, error) {
    45  	watch := Watch{UserID: userID, RepoID: repoID}
    46  	has, err := db.GetEngine(ctx).Get(&watch)
    47  	if err != nil {
    48  		return watch, err
    49  	}
    50  	if !has {
    51  		watch.Mode = WatchModeNone
    52  	}
    53  	return watch, nil
    54  }
    55  
    56  // IsWatchMode Decodes watchability of WatchMode
    57  func IsWatchMode(mode WatchMode) bool {
    58  	return mode != WatchModeNone && mode != WatchModeDont
    59  }
    60  
    61  // IsWatching checks if user has watched given repository.
    62  func IsWatching(ctx context.Context, userID, repoID int64) bool {
    63  	watch, err := GetWatch(ctx, userID, repoID)
    64  	return err == nil && IsWatchMode(watch.Mode)
    65  }
    66  
    67  func watchRepoMode(ctx context.Context, watch Watch, mode WatchMode) (err error) {
    68  	if watch.Mode == mode {
    69  		return nil
    70  	}
    71  	if mode == WatchModeAuto && (watch.Mode == WatchModeDont || IsWatchMode(watch.Mode)) {
    72  		// Don't auto watch if already watching or deliberately not watching
    73  		return nil
    74  	}
    75  
    76  	hadrec := watch.Mode != WatchModeNone
    77  	needsrec := mode != WatchModeNone
    78  	repodiff := 0
    79  
    80  	if IsWatchMode(mode) && !IsWatchMode(watch.Mode) {
    81  		repodiff = 1
    82  	} else if !IsWatchMode(mode) && IsWatchMode(watch.Mode) {
    83  		repodiff = -1
    84  	}
    85  
    86  	watch.Mode = mode
    87  
    88  	if !hadrec && needsrec {
    89  		watch.Mode = mode
    90  		if err = db.Insert(ctx, watch); err != nil {
    91  			return err
    92  		}
    93  	} else if needsrec {
    94  		watch.Mode = mode
    95  		if _, err := db.GetEngine(ctx).ID(watch.ID).AllCols().Update(watch); err != nil {
    96  			return err
    97  		}
    98  	} else if _, err = db.DeleteByID[Watch](ctx, watch.ID); err != nil {
    99  		return err
   100  	}
   101  	if repodiff != 0 {
   102  		_, err = db.GetEngine(ctx).Exec("UPDATE `repository` SET num_watches = num_watches + ? WHERE id = ?", repodiff, watch.RepoID)
   103  	}
   104  	return err
   105  }
   106  
   107  // WatchRepo watch or unwatch repository.
   108  func WatchRepo(ctx context.Context, doer *user_model.User, repo *Repository, doWatch bool) error {
   109  	watch, err := GetWatch(ctx, doer.ID, repo.ID)
   110  	if err != nil {
   111  		return err
   112  	}
   113  	if !doWatch && watch.Mode == WatchModeAuto {
   114  		return watchRepoMode(ctx, watch, WatchModeDont)
   115  	} else if !doWatch {
   116  		return watchRepoMode(ctx, watch, WatchModeNone)
   117  	}
   118  
   119  	if user_model.IsUserBlockedBy(ctx, doer, repo.OwnerID) {
   120  		return user_model.ErrBlockedUser
   121  	}
   122  
   123  	return watchRepoMode(ctx, watch, WatchModeNormal)
   124  }
   125  
   126  // GetWatchers returns all watchers of given repository.
   127  func GetWatchers(ctx context.Context, repoID int64) ([]*Watch, error) {
   128  	watches := make([]*Watch, 0, 10)
   129  	return watches, db.GetEngine(ctx).Where("`watch`.repo_id=?", repoID).
   130  		And("`watch`.mode<>?", WatchModeDont).
   131  		And("`user`.is_active=?", true).
   132  		And("`user`.prohibit_login=?", false).
   133  		Join("INNER", "`user`", "`user`.id = `watch`.user_id").
   134  		Find(&watches)
   135  }
   136  
   137  // GetRepoWatchersIDs returns IDs of watchers for a given repo ID
   138  // but avoids joining with `user` for performance reasons
   139  // User permissions must be verified elsewhere if required
   140  func GetRepoWatchersIDs(ctx context.Context, repoID int64) ([]int64, error) {
   141  	ids := make([]int64, 0, 64)
   142  	return ids, db.GetEngine(ctx).Table("watch").
   143  		Where("watch.repo_id=?", repoID).
   144  		And("watch.mode<>?", WatchModeDont).
   145  		Select("user_id").
   146  		Find(&ids)
   147  }
   148  
   149  // GetRepoWatchers returns range of users watching given repository.
   150  func GetRepoWatchers(ctx context.Context, repoID int64, opts db.ListOptions) ([]*user_model.User, error) {
   151  	sess := db.GetEngine(ctx).Where("watch.repo_id=?", repoID).
   152  		Join("LEFT", "watch", "`user`.id=`watch`.user_id").
   153  		And("`watch`.mode<>?", WatchModeDont)
   154  	if opts.Page > 0 {
   155  		sess = db.SetSessionPagination(sess, &opts)
   156  		users := make([]*user_model.User, 0, opts.PageSize)
   157  
   158  		return users, sess.Find(&users)
   159  	}
   160  
   161  	users := make([]*user_model.User, 0, 8)
   162  	return users, sess.Find(&users)
   163  }
   164  
   165  // WatchIfAuto subscribes to repo if AutoWatchOnChanges is set
   166  func WatchIfAuto(ctx context.Context, userID, repoID int64, isWrite bool) error {
   167  	if !isWrite || !setting.Service.AutoWatchOnChanges {
   168  		return nil
   169  	}
   170  	watch, err := GetWatch(ctx, userID, repoID)
   171  	if err != nil {
   172  		return err
   173  	}
   174  	if watch.Mode != WatchModeNone {
   175  		return nil
   176  	}
   177  	return watchRepoMode(ctx, watch, WatchModeAuto)
   178  }