Skip to content

Commit

Permalink
feat: implements RelayStateV2, fix RelayConfigV2
Browse files Browse the repository at this point in the history
  • Loading branch information
yukimochi committed Mar 21, 2024
1 parent 3e2eb60 commit 8201080
Show file tree
Hide file tree
Showing 5 changed files with 466 additions and 38 deletions.
16 changes: 16 additions & 0 deletions models/config_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/redis/go-redis/v9"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/yukimochi/machinery-v1/v1"
machineryConfig "github.com/yukimochi/machinery-v1/v1/config"
)

type ServerConfig struct {
Expand Down Expand Up @@ -176,3 +178,17 @@ func (config *RelayConfigV2) NewRedisClient(ctx context.Context) (*redis.Client,
}
return redisClient, nil
}

func (config *RelayConfigV2) NewMachineryServer() (*machinery.Server, error) {
cnf := &machineryConfig.Config{
Broker: config.redisOptions.Addr,
DefaultQueue: "relay",
ResultBackend: config.redisOptions.Addr,
ResultsExpireIn: 1,
}
server, err := machinery.NewServer(cnf)
if err != nil {
return nil, err
}
return server, nil
}
28 changes: 14 additions & 14 deletions models/config_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,37 @@ func TestNewRelayConfigV2(t *testing.T) {

// ServerConfig
if relayConfig.serverConfig.Domain.Host != "relay.toot.yukimochi.jp" {
t.Error("fail - parse: RelayConfig.serverConfig.Domain")
t.Error("fail - parse: RelayOption.serverConfig.Domain")
}
if relayConfig.serverConfig.Bind != "0.0.0.0:8080" {
t.Error("fail - parse: RelayConfig.serverConfig.Bind")
t.Error("fail - parse: RelayOption.serverConfig.Bind")
}
if relayConfig.serverConfig.PrivateKey == nil {
t.Error("fail - parse: RelayConfig.serverConfig.PrivateKey")
t.Error("fail - parse: RelayOption.serverConfig.PrivateKey")
}

// ServiceConfig
if relayConfig.serviceConfig.Name != "YUKIMOCHI Toot Relay Service" {
t.Error("fail - parse: RelayConfig.serviceConfig.Name")
t.Error("fail - parse: RelayOption.serviceConfig.Name")
}
if relayConfig.serviceConfig.Summary != "YUKIMOCHI Toot Relay Service is Running by Activity-Relay" {
t.Error("fail - parse: RelayConfig.serviceConfig.Summary")
t.Error("fail - parse: RelayOption.serviceConfig.Summary")
}
if relayConfig.serviceConfig.IconURL.String() != "https://example.com/example_icon.png" {
t.Error("fail - parse: RelayConfig.serviceConfig.IconURL")
t.Error("fail - parse: RelayOption.serviceConfig.IconURL")
}
if relayConfig.serviceConfig.ImageURL.String() != "https://example.com/example_image.png" {
t.Error("fail - parse: RelayConfig.serviceConfig.ImageURL")
t.Error("fail - parse: RelayOption.serviceConfig.ImageURL")
}

// RedisOptions
if relayConfig.redisOptions == nil {
t.Error("fail - parse: RelayConfig.redisOptions")
t.Error("fail - parse: RelayOption.redisOptions")
}

// JobConcurrency
if relayConfig.jobConcurrency != 50 {
t.Error("fail - parse: RelayConfig.jobConcurrency")
t.Error("fail - parse: RelayOption.jobConcurrency")
}
})

Expand All @@ -65,7 +65,7 @@ func TestNewRelayConfigV2(t *testing.T) {

// ServerConfig
if relayConfig.serverConfig != nil {
t.Error("fail - parse: RelayConfig.serverConfig")
t.Error("fail - parse: RelayOption.serverConfig")
}
})

Expand All @@ -80,7 +80,7 @@ func TestNewRelayConfigV2(t *testing.T) {

// JobConcurrency
if relayConfig.jobConcurrency != 0 {
t.Error("fail - parse: RelayConfig.jobConcurrency")
t.Error("fail - parse: RelayOption.jobConcurrency")
}
})

Expand All @@ -106,7 +106,7 @@ func TestNewRelayConfigV2(t *testing.T) {
}

func TestNewRedisClient(t *testing.T) {
t.Run("success create client for reachable redis serer", func(t *testing.T) {
t.Run("success create client for reachable redis server", func(t *testing.T) {
relayConfig, err := NewRelayConfigV2(RelayConfigV2BuilderOptions{
WithServerConfig: false,
WithJobConcurrency: false,
Expand All @@ -117,7 +117,7 @@ func TestNewRedisClient(t *testing.T) {

_, err = relayConfig.NewRedisClient(context.Background())
if err != nil {
t.Error("fail - create client for reachable redis serer")
t.Error("fail - create client for reachable redis server")
}
})

Expand All @@ -135,7 +135,7 @@ func TestNewRedisClient(t *testing.T) {

_, err = relayConfig.NewRedisClient(context.Background())
if err == nil {
t.Error("fail - create client for unreachable redis serer")
t.Error("fail - create client for unreachable redis server")
}

viper.Set("REDIS_URL", validURL)
Expand Down
26 changes: 2 additions & 24 deletions models/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RelayState struct {
RedisClient *redis.Client `json:"-"`
notifiable bool

RelayConfig relayConfig `json:"relayConfig,omitempty"`
RelayConfig RelayOption `json:"relayConfig,omitempty"`
LimitedDomains []string `json:"limitedDomains,omitempty"`
BlockedDomains []string `json:"blockedDomains,omitempty"`
Subscribers []Subscriber `json:"subscriptions,omitempty"`
Expand Down Expand Up @@ -237,29 +237,7 @@ func (config *RelayState) refresh() {
}
}

// Subscriber : Manage for Mastodon Traditional Style Relay Subscriber
type Subscriber struct {
Domain string `json:"domain,omitempty"`
InboxURL string `json:"inbox_url,omitempty"`
ActivityID string `json:"activity_id,omitempty"`
ActorID string `json:"actor_id,omitempty"`
}

// Follower : Manage for LitePub Style Relay Follower
type Follower struct {
Domain string `json:"domain,omitempty"`
InboxURL string `json:"inbox_url,omitempty"`
ActivityID string `json:"activity_id,omitempty"`
ActorID string `json:"actor_id,omitempty"`
MutuallyFollow bool `json:"mutually_follow,omitempty"`
}

type relayConfig struct {
PersonOnly bool `json:"blockService,omitempty"`
ManuallyAccept bool `json:"manuallyAccept,omitempty"`
}

func (config *relayConfig) load(redisClient *redis.Client) {
func (config *RelayOption) load(redisClient *redis.Client) {
personOnly, err := redisClient.HGet(context.TODO(), "relay:config", "block_service").Result()
if err != nil {
personOnly = "0"
Expand Down
Loading

0 comments on commit 8201080

Please sign in to comment.