This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/pkg/e621/builder/favorite.go
soxx 322bb41a97 refactor: basic implementation of favorites
Signed-off-by: soxx <soxx@fenpa.ws>
2023-11-27 22:37:36 +01:00

133 lines
3.3 KiB
Go

package builder
import (
"fmt"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"strings"
)
type FavoritesBuilder interface {
FromUsername(username string) FavoritesBuilder
FromUserID(userID model.UserID) FavoritesBuilder
WithTags(tags []string) FavoritesBuilder
Limit(amount int) FavoritesBuilder
Execute() ([]model.Post, error)
}
func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilder {
return &getFavorites{
requestContext: requestContext,
}
}
type getFavorites struct {
requestContext model.RequestContext
user model.User
tags string
limit int
error error
}
func (g *getFavorites) FromUsername(username string) FavoritesBuilder {
user := model.User{Name: username}
g.user = user
return g
}
func (g *getFavorites) FromUserID(userID model.UserID) FavoritesBuilder {
user := model.User{ID: userID}
g.user = user
return g
}
func (g *getFavorites) WithTags(tagList []string) FavoritesBuilder {
tags := strings.Join(tagList, ",")
g.tags = tags
return g
}
func (g *getFavorites) Limit(amount int) FavoritesBuilder {
g.limit = amount
return g
}
func (g getFavorites) Execute() ([]model.Post, error) {
var err error
basicFavoritesBuilder := basic.NewGetFavoritesBuilder(g.requestContext)
var userFavorites []model.Post
if g.user.ID == 0 && g.user.Name == "" {
return nil, fmt.Errorf("no UserID or UserName given")
}
if g.user.Name != "" {
g.user, err = basic.NewGetUserBuilder(g.requestContext).SetUsername(g.user.Name).Execute()
if err != nil {
return nil, err
}
}
if g.user.ID != 0 {
basicFavoritesBuilder.SetUserID(g.user.ID)
}
if g.limit != 0 {
basicFavoritesBuilder.SetLimit(g.limit)
}
if g.tags == "" {
userFavorites, err = basicFavoritesBuilder.Execute()
}
if g.tags != "" {
// TODO: implement with tags (channels)
} else {
return basicFavoritesBuilder.Execute()
}
return userFavorites, err
}
/*
// GetNFavoritesForUser retrieves a specified number of favorites for a user.
func (c *CLient) GetNFavoritesForUser(n int, favoriteBuilder basic.FavoritesBuilder) ([]model.Post, error) {
// Retrieves a specified number of favorite posts for a user.
if n < utils.E621_MAX_POST_COUNT {
favoriteBuilder.SetLimit(n)
}
var favorites []model.Post
var page = 1
for len(favorites) < n {
favoriteBuilder.Page(page)
favoriteBuilder.SetLimit(n - len(favorites))
newFavorites, err := favoriteBuilder.Execute()
if err != nil {
return nil, err
}
if len(newFavorites) == 0 {
break
}
favorites = append(favorites, newFavorites...)
page = page + 1
}
return favorites, nil
}
// GetAllFavoritesForUser retrieves all favorites for a user.
func (c *Client) GetAllFavoritesForUser(favoriteBuilder basic.FavoritesBuilder) ([]model.Post, error) {
// Retrieves all favorite posts for a user.
return c.GetNFavoritesForUser(math.MaxInt, favoriteBuilder)
}
// GetFavoritesForUserWithTags returns a posts builder for a user's favorites with specific tags.
func (c *Client) GetFavoritesForUserWithTags(username string, tags string) basic.PostsBuilder {
// Returns a posts builder for a user's favorites with specific tags.
favoritesBuilder := basic.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags))
return favoritesBuilder
}
*/