Add instructions for private group chat & new endpoint

This commit is contained in:
Andrea Maria Piana 2022-10-24 10:25:20 +01:00
parent 62334fc3dd
commit 65a8d11030
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
5 changed files with 122 additions and 35 deletions

View File

@ -30,3 +30,41 @@ curl -XPOST http://localhost:8545 -H 'Content-type: application/json' -d '{"json
``` ```
Just replace `id` with the public key you want to use, and `message` with the text you want to send. Just replace `id` with the public key you want to use, and `message` with the text you want to send.
### Creating a private group chat
To create a private group chat, you need interactions on both devices.
First add the user as a contact:
```
curl -XPOST http://localhost:8545 -H 'Content-type: application/json' -d '{"jsonrpc":"2.0","method":"wakuext_sendContactRequest","params":[{"id": "0x04d3c86dfc77b195b705e1831935066076018aa0d7c40044829801ebbfe9b06480ce4662072bf16a3ca7cb8f6289207614deceaf7d33e099dfc9281610375fec08", "message": "hello"}],"id":1}'
```
Accept the contact request in the receiving device (you should see a notification in the activity center)
Then create a group chat with the member(s):
```
curl -XPOST http://localhost:8545 -H 'Content-type: application/json' -d '{"jsonrpc":"2.0","method":"wakuext_createGroupChatWithMembers","params":[null, "group-chat-name", ["0x04d3c86dfc77b195b705e1831935066076018aa0d7c40044829801ebbfe9b06480ce4662072bf16a3ca7cb8f6289207614deceaf7d33e099dfc9281610375fec08"]],"id":1}'
```
You will need to note the ID returned by the response, for example, in the response:
```
{"jsonrpc":"2.0","id":1,"result":{"chats":[{"id":"8291eae1-338c-4481-9997-04edd2d2bbed-0x0490cbce029eaf094c7f2dcf1feb2d60e91ab1498847eb29fa98cc5ea5a36666b3f9ada142f3080f5074abd942c863438f6af9475f30781790c7e36f9acd2ac93e","name":"group-chat-name",
```
The ID is:
```
"8291eae1-338c-4481-9997-04edd2d2bbed-0x0490cbce029eaf094c7f2dcf1feb2d60e91ab1498847eb29fa98cc5ea5a36666b3f9ada142f3080f5074abd942c863438f6af9475f30781790c7e36f9acd2ac93e"
```
You can then send messages to this group chat similarly as you send messages for 1-to-1 chats, using the id of the newly created chat:
```
curl -XPOST http://localhost:8545 -H 'Content-type: application/json' -d '{"jsonrpc":"2.0","method":"wakuext_sendGroupChatMessage","params":[{"id": "8291eae1-338c-4481-9997-04edd2d2bbed-0x0490cbce029eaf094c7f2dcf1feb2d60e91ab1498847eb29fa98cc5ea5a36666b3f9ada142f3080f5074abd942c863438f6af9475f30781790c7e36f9acd2ac93e", "message": "hello"}],"id":1}'
```
Mind that if you restart the node, you will need to create a new group chat, since we are currently not keeping storage on restart.

View File

