diff --git a/cli/domain.go b/cli/domain.go index e5acff7..c49afb7 100644 --- a/cli/domain.go +++ b/cli/domain.go @@ -1,9 +1,12 @@ package main import ( + "encoding/json" "fmt" "github.com/spf13/cobra" + activitypub "github.com/yukimochi/Activity-Relay/ActivityPub" + state "github.com/yukimochi/Activity-Relay/State" ) func domainCmdInit() *cobra.Command { @@ -34,9 +37,33 @@ func domainCmdInit() *cobra.Command { domainSet.Flags().BoolP("undo", "u", false, "Unset domain as limited or blocked") domain.AddCommand(domainSet) + var domainUnfollow = &cobra.Command{ + Use: "unfollow [flags]", + Short: "Send Unfollow request for given domains", + Long: "Send unfollow request for given domains.", + RunE: unfollowDomains, + } + domain.AddCommand(domainUnfollow) + return domain } +func createUnfollowRequestResponse(subscription state.Subscription) error { + activity := activitypub.Activity{ + Context: []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"}, + ID: subscription.ActivityID, + Actor: subscription.ActorID, + Type: "Follow", + Object: "https://www.w3.org/ns/activitystreams#Public", + } + + resp := activity.GenerateResponse(hostname, "Reject") + jsonData, _ := json.Marshal(&resp) + pushRegistorJob(subscription.InboxURL, jsonData) + + return nil +} + func listDomains(cmd *cobra.Command, args []string) error { var domains []string switch cmd.Flag("type").Value.String() { @@ -88,3 +115,20 @@ func setDomainType(cmd *cobra.Command, args []string) error { return nil } + +func unfollowDomains(cmd *cobra.Command, args []string) error { + subscriptions := relayState.Subscriptions + for _, domain := range args { + for _, subscription := range subscriptions { + if domain == subscription.Domain { + cmd.Println("Unfollow [" + domain + "]") + createUnfollowRequestResponse(subscription) + relayState.DelSubscription(subscription.Domain) + break + } + fmt.Println("Invalid domain given") + } + } + + return nil +} diff --git a/cli/domain_test.go b/cli/domain_test.go index 17affc9..9156b0f 100644 --- a/cli/domain_test.go +++ b/cli/domain_test.go @@ -191,3 +191,30 @@ func TestSetDomainInvalid(t *testing.T) { relayState.RedisClient.FlushAll().Result() relayState.Load() } + +func TestUnfollowDomain(t *testing.T) { + app := buildNewCmd() + + app.SetArgs([]string{"config", "import", "--json", "../misc/exampleConfig.json"}) + app.Execute() + + buffer := new(bytes.Buffer) + app.SetOutput(buffer) + + app.SetArgs([]string{"domain", "unfollow", "subscription.example.jp"}) + app.Execute() + + valid := true + for _, domain := range relayState.LimitedDomains { + if domain == "subscription.example.jp" { + valid = false + } + } + + if !valid { + t.Fatalf("Do not unfollow domain") + } + + relayState.RedisClient.FlushAll().Result() + relayState.Load() +} diff --git a/cli/follow.go b/cli/follow.go index 8041e54..0e29619 100644 --- a/cli/follow.go +++ b/cli/follow.go @@ -161,8 +161,8 @@ func rejectFollow(cmd *cobra.Command, args []string) error { createFollowRequestResponse(domain, "Reject") break } + cmd.Println("Invalid domain given") } - cmd.Println("Invalid domain given") } return nil