Skip to content

Commit

Permalink
Fix nick handling in entities in the ui
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Mar 8, 2024
1 parent b6405d3 commit ec87d78
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 42 deletions.
4 changes: 0 additions & 4 deletions entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,3 @@ func GetOrCreateFromDID(id did.DID) (*Entity, error) {

return e, nil
}

func Lookup(id string) (*Entity, error) {
return GetOrCreate(GetDID(id))
}
43 changes: 37 additions & 6 deletions entity/nicks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var ErrFailedToSetNick = errors.New("failed to set entity nick")
// This is used before we know in an Entity exists or not. It can be used anywhere.
func GetDID(id string) string {

did, err := LookupNick(id)
did, err := LookupID(id)
if err == nil {
return did
}
Expand Down Expand Up @@ -60,24 +60,55 @@ func ListNicks() map[string]string {

// Takes a nick as input and returns the corresponding DID
// Else it returns the input as is with an error.
func LookupNick(id string) (string, error) {
func LookupID(nick string) (string, error) {

did := id
var did string

d, err := db.Get()
if err != nil {
return id, err
return nick, err
}

err = d.QueryRow(_SELECT_DID, id).Scan(&did)
err = d.QueryRow(_SELECT_DID, nick).Scan(&did)
if err != nil {
return id, err
return nick, err
}

return did, nil

}

// Takes a nick as input and returns the corresponding DID
// Else it returns the input as is with an error.
func LookupNick(did string) (string, error) {

var nick string

d, err := db.Get()
if err != nil {
return did, err
}

err = d.QueryRow(_SELECT_NICK, did).Scan(&nick)
if err != nil {
return did, err
}

return nick, nil

}

// Tries to find a DID for the input name whether DID or Nick
func Lookup(name string) string {

id, err := LookupID(name)
if err != nil {
return name
}

return id
}

// Removes a node from the database if it exists
func RemoveNick(id string) error {

Expand Down
6 changes: 5 additions & 1 deletion entity/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func (e *Entity) ConnectPeer() (pi p2peer.AddrInfo, err error) {
}

// Look for the peer in the DHT
pai := p.DHT.Host().Peerstore().PeerInfo(pid)
pai, err := p.DHT.FindPeer(e.Ctx, pid)
if err != nil {
log.Debugf("Failed to find peer: %v", err)
return pi, err
}
log.Debugf("PeerInfo: %v", pai.Addrs)

// Connect to the peer
Expand Down
2 changes: 1 addition & 1 deletion ui/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (ui *ChatUI) handleBroadcastCommand(args []string) {
}
msgBytes := []byte(message)
if log.GetLevel() == log.DebugLevel {
ui.displaySystemMessage(fmt.Sprintf("Broadcasting %s", message))
ui.displayDebugMessage(fmt.Sprintf("Broadcasting %s", message))
}

msg, err := msg.NewBroadcast(me, msgBytes, "text/plain", ui.a.Keyset.SigningKey.PrivKey)
Expand Down
8 changes: 4 additions & 4 deletions ui/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// with the sender's nick highlighted in green.
// func (ui *ChatUI) displayChatMessage(cm *msg.Message) {
func (ui *ChatUI) displayChatMessage(cm *msg.Message) {
e, err := entity.Lookup(cm.From)
e, err := entity.GetOrCreate(cm.From)
if err != nil {
log.Debugf("entity lookup error: %s", err)
return
Expand All @@ -23,7 +23,7 @@ func (ui *ChatUI) displayChatMessage(cm *msg.Message) {
}

func (ui *ChatUI) displayBroadcastMessage(cm *msg.Message) {
e, err := entity.Lookup(cm.From)
e, err := entity.GetOrCreate(cm.From)
if err != nil {
log.Debugf("entity lookup error: %s", err)
return
Expand All @@ -33,7 +33,7 @@ func (ui *ChatUI) displayBroadcastMessage(cm *msg.Message) {
}

func (ui *ChatUI) displayPrivateMessage(cm *msg.Message) {
e, err := entity.Lookup(cm.From)
e, err := entity.GetOrCreate(cm.From)
if err != nil {
log.Debugf("entity lookup error: %s", err)
return
Expand All @@ -43,7 +43,7 @@ func (ui *ChatUI) displayPrivateMessage(cm *msg.Message) {
}

func (ui *ChatUI) displaySentPrivateMessage(cm *msg.Message) {
e, err := entity.Lookup(cm.To)
e, err := entity.GetOrCreate(cm.To)
if err != nil {
log.Debugf("entity lookup error: %s", err)
return
Expand Down
4 changes: 2 additions & 2 deletions ui/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (ui *ChatUI) enterEntity(d string, reEntry bool) error {
}

// If we have a cached entity for this nick, use it.
e, err = entity.Lookup(d)
e, err = entity.GetOrCreate((entity.Lookup(d)))
if err != nil {
// If we don't have it stored, then create it.
e, err = entity.GetOrCreate(d)
Expand Down Expand Up @@ -86,7 +86,7 @@ func (ui *ChatUI) enterEntity(d string, reEntry bool) error {
titleNick, err := entity.LookupNick(e.DID.Id)
if err != nil {
titleNick = e.DID.Id
ui.displaySystemMessage("Error looking up nick: " + err.Error())
ui.displayDebugMessage("Error looking up nick for " + e.DID.Id + ": " + err.Error())
}
ui.msgBox.SetTitle(titleNick)

Expand Down
48 changes: 30 additions & 18 deletions ui/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import (
"fmt"
"strings"

"github.com/bahner/go-ma-actor/entity"
log "github.com/sirupsen/logrus"
Expand All @@ -11,14 +12,17 @@ const (
entityUsage = "/entity nick|show"
entityHelp = `Manages info on seen entities
At this point only nicks are handled`
entityConnectUsage = "/entity connect <id|nick>"
entityConnectHelp = "Connects to an entity's libp2p node"
entityNickUsage = "/entity nick list|set|remove|show"
entityNickHelp = "Manages nicks for entities"
entityNickListUsage = "/entity nick list"
entityNickListHelp = "Lists nicks for entities"
entityNickSetUsage = "/entity nick set <id|nick> <nick>"
entityNickSetHelp = "Sets a nick for an entity"
entityConnectUsage = "/entity connect <id|nick>"
entityConnectHelp = "Connects to an entity's libp2p node"
entityNickUsage = "/entity nick list|set|remove|show"
entityNickHelp = "Manages nicks for entities"
entityNickListUsage = "/entity nick list"
entityNickListHelp = "Lists nicks for entities"
entityNickSetUsage = "/entity nick set <id|nick> <nick>"
entityNickSetHelp = `Sets a nick for an entity
The entity to set nick for *MUST* be quoted if it contains spaces.
The nick after the entity to set nick for doesn't need to be quoted.
`
entityNickRemoveUsage = "/entity nick remove <id|nick>"
entityNickRemoveHelp = "Removes a nick for an entity"
entityNickShowUsage = "/entity nick show <id|nick>"
Expand Down Expand Up @@ -102,9 +106,10 @@ func (ui *ChatUI) handleEntityNickCommand(args []string) {
func (ui ChatUI) handleEntityNickSetCommand(args []string) {

if len(args) == 5 {
id := args[3]
nick := args[4]
e, err := entity.Lookup(id)
id := entity.Lookup(args[3])
nick := strings.Join(args[4:], separator)

e, err := entity.GetOrCreate(id)
if err != nil {
ui.displaySystemMessage("Error: " + err.Error())
return
Expand All @@ -114,6 +119,10 @@ func (ui ChatUI) handleEntityNickSetCommand(args []string) {
ui.displaySystemMessage("Error setting entity nick: " + err.Error())
return
}
// Change the window title if the ID matches the current entity
if id == ui.e.DID.Id {
ui.msgBox.SetTitle(nick)
}
ui.displaySystemMessage(e.DID.Id + " is now known as " + e.Nick)
} else {
ui.handleHelpCommand(entityNickSetUsage, entityNickSetHelp)
Expand All @@ -125,8 +134,8 @@ func (ui ChatUI) handleEntityNickSetCommand(args []string) {
func (ui *ChatUI) handleEntityNickShowCommand(args []string) {

if len(args) == 4 {
id := args[3]
e, err := entity.Lookup(id)
id := strings.Join(args[3:], separator)
e, err := entity.GetOrCreate(entity.Lookup(id))
if err != nil {
ui.displaySystemMessage("Error: " + err.Error())
return
Expand All @@ -143,7 +152,8 @@ func (ui *ChatUI) handleEntityNickShowCommand(args []string) {
func (ui *ChatUI) handleEntityNickRemoveCommand(args []string) {

if len(args) == 4 {
id := entity.GetDID(args[3])
id := strings.Join(args[3:], separator)
id = entity.Lookup(id)
entity.RemoveNick(id)
ui.displaySystemMessage("Nick removed for " + id + " if it existed")
} else {
Expand All @@ -154,19 +164,21 @@ func (ui *ChatUI) handleEntityNickRemoveCommand(args []string) {

func (ui *ChatUI) handleEntityConnectCommand(args []string) {

if len(args) == 3 {
e, err := entity.GetOrCreate(args[2])
if len(args) >= 3 {
id := strings.Join(args[2:], separator)
id = entity.Lookup(id)
e, err := entity.GetOrCreate(id)
if err != nil {
ui.displaySystemMessage("Error: " + err.Error())
return
}
log.Debugf("Connecting to peer for entity: %v", e.DID.Id)
log.Debugf("Connecting to peer for entity: %v", id)
pai, err := e.ConnectPeer()
if err != nil {
ui.displaySystemMessage("Error connecting to entity peer: " + err.Error())
return
}
ui.displaySystemMessage("Connected to " + e.DID.Id + ": " + pai.ID.String())
ui.displaySystemMessage("Connected to " + id + ": " + pai.ID.String())
} else {
ui.handleHelpCommand(entityConnectUsage, entityConnectHelp)
}
Expand Down
6 changes: 6 additions & 0 deletions ui/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func (ui *ChatUI) displaySystemMessage(msg string) {
fmt.Fprintf(ui.msgW, "%s\n", out)
}

func (ui *ChatUI) displayDebugMessage(m string) {
if log.GetLevel() == log.DebugLevel {
ui.displaySystemMessage(fmt.Sprintf("DEBUG: %s", m))
}
}

// handleEvents runs an event loop that sends user input to the chat room
// and displays messages received from the chat room. It also periodically
// refreshes the list of peers in the UI.
Expand Down
4 changes: 2 additions & 2 deletions ui/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (

func (ui *ChatUI) handleMsgCommand(input string) {

parts := strings.SplitN(input, " ", 2)
parts := strings.SplitN(input, separator, 2)

if len(parts) == 2 {

Expand All @@ -35,7 +35,7 @@ func (ui *ChatUI) handleMsgCommand(input string) {
message := parts[1]
msgBytes := []byte(message)
if log.GetLevel() == log.DebugLevel {
ui.displaySystemMessage(fmt.Sprintf("Sending message to %s: %s", recipient, message))
ui.displayDebugMessage(fmt.Sprintf("Sending message to %s: %s", recipient, message))
}

msg, err := msg.New(ui.a.Entity.DID.Id, recipient, msgBytes, "text/plain", ui.a.Keyset.SigningKey.PrivKey)
Expand Down
6 changes: 3 additions & 3 deletions ui/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ func (ui *ChatUI) handleRefreshCommand(args []string) {
}

func (ui *ChatUI) handleRefresh() {
ui.displaySystemMessage("Looking up nick for " + ui.e.DID.Id)
ui.displayDebugMessage("Looking up nick for " + ui.e.DID.Id)
nick, err := peer.GetNickForID(ui.e.DID.Id)
if err != nil {
ui.displaySystemMessage(fmt.Sprintf("Error looking up nick: %s", err))
}
ui.displaySystemMessage("Found nick " + nick)
ui.displayDebugMessage("Found nick " + nick)
ui.e.SetNick(nick)
ui.refreshPeers()
ui.msgBox.Clear()
ui.setupInputField()
ui.displaySystemMessage("Setting title to " + ui.e.Nick)
ui.displayDebugMessage("Setting title to " + ui.e.Nick)
ui.msgBox.SetTitle(ui.e.Nick)
ui.app.Draw()
}
Expand Down
2 changes: 1 addition & 1 deletion ui/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (ui *ChatUI) handleSetNickCommand(args []string) {

if len(args) >= 3 {

nick := strings.Join(args[2:], " ")
nick := strings.Join(args[2:], separator)

viper.Set("actor.nick", nick)
ui.inputField.SetLabel(nick + ": ")
Expand Down

0 comments on commit ec87d78

Please sign in to comment.