Skip to content

Commit

Permalink
feat: Implement Following endpoints (#84)
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
yitsushi committed Mar 19, 2022
1 parent ad3e552 commit c6122d0
Show file tree
Hide file tree
Showing 22 changed files with 756 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Check the `docs` directory for more information.
| :x: | [charts](https://misskey.io/api-doc#tag/charts) | [#7](https://github.com/yitsushi/go-misskey/issues/7) ||
| :white_check_mark: | [clips](https://misskey.io/api-doc#tag/clips) | [#8](https://github.com/yitsushi/go-misskey/issues/8) ||
| :white_check_mark: | [drive](https://misskey.io/api-doc#tag/drive) | [#9](https://github.com/yitsushi/go-misskey/issues/9) ||
| :x: | [following](https://misskey.io/api-doc#tag/following) | [#10](https://github.com/yitsushi/go-misskey/issues/10) ||
| :white_check_mark: | [following](https://misskey.io/api-doc#tag/following) | [#10](https://github.com/yitsushi/go-misskey/issues/10) ||
| :x: | [games](https://misskey.io/api-doc#tag/games) | [#11](https://github.com/yitsushi/go-misskey/issues/11) ||
| :white_check_mark: | [hashtags](https://misskey.io/api-doc#tag/hashtags) | [#12](https://github.com/yitsushi/go-misskey/issues/12) ||
| :x: | [messaging](https://misskey.io/api-doc#tag/messaging) | [#13](https://github.com/yitsushi/go-misskey/issues/13) ||
Expand Down
6 changes: 6 additions & 0 deletions services.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/yitsushi/go-misskey/services/clips"
"github.com/yitsushi/go-misskey/services/drive"
"github.com/yitsushi/go-misskey/services/federation"
"github.com/yitsushi/go-misskey/services/following"
"github.com/yitsushi/go-misskey/services/hashtags"
"github.com/yitsushi/go-misskey/services/meta"
"github.com/yitsushi/go-misskey/services/notes"
Expand Down Expand Up @@ -72,3 +73,8 @@ func (c *Client) Promo() *promo.Service {
func (c *Client) Admin() *admin.Service {
return admin.NewService(c.requestHandler)
}

// Following contains all endpoints under /following.
func (c *Client) Following() *following.Service {
return following.NewService(c.requestHandler)
}
38 changes: 38 additions & 0 deletions services/following/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package following

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

// CreateRequest is the request structure to create a following.
type CreateRequest struct {
UserID string `json:"userId"`
}

// Validate request.
func (r CreateRequest) Validate() error {
if r.UserID == "" {
return core.RequestValidationError{
Request: r,
Message: core.UndefinedRequiredField,
Field: "UserID",
}
}

return nil
}

// Create following endpoint.
func (s *Service) Create(userID string) (models.User, error) {
var response models.User

request := &CreateRequest{UserID: userID}

err := s.Call(
&core.JSONRequest{Request: request, Path: "/following/create"},
&response,
)

return response, err
}
56 changes: 56 additions & 0 deletions services/following/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package following_test

import (
"log"
"net/http"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey"
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/services/following"
"github.com/yitsushi/go-misskey/test"
)

func TestService_Create(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/following/create",
RequestData: &following.CreateRequest{},
ResponseFile: "user.json",
StatusCode: http.StatusOK,
})

user, err := client.Following().Create("88v9vu5nbu")
if !assert.NoError(t, err) {
return
}

assert.Equal(t, "88v9vu5nbu", user.ID)
}

func TestCreateRequest_Validate(t *testing.T) {
test.ValidateRequests(
t,
[]core.BaseRequest{
following.CreateRequest{},
following.CreateRequest{UserID: ""},
},
[]core.BaseRequest{
following.CreateRequest{UserID: "88v9vu5nbu"},
},
)
}

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

user, err := client.Following().Create("88v9vu5nbu")
if err != nil {
log.Printf("[Following/Create] %s", err)

return
}

log.Printf("[Following/Create] %s", user.Username)
}
38 changes: 38 additions & 0 deletions services/following/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package following

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

// DeleteRequest is the request structure to delete a following.
type DeleteRequest struct {
UserID string `json:"userId"`
}

// Validate request.
func (r DeleteRequest) Validate() error {
if r.UserID == "" {
return core.RequestValidationError{
Request: r,
Message: core.UndefinedRequiredField,
Field: "UserID",
}
}

return nil
}

// Delete following endpoint.
func (s *Service) Delete(userID string) (models.User, error) {
var response models.User

request := &DeleteRequest{UserID: userID}

err := s.Call(
&core.JSONRequest{Request: request, Path: "/following/delete"},
&response,
)

return response, err
}
56 changes: 56 additions & 0 deletions services/following/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package following_test

import (
"log"
"net/http"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey"
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/services/following"
"github.com/yitsushi/go-misskey/test"
)

func TestService_Delete(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/following/delete",
RequestData: &following.DeleteRequest{},
ResponseFile: "user.json",
StatusCode: http.StatusOK,
})

user, err := client.Following().Delete("88v9vu5nbu")
if !assert.NoError(t, err) {
return
}

assert.Equal(t, "88v9vu5nbu", user.ID)
}

func TestDeleteRequest_Validate(t *testing.T) {
test.ValidateRequests(
t,
[]core.BaseRequest{
following.DeleteRequest{},
following.DeleteRequest{UserID: ""},
},
[]core.BaseRequest{
following.DeleteRequest{UserID: "88v9vu5nbu"},
},
)
}

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

user, err := client.Following().Delete("88v9vu5nbu")
if err != nil {
log.Printf("[Following/Delete] %s", err)

return
}

log.Printf("[Following/Delete] %s", user.Username)
}
12 changes: 12 additions & 0 deletions services/following/fixtures/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "88v9vu5nbu",
"name": null,
"username": "kiki_test",
"host": null,
"avatarUrl": "https://slippy.xyz/identicon/88v9vu5nbu",
"avatarBlurhash": null,
"avatarColor": null,
"isBot": true,
"emojis": [],
"onlineStatus": "unknown"
}
38 changes: 38 additions & 0 deletions services/following/invalidate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package following

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

// InvalidateRequest is the request structure to invalidate a following.
type InvalidateRequest struct {
UserID string `json:"userId"`
}

// Validate request.
func (r InvalidateRequest) Validate() error {
if r.UserID == "" {
return core.RequestValidationError{
Request: r,
Message: core.UndefinedRequiredField,
Field: "UserID",
}
}

return nil
}

// Invalidate following endpoint.
func (s *Service) Invalidate(userID string) (models.User, error) {
var response models.User

request := &InvalidateRequest{UserID: userID}

err := s.Call(
&core.JSONRequest{Request: request, Path: "/following/invalidate"},
&response,
)

return response, err
}
56 changes: 56 additions & 0 deletions services/following/invalidate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package following_test

import (
"log"
"net/http"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/yitsushi/go-misskey"
"github.com/yitsushi/go-misskey/core"
"github.com/yitsushi/go-misskey/services/following"
"github.com/yitsushi/go-misskey/test"
)

func TestService_Invalidate(t *testing.T) {
client := test.MakeMockClient(test.SimpleMockOptions{
Endpoint: "/api/following/invalidate",
RequestData: &following.InvalidateRequest{},
ResponseFile: "user.json",
StatusCode: http.StatusOK,
})

user, err := client.Following().Invalidate("88v9vu5nbu")
if !assert.NoError(t, err) {
return
}

assert.Equal(t, "88v9vu5nbu", user.ID)
}

func TestInvalidateRequest_Validate(t *testing.T) {
test.ValidateRequests(
t,
[]core.BaseRequest{
following.InvalidateRequest{},
following.InvalidateRequest{UserID: ""},
},
[]core.BaseRequest{
following.InvalidateRequest{UserID: "88v9vu5nbu"},
},
)
}

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

user, err := client.Following().Invalidate("88v9vu5nbu")
if err != nil {
log.Printf("[Following/Invalidate] %s", err)

return
}

log.Printf("[Following/Invalidate] %s", user.Username)
}
35 changes: 35 additions & 0 deletions services/following/requests/accept.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package requests

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

// AcceptRequest is the request structure to accept a following request.
type AcceptRequest struct {
UserID string `json:"userId"`
}

// Validate request.
func (r AcceptRequest) Validate() error {
if r.UserID == "" {
return core.RequestValidationError{
Request: r,
Message: core.UndefinedRequiredField,
Field: "UserID",
}
}

return nil
}

// Accept following endpoint.
func (s *Service) Accept(userID string) error {
request := &AcceptRequest{UserID: userID}

err := s.Call(
&core.JSONRequest{Request: request, Path: "/following/requests/accept"},
&core.EmptyResponse{},
)

return err
}
Loading

0 comments on commit c6122d0

Please sign in to comment.