Update Actor name.

This commit is contained in:
Naoki Kosaka 2019-04-07 23:42:37 +09:00
parent bf1f91c24e
commit 9ddeb2353e
8 changed files with 87 additions and 7 deletions

View File

@ -31,6 +31,7 @@ type Actor struct {
Context interface{} `json:"@context"` Context interface{} `json:"@context"`
ID string `json:"id"` ID string `json:"id"`
Type string `json:"type"` Type string `json:"type"`
Name string `json:"name"`
PreferredUsername string `json:"preferredUsername"` PreferredUsername string `json:"preferredUsername"`
Inbox string `json:"inbox"` Inbox string `json:"inbox"`
Endpoints *Endpoints `json:"endpoints"` Endpoints *Endpoints `json:"endpoints"`
@ -38,11 +39,12 @@ type Actor struct {
} }
// GenerateSelfKey : Generate relay Actor from Publickey. // GenerateSelfKey : Generate relay Actor from Publickey.
func (actor *Actor) GenerateSelfKey(hostname *url.URL, publickey *rsa.PublicKey) { func (actor *Actor) GenerateSelfKey(hostname *url.URL, username string, publickey *rsa.PublicKey) {
actor.Context = []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"} actor.Context = []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"}
actor.ID = hostname.String() + "/actor" actor.ID = hostname.String() + "/actor"
actor.Type = "Service" actor.Type = "Service"
actor.PreferredUsername = "relay" actor.PreferredUsername = "relay"
actor.Name = username
actor.Inbox = hostname.String() + "/inbox" actor.Inbox = hostname.String() + "/inbox"
actor.PublicKey = PublicKey{ actor.PublicKey = PublicKey{
hostname.String() + "/actor#main-key", hostname.String() + "/actor#main-key",
@ -150,6 +152,16 @@ func (activity *Activity) NestedActivity() (*Activity, error) {
return nil, errors.New("Can't assart id") return nil, errors.New("Can't assart id")
} }
// ActivityObject : ActivityPub Activity.
type ActivityObject struct {
ID string `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
Content string `json:"content"`
To []string `json:"to"`
Cc []string `json:"cc"`
}
// Signature : ActivityPub Header Signature. // Signature : ActivityPub Header Signature.
type Signature struct { type Signature struct {
Type string `json:"type"` Type string `json:"type"`

View File

@ -9,10 +9,14 @@ import (
"github.com/go-redis/redis" "github.com/go-redis/redis"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
activitypub "github.com/yukimochi/Activity-Relay/ActivityPub"
keyloader "github.com/yukimochi/Activity-Relay/KeyLoader" keyloader "github.com/yukimochi/Activity-Relay/KeyLoader"
state "github.com/yukimochi/Activity-Relay/State" state "github.com/yukimochi/Activity-Relay/State"
) )
// Actor : Relay's Actor
var Actor activitypub.Actor
var hostname *url.URL var hostname *url.URL
var hostkey *rsa.PrivateKey var hostkey *rsa.PrivateKey
var macServer *machinery.Server var macServer *machinery.Server
@ -21,9 +25,16 @@ var relayState state.RelayState
func initConfig() { func initConfig() {
viper.BindEnv("actor_pem") viper.BindEnv("actor_pem")
viper.BindEnv("relay_domain") viper.BindEnv("relay_domain")
viper.BindEnv("relay_servicename")
viper.BindEnv("redis_url") viper.BindEnv("redis_url")
hostkey, _ = keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem")) hostkey, err := keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem"))
hostname, _ = url.Parse("https://" + viper.GetString("relay_domain")) if err != nil {
panic(err)
}
hostname, err = url.Parse("https://" + viper.GetString("relay_domain"))
if err != nil {
panic(err)
}
redOption, err := redis.ParseURL(viper.GetString("redis_url")) redOption, err := redis.ParseURL(viper.GetString("redis_url"))
if err != nil { if err != nil {
panic(err) panic(err)
@ -40,6 +51,7 @@ func initConfig() {
panic(err) panic(err)
} }
relayState = state.NewState(redClient) relayState = state.NewState(redClient)
Actor.GenerateSelfKey(hostname, viper.GetString("relay_servicename"), &hostkey.PublicKey)
} }
func buildNewCmd() *cobra.Command { func buildNewCmd() *cobra.Command {

View File

@ -8,7 +8,7 @@ import (
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
viper.Set("actor_pem", "misc/testKey.pem") viper.Set("actor_pem", "../misc/testKey.pem")
viper.Set("relay_domain", "relay.yukimochi.example.org") viper.Set("relay_domain", "relay.yukimochi.example.org")
initConfig() initConfig()

View File

@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/RichardKnop/machinery/v1/tasks" "github.com/RichardKnop/machinery/v1/tasks"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra" "github.com/spf13/cobra"
activitypub "github.com/yukimochi/Activity-Relay/ActivityPub" activitypub "github.com/yukimochi/Activity-Relay/ActivityPub"
state "github.com/yukimochi/Activity-Relay/State" state "github.com/yukimochi/Activity-Relay/State"
@ -45,6 +46,14 @@ func followCmdInit() *cobra.Command {
} }
follow.AddCommand(followReject) follow.AddCommand(followReject)
var updateActor = &cobra.Command{
Use: "update",
Short: "Update actor object",
Long: "Update actor object for whole subscribers.",
RunE: updateActor,
}
follow.AddCommand(updateActor)
return follow return follow
} }
@ -85,7 +94,10 @@ func createFollowRequestResponse(domain string, response string) error {
} }
resp := activity.GenerateResponse(hostname, response) resp := activity.GenerateResponse(hostname, response)
jsonData, _ := json.Marshal(&resp) jsonData, err := json.Marshal(&resp)
if err != nil {
return err
}
pushRegistorJob(data["inbox_url"], jsonData) pushRegistorJob(data["inbox_url"], jsonData)
relayState.RedisClient.Del("relay:pending:" + domain) relayState.RedisClient.Del("relay:pending:" + domain)
if response == "Accept" { if response == "Accept" {
@ -100,6 +112,25 @@ func createFollowRequestResponse(domain string, response string) error {
return nil return nil
} }
func createUpdateActorActivity(subscription state.Subscription) error {
activity := activitypub.Activity{
Context: []string{"https://www.w3.org/ns/activitystreams"},
ID: hostname.String() + "/activities/" + uuid.NewV4().String(),
Actor: hostname.String() + "/actor",
Type: "Update",
To: []string{"https://www.w3.org/ns/activitystreams#Public"},
Object: Actor,
}
jsonData, err := json.Marshal(&activity)
if err != nil {
return err
}
pushRegistorJob(subscription.InboxURL, jsonData)
return nil
}
func listFollows(cmd *cobra.Command, args []string) error { func listFollows(cmd *cobra.Command, args []string) error {
var domains []string var domains []string
cmd.Println(" - Follow request :") cmd.Println(" - Follow request :")
@ -165,3 +196,13 @@ func rejectFollow(cmd *cobra.Command, args []string) error {
return nil return nil
} }
func updateActor(cmd *cobra.Command, args []string) error {
for _, subscription := range relayState.Subscriptions {
err := createUpdateActorActivity(subscription)
if err != nil {
cmd.Println("Failed Update Actor for " + subscription.Domain)
}
}
return nil
}

View File

@ -127,3 +127,16 @@ func TestInvalidRejectFollow(t *testing.T) {
relayState.RedisClient.FlushAll().Result() relayState.RedisClient.FlushAll().Result()
relayState.Load() relayState.Load()
} }
func TestCreateUpdateActorActivity(t *testing.T) {
app := buildNewCmd()
app.SetArgs([]string{"config", "import", "--json", "../misc/exampleConfig.json"})
app.Execute()
app.SetArgs([]string{"follow", "update"})
app.Execute()
relayState.RedisClient.FlushAll().Result()
relayState.Load()
}

1
go.mod
View File

@ -7,6 +7,7 @@ require (
github.com/Songmu/go-httpdate v1.0.0 github.com/Songmu/go-httpdate v1.0.0
github.com/go-redis/redis v6.15.2+incompatible github.com/go-redis/redis v6.15.2+incompatible
github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/satori/go.uuid v1.2.0 github.com/satori/go.uuid v1.2.0
github.com/spf13/cobra v0.0.3 github.com/spf13/cobra v0.0.3

View File

@ -34,6 +34,7 @@ func initConfig() {
viper.BindEnv("actor_pem") viper.BindEnv("actor_pem")
viper.BindEnv("relay_domain") viper.BindEnv("relay_domain")
viper.BindEnv("relay_bind") viper.BindEnv("relay_bind")
viper.BindEnv("relay_servicename")
viper.BindEnv("redis_url") viper.BindEnv("redis_url")
hostURL, _ = url.Parse("https://" + viper.GetString("relay_domain")) hostURL, _ = url.Parse("https://" + viper.GetString("relay_domain"))
hostPrivatekey, _ = keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem")) hostPrivatekey, _ = keyloader.ReadPrivateKeyRSAfromPath(viper.GetString("actor_pem"))
@ -55,7 +56,7 @@ func initConfig() {
uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.2; " + hostURL.Host + ")" uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.2; " + hostURL.Host + ")"
relayState = state.NewState(redisClient) relayState = state.NewState(redisClient)
actorCache = cache.New(5*time.Minute, 10*time.Minute) actorCache = cache.New(5*time.Minute, 10*time.Minute)
Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey) Actor.GenerateSelfKey(hostURL, viper.GetString("relay_servicename"), &hostPrivatekey.PublicKey)
WebfingerResource.GenerateFromActor(hostURL, &Actor) WebfingerResource.GenerateFromActor(hostURL, &Actor)
fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Server]\n - Configrations") fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Server]\n - Configrations")

View File

@ -72,7 +72,7 @@ func initConfig() {
newNullLogger := NewNullLogger() newNullLogger := NewNullLogger()
log.DEBUG = newNullLogger log.DEBUG = newNullLogger
uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.2; " + hostURL.Host + ")" uaString = viper.GetString("relay_servicename") + " (golang net/http; Activity-Relay v0.2.2; " + hostURL.Host + ")"
Actor.GenerateSelfKey(hostURL, &hostPrivatekey.PublicKey) Actor.GenerateSelfKey(hostURL, viper.GetString("relay_servicename"), &hostPrivatekey.PublicKey)
fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Worker]\n - Configrations") fmt.Println("Welcome to YUKIMOCHI Activity-Relay [Worker]\n - Configrations")
fmt.Println("RELAY DOMAIN : ", hostURL.Host) fmt.Println("RELAY DOMAIN : ", hostURL.Host)