users_blocking.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2017 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // ListBlockedUsers lists all the blocked users by the authenticated user.
  11. //
  12. // GitHub API docs: https://developer.github.com/v3/users/blocking/#list-blocked-users
  13. func (s *UsersService) ListBlockedUsers(ctx context.Context, opt *ListOptions) ([]*User, *Response, error) {
  14. u := "user/blocks"
  15. u, err := addOptions(u, opt)
  16. if err != nil {
  17. return nil, nil, err
  18. }
  19. req, err := s.client.NewRequest("GET", u, nil)
  20. if err != nil {
  21. return nil, nil, err
  22. }
  23. // TODO: remove custom Accept header when this API fully launches.
  24. req.Header.Set("Accept", mediaTypeBlockUsersPreview)
  25. var blockedUsers []*User
  26. resp, err := s.client.Do(ctx, req, &blockedUsers)
  27. if err != nil {
  28. return nil, resp, err
  29. }
  30. return blockedUsers, resp, nil
  31. }
  32. // IsBlocked reports whether specified user is blocked by the authenticated user.
  33. //
  34. // GitHub API docs: https://developer.github.com/v3/users/blocking/#check-whether-youve-blocked-a-user
  35. func (s *UsersService) IsBlocked(ctx context.Context, user string) (bool, *Response, error) {
  36. u := fmt.Sprintf("user/blocks/%v", user)
  37. req, err := s.client.NewRequest("GET", u, nil)
  38. if err != nil {
  39. return false, nil, err
  40. }
  41. // TODO: remove custom Accept header when this API fully launches.
  42. req.Header.Set("Accept", mediaTypeBlockUsersPreview)
  43. resp, err := s.client.Do(ctx, req, nil)
  44. isBlocked, err := parseBoolResponse(err)
  45. return isBlocked, resp, err
  46. }
  47. // BlockUser blocks specified user for the authenticated user.
  48. //
  49. // GitHub API docs: https://developer.github.com/v3/users/blocking/#block-a-user
  50. func (s *UsersService) BlockUser(ctx context.Context, user string) (*Response, error) {
  51. u := fmt.Sprintf("user/blocks/%v", user)
  52. req, err := s.client.NewRequest("PUT", u, nil)
  53. if err != nil {
  54. return nil, err
  55. }
  56. // TODO: remove custom Accept header when this API fully launches.
  57. req.Header.Set("Accept", mediaTypeBlockUsersPreview)
  58. return s.client.Do(ctx, req, nil)
  59. }
  60. // UnblockUser unblocks specified user for the authenticated user.
  61. //
  62. // GitHub API docs: https://developer.github.com/v3/users/blocking/#unblock-a-user
  63. func (s *UsersService) UnblockUser(ctx context.Context, user string) (*Response, error) {
  64. u := fmt.Sprintf("user/blocks/%v", user)
  65. req, err := s.client.NewRequest("DELETE", u, nil)
  66. if err != nil {
  67. return nil, err
  68. }
  69. // TODO: remove custom Accept header when this API fully launches.
  70. req.Header.Set("Accept", mediaTypeBlockUsersPreview)
  71. return s.client.Do(ctx, req, nil)
  72. }