Skip to content

Commit

Permalink
Add persistent history
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Apr 7, 2024
1 parent 8ad1059 commit 1c44e91
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 37 deletions.
17 changes: 9 additions & 8 deletions cmd/pong/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ func init() {
pflag.String("pong-fortune-args", defaultFortuneArgs, "Arguments to pass to the fortune command")
pflag.Bool("pong-fortune", defaultFortuneMode, "The message to send back to the sender")

viper.BindPFlag("mode.pong.reply", pflag.Lookup("pong-reply"))
viper.SetDefault("mode.pong.reply", defaultPongReply)
viper.BindPFlag("pong.reply", pflag.Lookup("pong-reply"))
viper.SetDefault("pong.reply", defaultPongReply)

viper.BindPFlag("mode.pong.fortune.enable", pflag.Lookup("pong-fortune"))
viper.SetDefault("mode.pong.fortune.enable", defaultFortuneMode)
viper.BindPFlag("pong.fortune.enable", pflag.Lookup("pong-fortune"))
viper.SetDefault("pong.fortune.enable", defaultFortuneMode)

viper.SetDefault("mode.pong.fortune.args", defaultFortuneArgs)
viper.BindPFlag("pong.fortune.args", pflag.Lookup("pong-fortune-args"))
viper.SetDefault("pong.fortune.args", defaultFortuneArgs)
}

type PongFortuneStruct struct {
Expand Down Expand Up @@ -93,13 +94,13 @@ func (c *PongConfig) Save() error {
}

func pongFortuneMode() bool {
return viper.GetBool("mode.pong.fortune.enable")
return viper.GetBool("pong.fortune.enable")
}

func pongFortuneArgs() []string {
return viper.GetStringSlice("mode.pong.fortune.args")
return viper.GetStringSlice("pong.fortune.args")
}

func pongReply() string {
return viper.GetString("mode.pong.reply")
return viper.GetString("pong.reply")
}
2 changes: 1 addition & 1 deletion cmd/pong/envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func envelopeReply(ctx context.Context, a *actor.Actor, m *msg.Message) error {
// We need to reverse the to and from here. The message is from the other actor, and we are sending to them.
replyTo := m.From
replyFrom := m.To
replyMsg := []byte(viper.GetString("mode.pong.reply"))
replyMsg := []byte(viper.GetString("pong.reply"))

// Broadcast are sent to the topic, and the topic is the DID of the recipient
reply, err := msg.New(replyFrom, replyTo, replyMsg, "text/plain", a.Keyset.SigningKey.PrivKey)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pong/reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func reply(m *entity.Message) []byte {
}

func replyBytes() []byte {
replyMsg := viper.GetString("mode.pong.reply")
replyMsg := viper.GetString("pong.reply")
return []byte(replyMsg)
}

func angryBytes() []byte {
replyMsg := viper.GetString("mode.pong.reply")
replyMsg := viper.GetString("pong.reply")
return []byte(fmt.Sprintf("I'm doing the %s here! 😤", replyMsg))
}

Expand Down
10 changes: 10 additions & 0 deletions config/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,35 @@ const CSVMode = 0664
var (
defaultPeersPath = internal.NormalisePath(dataHome + "/peers.csv")
defaultEntitiesPath = internal.NormalisePath(dataHome + "/entities.csv")
defaultHistoryPath = internal.NormalisePath(dataHome + "/" + Profile() + ".history")
)

func init() {
pflag.String("peers", defaultPeersPath, "Filename for CSV peers file.")
pflag.String("entities", defaultEntitiesPath, "Filename for CSV entities file.")
pflag.String("history", defaultHistoryPath, "Filename for CSV history file.")

viper.BindPFlag("db.peers", pflag.Lookup("peers"))
viper.BindPFlag("db.entities", pflag.Lookup("entities"))
viper.BindPFlag("db.history", pflag.Lookup("history"))

viper.SetDefault("db.peers", defaultPeersPath)
viper.SetDefault("db.entities", defaultEntitiesPath)
viper.SetDefault("db.history", defaultHistoryPath)
}

type DBConfig struct {
Peers string `yaml:"peers"`
Entities string `yaml:"entities"`
History string `yaml:"history"`
}

func DB() DBConfig {

return DBConfig{
Peers: DBPeers(),
Entities: DBEntities(),
History: DBHistory(),
}

}
Expand All @@ -45,3 +51,7 @@ func DBPeers() string {
func DBEntities() string {
return viper.GetString("db.entities")
}

func DBHistory() string {
return viper.GetString("db.history")
}
11 changes: 4 additions & 7 deletions p2p/peer/nicks.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@ func AssertNick(pid p2peer.ID) error {
return SetNick(id, nick)
}

// GetOrCreateNick looks for a peer's nick by its ID.
// If it does not exist, creates a default nick, stores, and returns it.
// Takes a peerID as input, to make sure it's valid so we don't have to
// do any more error checking.
func GetOrCreateNick(peerID p2peer.ID) string {
// LookupNick looks for a peer's nick by its ID.
// If it does not exist it returns the input
func LookupNick(peerID p2peer.ID) string {

id := peerID.String()

if nick, ok := nicks.Load(id); ok {
return nick.(string)
}

// If not found, create a default nick
return createNick(id)
return id
}

func DeleteNick(id string) {
Expand Down
17 changes: 0 additions & 17 deletions ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,8 @@ import (
"github.com/bahner/go-ma-actor/config"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/spf13/viper"
)

func historySize() int {
return viper.GetInt("ui.history-size")
}

func (ui *ChatUI) pushToHistory(line string) {

historySize := historySize()

if len(ui.inputHistory) == historySize {
// Remove the oldest entry when we reach max size
copy(ui.inputHistory, ui.inputHistory[1:])
ui.inputHistory = ui.inputHistory[:historySize-1]
}
ui.inputHistory = append(ui.inputHistory, line)
}

func (ui *ChatUI) setupInputField() *tview.InputField {
inputField := tview.NewInputField().
SetLabel(config.ActorNick() + ": ").
Expand Down
2 changes: 1 addition & 1 deletion ui/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (ui *ChatUI) handlePeerShowCommand(args []string) {
}

ui.displaySystemMessage("ID: " + id)
ui.displaySystemMessage("Nick: " + peer.GetOrCreateNick(pid))
ui.displaySystemMessage("Nick: " + peer.LookupNick(pid))
ui.displaySystemMessage("Maddrs:")
for _, maddr := range ui.p.Host.Peerstore().Addrs(pid) {
ui.displaySystemMessage(maddr.String())
Expand Down
1 change: 1 addition & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func New(p *p2p.P2P, a *actor.Actor) (*ChatUI, error) {

// Since tview is global we can just run this which sets the style for the whole app.
ui.setupApp()
ui.loadHistory()

return ui, nil
}
Expand Down
2 changes: 1 addition & 1 deletion ui/web/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func unorderedListFromPeerIDSlice(peers p2peer.IDSlice) string {
peersMap := make(map[string]string)
for _, p := range peers {
nick := peer.GetOrCreateNick(p)
nick := peer.LookupNick(p)
peersMap[p.String()] = nick
}

Expand Down

0 comments on commit 1c44e91

Please sign in to comment.