Skip to content

Commit

Permalink
WIP: something
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed May 10, 2024
1 parent 5a45743 commit 92bfed3
Show file tree
Hide file tree
Showing 26 changed files with 192 additions and 85 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ go.work
/.vault
vault/**
/robot
go.work
go.work.sum
5 changes: 2 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@
},
"args": [
"--generate",
"--profile",
"FUBAR",
"--nick",
"FUBAR"
"FUBAR",
"--force"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion cmd/actor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func initConfig(defaultProfileName string) actor.ActorConfig {
c := actor.Config()

if config.GenerateFlag() {
config.Generate(&c)
config.GenerateConfig(&c)
}

if config.ShowConfigFlag() {
Expand Down
16 changes: 11 additions & 5 deletions cmd/actor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"

"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/db"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma-actor/p2p"
"github.com/bahner/go-ma-actor/ui"
Expand All @@ -22,19 +24,23 @@ func main() {
fmt.Println("Initialising actor configuation...")
// actor.InitConfig(config.Profile())

// ACTOR
fmt.Println("Initialising actor...")
a := actor.Init()

aIdentity, err := db.GetOrCreateIdentity(config.ActorNick())
if err != nil {
panic(fmt.Sprintf("failed to get or create identity: %v", err))
}
// P2P
fmt.Println("Setting default p2p options...")
p2pOpts := p2p.DefaultOptions()
fmt.Println("Initialising p2p...")
p2P, err := p2p.Init(a.Keyset.Identity, p2pOpts)
p2P, err := p2p.Init(aIdentity, p2pOpts)
if err != nil {
panic(fmt.Sprintf("failed to initialize p2p: %v", err))
}

// ACTOR
fmt.Println("Initialising actor...")
a := actor.Init()

// WEB
fmt.Println("Initialising web UI...")
wh := web.NewEntityHandler(p2P, a.Entity)
Expand Down
8 changes: 4 additions & 4 deletions cmd/node/actors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func getOrCreateActor(id string) (*actor.Actor, error) {
return nil, fmt.Errorf("failed to create DID Document: %w", err)
}

// Force publication of document.
o := doc.DefaultPublishOptions()
o.Force = true
a.Entity.Doc.Publish()
_, err = a.Entity.Doc.Publish()
if err != nil {
return nil, fmt.Errorf("failed to publish DID Document: %w", err)
}

// Cache the newly created entity for future retrievals
actors.Set(id, a)
Expand Down
2 changes: 1 addition & 1 deletion cmd/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Config(defaultProfileName string) NodeConfig {
}

if config.GenerateFlag() {
config.Generate(&c)
config.GenerateConfig(&c)
}

if config.ShowConfigFlag() {
Expand Down
3 changes: 3 additions & 0 deletions cmd/node/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func getOrCreateKeysetFromVault(id string) (set.Keyset, error) {
}

privKey, err := db.GetOrCreateIdentity(id)
if err != nil {
return set.Keyset{}, fmt.Errorf("error getting or creating identity: %s", err)
}

keyset, err = set.New(privKey, id)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/pong/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func initConfig(defaultProfileName string) PongConfig {
}

if config.GenerateFlag() {
config.Generate(&c)
config.GenerateConfig(&c)
}

if config.ShowConfigFlag() {
Expand Down
4 changes: 3 additions & 1 deletion cmd/pong/envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/entity/actor"
actormsg "github.com/bahner/go-ma-actor/msg"
"github.com/bahner/go-ma/msg"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -58,5 +59,6 @@ func envelopeReply(ctx context.Context, a *actor.Actor, m *msg.Message) error {
fmt.Printf("Sending private envelope to %s over %s\n", replyTo, a.Entity.Topic.String())

replyMsg := []byte(viper.GetString("pong.reply"))
return m.Reply(ctx, replyMsg, a.Keyset.SigningKey.PrivKey, replyToEntity.Topic)

return actormsg.Reply(ctx, *m, replyMsg, a.Keyset.SigningKey.PrivKey, replyToEntity.Topic)
}
16 changes: 14 additions & 2 deletions cmd/pong/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/entity/actor"
actormsg "github.com/bahner/go-ma-actor/msg"
"github.com/bahner/go-ma/msg"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand Down Expand Up @@ -43,7 +44,18 @@ func handleMessageEvents(ctx context.Context, a *actor.Actor) {
continue
}

if m.Message.To == me && m.Message.Type == msg.CHAT {
messageType, err := m.Message.MessageType()
if err != nil {
log.Debugf("Failed to get message type: %v", err)
continue
}

if messageType != actormsg.CHAT_MESSAGE_TYPE {
log.Debugf("Received message of unknown type: %s, ignoring...", messageType)
continue
}

if m.Message.To == me {
messageReply(ctx, a, m.Message)
}
}
Expand All @@ -63,7 +75,7 @@ func messageReply(ctx context.Context, a *actor.Actor, m *msg.Message) error {
reply := []byte(viper.GetString("pong.reply"))
fmt.Printf("Sending private message to %s over %s\n", replyTo, a.Entity.Topic.String())

err = m.Reply(ctx, reply, a.Keyset.SigningKey.PrivKey, replyToEntity.Topic)
err = actormsg.Reply(ctx, *m, reply, a.Keyset.SigningKey.PrivKey, replyToEntity.Topic)
if err != nil {
log.Errorf("messageReply: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/relay/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func initConfig(defaultProfileName string) RelayConfig {
}

if config.GenerateFlag() {
config.Generate(&c)
config.GenerateConfig(&c)
}

if config.ShowConfigFlag() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/robot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func initConfig(defaultProfileName string) RobotConfig {
c := robotConfig

if config.GenerateFlag() {
config.Generate(&c)
config.GenerateConfig(&c)
}

if config.ShowConfigFlag() {
Expand Down
12 changes: 9 additions & 3 deletions cmd/robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma-actor/entity/actor"
"github.com/bahner/go-ma/msg"
"github.com/bahner/go-ma-actor/msg"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -97,7 +97,13 @@ func (i *RobotStruct) handleEntityMessageEvents() {
continue
}

if m.Message.Type == msg.CHAT {
messageType, err := m.Message.MessageType()
if err != nil {
log.Debugf(errPrefix+"Failed to get message type: %v", err)
continue
}

if messageType == msg.CHAT_MESSAGE_TYPE {
i.handleMessage(ctx, m)
}
}
Expand All @@ -118,7 +124,7 @@ func (i *RobotStruct) handleMessage(ctx context.Context, m *entity.Message) erro

replyBytes := reply(m)

err = m.Message.Reply(ctx, replyBytes, i.Robot.Keyset.SigningKey.PrivKey, replyToEntity.Topic)
err = msg.Reply(ctx, *m.Message, replyBytes, i.Robot.Keyset.SigningKey.PrivKey, replyToEntity.Topic)
if err != nil {
log.Errorf("failed to reply to message: %v", err)
}
Expand Down
6 changes: 0 additions & 6 deletions config/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,6 @@ func publishIdentityFromKeyset(k set.Keyset) error {
return fmt.Errorf("config.publishIdentityFromKeyset: failed to create DOC: %w", err)
}

assertionMethod, err := d.GetAssertionMethod()
if err != nil {
return fmt.Errorf("config.publishIdentityFromKeyset: %w", err)
}
d.Sign(k.SigningKey, assertionMethod)

_, err = d.Publish()
if err != nil {
return fmt.Errorf("config.publishIdentityFromKeyset: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion config/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// This is a function that'll handle generateing and possible printing of the config.
// You can called his after gerenating your config to properly handle generate and show-config flags.
func Generate(c Config) {
func GenerateConfig(c Config) {

// Just a precaution, if the generate flag is not set, we don't want to generate the config.
if !GenerateFlag() {
Expand Down
14 changes: 4 additions & 10 deletions config/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ var (
)

type HTTPConfig struct {
Socket string `yaml:"socket"`
Refresh int `yaml:"refresh"`
DebugSocket string `yaml:"debug_socket"`
Socket string `yaml:"socket"`
Refresh int `yaml:"refresh"`
}

func initHTTPFlagset() {
Expand All @@ -47,9 +46,8 @@ func initHTTPFlagset() {
func HTTP() HTTPConfig {

return HTTPConfig{
Socket: HttpSocket(),
Refresh: HttpRefresh(),
DebugSocket: HttpDebugSocket(),
Socket: HttpSocket(),
Refresh: HttpRefresh(),
}
}

Expand All @@ -60,7 +58,3 @@ func HttpSocket() string {
func HttpRefresh() int {
return viper.GetInt("http.refresh")
}

func HttpDebugSocket() string {
return viper.GetString("http.debug-socket")
}
14 changes: 10 additions & 4 deletions entity/actor/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import (
"fmt"
"strings"

actormsg "github.com/bahner/go-ma-actor/msg"
"github.com/bahner/go-ma/msg"
)

var ErrInvalidDotContentType = fmt.Errorf("actor: invalid content type for dot message. Must be '%s'", msg.DEFAULT_CONTENT_TYPE)
var ErrInvalidDotContentType = fmt.Errorf("actor: invalid content type for dot message. Must be '%s'", msg.CONTENT_TYPE)

func (a *Actor) defaultMessageHandler(m *msg.Message) error {

switch m.Type {
case msg.REQUEST:
messageType, err := m.MessageType()
if err != nil {
return err
}

switch messageType {
case actormsg.CHAT_MESSAGE_TYPE:
return a.handleAtMessage(m)
default:
return msg.ErrInvalidMessageType
Expand All @@ -22,7 +28,7 @@ func (a *Actor) defaultMessageHandler(m *msg.Message) error {
func (a *Actor) handleAtMessage(m *msg.Message) error {

// Only receive messages with default content type
if m.ContentType != msg.DEFAULT_CONTENT_TYPE {
if m.ContentType != msg.CONTENT_TYPE {
return ErrInvalidDotContentType
}

Expand Down
19 changes: 8 additions & 11 deletions entity/actor/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/bahner/go-ma-actor/entity"
actormsg "github.com/bahner/go-ma-actor/msg"
"github.com/bahner/go-ma/did"
"github.com/bahner/go-ma/msg"
"github.com/pkg/errors"
Expand Down Expand Up @@ -37,16 +38,6 @@ func (a *Actor) SetLocationFromDID(did did.DID) error {
return a.SetLocation(e)
}

// func (a *Actor) SetLocationFromDIDString(didStr string) error {

// d, err := did.NewFromString(didStr)
// if err != nil {
// return err
// }

// return a.SetLocationFromDID(d)
// }

func (a *Actor) GetLocation() (string, error) {

if a.Location == nil {
Expand Down Expand Up @@ -74,6 +65,12 @@ func (a *Actor) HandleLocationMessage(m *msg.Message) error {

log.Debugf("Sending location to %s over %s", m.From, a.Entity.Topic.String())

return m.Reply(ctx, replyBytes, a.Keyset.SigningKey.PrivKey, e.Topic)
return actormsg.Reply(
ctx,
*m,
replyBytes,
a.Keyset.SigningKey.PrivKey,
e.Topic,
)

}
2 changes: 1 addition & 1 deletion entity/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func (e *Entity) ConnectPeer() (pai p2peer.AddrInfo, err error) {

p := p2p.Get()
pid := e.DID.Name.Peer()
pid := e.DID.IPNSName().Peer()

// If we're already connected, return
if p.Host.Network().Connectedness(pid) == network.Connected {
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22
require (
github.com/adrg/xdg v0.4.0
github.com/ayush6624/go-chatgpt v0.3.0
github.com/bahner/go-ma v0.7.7-rc1.0.20240422182336-4bc562a182a9
github.com/bahner/go-ma v0.7.7-rc1.0.20240509182543-8f9b5f8ac44b
github.com/ergo-services/ergo v1.999.224
github.com/fsnotify/fsnotify v1.7.0
github.com/fxamacker/cbor/v2 v2.6.0
Expand All @@ -31,7 +31,7 @@ require (
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cenkalti/backoff/v3 v3.0.0 // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
Expand Down Expand Up @@ -61,9 +61,9 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.6.6 // indirect
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.6 // indirect
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.7 // indirect
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
Expand Down
Loading

0 comments on commit 92bfed3

Please sign in to comment.