-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement Admin/Moderators (#87)
Closes #64
- Loading branch information
Showing
7 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package moderators | ||
|
||
import ( | ||
"github.com/yitsushi/go-misskey/core" | ||
) | ||
|
||
// AddRequest represents an Add moderator request. | ||
type AddRequest struct { | ||
UserID string `json:"userId"` | ||
} | ||
|
||
// Validate the request. | ||
func (r AddRequest) Validate() error { | ||
if r.UserID == "" { | ||
return core.RequestValidationError{ | ||
Request: r, | ||
Message: core.UndefinedRequiredField, | ||
Field: "UserID", | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Add a moderator. | ||
func (s *Service) Add(userID string) error { | ||
request := AddRequest{UserID: userID} | ||
err := s.Call( | ||
&core.JSONRequest{Request: &request, Path: "/admin/moderators/add"}, | ||
&core.EmptyResponse{}, | ||
) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package moderators_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/admin/moderators" | ||
"github.com/yitsushi/go-misskey/test" | ||
) | ||
|
||
func TestService_Add(t *testing.T) { | ||
client := test.MakeMockClient(test.SimpleMockOptions{ | ||
Endpoint: "/api/admin/moderators/add", | ||
RequestData: &moderators.AddRequest{}, | ||
ResponseFile: "empty", | ||
StatusCode: http.StatusNoContent, | ||
}) | ||
|
||
err := client.Admin().Moderators().Add("88v9vu5nbu") | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func TestAddRequest_Validate(t *testing.T) { | ||
test.ValidateRequests( | ||
t, | ||
[]core.BaseRequest{ | ||
moderators.AddRequest{}, | ||
}, | ||
[]core.BaseRequest{ | ||
moderators.AddRequest{UserID: "asd"}, | ||
}, | ||
) | ||
} | ||
|
||
func ExampleService_Add() { | ||
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN")) | ||
|
||
err := client.Admin().Moderators().Add("88v9vu5nbu") | ||
if err != nil { | ||
log.Printf("[Admin/Moderators/Add] %s", err) | ||
|
||
return | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package moderators | ||
|
||
import ( | ||
"github.com/yitsushi/go-misskey/core" | ||
) | ||
|
||
// RemoveRequest represents an Remove moderator request. | ||
type RemoveRequest struct { | ||
UserID string `json:"userId"` | ||
} | ||
|
||
// Validate the request. | ||
func (r RemoveRequest) Validate() error { | ||
if r.UserID == "" { | ||
return core.RequestValidationError{ | ||
Request: r, | ||
Message: core.UndefinedRequiredField, | ||
Field: "UserID", | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Remove a moderator. | ||
func (s *Service) Remove(userID string) error { | ||
request := RemoveRequest{UserID: userID} | ||
err := s.Call( | ||
&core.JSONRequest{Request: &request, Path: "/admin/moderators/remove"}, | ||
&core.EmptyResponse{}, | ||
) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package moderators_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/admin/moderators" | ||
"github.com/yitsushi/go-misskey/test" | ||
) | ||
|
||
func TestService_Remove(t *testing.T) { | ||
client := test.MakeMockClient(test.SimpleMockOptions{ | ||
Endpoint: "/api/admin/moderators/remove", | ||
RequestData: &moderators.RemoveRequest{}, | ||
ResponseFile: "empty", | ||
StatusCode: http.StatusNoContent, | ||
}) | ||
|
||
err := client.Admin().Moderators().Remove("88v9vu5nbu") | ||
|
||
assert.NoError(t, err) | ||
} | ||
|
||
func TestRemoveRequest_Validate(t *testing.T) { | ||
test.ValidateRequests( | ||
t, | ||
[]core.BaseRequest{ | ||
moderators.RemoveRequest{}, | ||
}, | ||
[]core.BaseRequest{ | ||
moderators.RemoveRequest{UserID: "asd"}, | ||
}, | ||
) | ||
} | ||
|
||
func ExampleService_Remove() { | ||
client := misskey.NewClient("https://slippy.xyz", os.Getenv("MISSKEY_TOKEN")) | ||
|
||
err := client.Admin().Moderators().Remove("88v9vu5nbu") | ||
if err != nil { | ||
log.Printf("[Admin/Moderators/Remove] %s", err) | ||
|
||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package moderators | ||
|
||
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters