Skip to content

Commit

Permalink
Colourcode messages private and broadcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Feb 11, 2024
1 parent 169ae99 commit 9e074e2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
4 changes: 2 additions & 2 deletions cmd/pong/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func broadcast(ctx context.Context, a *entity.Entity) error {
topic := a.DID.String()

// Broadcast are sent to the topic, and the topic is the DID of the recipient
r, err := msg.NewBroadcast(topic, topic, []byte("PA:"+viper.GetString("pong.msg")), "text/plain", a.Keyset.SigningKey.PrivKey)
r, err := msg.NewBroadcast(topic, topic, []byte("Public Announcment: "+viper.GetString("pong.msg")), "text/plain", a.Keyset.SigningKey.PrivKey)
if err != nil {
return fmt.Errorf("failed creating new message: %w", errors.Cause(err))
}
Expand All @@ -91,7 +91,7 @@ func reply(ctx context.Context, a *entity.Entity, m *msg.Message) error {
from := m.To

// Broadcast are sent to the topic, and the topic is the DID of the recipient
r, err := msg.New(from, to, []byte("private:"+viper.GetString("pong.msg")), "text/plain", a.Keyset.SigningKey.PrivKey)
r, err := msg.New(from, to, []byte("Private reply: "+viper.GetString("pong.msg")), "text/plain", a.Keyset.SigningKey.PrivKey)
if err != nil {
return fmt.Errorf("failed creating new message: %w", errors.Cause(err))
}
Expand Down
31 changes: 12 additions & 19 deletions ui/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"fmt"
"strings"

"github.com/bahner/go-ma-actor/alias"
"github.com/bahner/go-ma-actor/entity"
"github.com/bahner/go-ma/did"
"github.com/bahner/go-ma/msg"
log "github.com/sirupsen/logrus"
)
Expand All @@ -16,25 +14,18 @@ func (ui *ChatUI) handleBroadcastCommand(args []string) {

if len(args) > 1 {

if len(args) < 3 {
if len(args) < 2 {
ui.displaySystemMessage("Public announcement can't be empty")
return
}

recipient := args[1]
if !did.IsValidDID(recipient) {
recipient = alias.LookupEntityNick(recipient)
}
if recipient == "" {
ui.displaySystemMessage(fmt.Sprintf("Invalid DID: %s", args[1]))
return
}
recipient := ui.e.DID.String()

var message string
if len(args) > 3 {
message = strings.Join(args[2:], " ")
if len(args) > 2 {
message = strings.Join(args[1:], " ")
} else {
message = args[2]
message = args[1]
}
msgBytes := []byte(message)
if log.GetLevel() == log.DebugLevel {
Expand All @@ -43,7 +34,7 @@ func (ui *ChatUI) handleBroadcastCommand(args []string) {
ui.displaySystemMessage(fmt.Sprintf("Broadcasting to %s", recipient))
}

msg, err := msg.New(ui.a.DID.String(), recipient, msgBytes, "text/plain", ui.a.Keyset.SigningKey.PrivKey)
msg, err := msg.NewBroadcast(ui.a.DID.String(), recipient, msgBytes, "text/plain", ui.a.Keyset.SigningKey.PrivKey)
if err != nil {
ui.displaySystemMessage(fmt.Sprintf("Broadcast creation error: %s", err))
}
Expand All @@ -57,13 +48,15 @@ func (ui *ChatUI) handleBroadcastCommand(args []string) {
if err != nil {
ui.displaySystemMessage(fmt.Sprintf("Broadcast error: %s", err))
}

log.Debugf("Message broadcasted to topic: %s", ui.e.Topic.String())
} else {
ui.handleHelpMsgCommand(args)
ui.handleHelpBroadcastCommand(args)
}

}

func (ui *ChatUI) handleHelpMsgCommand(args []string) {
ui.displaySystemMessage("Usage: /broadcast <DID|NICK> <message>")
ui.displaySystemMessage("Sends a public announcement to the specified DID")
func (ui *ChatUI) handleHelpBroadcastCommand(args []string) {
ui.displaySystemMessage("Usage: /broadcast <message>")
ui.displaySystemMessage("Sends a public announcement to the current entity")
}
14 changes: 13 additions & 1 deletion ui/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ import (
// with the sender's nick highlighted in green.
// func (ui *ChatUI) displayChatMessage(cm *msg.Message) {
func (ui *ChatUI) displayChatMessage(cm *msg.Message) {
from := alias.GetOrCreateEntityAlias(cm.From)
prompt := withColor("yellow", fmt.Sprintf("<%s>:", from))
fmt.Fprintf(ui.msgW, "%s %s\n", prompt, string(cm.Content))
}

func (ui *ChatUI) displayBroadcastMessage(cm *msg.Message) {
from := alias.GetOrCreateEntityAlias(cm.From)
prompt := withColor("blue", fmt.Sprintf("<%s>:", from))
fmt.Fprintf(ui.msgW, "%s %s\n", prompt, string(cm.Content))
}

func (ui *ChatUI) displayPrivateMessage(cm *msg.Message) {
from := alias.GetOrCreateEntityAlias(cm.From)
prompt := withColor("green", fmt.Sprintf("<%s>:", from))
fmt.Fprintf(ui.msgW, "%s %s\n", prompt, string(cm.Content))
Expand All @@ -28,7 +40,7 @@ func (ui *ChatUI) handleChatMessage(input string) error {
msgBytes := []byte(input)
log.Debugf("ui.a.DID.Fragment: %s", ui.a.DID.Fragment)
log.Debugf("ui.e.ID: %s", ui.e.DID)
msg, err := msg.NewBroadcast(ui.a.DID.String(), ui.e.DID.String(), msgBytes, "text/plain", ui.a.Keyset.SigningKey.PrivKey)
msg, err := msg.New(ui.a.DID.String(), ui.e.DID.String(), msgBytes, "text/plain", ui.a.Keyset.SigningKey.PrivKey)
if err != nil {
log.Debugf("message creation error: %s", err)
return fmt.Errorf("message creation error: %w", err)
Expand Down
4 changes: 3 additions & 1 deletion ui/envelopes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func (ui *ChatUI) handleIncomingEnvelopes(a *entity.Entity) {
log.Debugf("Opened envelope and found message: %v\n", string(m.Content))

// Send the message to the actor for processing. It can decide to ignore it.
a.Messages <- m

ui.displayPrivateMessage(m)

}
}

Expand Down
7 changes: 7 additions & 0 deletions ui/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"time"

"github.com/bahner/go-ma"
)

// // displaySelfMessage writes a message from ourself to the message window,
Expand Down Expand Up @@ -40,6 +42,11 @@ func (ui *ChatUI) handleEvents() {
}

case m := <-ui.chMessage:
if m.MimeType == ma.BROADCAST_MIME_TYPE {
ui.displayBroadcastMessage(m)
continue
}

ui.displayChatMessage(m)

case <-peerRefreshTicker.C:
Expand Down
2 changes: 1 addition & 1 deletion ui/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (ui *ChatUI) handleMsgCommand(args []string) {
}
}

func (ui *ChatUI) handleHelpBroadcastCommand(args []string) {
func (ui *ChatUI) handleHelpMsgCommand(args []string) {
ui.displaySystemMessage("Usage: /broadcast <DID|NICK> <message>")
ui.displaySystemMessage("Sends a public announcement to the specified DID")
}

0 comments on commit 9e074e2

Please sign in to comment.