sonda/internal/about/source.go
tgiachi 762d424816 fix(about): unusable pages fall through, MusicBrainz switch, merged items
- A page that cannot be used (no text, an address off Wikipedia, a redirect that is
  never followed) is now an answer: the chain goes on to the next language and the
  outcome is stored. Before, it was a failure asked about again at every view, and
  redirects counted against the provider's breaker.
- The chain also needs the MusicBrainz provider to be on, since the id goes to it
  with its own contact: nothing is sent to MusicBrainz that the admin did not allow.
  The disclosure in the provider settings, the spec and the README say so.
- A Wikidata item that was merged into another is understood through the target: the
  entity's own id is returned, compared with the page's item and stored. Verified on
  the real API.
- The title shown is the one Wikidata names (with spaces), control characters are
  dropped from the text (PostgreSQL refuses NUL), the text is cut to at most 4000
  characters with the mark included, and a stored description is reported disabled
  when the provider is off.
- A read that started before the provider was switched off does not save its result.
- Tests: the real clients with a redirect and a page without text, merged items,
  MusicBrainz off, switching off during a read.
2026-11-11 21:45:20 +01:00

51 lines
1.7 KiB
Go

package about
import (
"context"
"errors"
"codeberg.org/squidd/sonda/internal/providers/musicbrainz"
"codeberg.org/squidd/sonda/internal/providers/wikipedia"
)
// WikipediaSource follows the chain with the real clients: MusicBrainz for the link to
// Wikidata, Wikidata for the pages, Wikipedia for the summary.
type WikipediaSource struct {
MusicBrainz *musicbrainz.Client
Wikipedia *wikipedia.Client
}
// notFound turns what means "there is nothing to find" into ErrSourceNotFound. An
// input the clients refuse (an id that is not one) is nothing to find either: asking
// again would not change that.
func notFound(err error) error {
if errors.Is(err, musicbrainz.ErrNotFound) || errors.Is(err, musicbrainz.ErrBadRequest) ||
errors.Is(err, wikipedia.ErrNotFound) || errors.Is(err, wikipedia.ErrBadRequest) {
return ErrSourceNotFound
}
return err
}
// WikidataID implements Source.
func (w WikipediaSource) WikidataID(ctx context.Context, musicbrainzID string) (string, error) {
id, err := w.MusicBrainz.WikidataID(ctx, musicbrainzID)
return id, notFound(err)
}
// Pages implements Source.
func (w WikipediaSource) Pages(ctx context.Context, item string, languages []string) (string, map[string]string, error) {
resolved, pages, err := w.Wikipedia.Pages(ctx, item, languages)
return resolved, pages, notFound(err)
}
// Summary implements Source.
func (w WikipediaSource) Summary(ctx context.Context, language, title string) (Summary, error) {
s, err := w.Wikipedia.Summary(ctx, language, title)
if err != nil {
return Summary{}, notFound(err)
}
return Summary{
Type: s.Type, Title: s.Title, Lang: s.Lang, Extract: s.Extract, Description: s.Description,
URL: s.URL, WikibaseItem: s.WikibaseItem,
}, nil
}