Refactoring-1811
This commit is contained in:
@ -12,12 +12,14 @@ import (
|
||||
"github.com/go-redis/redis"
|
||||
"github.com/urfave/cli"
|
||||
"github.com/yukimochi/Activity-Relay/KeyLoader"
|
||||
"github.com/yukimochi/Activity-Relay/RelayConf"
|
||||
)
|
||||
|
||||
var hostname *url.URL
|
||||
var hostkey *rsa.PrivateKey
|
||||
var redClient *redis.Client
|
||||
var macServer *machinery.Server
|
||||
var relConfig relayconf.RelayConfig
|
||||
|
||||
func main() {
|
||||
pemPath := os.Getenv("ACTOR_PEM")
|
||||
|
36
cli/cli_test.go
Normal file
36
cli/cli_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/RichardKnop/machinery/v1"
|
||||
"github.com/RichardKnop/machinery/v1/config"
|
||||
"github.com/go-redis/redis"
|
||||
"github.com/yukimochi/Activity-Relay/KeyLoader"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
os.Setenv("ACTOR_PEM", "misc/testKey.pem")
|
||||
os.Setenv("RELAY_DOMAIN", "relay.yukimochi.example.org")
|
||||
pemPath := os.Getenv("ACTOR_PEM")
|
||||
relayDomain := os.Getenv("RELAY_DOMAIN")
|
||||
redisURL := os.Getenv("REDIS_URL")
|
||||
hostkey, _ = keyloader.ReadPrivateKeyRSAfromPath(pemPath)
|
||||
hostname, _ = url.Parse("https://" + relayDomain)
|
||||
redClient = redis.NewClient(&redis.Options{
|
||||
Addr: redisURL,
|
||||
})
|
||||
var macConfig = &config.Config{
|
||||
Broker: "redis://" + redisURL,
|
||||
DefaultQueue: "relay",
|
||||
ResultBackend: "redis://" + redisURL,
|
||||
ResultsExpireIn: 5,
|
||||
}
|
||||
macServer, _ = machinery.NewServer(macConfig)
|
||||
redClient.FlushAll().Result()
|
||||
relConfig.Load(redClient)
|
||||
code := m.Run()
|
||||
os.Exit(code)
|
||||
}
|
@ -7,40 +7,46 @@ import (
|
||||
"github.com/yukimochi/Activity-Relay/RelayConf"
|
||||
)
|
||||
|
||||
const (
|
||||
BlockService relayconf.Config = iota
|
||||
ManuallyAccept
|
||||
CreateAsAnnounce
|
||||
)
|
||||
|
||||
func serviceBlock(c *cli.Context) {
|
||||
if c.Bool("undo") {
|
||||
relayconf.SetConfig(redClient, "block_service", false)
|
||||
relConfig.Set(redClient, BlockService, false)
|
||||
fmt.Println("Blocking for service-type actor is Disabled.")
|
||||
} else {
|
||||
relayconf.SetConfig(redClient, "block_service", true)
|
||||
relConfig.Set(redClient, BlockService, true)
|
||||
fmt.Println("Blocking for service-type actor is Enabled.")
|
||||
}
|
||||
}
|
||||
|
||||
func manuallyAccept(c *cli.Context) {
|
||||
if c.Bool("undo") {
|
||||
relayconf.SetConfig(redClient, "manually_accept", false)
|
||||
relConfig.Set(redClient, ManuallyAccept, false)
|
||||
fmt.Println("Manually accept follow-request is Disabled.")
|
||||
} else {
|
||||
relayconf.SetConfig(redClient, "manually_accept", true)
|
||||
relConfig.Set(redClient, ManuallyAccept, true)
|
||||
fmt.Println("Manually accept follow-request is Enabled.")
|
||||
}
|
||||
}
|
||||
|
||||
func createAsAnnounce(c *cli.Context) {
|
||||
if c.Bool("undo") {
|
||||
relayconf.SetConfig(redClient, "create_as_announce", false)
|
||||
relConfig.Set(redClient, CreateAsAnnounce, false)
|
||||
fmt.Println("Announce activity instead of relay create activity is Disabled.")
|
||||
} else {
|
||||
relayconf.SetConfig(redClient, "create_as_announce", true)
|
||||
relConfig.Set(redClient, CreateAsAnnounce, true)
|
||||
fmt.Println("Announce activity instead of relay create activity is Enabled.")
|
||||
}
|
||||
}
|
||||
|
||||
func listConfigs(c *cli.Context) {
|
||||
config := relayconf.LoadConfig(redClient)
|
||||
relConfig.Load(redClient)
|
||||
|
||||
fmt.Println("Blocking for service-type actor : ", config.BlockService)
|
||||
fmt.Println("Manually accept follow-request : ", config.ManuallyAccept)
|
||||
fmt.Println("Announce activity instead of relay create activity : ", config.CreateAsAnnounce)
|
||||
fmt.Println("Blocking for service-type actor : ", relConfig.BlockService)
|
||||
fmt.Println("Manually accept follow-request : ", relConfig.ManuallyAccept)
|
||||
fmt.Println("Announce activity instead of relay create activity : ", relConfig.CreateAsAnnounce)
|
||||
}
|
||||
|
156
cli/config_test.go
Normal file
156
cli/config_test.go
Normal file
@ -0,0 +1,156 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kami-zh/go-capturer"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func TestServiceBlock(t *testing.T) {
|
||||
app := cli.NewApp()
|
||||
fooCmd := cli.Command{
|
||||
Name: "service-block",
|
||||
Usage: "Enable blocking for service-type actor",
|
||||
Flags: []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "undo, u",
|
||||
Usage: "Undo block",
|
||||
},
|
||||
},
|
||||
Action: serviceBlock,
|
||||
}
|
||||
app.Commands = []cli.Command{
|
||||
fooCmd,
|
||||
}
|
||||
|
||||
relConfig.Set(redClient, BlockService, false)
|
||||
app.Run([]string{"", "service-block"})
|
||||
if !relConfig.BlockService {
|
||||
t.Fatalf("Not Enabled ServiceBlock feature,")
|
||||
}
|
||||
|
||||
app.Run([]string{"", "service-block", "-u"})
|
||||
if relConfig.BlockService {
|
||||
t.Fatalf("Not Disabled ServiceBlock feature,")
|
||||
}
|
||||
}
|
||||
|
||||
func TestManuallyAccept(t *testing.T) {
|
||||
app := cli.NewApp()
|
||||
fooCmd := cli.Command{
|
||||
Name: "manually-accept",
|
||||
Usage: "Enable Manually accept follow-request",
|
||||
Flags: []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "undo, u",
|
||||
Usage: "Undo block",
|
||||
},
|
||||
},
|
||||
Action: manuallyAccept,
|
||||
}
|
||||
app.Commands = []cli.Command{
|
||||
fooCmd,
|
||||
}
|
||||
|
||||
relConfig.Set(redClient, ManuallyAccept, false)
|
||||
app.Run([]string{"", "manually-accept"})
|
||||
if !relConfig.ManuallyAccept {
|
||||
t.Fatalf("Not Enabled Manually accept follow-request feature,")
|
||||
}
|
||||
|
||||
app.Run([]string{"", "manually-accept", "-u"})
|
||||
if relConfig.ManuallyAccept {
|
||||
t.Fatalf("Not Disabled Manually accept follow-request feature,")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateAsAnnounce(t *testing.T) {
|
||||
app := cli.NewApp()
|
||||
fooCmd := cli.Command{
|
||||
Name: "create-as-announce",
|
||||
Usage: "Enable Announce activity instead of relay create activity (Not recommended)",
|
||||
Flags: []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "undo, u",
|
||||
Usage: "Undo block",
|
||||
},
|
||||
},
|
||||
Action: createAsAnnounce,
|
||||
}
|
||||
app.Commands = []cli.Command{
|
||||
fooCmd,
|
||||
}
|
||||
|
||||
relConfig.Set(redClient, CreateAsAnnounce, false)
|
||||
app.Run([]string{"", "create-as-announce"})
|
||||
if !relConfig.CreateAsAnnounce {
|
||||
t.Fatalf("Not Enabled Announce activity instead of relay create activity feature,")
|
||||
}
|
||||
|
||||
app.Run([]string{"", "create-as-announce", "-u"})
|
||||
if relConfig.CreateAsAnnounce {
|
||||
t.Fatalf("Not Disabled Announce activity instead of relay create activity feature,")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListConfigs(t *testing.T) {
|
||||
app := cli.NewApp()
|
||||
fooCmd := cli.Command{
|
||||
Name: "show",
|
||||
Usage: "Show all relay configrations",
|
||||
Action: listConfigs,
|
||||
}
|
||||
app.Commands = []cli.Command{
|
||||
fooCmd,
|
||||
}
|
||||
|
||||
relConfig.Set(redClient, BlockService, true)
|
||||
relConfig.Set(redClient, ManuallyAccept, true)
|
||||
relConfig.Set(redClient, CreateAsAnnounce, true)
|
||||
out := capturer.CaptureStdout(func() {
|
||||
app.Run([]string{"", "show"})
|
||||
})
|
||||
|
||||
for _, row := range strings.Split(out, "\n") {
|
||||
switch strings.Split(row, ":")[0] {
|
||||
case "Blocking for service-type actor ":
|
||||
if !(strings.Split(row, ":")[1] == " true") {
|
||||
t.Fatalf(strings.Split(row, ":")[1])
|
||||
}
|
||||
case "Manually accept follow-request ":
|
||||
if !(strings.Split(row, ":")[1] == " true") {
|
||||
t.Fatalf("Invalid Responce.")
|
||||
}
|
||||
case "Announce activity instead of relay create activity ":
|
||||
if !(strings.Split(row, ":")[1] == " true") {
|
||||
t.Fatalf("Invalid Responce.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
relConfig.Set(redClient, BlockService, false)
|
||||
relConfig.Set(redClient, ManuallyAccept, false)
|
||||
relConfig.Set(redClient, CreateAsAnnounce, false)
|
||||
out = capturer.CaptureStdout(func() {
|
||||
app.Run([]string{"", "show"})
|
||||
})
|
||||
|
||||
for _, row := range strings.Split(out, "\n") {
|
||||
switch strings.Split(row, ":")[0] {
|
||||
case "Blocking for service-type actor ":
|
||||
if !(strings.Split(row, ":")[1] == " false") {
|
||||
t.Fatalf("Invalid Responce.")
|
||||
}
|
||||
case "Manually accept follow-request ":
|
||||
if !(strings.Split(row, ":")[1] == " false") {
|
||||
t.Fatalf("Invalid Responce.")
|
||||
}
|
||||
case "Announce activity instead of relay create activity ":
|
||||
if !(strings.Split(row, ":")[1] == " false") {
|
||||
t.Fatalf("Invalid Responce.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -3,9 +3,7 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/RichardKnop/machinery/v1/tasks"
|
||||
"github.com/urfave/cli"
|
||||
@ -25,7 +23,7 @@ func pushRegistorJob(inboxURL string, body []byte) {
|
||||
{
|
||||
Name: "body",
|
||||
Type: "string",
|
||||
Value: *(*string)(unsafe.Pointer(&body)),
|
||||
Value: string(body),
|
||||
},
|
||||
},
|
||||
}
|
||||
@ -80,8 +78,7 @@ func acceptFollow(c *cli.Context) error {
|
||||
nil,
|
||||
}
|
||||
|
||||
actorDomain, _ := url.Parse(activity.Actor)
|
||||
resp := activitypub.GenerateActivityResponse(hostname, actorDomain, "Accept", activity)
|
||||
resp := activity.GenerateResponse(hostname, "Accept")
|
||||
jsonData, _ := json.Marshal(&resp)
|
||||
|
||||
pushRegistorJob(data["inbox_url"], jsonData)
|
||||
@ -121,8 +118,7 @@ func rejectFollow(c *cli.Context) error {
|
||||
nil,
|
||||
}
|
||||
|
||||
actorDomain, _ := url.Parse(activity.Actor)
|
||||
resp := activitypub.GenerateActivityResponse(hostname, actorDomain, "Reject", activity)
|
||||
resp := activity.GenerateResponse(hostname, "Reject")
|
||||
jsonData, _ := json.Marshal(&resp)
|
||||
|
||||
pushRegistorJob(data["inbox_url"], jsonData)
|
||||
|
Reference in New Issue
Block a user