mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-01 00:52:24 -05:00
Use httpclient.Client instead of standard net/http
This commit is contained in:
parent
d11efa1e2f
commit
c42c391094
12 changed files with 33 additions and 10 deletions
|
|
@ -190,9 +190,9 @@ func (p *Processor) Create(
|
|||
}
|
||||
|
||||
// Get preview card
|
||||
card, errWithCode := FetchPreview(content.Content)
|
||||
card, errWithCode := FetchPreview(ctx, p.client, content.Content)
|
||||
if errWithCode != nil {
|
||||
return nil, errWithCode
|
||||
log.Errorf(ctx, "error loading preview card: %v", errWithCode)
|
||||
}
|
||||
|
||||
if card != nil {
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
package status
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
|
@ -27,6 +28,7 @@ import (
|
|||
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/httpclient"
|
||||
)
|
||||
|
||||
var urlRegex = regexp.MustCompile(`https?://[a-zA-Z0-9./?=_-]+`)
|
||||
|
|
@ -40,7 +42,7 @@ func extractLastURL(text string) string {
|
|||
}
|
||||
|
||||
// FetchPreview retrieves OpenGraph metadata from a URL.
|
||||
func FetchPreview(text string) (*gtsmodel.Card, gtserror.WithCode) {
|
||||
func FetchPreview(ctx context.Context, httpClient *httpclient.Client, text string) (*gtsmodel.Card, gtserror.WithCode) {
|
||||
link := extractLastURL(text)
|
||||
if link == "" {
|
||||
return nil, nil
|
||||
|
|
@ -55,7 +57,12 @@ func FetchPreview(text string) (*gtsmodel.Card, gtserror.WithCode) {
|
|||
return nil, gtserror.NewErrorInternalError(fmt.Errorf("unsupported scheme: %s", parsed.Scheme))
|
||||
}
|
||||
|
||||
resp, err := safeGet(parsed)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, link, nil)
|
||||
if err != nil {
|
||||
return nil, gtserror.NewErrorInternalError(err, "failed to create request")
|
||||
}
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, gtserror.NewErrorInternalError(err, "request failed")
|
||||
}
|
||||
|
|
@ -109,8 +116,3 @@ func FetchPreview(text string) (*gtsmodel.Card, gtserror.WithCode) {
|
|||
|
||||
return card, nil
|
||||
}
|
||||
|
||||
func safeGet(u *url.URL) (*http.Response, error) {
|
||||
// #nosec G107 -- URL was already validated
|
||||
return http.Get(u.String())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/filter/interaction"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/filter/visibility"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/httpclient"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/processing/common"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/processing/interactionrequests"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/processing/polls"
|
||||
|
|
@ -41,6 +42,7 @@ type Processor struct {
|
|||
intFilter *interaction.Filter
|
||||
formatter *text.Formatter
|
||||
parseMention gtsmodel.ParseMentionFunc
|
||||
client *httpclient.Client
|
||||
|
||||
// other processors
|
||||
polls *polls.Processor
|
||||
|
|
@ -58,6 +60,7 @@ func New(
|
|||
visFilter *visibility.Filter,
|
||||
intFilter *interaction.Filter,
|
||||
parseMention gtsmodel.ParseMentionFunc,
|
||||
client *httpclient.Client,
|
||||
) Processor {
|
||||
return Processor{
|
||||
c: common,
|
||||
|
|
@ -70,5 +73,6 @@ func New(
|
|||
parseMention: parseMention,
|
||||
polls: polls,
|
||||
intReqs: intReqs,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import (
|
|||
"github.com/superseriousbusiness/gotosocial/internal/filter/interaction"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/filter/visibility"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/httpclient"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/media"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/processing"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/processing/common"
|
||||
|
|
@ -116,6 +117,7 @@ func (suite *StatusStandardTestSuite) SetupTest() {
|
|||
&suite.state,
|
||||
suite.federator,
|
||||
),
|
||||
&httpclient.Client{},
|
||||
)
|
||||
|
||||
testrig.StandardDBSetup(suite.db, suite.testAccounts)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue