Add preview card support

This commit is contained in:
kaimanhub 2025-03-21 23:58:13 +02:00
commit 8cbfc9b188
5 changed files with 218 additions and 6 deletions

View file

@ -189,6 +189,17 @@ func (p *Processor) Create(
PendingApproval: util.Ptr(false),
}
// Get preview card
card, errWithCode := FetchPreview(content.Content)
if errWithCode != nil {
return nil, errWithCode
}
if card != nil {
status.CardID = id.NewULIDFromTime(now)
status.Card = card
}
// Only store ContentWarningText if the parsed
// result is different from the given SpoilerText,
// otherwise skip to avoid duplicating db columns.
@ -315,7 +326,6 @@ func (p *Processor) Create(
// backfilledStatusID tries to find an unused ULID for a backfilled status.
func (p *Processor) backfilledStatusID(ctx context.Context, createdAt time.Time) (string, error) {
// Any fetching of statuses here is
// only to check availability of ID,
// no need for any attached models.
@ -522,7 +532,6 @@ func processInteractionPolicy(
settings *gtsmodel.AccountSettings,
status *gtsmodel.Status,
) gtserror.WithCode {
// If policy is set on the
// form then prefer this.
//
@ -535,7 +544,6 @@ func processInteractionPolicy(
form.InteractionPolicy,
form.Visibility,
)
if err != nil {
errWithCode := gtserror.NewErrorBadRequest(err, err.Error())
return errWithCode

View file

@ -0,0 +1,111 @@
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package status
import (
"fmt"
"net/http"
"net/url"
"regexp"
"github.com/PuerkitoBio/goquery"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
var urlRegex = regexp.MustCompile(`https?://[a-zA-Z0-9./?=_-]+`)
func extractLastURL(text string) string {
matches := urlRegex.FindAllString(text, -1)
if len(matches) == 0 {
return ""
}
return matches[len(matches)-1]
}
// FetchPreview retrieves OpenGraph metadata from a URL.
func FetchPreview(text string) (*gtsmodel.Card, gtserror.WithCode) {
link := extractLastURL(text)
if link == "" {
return nil, nil
}
parsed, err := url.ParseRequestURI(link)
if err != nil {
return nil, gtserror.NewErrorInternalError(err, "invalid URL")
}
if parsed.Scheme != "http" && parsed.Scheme != "https" {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("unsupported scheme: %s", parsed.Scheme))
}
resp, err := http.Get(link)
if err != nil {
return nil, gtserror.NewErrorInternalError(err, "request failed")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("unexpected status: %s", resp.Status))
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, gtserror.NewErrorInternalError(fmt.Errorf("failed to parse HTML: %w", err))
}
card := &gtsmodel.Card{
URL: link,
}
doc.Find("meta").Each(func(i int, s *goquery.Selection) {
property, _ := s.Attr("property")
content, _ := s.Attr("content")
switch property {
case "og:title":
card.Title = content
case "og:description":
card.Description = content
case "og:type":
card.Type = content
case "og:image":
card.Image = content
case "og:url":
if content != "" {
card.URL = content
}
case "og:site_name":
card.ProviderName = content
}
})
if card.Title == "" {
card.Title = doc.Find("title").Text()
}
if card.Description == "" {
desc, exists := doc.Find("meta[name='description']").Attr("content")
if exists {
card.Description = desc
}
}
return card, nil
}