Skip to content

Commit

Permalink
Merge pull request #11 from gender-equality-community/set_presence
Browse files Browse the repository at this point in the history
Set presence when message is received
  • Loading branch information
jspc authored Oct 1, 2022
2 parents fbb6e75 + f55eda0 commit 63afb84
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type whatsappClient interface {
Disconnect()
MarkRead([]string, time.Time, types.JID, types.JID) error
SendMessage(context.Context, types.JID, string, *waProto.Message) (whatsmeow.SendResponse, error)
SendPresence(types.Presence) error
}

type Client struct {
Expand Down Expand Up @@ -79,6 +80,9 @@ func (c *Client) disconnect() {
}

func (c Client) handler(evt interface{}) {
// #nosec
c.c.SendPresence(types.PresenceAvailable)

switch v := evt.(type) {
case *events.Message:
c.handleMessage(v)
Expand Down
19 changes: 16 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
)

type dummyClient struct {
err bool
read bool
msg string
err bool
read bool
msg string
presence bool
}

func (dummyClient) AddEventHandler(handler whatsmeow.EventHandler) uint32 { return 1 }
Expand Down Expand Up @@ -59,6 +60,12 @@ func (c *dummyClient) SendMessage(_ context.Context, _ types.JID, _ string, msg
return
}

func (c *dummyClient) SendPresence(types.Presence) error {
c.presence = true

return nil
}

func TestNew(t *testing.T) {
defer func() {
err := recover()
Expand Down Expand Up @@ -113,6 +120,12 @@ func TestClient_Handle(t *testing.T) {
}
})

t.Run("presence is set", func(t *testing.T) {
if !test.wc.presence {
t.Error("presence was not set")
}
})

t.Run("correct message is sent", func(t *testing.T) {
if test.expectResponse != test.wc.msg {
t.Errorf("expected %q, received %q", test.expectResponse, test.wc.msg)
Expand Down

0 comments on commit 63afb84

Please sign in to comment.