fix: use browser UA for link preview fetch, add error logging to bg goroutines

This commit is contained in:
Patrick Britton 2026-02-09 08:22:27 -06:00
parent 9879064824
commit 41407feb58
2 changed files with 22 additions and 8 deletions

View file

@ -118,7 +118,7 @@ func (s *LinkPreviewService) FetchPreview(ctx context.Context, rawURL string, tr
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Sojorn/1.0; +https://sojorn.net)")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
req.Header.Set("Accept", "text/html")
resp, err := s.httpClient.Do(req)

View file

@ -740,10 +740,17 @@ func (s *OfficialAccountsService) CreatePostForAccount(ctx context.Context, conf
}
if linkURL != "" {
lps := NewLinkPreviewService(s.pool)
lp, err := lps.FetchPreview(bgCtx, linkURL, true)
if err == nil && lp != nil {
_ = lps.SaveLinkPreview(bgCtx, postID.String(), lp)
log.Debug().Str("post_id", postID.String()).Str("url", linkURL).Msg("Saved link preview for official account post")
lp, lpErr := lps.FetchPreview(bgCtx, linkURL, true)
if lpErr != nil {
log.Warn().Err(lpErr).Str("post_id", postID.String()).Str("url", linkURL).Msg("[OfficialAccounts] Link preview fetch failed")
return
}
if lp != nil {
if saveErr := lps.SaveLinkPreview(bgCtx, postID.String(), lp); saveErr != nil {
log.Warn().Err(saveErr).Str("post_id", postID.String()).Msg("[OfficialAccounts] Link preview save failed")
} else {
log.Info().Str("post_id", postID.String()).Str("url", linkURL).Str("title", lp.Title).Msg("[OfficialAccounts] Link preview saved")
}
}
}
}()
@ -814,9 +821,16 @@ func (s *OfficialAccountsService) CreatePostForArticle(ctx context.Context, conf
if linkURL != "" {
lps := NewLinkPreviewService(s.pool)
lp, lpErr := lps.FetchPreview(bgCtx, linkURL, true)
if lpErr == nil && lp != nil {
_ = lps.SaveLinkPreview(bgCtx, postID.String(), lp)
log.Debug().Str("post_id", postID.String()).Str("url", linkURL).Msg("Saved link preview for official account post")
if lpErr != nil {
log.Warn().Err(lpErr).Str("post_id", postID.String()).Str("url", linkURL).Msg("[OfficialAccounts] Link preview fetch failed")
return
}
if lp != nil {
if saveErr := lps.SaveLinkPreview(bgCtx, postID.String(), lp); saveErr != nil {
log.Warn().Err(saveErr).Str("post_id", postID.String()).Msg("[OfficialAccounts] Link preview save failed")
} else {
log.Info().Str("post_id", postID.String()).Str("url", linkURL).Str("title", lp.Title).Msg("[OfficialAccounts] Link preview saved")
}
}
}
}()