go-forge/users.go
Snider 0a9564274d
All checks were successful
Security Scan / security (pull_request) Successful in 9s
Test / test (pull_request) Successful in 1m20s
refactor(module): migrate module path to dappco.re/go/core/forge
Update Go module path from forge.lthn.ai/core/go-forge to
dappco.re/go/core/forge. Migrate all import paths and dependency
references (go-io → dappco.re/go/core/io, go-log → dappco.re/go/core/log).
Update documentation (CLAUDE.md, README.md, docs/) to reflect new paths.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-22 01:51:29 +00:00

91 lines
3.2 KiB
Go

package forge
import (
"context"
"fmt"
"iter"
"dappco.re/go/core/forge/types"
)
// UserService handles user operations.
type UserService struct {
Resource[types.User, struct{}, struct{}]
}
func newUserService(c *Client) *UserService {
return &UserService{
Resource: *NewResource[types.User, struct{}, struct{}](
c, "/api/v1/users/{username}",
),
}
}
// GetCurrent returns the authenticated user.
func (s *UserService) GetCurrent(ctx context.Context) (*types.User, error) {
var out types.User
if err := s.client.Get(ctx, "/api/v1/user", &out); err != nil {
return nil, err
}
return &out, nil
}
// ListFollowers returns all followers of a user.
func (s *UserService) ListFollowers(ctx context.Context, username string) ([]types.User, error) {
path := fmt.Sprintf("/api/v1/users/%s/followers", username)
return ListAll[types.User](ctx, s.client, path, nil)
}
// IterFollowers returns an iterator over all followers of a user.
func (s *UserService) IterFollowers(ctx context.Context, username string) iter.Seq2[types.User, error] {
path := fmt.Sprintf("/api/v1/users/%s/followers", username)
return ListIter[types.User](ctx, s.client, path, nil)
}
// ListFollowing returns all users that a user is following.
func (s *UserService) ListFollowing(ctx context.Context, username string) ([]types.User, error) {
path := fmt.Sprintf("/api/v1/users/%s/following", username)
return ListAll[types.User](ctx, s.client, path, nil)
}
// IterFollowing returns an iterator over all users that a user is following.
func (s *UserService) IterFollowing(ctx context.Context, username string) iter.Seq2[types.User, error] {
path := fmt.Sprintf("/api/v1/users/%s/following", username)
return ListIter[types.User](ctx, s.client, path, nil)
}
// Follow follows a user as the authenticated user.
func (s *UserService) Follow(ctx context.Context, username string) error {
path := fmt.Sprintf("/api/v1/user/following/%s", username)
return s.client.Put(ctx, path, nil, nil)
}
// Unfollow unfollows a user as the authenticated user.
func (s *UserService) Unfollow(ctx context.Context, username string) error {
path := fmt.Sprintf("/api/v1/user/following/%s", username)
return s.client.Delete(ctx, path)
}
// ListStarred returns all repositories starred by a user.
func (s *UserService) ListStarred(ctx context.Context, username string) ([]types.Repository, error) {
path := fmt.Sprintf("/api/v1/users/%s/starred", username)
return ListAll[types.Repository](ctx, s.client, path, nil)
}
// IterStarred returns an iterator over all repositories starred by a user.
func (s *UserService) IterStarred(ctx context.Context, username string) iter.Seq2[types.Repository, error] {
path := fmt.Sprintf("/api/v1/users/%s/starred", username)
return ListIter[types.Repository](ctx, s.client, path, nil)
}
// Star stars a repository as the authenticated user.
func (s *UserService) Star(ctx context.Context, owner, repo string) error {
path := fmt.Sprintf("/api/v1/user/starred/%s/%s", owner, repo)
return s.client.Put(ctx, path, nil, nil)
}
// Unstar unstars a repository as the authenticated user.
func (s *UserService) Unstar(ctx context.Context, owner, repo string) error {
path := fmt.Sprintf("/api/v1/user/starred/%s/%s", owner, repo)
return s.client.Delete(ctx, path)
}