2023-10-23 08:21:51 +00:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
2023-10-24 13:10:39 +00:00
|
|
|
"context"
|
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"
|
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
|
2023-10-23 08:21:51 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// UsersBuilder represents a builder for constructing queries to retrieve user information.
|
2023-10-23 08:21:51 +00:00
|
|
|
type UsersBuilder interface {
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetPage sets the page number for paginated results.
|
2023-10-23 08:21:51 +00:00
|
|
|
SetPage(pageNumber int) UsersBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetLimit sets the maximum number of users to retrieve.
|
2023-10-23 08:21:51 +00:00
|
|
|
SetLimit(limitUser int) UsersBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByName specifies a username to search for.
|
2023-10-24 13:10:39 +00:00
|
|
|
SearchByName(userName string) UsersBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByAbout specifies a text to search in the 'about' field of user profiles.
|
2023-10-24 13:10:39 +00:00
|
|
|
SearchByAbout(about string) UsersBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByAvatarID specifies an avatar ID to search for.
|
|
|
|
SearchByAvatarID(avatarID model.PostID) UsersBuilder
|
|
|
|
// SearchByLevel specifies a user level to filter by.
|
2023-10-24 13:10:39 +00:00
|
|
|
SearchByLevel(level model.UserLevel) UsersBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByMinLevel specifies the minimum user level to filter by.
|
|
|
|
SearchByMinLevel(minLevel model.UserLevel) UsersBuilder
|
|
|
|
// SearchByMaxLevel specifies the maximum user level to filter by.
|
|
|
|
SearchByMaxLevel(maxLevel model.UserLevel) UsersBuilder
|
|
|
|
// SearchByCanUpload specifies whether users can upload free content.
|
|
|
|
SearchByCanUpload(canUpload bool) UsersBuilder
|
|
|
|
// SearchByIsApprover specifies whether users can approve posts.
|
2023-10-24 13:10:39 +00:00
|
|
|
SearchByIsApprover(isApprover bool) UsersBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByOrder specifies the order in which users are retrieved.
|
2023-10-24 13:10:39 +00:00
|
|
|
SearchByOrder(order model.Order) UsersBuilder
|
2023-11-06 14:56:36 +00:00
|
|
|
// Execute sends the constructed query and returns the requested user information and an error, if any.
|
2023-10-23 08:21:51 +00:00
|
|
|
Execute() ([]model.User, error)
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// NewGetUsersBuilder creates a new UsersBuilder with the provided RequestContext.
|
2023-10-23 08:21:51 +00:00
|
|
|
func NewGetUsersBuilder(requestContext model.RequestContext) UsersBuilder {
|
|
|
|
return &getUsers{requestContext: requestContext, query: make(map[string]string)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type getUsers struct {
|
|
|
|
requestContext model.RequestContext
|
|
|
|
query map[string]string
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByName specifies a username to search for.
|
2023-10-24 13:10:39 +00:00
|
|
|
func (g *getUsers) SearchByName(userName string) UsersBuilder {
|
|
|
|
g.query["search[name_matches]"] = userName
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByAbout specifies a text to search in the 'about' field of user profiles.
|
2023-10-24 13:10:39 +00:00
|
|
|
func (g *getUsers) SearchByAbout(about string) UsersBuilder {
|
2023-11-06 14:56:36 +00:00
|
|
|
g.query["search[about_me]"] = about
|
2023-10-24 13:10:39 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByAvatarID specifies an avatar ID to search for.
|
|
|
|
func (g *getUsers) SearchByAvatarID(avatarID model.PostID) UsersBuilder {
|
|
|
|
g.query["search[avatar_id]"] = strconv.FormatInt(int64(avatarID), 10)
|
2023-10-24 13:10:39 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByLevel specifies a user level to filter by.
|
2023-10-24 13:10:39 +00:00
|
|
|
func (g *getUsers) SearchByLevel(level model.UserLevel) UsersBuilder {
|
|
|
|
g.query["search[level]"] = strconv.Itoa(int(level))
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByMinLevel specifies the minimum user level to filter by.
|
|
|
|
func (g *getUsers) SearchByMinLevel(minLevel model.UserLevel) UsersBuilder {
|
|
|
|
g.query["search[min_level]"] = strconv.Itoa(int(minLevel))
|
2023-10-24 13:10:39 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByMaxLevel specifies the maximum user level to filter by.
|
|
|
|
func (g *getUsers) SearchByMaxLevel(maxLevel model.UserLevel) UsersBuilder {
|
|
|
|
g.query["search[max_level]"] = strconv.Itoa(int(maxLevel))
|
2023-10-24 13:10:39 +00:00
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByCanUpload specifies whether users can upload free content.
|
|
|
|
func (g *getUsers) SearchByCanUpload(canUpload bool) UsersBuilder {
|
2023-10-24 13:10:39 +00:00
|
|
|
g.query["search[can_upload_free]"] = strconv.FormatBool(canUpload)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByIsApprover specifies whether users can approve posts.
|
2023-10-24 13:10:39 +00:00
|
|
|
func (g *getUsers) SearchByIsApprover(isApprover bool) UsersBuilder {
|
|
|
|
g.query["search[can_approve_posts]"] = strconv.FormatBool(isApprover)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SearchByOrder specifies the order in which users are retrieved.
|
2023-10-24 13:10:39 +00:00
|
|
|
func (g *getUsers) SearchByOrder(order model.Order) UsersBuilder {
|
|
|
|
g.query["search[order]"] = string(order)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetPage sets the page number for paginated results.
|
2023-10-23 08:21:51 +00:00
|
|
|
func (g *getUsers) SetPage(pageNumber int) UsersBuilder {
|
|
|
|
g.query["page"] = strconv.Itoa(pageNumber)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// SetLimit sets the maximum number of users to retrieve.
|
2023-10-23 08:21:51 +00:00
|
|
|
func (g *getUsers) SetLimit(limitUser int) UsersBuilder {
|
|
|
|
g.query["limit"] = strconv.Itoa(limitUser)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2023-11-06 14:56:36 +00:00
|
|
|
// Execute sends the constructed query and returns the requested user information and an error, if any.
|
2023-10-23 08:21:51 +00:00
|
|
|
func (g *getUsers) Execute() ([]model.User, error) {
|
2023-10-24 13:10:39 +00:00
|
|
|
if g.requestContext.RateLimiter != nil {
|
|
|
|
err := g.requestContext.RateLimiter.Wait(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-10-23 08:21:51 +00:00
|
|
|
users, err := endpoints.GetUsers(g.requestContext, g.query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|