Skip to content

Commit

Permalink
feat: Implement Users/Groups
Browse files Browse the repository at this point in the history
Related to #17
  • Loading branch information
yitsushi committed Mar 21, 2022
1 parent 0c1662f commit 01dd04c
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 10 deletions.
12 changes: 12 additions & 0 deletions models/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package models

import "time"

// Group represents a user group.
type Group struct {
ID string `json:"id"`
CreatedAt time.Time `json:"createdAt"`
Name string `json:"name"`
OwnerID string `json:"ownerId"`
UserIDs []string `json:"userIds"`
}
6 changes: 6 additions & 0 deletions services.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/yitsushi/go-misskey/services/notes"
"github.com/yitsushi/go-misskey/services/notifications"
"github.com/yitsushi/go-misskey/services/promo"
"github.com/yitsushi/go-misskey/services/users"
)

func (c *Client) requestHandler(request core.Request, response interface{}) error {
Expand Down Expand Up @@ -78,3 +79,8 @@ func (c *Client) Admin() *admin.Service {
func (c *Client) Following() *following.Service {
return following.NewService(c.requestHandler)
}

// Users contains all endpoints under /users.
func (c *Client) Users() *users.Service {
return users.NewService(c.requestHandler)
}
4 changes: 2 additions & 2 deletions services/admin/announcements/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestService_Create(t *testing.T) {
StatusCode: http.StatusOK,
})

response, err := client.Admin().Accouncements().Create(announcements.CreateRequest{
response, err := client.Admin().Announcements().Create(announcements.CreateRequest{
Title: "title",
Text: "text",
})
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestCreateRequest_Validate(t *testing.T) {
func ExampleService_Create() {
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN"))

response, err := client.Admin().Accouncements().Create(announcements.CreateRequest{
response, err := client.Admin().Announcements().Create(announcements.CreateRequest{
Title: "New Announcement",
Text: "Because we can do it!",
})
Expand Down
4 changes: 2 additions & 2 deletions services/admin/announcements/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestService_Delete(t *testing.T) {
StatusCode: http.StatusNoContent,
})

err := client.Admin().Accouncements().Delete("8d44utwtj6")
err := client.Admin().Announcements().Delete("8d44utwtj6")

assert.NoError(t, err)
}
Expand All @@ -42,7 +42,7 @@ func TestDeleteRequest_Validate(t *testing.T) {
func ExampleService_Delete() {
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN"))

err := client.Admin().Accouncements().Delete("8d44utwtj6")
err := client.Admin().Announcements().Delete("8d44utwtj6")
if err != nil {
log.Printf("[Admin/Announcements] %s", err)

Expand Down
4 changes: 2 additions & 2 deletions services/admin/announcements/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestService_List(t *testing.T) {
StatusCode: http.StatusOK,
})

response, err := client.Admin().Accouncements().List(announcements.ListRequest{
response, err := client.Admin().Announcements().List(announcements.ListRequest{
Limit: 1,
})
if !assert.NoError(t, err) {
Expand All @@ -47,7 +47,7 @@ func TestListRequest_Validate(t *testing.T) {
func ExampleService_List() {
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN"))

response, err := client.Admin().Accouncements().List(announcements.ListRequest{
response, err := client.Admin().Announcements().List(announcements.ListRequest{
Limit: 10,
})
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions services/admin/announcements/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestService_Update(t *testing.T) {
StatusCode: http.StatusNoContent,
})

err := client.Admin().Accouncements().Update(announcements.UpdateRequest{
err := client.Admin().Announcements().Update(announcements.UpdateRequest{
ID: "8d44utwtj6",
Title: "New Title",
Text: "New text",
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestUpdateRequest_Validate(t *testing.T) {
func ExampleService_Update() {
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN"))

err := client.Admin().Accouncements().Update(announcements.UpdateRequest{
err := client.Admin().Announcements().Update(announcements.UpdateRequest{
ID: "8d44utwtj6",
Title: "New Title",
Text: "New text",
Expand Down
4 changes: 2 additions & 2 deletions services/admin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func NewService(requestHandler core.RequestHandlerFunc) *Service {
return &Service{Call: requestHandler}
}

// Accouncements contains all endpoints under /admin/announcements.
func (s *Service) Accouncements() *announcements.Service {
// Announcements contains all endpoints under /admin/announcements.
func (s *Service) Announcements() *announcements.Service {
return announcements.NewService(s.Call)
}

Expand Down
11 changes: 11 additions & 0 deletions services/users/groups/fixtures/joined.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"createdAt": "2020-02-16T18:57:39.746Z",
"id": "93tyd132e7",
"name": "Test",
"ownerId": "83sv4lyx22",
"userIds": [
"83sv4lyx22"
]
}
]
11 changes: 11 additions & 0 deletions services/users/groups/fixtures/owned.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"createdAt": "2020-02-16T18:57:39.746Z",
"id": "93tyd132e7",
"name": "Admin",
"ownerId": "83sv4lyx22",
"userIds": [
"83sv4lyx22"
]
}
]
25 changes: 25 additions & 0 deletions services/users/groups/joined.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package groups

import (
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/models"
)

// JoinedRequest represents an List request.
type JoinedRequest struct{}

// Validate the request.
func (r JoinedRequest) Validate() error {
return nil
}

// Joined clips.
func (s *Service) Joined() ([]models.Group, error) {
var response []models.Group
err := s.Call(
&core.JSONRequest{Request: &JoinedRequest{}, Path: "/users/groups/joined"},
&response,
)

return response, err
}
43 changes: 43 additions & 0 deletions services/users/groups/joined_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package groups_test

import (
"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey"
"github.com/yitsushi/go-misskey/services/users/groups"
"github.com/yitsushi/go-misskey/test"
"log"
"net/http"
"os"
"testing"
)

func TestService_Joined(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/users/groups/joined",
RequestData: &groups.JoinedRequest{},
ResponseFile: "joined.json",
StatusCode: http.StatusOK,
})

resp, err := client.Users().Groups().Joined()
if !assert.NoError(t, err) {
return
}

assert.Len(t, resp, 1)
}

func ExampleService_Joined() {
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN"))

resp, err := client.Users().Groups().Joined()
if err != nil {
log.Printf("[Users/Groups/Joined] %s", err)

return
}

for _, group := range resp {
log.Printf("[Users/Groups/Joined] %s", group.Name)
}
}
25 changes: 25 additions & 0 deletions services/users/groups/owned.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package groups

import (
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/models"
)

// OwnedRequest represents an List request.
type OwnedRequest struct{}

// Validate the request.
func (r OwnedRequest) Validate() error {
return nil
}

// Owned clips.
func (s *Service) Owned() ([]models.Group, error) {
var response []models.Group
err := s.Call(
&core.JSONRequest{Request: &OwnedRequest{}, Path: "/users/groups/owned"},
&response,
)

return response, err
}
43 changes: 43 additions & 0 deletions services/users/groups/owned_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package groups_test

import (
"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey"
"github.com/yitsushi/go-misskey/services/users/groups"
"github.com/yitsushi/go-misskey/test"
"log"
"net/http"
"os"
"testing"
)

func TestService_Owned(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/users/groups/owned",
RequestData: &groups.OwnedRequest{},
ResponseFile: "owned.json",
StatusCode: http.StatusOK,
})

resp, err := client.Users().Groups().Owned()
if !assert.NoError(t, err) {
return
}

assert.Len(t, resp, 1)
}

func ExampleService_Owned() {
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN"))

resp, err := client.Users().Groups().Owned()
if err != nil {
log.Printf("[Users/Groups/Owned] %s", err)

return
}

for _, group := range resp {
log.Printf("[Users/Groups/Owned] %s", group.Name)
}
}
15 changes: 15 additions & 0 deletions services/users/groups/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package groups

import (
"github.com/yitsushi/go-misskey/core"
)

// Service is the base for all the endpoints on this service.
type Service struct {
Call core.RequestHandlerFunc
}

// NewService creates a new Service instance.
func NewService(requestHandler core.RequestHandlerFunc) *Service {
return &Service{Call: requestHandler}
}
21 changes: 21 additions & 0 deletions services/users/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package users

import (
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/services/users/groups"
)

// Service is the base for all the endpoints on this service.
type Service struct {
Call core.RequestHandlerFunc
}

// NewService creates a new Service instance.
func NewService(requestHandler core.RequestHandlerFunc) *Service {
return &Service{Call: requestHandler}
}

// Groups contains all endpoints under /users/groups.
func (s *Service) Groups() *groups.Service {
return groups.NewService(s.Call)
}

0 comments on commit 01dd04c

Please sign in to comment.