84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/article"
|
|
"RenewCMS/internal/domain/audit"
|
|
"RenewCMS/internal/domain/builtintype"
|
|
"RenewCMS/internal/domain/language"
|
|
"RenewCMS/internal/domain/localization"
|
|
"RenewCMS/internal/domain/role"
|
|
"cmp"
|
|
"slices"
|
|
)
|
|
|
|
func (c ContentCaller) isAnonymous() bool {
|
|
return c.UserID == 0
|
|
}
|
|
|
|
func (c ContentCaller) capabilitiesFor(art article.Article) []string {
|
|
capabilities := []string{article.CapabilityRead}
|
|
if c.Grants.Permits(role.KeyContentUpdate, builtintype.KeyArticle, c.UserID, art.CreatedBy) {
|
|
capabilities = append(capabilities, article.CapabilityEdit)
|
|
}
|
|
if c.Grants.Permits(role.KeyContentPublish, builtintype.KeyArticle, c.UserID, art.CreatedBy) {
|
|
capabilities = append(capabilities, article.CapabilityPublish)
|
|
}
|
|
if c.Grants.Permits(role.KeyContentDelete, builtintype.KeyArticle, c.UserID, art.CreatedBy) {
|
|
capabilities = append(capabilities, article.CapabilityDelete)
|
|
}
|
|
if c.Grants.HoldsFor(role.KeyContentCreate, builtintype.KeyArticle) {
|
|
capabilities = append(capabilities, article.CapabilityCreateTranslation)
|
|
}
|
|
return capabilities
|
|
}
|
|
|
|
func (c ContentCaller) readableVersions(versions []article.Article) []article.Article {
|
|
readable := make([]article.Article, 0, len(versions))
|
|
for _, version := range versions {
|
|
if c.seesArticle(version) {
|
|
readable = append(readable, version)
|
|
}
|
|
}
|
|
return readable
|
|
}
|
|
|
|
func sortByCreation(versions []article.Article) []article.Article {
|
|
ordered := slices.Clone(versions)
|
|
slices.SortFunc(ordered, func(left article.Article, right article.Article) int {
|
|
if byTime := left.CreatedAt.Compare(right.CreatedAt); byTime != 0 {
|
|
return byTime
|
|
}
|
|
return cmp.Compare(left.ID, right.ID)
|
|
})
|
|
return ordered
|
|
}
|
|
|
|
func sortByLanguage(versions []article.Article, rows []localization.Row) []article.Article {
|
|
defaultTag := localization.DefaultTag(rows)
|
|
ordered := slices.Clone(versions)
|
|
slices.SortFunc(ordered, func(left article.Article, right article.Article) int {
|
|
if byTag := localization.CompareTags(defaultTag, left.Lang, right.Lang); byTag != 0 {
|
|
return byTag
|
|
}
|
|
return cmp.Compare(left.ID, right.ID)
|
|
})
|
|
return ordered
|
|
}
|
|
|
|
func languagesOf(versions []article.Article) []string {
|
|
tags := make([]string, 0, len(versions))
|
|
for _, version := range versions {
|
|
tags = append(tags, version.Lang)
|
|
}
|
|
return tags
|
|
}
|
|
|
|
func articleEvent(action string, art article.Article) audit.Event {
|
|
event := audit.NewEvent(action, audit.CategoryArticles)
|
|
event.Target = articleTarget(art)
|
|
if art.Lang != language.Unassigned {
|
|
event.Params[audit.ParamLang] = art.Lang
|
|
}
|
|
return event
|
|
}
|