2023-12-06 04:47:41 +00:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-16 14:19:53 +00:00
|
|
|
"fmt"
|
2023-12-06 04:47:41 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
resolver "github.com/satisfactorymodding/ficsit-resolver"
|
2023-12-06 04:47:41 +00:00
|
|
|
|
|
|
|
"github.com/satisfactorymodding/ficsit-cli/cli/cache"
|
2024-10-02 08:59:56 +00:00
|
|
|
"github.com/satisfactorymodding/ficsit-cli/cli/localregistry"
|
2023-12-06 04:47:41 +00:00
|
|
|
"github.com/satisfactorymodding/ficsit-cli/ficsit"
|
|
|
|
)
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
type LocalProvider struct{}
|
2023-12-06 04:47:41 +00:00
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func NewLocalProvider() LocalProvider {
|
|
|
|
return LocalProvider{}
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func (p LocalProvider) Mods(_ context.Context, filter ficsit.ModFilter) (*ficsit.ModsResponse, error) {
|
2024-10-02 08:59:56 +00:00
|
|
|
cachedMods, err := cache.GetCacheMods()
|
2023-12-06 04:47:41 +00:00
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to get cache: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mods := make([]ficsit.ModsModsGetModsModsMod, 0)
|
|
|
|
|
2024-10-02 08:59:56 +00:00
|
|
|
cachedMods.Range(func(modReference string, cachedMod cache.Mod) bool {
|
2023-12-06 04:47:41 +00:00
|
|
|
if len(filter.References) > 0 {
|
|
|
|
skip := true
|
|
|
|
|
|
|
|
for _, a := range filter.References {
|
|
|
|
if a == modReference {
|
|
|
|
skip = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if skip {
|
2023-12-07 16:57:31 +00:00
|
|
|
return true
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mods = append(mods, ficsit.ModsModsGetModsModsMod{
|
|
|
|
Id: modReference,
|
2024-10-02 08:59:56 +00:00
|
|
|
Name: cachedMod.Name,
|
2023-12-06 04:47:41 +00:00
|
|
|
Mod_reference: modReference,
|
|
|
|
Last_version_date: time.Now(),
|
|
|
|
Created_at: time.Now(),
|
|
|
|
Downloads: 0,
|
|
|
|
Popularity: 0,
|
|
|
|
Hotness: 0,
|
|
|
|
})
|
2023-12-07 16:57:31 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
2023-12-06 04:47:41 +00:00
|
|
|
|
|
|
|
if filter.Limit == 0 {
|
|
|
|
filter.Limit = 25
|
|
|
|
}
|
|
|
|
|
|
|
|
low := filter.Offset
|
|
|
|
high := filter.Offset + filter.Limit
|
|
|
|
|
|
|
|
if low > len(mods) {
|
|
|
|
return &ficsit.ModsResponse{
|
|
|
|
Mods: ficsit.ModsModsGetMods{
|
|
|
|
Count: 0,
|
|
|
|
Mods: []ficsit.ModsModsGetModsModsMod{},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if high > len(mods) {
|
|
|
|
high = len(mods)
|
|
|
|
}
|
|
|
|
|
|
|
|
mods = mods[low:high]
|
|
|
|
|
|
|
|
return &ficsit.ModsResponse{
|
|
|
|
Mods: ficsit.ModsModsGetMods{
|
|
|
|
Count: len(mods),
|
|
|
|
Mods: mods,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func (p LocalProvider) GetMod(_ context.Context, modReference string) (*ficsit.GetModResponse, error) {
|
2024-10-02 08:59:56 +00:00
|
|
|
cachedMod, err := cache.GetCacheMod(modReference)
|
2023-12-06 04:47:41 +00:00
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to get cache: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
authors := make([]ficsit.GetModModAuthorsUserMod, 0)
|
|
|
|
|
2024-10-02 08:59:56 +00:00
|
|
|
for _, author := range strings.Split(cachedMod.Author, ",") {
|
2023-12-06 04:47:41 +00:00
|
|
|
authors = append(authors, ficsit.GetModModAuthorsUserMod{
|
|
|
|
Role: "Unknown",
|
|
|
|
User: ficsit.GetModModAuthorsUserModUser{
|
|
|
|
Username: author,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ficsit.GetModResponse{
|
|
|
|
Mod: ficsit.GetModMod{
|
|
|
|
Id: modReference,
|
2024-10-02 08:59:56 +00:00
|
|
|
Name: cachedMod.Name,
|
2023-12-06 04:47:41 +00:00
|
|
|
Mod_reference: modReference,
|
|
|
|
Created_at: time.Now(),
|
|
|
|
Views: 0,
|
|
|
|
Downloads: 0,
|
|
|
|
Authors: authors,
|
|
|
|
Full_description: "",
|
|
|
|
Source_url: "",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func (p LocalProvider) ModVersionsWithDependencies(_ context.Context, modID string) ([]resolver.ModVersion, error) {
|
2024-10-02 08:59:56 +00:00
|
|
|
modVersions, err := localregistry.GetModVersions(modID)
|
2023-12-06 04:47:41 +00:00
|
|
|
if err != nil {
|
2024-10-02 08:59:56 +00:00
|
|
|
return nil, fmt.Errorf("failed to get local mod versions: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 08:59:56 +00:00
|
|
|
// TODO: only list as available the versions that have at least one target cached
|
2023-12-06 04:47:41 +00:00
|
|
|
|
2024-10-04 17:23:51 +00:00
|
|
|
return convertFicsitVersionsToResolver(modVersions), nil
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
func (p LocalProvider) GetModName(_ context.Context, modReference string) (*resolver.ModName, error) {
|
2024-10-02 08:59:56 +00:00
|
|
|
cachedMod, err := cache.GetCacheMod(modReference)
|
2023-12-06 04:47:41 +00:00
|
|
|
if err != nil {
|
2023-12-16 14:19:53 +00:00
|
|
|
return nil, fmt.Errorf("failed to get cache: %w", err)
|
2023-12-06 04:47:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 11:59:58 +00:00
|
|
|
return &resolver.ModName{
|
|
|
|
ID: modReference,
|
2024-10-02 08:59:56 +00:00
|
|
|
Name: cachedMod.Name,
|
2023-12-16 11:59:58 +00:00
|
|
|
ModReference: modReference,
|
2023-12-06 04:47:41 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2023-12-16 11:59:58 +00:00
|
|
|
|
|
|
|
func (p LocalProvider) IsOffline() bool {
|
|
|
|
return true
|
|
|
|
}
|