Skip to content

Commit

Permalink
feat: add user service
Browse files Browse the repository at this point in the history
  • Loading branch information
itschip committed Jul 2, 2023
1 parent 9d05195 commit 1dec627
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 36 deletions.
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Client struct {
Docs DocsService
Socials SocialsService
Announcements AnnouncementService
Users UserService

events map[string][]Event
commands map[string]Command
Expand Down Expand Up @@ -63,6 +64,7 @@ func NewClient(config *Config) *Client {
c.DocComments = &docCommentService{client: c}
c.Socials = &socialsService{client: c}
c.Announcements = &announcementService{client: c}
c.Users = &userService{client: c}

c.events = make(map[string][]Event)

Expand Down
5 changes: 1 addition & 4 deletions guildedgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,5 @@ func TestNewClient(t *testing.T) {

c := NewClient(config)

c.Announcements.GetAnnouncements("123", &GetAnnouncementParams{
Before: "123",
Limit: 1,
})
c.Users.GetUser("me")
}
32 changes: 0 additions & 32 deletions members.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,6 @@ type ServerMemberPermissions struct {
Permissions []string `json:"permissions"`
}

type User struct {
// The ID of the user
Id string `json:"id"`

// The type of user. If this property is absent, it can assumed to be of type user
Type string `json:"type,omitempty"`

Name string `json:"name"`

// The avatar image associated with the user
Avatar string `json:"avatar,omitempty"`

// The banner image associated with the user
Banner string `json:"banner,omitempty"`

// The ISO 8601 timestamp that the user was created at
CreatedAt string `json:"createdAt"`
}

type UserSummary struct {
// The ID of the user
Id string `json:"id"`

// The type of user. If this property is absent, it can assumed to be of type user
Type string `json:"type,omitempty"`

Name string `json:"name"`

// The avatar image associated with the user
Avatar string `json:"avatar,omitempty"`
}

type ServerMemberBan struct {
User UserSummary `json:"user"`

Expand Down
114 changes: 114 additions & 0 deletions user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package guildedgo

type User struct {
// The ID of the user
Id string `json:"id"`

// The type of user. If this property is absent, it can assumed to be of type user
Type string `json:"type,omitempty"`

Name string `json:"name"`

// The avatar image associated with the user
Avatar string `json:"avatar,omitempty"`

// The banner image associated with the user
Banner string `json:"banner,omitempty"`

// The ISO 8601 timestamp that the user was created at
CreatedAt string `json:"createdAt"`

Status UserStatus `json:"status,omitempty"`
}

type UserStatus struct {
Content string `json:"content,omitempty"`
EmoteID int `json:"emoteId"`
}

type UserSummary struct {
// The ID of the user
Id string `json:"id"`

// The type of user. If this property is absent, it can assumed to be of type user
Type string `json:"type,omitempty"`

Name string `json:"name"`

// The avatar image associated with the user
Avatar string `json:"avatar,omitempty"`
}

type UserStatusUpdate struct {
Content string `json:"content,omitempty"`
EmoteID int `json:"emoteId"`
ExpiresAt string `json:"expiresAt,omitempty"`
}

type UserResponse struct {
User `json:"user"`
}

const (
UserTypeUser = "user"
UserTypeBot = "bot"
)

type UserService interface {
GetUser(id string) (*User, error)
GetUsersServers(id string) ([]Server, error)
UpdateStatus(id string, status UserStatusUpdate) error
DeleteStatus(id string) error
}

type userService struct {
client *Client
}

var _ UserService = &userService{}

func (service *userService) GetUser(id string) (*User, error) {
endpoint := guildedApi + "/users/" + id

var response UserResponse
err := service.client.GetRequestV2(endpoint, &response)
if err != nil {
return nil, err
}
return &response.User, nil
}

func (service *userService) GetUsersServers(id string) ([]Server, error) {
endpoint := guildedApi + "/users/" + id + "/servers"

var response struct {
Servers []Server `json:"servers"`
}
err := service.client.GetRequestV2(endpoint, &response)
if err != nil {
return nil, err
}
return response.Servers, nil
}

func (service *userService) UpdateStatus(id string, status UserStatusUpdate) error {
endpoint := guildedApi + "/users/" + id + "/status"

err := service.client.PutRequestV2(endpoint, &status, nil)
if err != nil {
return err
}

return nil
}

func (service *userService) DeleteStatus(id string) error {
endpoint := guildedApi + "/users/" + id + "/status"

_, err := service.client.DeleteRequest(endpoint)
if err != nil {
return err
}

return nil
}

0 comments on commit 1dec627

Please sign in to comment.