Skip to content

Commit

Permalink
Merge pull request #23 from deltachat/adb/update-api
Browse files Browse the repository at this point in the history
api: update to new JSON-RPC API (core v1.34.0)
  • Loading branch information
Asiel Díaz Benítez authored Feb 3, 2024
2 parents ca6881d + d6636ee commit c7ed4b2
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21

- name: Test
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,4 @@ Check the complete example at [examples/echobot_full](https://github.com/deltach
## Contributing
Pull requests are welcome! check [CONTRIBUTING.md](https://github.com/deltachat/deltachat-rpc-client-go/blob/master/CONTRIBUTING.md)
Pull requests are welcome! check [CONTRIBUTING.md](./CONTRIBUTING.md)
36 changes: 29 additions & 7 deletions deltachat/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,20 @@ func (self *Rpc) GetChatEphemeralTimer(accountId AccountId, chatId ChatId) (uint
return timer, err
}

// for now only text messages, because we only used text messages in desktop thusfar
func (self *Rpc) AddDeviceMessage(accountId AccountId, label string, text string) (MsgId, error) {
// Add a message to the device-chat.
// Device-messages usually contain update information
// and some hints that are added during the program runs, multi-device etc.
// The device-message may be defined by a label;
// if a message with the same label was added or skipped before,
// the message is not added again, even if the message was deleted in between.
// If needed, the device-chat is created before.
//
// Sends the `MsgsChanged` event on success.
//
// Setting msg to None will prevent the device message with this label from being added in the future.
func (self *Rpc) AddDeviceMessage(accountId AccountId, label string, msg option.Option[MsgData]) (MsgId, error) {
var id MsgId
err := self.Transport.CallResult(self.Context, &id, "add_device_message", accountId, label, text)
err := self.Transport.CallResult(self.Context, &id, "add_device_message", accountId, label, msg)
return id, err
}

Expand Down Expand Up @@ -548,6 +558,9 @@ func (self *Rpc) GetMessageInfo(accountId AccountId, msgId MsgId) (string, error
return info, err
}

// TODO: get_message_info_object
// TODO: get_message_read_receipts

// Asks the core to start downloading a message fully.
// This function is typically called when the user hits the "Download" button
// that is shown by the UI in case `download_state` is `'Available'` or `'Failure'`
Expand Down Expand Up @@ -669,6 +682,8 @@ func (self *Rpc) LookupContactIdByAddr(accountId AccountId, addr string) (option
// chat
// ---------------------------------------------

// TODO: get_chat_id_by_contact_id

// Returns all message IDs of the given types in a chat.
// Typically used to show a gallery.
//
Expand Down Expand Up @@ -884,9 +899,9 @@ func (self *Rpc) GetMessageReactions(accountId AccountId, msgId MsgId) (option.O
}

// Send a message and return the resulting Message instance.
func (self *Rpc) SendMsg(accountId AccountId, chatId ChatId, msgData MsgData) (MsgId, error) {
func (self *Rpc) SendMsg(accountId AccountId, chatId ChatId, data MsgData) (MsgId, error) {
var id MsgId
err := self.Transport.CallResult(self.Context, &id, "send_msg", accountId, chatId, msgData)
err := self.Transport.CallResult(self.Context, &id, "send_msg", accountId, chatId, data)
return id, err
}

Expand Down Expand Up @@ -941,6 +956,13 @@ func (self *Rpc) MiscSendTextMessage(accountId AccountId, chatId ChatId, text st
// the better version should support:
// - changing viewtype to enable/disable compression
// - keeping same message id as long as attachment does not change for webxdc messages
func (self *Rpc) MiscSetDraft(accountId AccountId, chatId ChatId, text option.Option[string], file option.Option[string], quotedMessageId option.Option[MsgId]) error {
return self.Transport.Call(self.Context, "misc_set_draft", accountId, chatId, text, file, quotedMessageId)
func (self *Rpc) MiscSetDraft(accountId AccountId, chatId ChatId, text option.Option[string], file option.Option[string], quotedMessageId option.Option[MsgId], viewType option.Option[MsgType]) error {
return self.Transport.Call(self.Context, "misc_set_draft", accountId, chatId, text, file, quotedMessageId, viewType)
}

// send the chat's current set draft
func (self *Rpc) MiscSendDraft(accountId AccountId, chatId ChatId) (MsgId, error) {
var id MsgId
err := self.Transport.CallResult(self.Context, &id, "misc_send_draft", accountId, chatId)
return id, err
}
65 changes: 44 additions & 21 deletions deltachat/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,41 @@ import (

"github.com/deltachat/deltachat-rpc-client-go/deltachat/option"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRpc_CheckEmailValidity(t *testing.T) {
t.Parallel()
acfactory.WithOnlineAccount(func(rpc *Rpc, accId AccountId) {
valid, err := rpc.CheckEmailValidity("[email protected]")
require.Nil(t, err)
require.True(t, valid)
})
}

func TestRpc_MiscSetDraft_and_MiscSendDraft(t *testing.T) {
t.Parallel()
acfactory.WithOnlineAccount(func(rpc *Rpc, accId AccountId) {
chatId, err := rpc.CreateGroupChat(accId, "test group", true)
require.Nil(t, err)
err = rpc.MiscSetDraft(accId, chatId, option.Some("test"), option.None[string](), option.None[MsgId](), option.None[MsgType]())
require.Nil(t, err)
_, err = rpc.MiscSendDraft(accId, chatId)
require.Nil(t, err)
})
}

func TestRpc_SetChatVisibility(t *testing.T) {
t.Parallel()
acfactory.WithOnlineAccount(func(rpc *Rpc, accId AccountId) {
chatId, err := rpc.CreateGroupChat(accId, "test group", true)
assert.Nil(t, err)
assert.Nil(t, rpc.SetChatVisibility(accId, chatId, ChatVisibilityPinned))
assert.Nil(t, rpc.SetChatVisibility(accId, chatId, ChatVisibilityArchived))
assert.Nil(t, rpc.SetChatVisibility(accId, chatId, ChatVisibilityNormal))
})
}

func TestAccount_Select(t *testing.T) {
t.Parallel()
acfactory.WithRpc(func(rpc *Rpc) {
Expand Down Expand Up @@ -41,16 +74,17 @@ func TestAccount_Connectivity(t *testing.T) {
t.Parallel()
acfactory.WithUnconfiguredAccount(func(rpc *Rpc, accId AccountId) {
conn, err := rpc.GetConnectivity(accId)
assert.Nil(t, err)
assert.True(t, conn > 0)
require.Nil(t, err)
require.Greater(t, conn, uint(0))

_, err = rpc.GetConnectivityHtml(accId)
assert.NotNil(t, err)
html, err := rpc.GetConnectivityHtml(accId)
require.Nil(t, err)
require.NotEmpty(t, html)
})
acfactory.WithOnlineAccount(func(rpc *Rpc, accId AccountId) {
html, err := rpc.GetConnectivityHtml(accId)
assert.Nil(t, err)
assert.NotEmpty(t, html)
require.Nil(t, err)
require.NotEmpty(t, html)
})
}

Expand Down Expand Up @@ -395,22 +429,11 @@ func TestAccount_GetChatlistEntries(t *testing.T) {
func TestAccount_AddDeviceMsg(t *testing.T) {
t.Parallel()
acfactory.WithOnlineAccount(func(rpc *Rpc, accId AccountId) {
msgId, err := rpc.AddDeviceMessage(accId, "test", "new message")
assert.Nil(t, err)
msgId, err := rpc.AddDeviceMessage(accId, "test", option.Some(MsgData{Text: "new message"}))
require.Nil(t, err)
msg, err := rpc.GetMessage(accId, msgId)
assert.Nil(t, err)
assert.Equal(t, msg.Text, "new message")
})
}

func TestRpc_SetChatVisibility(t *testing.T) {
t.Parallel()
acfactory.WithOnlineAccount(func(rpc *Rpc, accId AccountId) {
chatId, err := rpc.CreateGroupChat(accId, "test group", true)
assert.Nil(t, err)
assert.Nil(t, rpc.SetChatVisibility(accId, chatId, ChatVisibilityPinned))
assert.Nil(t, rpc.SetChatVisibility(accId, chatId, ChatVisibilityArchived))
assert.Nil(t, rpc.SetChatVisibility(accId, chatId, ChatVisibilityNormal))
require.Nil(t, err)
require.Equal(t, msg.Text, "new message")
})
}

Expand Down
31 changes: 16 additions & 15 deletions deltachat/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ type Account struct {

// Delta Chat Contact snapshot.
type ContactSnapshot struct {
Address string
Color string
AuthName string
Status string
DisplayName string
Id ContactId
Name string
ProfileImage string
NameAndAddr string
IsBlocked bool
IsVerified bool
VerifierAddr string
VerifierId ContactId
LastSeen Timestamp
WasSeenRecently bool
Address string
Color string
AuthName string
Status string
DisplayName string
Id ContactId
Name string
ProfileImage string
NameAndAddr string
IsBlocked bool
IsVerified bool
IsProfileVerified bool
VerifierId ContactId
LastSeen Timestamp
WasSeenRecently bool
IsBot bool
}

// Full chat snapshot.
Expand Down
10 changes: 6 additions & 4 deletions examples/echobot_full/go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
module echobot

go 1.19
go 1.21

toolchain go1.21.0

require (
github.com/deltachat/deltachat-rpc-client-go v0.17.1-0.20230731132031-99c0b7b46920
github.com/stretchr/testify v1.8.2
)

require (
github.com/creachadair/jrpc2 v1.1.0 // indirect
github.com/creachadair/mds v0.1.0 // indirect
github.com/creachadair/jrpc2 v1.1.2 // indirect
github.com/creachadair/mds v0.8.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sync v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
22 changes: 9 additions & 13 deletions examples/echobot_full/go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
github.com/creachadair/jrpc2 v1.0.0 h1:MJecUFNbda24o0Qa4CYru5NJvs6BEnmzupdAx3f7+98=
github.com/creachadair/jrpc2 v1.0.0/go.mod h1:VNiDz7Qbua37RrefUJdMki9dVmsbrQPIQOPy907UvtE=
github.com/creachadair/jrpc2 v1.1.0 h1:SgpJf0v1rVCZx68+4APv6dgsTFsIHlpgFD1NlQAWA0A=
github.com/creachadair/jrpc2 v1.1.0/go.mod h1:5jN7MKwsm8qvgfTsTzLX3JIfidsAkZ1c8DZSQmp+g38=
github.com/creachadair/mds v0.0.1 h1:2nX6Sww4dXpScx3b6aYjH1n7iuEH715+jj+cKkKw9BY=
github.com/creachadair/mds v0.0.1/go.mod h1:caBACU+n1Q/rZ252FTzfnG0/H+ZUi+UnIQtEOraMv/g=
github.com/creachadair/mds v0.1.0 h1:XgPCNnq1T+IW+HaiIufqQMRuqenKoZAZXkVShj/m/Wg=
github.com/creachadair/mds v0.1.0/go.mod h1:caBACU+n1Q/rZ252FTzfnG0/H+ZUi+UnIQtEOraMv/g=
github.com/creachadair/jrpc2 v1.1.2 h1:UOYMipEFYlwd5qmcvs9GZBurn3oXt1UDIX5JLjWWFzo=
github.com/creachadair/jrpc2 v1.1.2/go.mod h1:JcCe2Eny3lIvVwZLm92WXyU+tNUgTBWFCLMsfNkjEGk=
github.com/creachadair/mds v0.8.2 h1:+Jvq8XBrREerXI/QZpNAeiLjIBuVMOl8p3v+mKgSexY=
github.com/creachadair/mds v0.8.2/go.mod h1:4vrFYUzTXMJpMBU+OA292I6IUxKWCCfZkgXg+/kBZMo=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -20,10 +18,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ module github.com/deltachat/deltachat-rpc-client-go

go 1.21

toolchain go1.21.0

require (
github.com/creachadair/jrpc2 v1.0.0
github.com/creachadair/jrpc2 v1.1.2
github.com/stretchr/testify v1.8.2
)

require (
github.com/creachadair/mds v0.0.1 // indirect
github.com/creachadair/mds v0.8.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sync v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 9 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
github.com/creachadair/jrpc2 v1.0.0 h1:MJecUFNbda24o0Qa4CYru5NJvs6BEnmzupdAx3f7+98=
github.com/creachadair/jrpc2 v1.0.0/go.mod h1:VNiDz7Qbua37RrefUJdMki9dVmsbrQPIQOPy907UvtE=
github.com/creachadair/mds v0.0.1 h1:2nX6Sww4dXpScx3b6aYjH1n7iuEH715+jj+cKkKw9BY=
github.com/creachadair/mds v0.0.1/go.mod h1:caBACU+n1Q/rZ252FTzfnG0/H+ZUi+UnIQtEOraMv/g=
github.com/creachadair/jrpc2 v1.1.2 h1:UOYMipEFYlwd5qmcvs9GZBurn3oXt1UDIX5JLjWWFzo=
github.com/creachadair/jrpc2 v1.1.2/go.mod h1:JcCe2Eny3lIvVwZLm92WXyU+tNUgTBWFCLMsfNkjEGk=
github.com/creachadair/mds v0.8.2 h1:+Jvq8XBrREerXI/QZpNAeiLjIBuVMOl8p3v+mKgSexY=
github.com/creachadair/mds v0.8.2/go.mod h1:4vrFYUzTXMJpMBU+OA292I6IUxKWCCfZkgXg+/kBZMo=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -16,8 +18,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
13 changes: 9 additions & 4 deletions scripts/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fi
if ! command -v golangci-lint &> /dev/null
then
# binary will be $(go env GOPATH)/bin/golangci-lint
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.52.2
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.55.2
fi

if ! golangci-lint run
Expand All @@ -21,7 +21,9 @@ fi
if ! command -v deltachat-rpc-server &> /dev/null
then
echo "deltachat-rpc-server could not be found, installing..."
pip3 install -U deltachat-rpc-server
curl -L https://github.com/deltachat/deltachat-core-rust/releases/latest/download/deltachat-rpc-server-x86_64-linux --output deltachat-rpc-server
chmod +x deltachat-rpc-server
export PATH=`pwd`:"$PATH"
fi

if ! command -v courtney &> /dev/null
Expand All @@ -30,20 +32,23 @@ then
go install github.com/dave/courtney@latest
fi

# test examples
for i in ./examples/*.go
do
echo "Testing examples: $i"
if ! go build -v "$i"
then
exit 1
fi
done
cd examples/echobot_full/
echobot_full="examples/echobot_full/"
echo "Testing examples: $echobot_full"
cd $echobot_full
if ! go test -v
then
exit 1
fi
cd ../..
echo "Done testing examples"

courtney -v -t="./..." ${TEST_EXTRA_TAGS:--t="-parallel=1"}
go tool cover -func=coverage.out -o=coverage-percent.out

0 comments on commit c7ed4b2

Please sign in to comment.