@ -1888,41 +1888,6 @@ func (m *Messenger) SendChatMessage(ctx context.Context, message *common.Message
return m.sendChatMessage(ctx, message) return m.sendChatMessage(ctx, message)
} }
func (m *Messenger) SendOneToOneMessage(request *requests.SendOneToOneMessage) (*MessengerResponse, error) {
if err := request.Validate(); err != nil {
return nil, err
}
chatID := request.ID.String()
chat, ok := m.allChats.Load(chatID)
if !ok {
// Only one to one chan be muted when it's not in the database
publicKey, err := common.HexToPubkey(chatID)
if err != nil {
return nil, err
}
// Create a one to one chat
chat = CreateOneToOneChat(chatID, publicKey, m.getTimesource())
err = m.initChatSyncFields(chat)
if err != nil {
return nil, err
}
err = m.saveChat(chat)
if err != nil {
return nil, err
}
}
message := &common.Message{}
message.Text = request.Message
message.ChatId = chatID
message.ContentType = protobuf.ChatMessage_TEXT_PLAIN
return m.sendChatMessage(context.Background(), message)
}
// SendChatMessages takes a array of messages and sends it based on the corresponding chats // SendChatMessages takes a array of messages and sends it based on the corresponding chats
func (m *Messenger) SendChatMessages(ctx context.Context, messages []*common.Message) (*MessengerResponse, error) { func (m *Messenger) SendChatMessages(ctx context.Context, messages []*common.Message) (*MessengerResponse, error) {
var response MessengerResponse var response MessengerResponse

View File

@ -292,3 +292,59 @@ func (m *Messenger) applyDeleteForMeMessage(messageDeletes []*DeleteForMeMessage
return nil return nil
} }
func (m *Messenger) SendOneToOneMessage(request *requests.SendOneToOneMessage) (*MessengerResponse, error) {
if err := request.Validate(); err != nil {
return nil, err
}
chatID := request.ID.String()
chat, ok := m.allChats.Load(chatID)
if !ok {
// Only one to one chan be muted when it's not in the database
publicKey, err := common.HexToPubkey(chatID)
if err != nil {
return nil, err
}
// Create a one to one chat
chat = CreateOneToOneChat(chatID, publicKey, m.getTimesource())
err = m.initChatSyncFields(chat)
if err != nil {
return nil, err
}
err = m.saveChat(chat)
if err != nil {
return nil, err
}
}
message := &common.Message{}
message.Text = request.Message
message.ChatId = chatID
message.ContentType = protobuf.ChatMessage_TEXT_PLAIN
return m.sendChatMessage(context.Background(), message)
}
func (m *Messenger) SendGroupChatMessage(request *requests.SendGroupChatMessage) (*MessengerResponse, error) {
if err := request.Validate(); err != nil {
return nil, err
}
chatID := request.ID
_, ok := m.allChats.Load(chatID)
if !ok {
return nil, ErrChatNotFound
}
message := &common.Message{}
message.Text = request.Message
message.ChatId = chatID
message.ContentType = protobuf.ChatMessage_TEXT_PLAIN
return m.sendChatMessage(context.Background(), message)
}

View File

@ -0,0 +1,25 @@
package requests
import (
"errors"
)
var ErrSendGroupChatMessageInvalidID = errors.New("send-group-chat-message: invalid id")
var ErrSendGroupChatMessageInvalidMessage = errors.New("send-group-chat-message: invalid message")
type SendGroupChatMessage struct {
ID string `json:"id"`
Message string `json:"message"`
}
func (a *SendGroupChatMessage) Validate() error {
if len(a.ID) == 0 {
return ErrSendGroupChatMessageInvalidID
}
if len(a.Message) == 0 {
return ErrSendGroupChatMessageInvalidMessage
}
return nil
}

View File

@ -724,6 +724,9 @@ func (api *PublicAPI) SendOneToOneMessage(request *requests.SendOneToOneMessage)
return api.service.messenger.SendOneToOneMessage(request) return api.service.messenger.SendOneToOneMessage(request)
} }
func (api *PublicAPI) SendGroupChatMessage(request *requests.SendGroupChatMessage) (*protocol.MessengerResponse, error) {
return api.service.messenger.SendGroupChatMessage(request)
}
func (api *PublicAPI) EditMessage(ctx context.Context, request *requests.EditMessage) (*protocol.MessengerResponse, error) { func (api *PublicAPI) EditMessage(ctx context.Context, request *requests.EditMessage) (*protocol.MessengerResponse, error) {
return api.service.messenger.EditMessage(ctx, request) return api.service.messenger.EditMessage(ctx, request)