6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-30 18:51:03 +00:00
prologic-saltyim/readmsgs.go
2022-03-19 22:56:23 +10:00

45 lines
772 B
Go

package saltyim
import (
"fmt"
"os"
"git.mills.io/prologic/msgbus"
"git.mills.io/prologic/msgbus/client"
"github.com/keys-pub/keys"
"go.mills.io/salty"
)
func handleMessage(key *keys.EdX25519Key, msgs chan string) msgbus.HandlerFunc {
return func(msg *msgbus.Message) error {
data, _, err := salty.Decrypt(key, msg.Payload)
if err != nil {
fmt.Fprintf(os.Stderr, "error decrypting message")
return err
}
msgs <- string(data)
return nil
}
}
// Read ...
func Read(key *keys.EdX25519Key, uri, inbox string, stop chan struct{}) chan string {
client := client.NewClient(uri, nil)
msgs := make(chan string)
s := client.Subscribe(inbox, handleMessage(key, msgs))
s.Start()
go func() {
<-stop
s.Stop()
close(msgs)
}()
return msgs
}