mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-16 11:58:24 +00:00
feat: sbscribe to outbox (#128)
Co-authored-by: Jon Lundy <jon@xuu.cc> Co-authored-by: xuu <me@sour.is> Reviewed-on: https://git.mills.io/saltyim/saltyim/pulls/128 Reviewed-by: James Mills <james@mills.io> Co-authored-by: xuu <xuu@noreply@mills.io> Co-committed-by: xuu <xuu@noreply@mills.io>
This commit is contained in:
parent
c7ad4734b3
commit
35c4ecca6f
40
client.go
40
client.go
@ -60,6 +60,7 @@ func parseExtraEnvs(extraenvs string) map[string]string {
|
||||
// PackMessage formts an outoing message in the Message Format
|
||||
// <timestamp>\t(<sender>) <message>
|
||||
func PackMessage(me *Addr, msg string) []byte {
|
||||
log.Print("pack: ", me.Formatted(), msg)
|
||||
return []byte(
|
||||
fmt.Sprint(
|
||||
time.Now().UTC().Format(time.RFC3339), "\t",
|
||||
@ -247,6 +248,43 @@ func (cli *Client) OutboxAddr(to *Addr) *Addr {
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *Client) OutboxClient(to *Addr) *Client {
|
||||
if to == nil {
|
||||
to = cli.me
|
||||
}
|
||||
|
||||
return &Client{
|
||||
me: &Addr{
|
||||
User: to.User,
|
||||
Domain: to.Domain,
|
||||
key: cli.me.key,
|
||||
endpoint: cli.Outbox(),
|
||||
discoveredDomain: cli.me.discoveredDomain,
|
||||
avatar: cli.me.avatar,
|
||||
capabilities: cli.me.capabilities,
|
||||
checkedAvatar: cli.me.checkedAvatar,
|
||||
},
|
||||
id: &Identity{
|
||||
addr: &Addr{
|
||||
User: to.User,
|
||||
Domain: to.Domain,
|
||||
key: cli.me.key,
|
||||
endpoint: cli.Outbox(),
|
||||
discoveredDomain: cli.me.discoveredDomain,
|
||||
avatar: cli.me.avatar,
|
||||
capabilities: cli.me.capabilities,
|
||||
checkedAvatar: cli.me.checkedAvatar,
|
||||
},
|
||||
key: cli.key,
|
||||
},
|
||||
key: cli.key,
|
||||
cache: cli.cache,
|
||||
state: NewState(),
|
||||
lookup: cli.lookup,
|
||||
send: cli.send,
|
||||
}
|
||||
}
|
||||
|
||||
func (cli *Client) String() string {
|
||||
b := &bytes.Buffer{}
|
||||
fmt.Fprintln(b, "Me: ", cli.me)
|
||||
@ -333,7 +371,7 @@ func (cli *Client) Send(user, msg string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return cli.SendToAddr(cli.OutboxAddr(addr), msg)
|
||||
return cli.OutboxClient(addr).SendToAddr(cli.OutboxAddr(addr), msg)
|
||||
}
|
||||
|
||||
func (cli *Client) SendToAddr(addr *Addr, msg string) error {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package components
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/maxence-charriere/go-app/v9/pkg/app"
|
||||
@ -15,8 +14,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
descText = `salty.im is an open specification for a new Saltpack based e2e encrypted messaging protocol and platform for secure communications with a focus on privacy, security and being self-hosted.`
|
||||
saltyChatMessageAction = "saltychat.message.action"
|
||||
descText = `salty.im is an open specification for a new Saltpack based e2e encrypted messaging protocol and platform for secure communications with a focus on privacy, security and being self-hosted.`
|
||||
saltyChatRecvMessageAction = "saltychat.recv-message.action"
|
||||
saltyChatSentMessageAction = "saltychat.sent-message.action"
|
||||
)
|
||||
|
||||
var client *saltyim.Client
|
||||
@ -84,7 +84,8 @@ func (h *SaltyChat) OnMount(ctx app.Context) {
|
||||
} else {
|
||||
log.Println("app not running as a client?")
|
||||
}
|
||||
ctx.Handle(saltyChatMessageAction, h.incomingMessage)
|
||||
ctx.Handle(saltyChatRecvMessageAction, h.incomingMessage)
|
||||
ctx.Handle(saltyChatSentMessageAction, h.outgoingMessage)
|
||||
}
|
||||
|
||||
func (h *SaltyChat) connect(ctx app.Context) {
|
||||
@ -113,22 +114,32 @@ func (h *SaltyChat) connect(ctx app.Context) {
|
||||
client = newClient
|
||||
|
||||
ctx.Async(func() {
|
||||
for msg := range client.Subscribe(context.Background(), "", "", "") {
|
||||
// passing both the message and the text in case we need the message key at some point
|
||||
ctx.NewActionWithValue(saltyChatMessageAction, msg, app.T("text", msg.Text))
|
||||
inboxCh := client.Subscribe(ctx, "", "", "")
|
||||
outboxCh := client.OutboxClient(nil).Subscribe(ctx, "", "", "")
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case msg := <-inboxCh:
|
||||
// passing both the message and the text in case we need the message key at some point
|
||||
ctx.NewActionWithValue(saltyChatRecvMessageAction, msg, app.T("text", msg.Text))
|
||||
case msg := <-outboxCh:
|
||||
// passing both the message and the text in case we need the message key at some point
|
||||
ctx.NewActionWithValue(saltyChatSentMessageAction, msg, app.T("text", msg.Text))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (h *SaltyChat) incomingMessage(ctx app.Context, action app.Action) {
|
||||
|
||||
messageText := action.Tags.Get("text")
|
||||
s, err := lextwt.ParseSalty(messageText)
|
||||
if err != nil {
|
||||
h.dialog.ShowDialog("incoming message error", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
log.Print("in: ", s)
|
||||
switch s := s.(type) {
|
||||
case *lextwt.SaltyText:
|
||||
user := s.User.String()
|
||||
@ -148,7 +159,24 @@ func (h *SaltyChat) incomingMessage(ctx app.Context, action app.Action) {
|
||||
log.Printf("new incoming chat from %s", user)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SaltyChat) outgoingMessage(ctx app.Context, action app.Action) {
|
||||
messageText := action.Tags.Get("text")
|
||||
s, err := lextwt.ParseSalty(messageText)
|
||||
if err != nil {
|
||||
h.dialog.ShowDialog("outgoing message error", err.Error())
|
||||
return
|
||||
}
|
||||
log.Print("out:", s)
|
||||
switch s := s.(type) {
|
||||
case *lextwt.SaltyText:
|
||||
friend := s.User.String()
|
||||
storage.ContactsLocalStorage(ctx).Add(friend)
|
||||
storage.ConversationsLocalStorage(ctx, friend).
|
||||
Append(string(saltyim.PackMessage(client.Me(), s.LiteralText())))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SaltyChat) Render() app.UI {
|
||||
|
@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:4f36963d6c7744e1a470336f6b0ed62360d43baa4451672e6e2cdce4b6333507
|
||||
size 28571892
|
||||
oid sha256:2ec05cfce9cd986c818ce909a2b0048f032f71df80c50497f15d3c7aaf1e9ef2
|
||||
size 28584522
|
||||
|
Loading…
Reference in New Issue
Block a user