96 lines
2.7 KiB
Go
96 lines
2.7 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/article"
|
|
"RenewCMS/internal/domain/language"
|
|
"RenewCMS/internal/domain/localization"
|
|
"errors"
|
|
)
|
|
|
|
type DescribeArticleUseCase struct {
|
|
articles article.Repository
|
|
localization localization.Repository
|
|
}
|
|
|
|
type ArticleContext struct {
|
|
Capabilities []string
|
|
Translations []article.Translation
|
|
Source *article.Source
|
|
}
|
|
|
|
func NewDescribeArticleUseCase(articles article.Repository, localizationRepository localization.Repository) *DescribeArticleUseCase {
|
|
return &DescribeArticleUseCase{articles: articles, localization: localizationRepository}
|
|
}
|
|
|
|
func (d *DescribeArticleUseCase) Describe(art article.Article, caller ContentCaller) (ArticleContext, error) {
|
|
described := ArticleContext{Capabilities: caller.capabilitiesFor(art), Translations: []article.Translation{}}
|
|
if caller.isAnonymous() {
|
|
return described, nil
|
|
}
|
|
translations, err := d.translations(art, caller)
|
|
if err != nil {
|
|
return ArticleContext{}, err
|
|
}
|
|
described.Translations = translations
|
|
described.Source, err = d.source(art, caller)
|
|
if err != nil {
|
|
return ArticleContext{}, err
|
|
}
|
|
return described, nil
|
|
}
|
|
|
|
func (d *DescribeArticleUseCase) translations(art article.Article, caller ContentCaller) ([]article.Translation, error) {
|
|
versions := []article.Article{art}
|
|
if art.StoryID != 0 {
|
|
found, err := d.articles.ListByStory(art.StoryID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
versions = found
|
|
}
|
|
rows, err := d.rows(art.SiteID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
translations := []article.Translation{}
|
|
for _, version := range sortByLanguage(caller.readableVersions(versions), rows) {
|
|
translations = append(translations, article.Translation{
|
|
ID: version.ID,
|
|
Lang: version.Lang,
|
|
Title: version.Title,
|
|
IsOnline: version.IsOnline,
|
|
LanguageEnabled: d.languageEnabled(rows, version.Lang),
|
|
Capabilities: caller.capabilitiesFor(version),
|
|
})
|
|
}
|
|
return translations, nil
|
|
}
|
|
|
|
func (d *DescribeArticleUseCase) rows(siteID uint32) ([]localization.Row, error) {
|
|
return d.localization.Rows(siteID)
|
|
}
|
|
|
|
func (d *DescribeArticleUseCase) languageEnabled(rows []localization.Row, tag string) bool {
|
|
if tag == language.Unassigned {
|
|
return true
|
|
}
|
|
return localization.EnabledTag(rows, tag)
|
|
}
|
|
|
|
func (d *DescribeArticleUseCase) source(art article.Article, caller ContentCaller) (*article.Source, error) {
|
|
if art.SourceArticleID == nil {
|
|
return nil, nil
|
|
}
|
|
origin, err := d.articles.Get(*art.SourceArticleID)
|
|
if errors.Is(err, article.ErrArticleNotFound) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !caller.seesArticle(origin) {
|
|
return nil, nil
|
|
}
|
|
return &article.Source{ArticleID: origin.ID, Revision: art.SourceRevision}, nil
|
|
}
|