diff --git a/docs/api-contract.md b/docs/api-contract.md index b02b74e..c95f258 100644 --- a/docs/api-contract.md +++ b/docs/api-contract.md @@ -220,6 +220,7 @@ Coverage notes: rows list direct tests when a symbol is named in test names or r | method | TeamService.RemoveMember | `func (s *TeamService) RemoveMember(ctx context.Context, teamID int64, username string) error` | RemoveMember removes a user from a team. | No direct tests. | | method | TeamService.RemoveRepo | `func (s *TeamService) RemoveRepo(ctx context.Context, teamID int64, org, repo string) error` | RemoveRepo removes a repository from a team. | No direct tests. | | method | UserService.Follow | `func (s *UserService) Follow(ctx context.Context, username string) error` | Follow follows a user as the authenticated user. | No direct tests. | +| method | UserService.CheckFollowing | `func (s *UserService) CheckFollowing(ctx context.Context, username, target string) (bool, error)` | CheckFollowing reports whether one user is following another user. | `TestUserService_CheckFollowing_Good`, `TestUserService_CheckFollowing_Bad_NotFound` | | method | UserService.GetCurrent | `func (s *UserService) GetCurrent(ctx context.Context) (*types.User, error)` | GetCurrent returns the authenticated user. | `TestUserService_Good_GetCurrent` | | method | UserService.GetQuota | `func (s *UserService) GetQuota(ctx context.Context) (*types.QuotaInfo, error)` | GetQuota returns the authenticated user's quota information. | `TestUserService_GetQuota_Good` | | method | UserService.ListStopwatches | `func (s *UserService) ListStopwatches(ctx context.Context) ([]types.StopWatch, error)` | ListStopwatches returns all existing stopwatches for the authenticated user. | `TestUserService_ListStopwatches_Good` | diff --git a/users.go b/users.go index 3f676b1..6e9c282 100644 --- a/users.go +++ b/users.go @@ -3,6 +3,7 @@ package forge import ( "context" "iter" + "net/http" "dappco.re/go/core/forge/types" ) @@ -179,6 +180,19 @@ func (s *UserService) Follow(ctx context.Context, username string) error { return s.client.Put(ctx, path, nil, nil) } +// CheckFollowing reports whether one user is following another user. +func (s *UserService) CheckFollowing(ctx context.Context, username, target string) (bool, error) { + path := ResolvePath("/api/v1/users/{username}/following/{target}", pathParams("username", username, "target", target)) + resp, err := s.client.doJSON(ctx, http.MethodGet, path, nil, nil) + if err != nil { + if IsNotFound(err) { + return false, nil + } + return false, err + } + return resp.StatusCode == http.StatusNoContent, nil +} + // Unfollow unfollows a user as the authenticated user. func (s *UserService) Unfollow(ctx context.Context, username string) error { path := ResolvePath("/api/v1/user/following/{username}", pathParams("username", username)) diff --git a/users_test.go b/users_test.go index 5cb55a4..edcf601 100644 --- a/users_test.go +++ b/users_test.go @@ -268,6 +268,50 @@ func TestUserService_Block_Good(t *testing.T) { } } +func TestUserService_CheckFollowing_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/users/alice/following/bob" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + following, err := f.Users.CheckFollowing(context.Background(), "alice", "bob") + if err != nil { + t.Fatal(err) + } + if !following { + t.Fatal("got following=false, want true") + } +} + +func TestUserService_CheckFollowing_Bad_NotFound(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/users/alice/following/bob" { + t.Errorf("wrong path: %s", r.URL.Path) + } + http.Error(w, "not found", http.StatusNotFound) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + following, err := f.Users.CheckFollowing(context.Background(), "alice", "bob") + if err != nil { + t.Fatal(err) + } + if following { + t.Fatal("got following=true, want false") + } +} + func TestUserService_Unblock_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPut {