aboutsummaryrefslogtreecommitdiffstats
path: root/models/user
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2022-11-19 09:12:33 +0100
committerGitHub <noreply@github.com>2022-11-19 16:12:33 +0800
commit044c754ea53f5b81f451451df53aea366f6f700a (patch)
tree45688c28a84f87f71ec3f99eb0e8456eb7d19c42 /models/user
parentfefdb7ffd11bbfbff66dae8e88681ec840dedfde (diff)
downloadgitea-044c754ea53f5b81f451451df53aea366f6f700a.tar.gz
gitea-044c754ea53f5b81f451451df53aea366f6f700a.zip
Add `context.Context` to more methods (#21546)
This PR adds a context parameter to a bunch of methods. Some helper `xxxCtx()` methods got replaced with the normal name now. Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'models/user')
-rw-r--r--models/user/user.go12
-rw-r--r--models/user/user_test.go8
2 files changed, 11 insertions, 9 deletions
diff --git a/models/user/user.go b/models/user/user.go
index c36fc21c77..1a71acb0b7 100644
--- a/models/user/user.go
+++ b/models/user/user.go
@@ -1042,14 +1042,15 @@ func GetUserEmailsByNames(ctx context.Context, names []string) []string {
}
// GetMaileableUsersByIDs gets users from ids, but only if they can receive mails
-func GetMaileableUsersByIDs(ids []int64, isMention bool) ([]*User, error) {
+func GetMaileableUsersByIDs(ctx context.Context, ids []int64, isMention bool) ([]*User, error) {
if len(ids) == 0 {
return nil, nil
}
ous := make([]*User, 0, len(ids))
if isMention {
- return ous, db.GetEngine(db.DefaultContext).In("id", ids).
+ return ous, db.GetEngine(ctx).
+ In("id", ids).
Where("`type` = ?", UserTypeIndividual).
And("`prohibit_login` = ?", false).
And("`is_active` = ?", true).
@@ -1057,7 +1058,8 @@ func GetMaileableUsersByIDs(ids []int64, isMention bool) ([]*User, error) {
Find(&ous)
}
- return ous, db.GetEngine(db.DefaultContext).In("id", ids).
+ return ous, db.GetEngine(ctx).
+ In("id", ids).
Where("`type` = ?", UserTypeIndividual).
And("`prohibit_login` = ?", false).
And("`is_active` = ?", true).
@@ -1090,10 +1092,10 @@ func GetUserNameByID(ctx context.Context, id int64) (string, error) {
}
// GetUserIDsByNames returns a slice of ids corresponds to names.
-func GetUserIDsByNames(names []string, ignoreNonExistent bool) ([]int64, error) {
+func GetUserIDsByNames(ctx context.Context, names []string, ignoreNonExistent bool) ([]int64, error) {
ids := make([]int64, 0, len(names))
for _, name := range names {
- u, err := GetUserByName(db.DefaultContext, name)
+ u, err := GetUserByName(ctx, name)
if err != nil {
if ignoreNonExistent {
continue
diff --git a/models/user/user_test.go b/models/user/user_test.go
index 5f2ac0a60c..1cdfb5978c 100644
--- a/models/user/user_test.go
+++ b/models/user/user_test.go
@@ -257,12 +257,12 @@ func TestGetUserIDsByNames(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
// ignore non existing
- IDs, err := user_model.GetUserIDsByNames([]string{"user1", "user2", "none_existing_user"}, true)
+ IDs, err := user_model.GetUserIDsByNames(db.DefaultContext, []string{"user1", "user2", "none_existing_user"}, true)
assert.NoError(t, err)
assert.Equal(t, []int64{1, 2}, IDs)
// ignore non existing
- IDs, err = user_model.GetUserIDsByNames([]string{"user1", "do_not_exist"}, false)
+ IDs, err = user_model.GetUserIDsByNames(db.DefaultContext, []string{"user1", "do_not_exist"}, false)
assert.Error(t, err)
assert.Equal(t, []int64(nil), IDs)
}
@@ -270,14 +270,14 @@ func TestGetUserIDsByNames(t *testing.T) {
func TestGetMaileableUsersByIDs(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
- results, err := user_model.GetMaileableUsersByIDs([]int64{1, 4}, false)
+ results, err := user_model.GetMaileableUsersByIDs(db.DefaultContext, []int64{1, 4}, false)
assert.NoError(t, err)
assert.Len(t, results, 1)
if len(results) > 1 {
assert.Equal(t, results[0].ID, 1)
}
- results, err = user_model.GetMaileableUsersByIDs([]int64{1, 4}, true)
+ results, err = user_model.GetMaileableUsersByIDs(db.DefaultContext, []int64{1, 4}, true)
assert.NoError(t, err)
assert.Len(t, results, 2)
if len(results) > 2 {