aboutsummaryrefslogtreecommitdiffstats
path: root/modules/activitypub
diff options
context:
space:
mode:
authorJakobDev <jakobdev@gmx.de>2023-09-15 08:13:19 +0200
committerGitHub <noreply@github.com>2023-09-15 06:13:19 +0000
commitc548dde205244a39a26ba98377c0f5cc11da7041 (patch)
treef9d9e1185609703e320ed07fd2ff3f95dbdcc230 /modules/activitypub
parentf8a109440655b77e8554e1744e31bf52a7c63df7 (diff)
downloadgitea-c548dde205244a39a26ba98377c0f5cc11da7041.tar.gz
gitea-c548dde205244a39a26ba98377c0f5cc11da7041.zip
More refactoring of `db.DefaultContext` (#27083)
Next step of #27065
Diffstat (limited to 'modules/activitypub')
-rw-r--r--modules/activitypub/client.go5
-rw-r--r--modules/activitypub/client_test.go3
-rw-r--r--modules/activitypub/user_settings.go18
-rw-r--r--modules/activitypub/user_settings_test.go7
4 files changed, 19 insertions, 14 deletions
diff --git a/modules/activitypub/client.go b/modules/activitypub/client.go
index fa1b57638f..66b977c01f 100644
--- a/modules/activitypub/client.go
+++ b/modules/activitypub/client.go
@@ -5,6 +5,7 @@ package activitypub
import (
"bytes"
+ "context"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
@@ -61,14 +62,14 @@ type Client struct {
}
// NewClient function
-func NewClient(user *user_model.User, pubID string) (c *Client, err error) {
+func NewClient(ctx context.Context, user *user_model.User, pubID string) (c *Client, err error) {
if err = containsRequiredHTTPHeaders(http.MethodGet, setting.Federation.GetHeaders); err != nil {
return nil, err
} else if err = containsRequiredHTTPHeaders(http.MethodPost, setting.Federation.PostHeaders); err != nil {
return nil, err
}
- priv, err := GetPrivateKey(user)
+ priv, err := GetPrivateKey(ctx, user)
if err != nil {
return nil, err
}
diff --git a/modules/activitypub/client_test.go b/modules/activitypub/client_test.go
index 83000b96d5..65ea8d4d5b 100644
--- a/modules/activitypub/client_test.go
+++ b/modules/activitypub/client_test.go
@@ -11,6 +11,7 @@ import (
"regexp"
"testing"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
@@ -22,7 +23,7 @@ func TestActivityPubSignedPost(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
pubID := "https://example.com/pubID"
- c, err := NewClient(user, pubID)
+ c, err := NewClient(db.DefaultContext, user, pubID)
assert.NoError(t, err)
expected := "BODY"
diff --git a/modules/activitypub/user_settings.go b/modules/activitypub/user_settings.go
index ef9bc0a864..d196a3806e 100644
--- a/modules/activitypub/user_settings.go
+++ b/modules/activitypub/user_settings.go
@@ -4,6 +4,8 @@
package activitypub
import (
+ "context"
+
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/util"
)
@@ -11,19 +13,19 @@ import (
const rsaBits = 3072
// GetKeyPair function returns a user's private and public keys
-func GetKeyPair(user *user_model.User) (pub, priv string, err error) {
+func GetKeyPair(ctx context.Context, user *user_model.User) (pub, priv string, err error) {
var settings map[string]*user_model.Setting
- settings, err = user_model.GetSettings(user.ID, []string{user_model.UserActivityPubPrivPem, user_model.UserActivityPubPubPem})
+ settings, err = user_model.GetSettings(ctx, user.ID, []string{user_model.UserActivityPubPrivPem, user_model.UserActivityPubPubPem})
if err != nil {
return pub, priv, err
} else if len(settings) == 0 {
if priv, pub, err = util.GenerateKeyPair(rsaBits); err != nil {
return pub, priv, err
}
- if err = user_model.SetUserSetting(user.ID, user_model.UserActivityPubPrivPem, priv); err != nil {
+ if err = user_model.SetUserSetting(ctx, user.ID, user_model.UserActivityPubPrivPem, priv); err != nil {
return pub, priv, err
}
- if err = user_model.SetUserSetting(user.ID, user_model.UserActivityPubPubPem, pub); err != nil {
+ if err = user_model.SetUserSetting(ctx, user.ID, user_model.UserActivityPubPubPem, pub); err != nil {
return pub, priv, err
}
return pub, priv, err
@@ -35,13 +37,13 @@ func GetKeyPair(user *user_model.User) (pub, priv string, err error) {
}
// GetPublicKey function returns a user's public key
-func GetPublicKey(user *user_model.User) (pub string, err error) {
- pub, _, err = GetKeyPair(user)
+func GetPublicKey(ctx context.Context, user *user_model.User) (pub string, err error) {
+ pub, _, err = GetKeyPair(ctx, user)
return pub, err
}
// GetPrivateKey function returns a user's private key
-func GetPrivateKey(user *user_model.User) (priv string, err error) {
- _, priv, err = GetKeyPair(user)
+func GetPrivateKey(ctx context.Context, user *user_model.User) (priv string, err error) {
+ _, priv, err = GetKeyPair(ctx, user)
return priv, err
}
diff --git a/modules/activitypub/user_settings_test.go b/modules/activitypub/user_settings_test.go
index 78ebf8e824..2d77906521 100644
--- a/modules/activitypub/user_settings_test.go
+++ b/modules/activitypub/user_settings_test.go
@@ -6,6 +6,7 @@ package activitypub
import (
"testing"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
@@ -17,12 +18,12 @@ import (
func TestUserSettings(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
- pub, priv, err := GetKeyPair(user1)
+ pub, priv, err := GetKeyPair(db.DefaultContext, user1)
assert.NoError(t, err)
- pub1, err := GetPublicKey(user1)
+ pub1, err := GetPublicKey(db.DefaultContext, user1)
assert.NoError(t, err)
assert.Equal(t, pub, pub1)
- priv1, err := GetPrivateKey(user1)
+ priv1, err := GetPrivateKey(db.DefaultContext, user1)
assert.NoError(t, err)
assert.Equal(t, priv, priv1)
}