From 1dec6272a984700dd6622f721329242c569300bc Mon Sep 17 00:00:00 2001 From: itschip Date: Sun, 2 Jul 2023 02:27:32 +0200 Subject: [PATCH] feat: add user service --- client.go | 2 + guildedgo_test.go | 5 +- members.go | 32 ------------- user.go | 114 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 36 deletions(-) create mode 100644 user.go diff --git a/client.go b/client.go index cd4f48d..ee8df93 100644 --- a/client.go +++ b/client.go @@ -26,6 +26,7 @@ type Client struct { Docs DocsService Socials SocialsService Announcements AnnouncementService + Users UserService events map[string][]Event commands map[string]Command @@ -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) diff --git a/guildedgo_test.go b/guildedgo_test.go index 57eca81..9a0ec4b 100644 --- a/guildedgo_test.go +++ b/guildedgo_test.go @@ -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") } diff --git a/members.go b/members.go index b6b53e3..0f08f86 100644 --- a/members.go +++ b/members.go @@ -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"` diff --git a/user.go b/user.go new file mode 100644 index 0000000..a20c753 --- /dev/null +++ b/user.go @@ -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 +}