feature/basic-messages (#3)

* started basic message definitions

* returns message id
This commit is contained in:
Dean Eigenmann 2019-08-06 19:20:48 +02:00 committed by GitHub
parent 2fb2649a28
commit 5f869f2eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 13 deletions

View File

@ -37,27 +37,28 @@ func (c *Client) Invite(chat Chat, peer Peer) {
}
// Join joins a chat.
func (c *Client) Join(chat Chat) {
func (c *Client) Join(chat Chat) (state.MessageID, error) {
return c.send(chat, protobuf.Message_JOIN, c.node.ID[:])
}
// Leave leaves a chat.
func (c *Client) Leave(chat Chat) {
func (c *Client) Leave(chat Chat) (state.MessageID, error) {
return c.send(chat, protobuf.Message_LEAVE, c.node.ID[:])
}
// Kick kicks peer from a chat.
func (c *Client) Kick(chat Chat, peer Peer) {
func (c *Client) Kick(chat Chat, peer Peer) (state.MessageID, error) {
return c.send(chat, protobuf.Message_KICK, peer[:])
}
// Ack acknowledges `Join`, `Leave` and `Kick` messages.
func (c *Client) Ack(chat Chat, messageID state.MessageID) {
// @todo: we may not need this as we can rely on the acks of data sync
func (c *Client) Ack(chat Chat, messageID state.MessageID) (state.MessageID, error) {
// @todo We may not need this as we can rely on the acks of data sync
return c.send(chat, protobuf.Message_ACK, messageID[:])
}
// Post sends a message to a chat.
func (c *Client) Post(chat Chat, body []byte) error {
func (c *Client) Post(chat Chat, body []byte) (state.MessageID, error) {
return c.send(chat, protobuf.Message_POST, body)
}
@ -71,7 +72,7 @@ func (c *Client) Listen() {
}
}
func (c *Client) send(chat Chat, t protobuf.Message_MessageType, body []byte) error {
func (c *Client) send(chat Chat, t protobuf.Message_MessageType, body []byte) (state.MessageID, error) {
lastMessage := c.lastMessages[chat]
msg := &protobuf.Message{
MessageType: protobuf.Message_MessageType(t),
@ -86,17 +87,17 @@ func (c *Client) send(chat Chat, t protobuf.Message_MessageType, body []byte) er
buf, err := proto.Marshal(msg)
if err != nil {
return errors.Wrap(err, "failed to marshall message")
return state.MessageID{}, errors.Wrap(err, "failed to marshall message")
}
id, err := c.node.AppendMessage(state.GroupID(chat), buf)
if err != nil {
return errors.Wrap(err, "failed to append message")
return state.MessageID{}, errors.Wrap(err, "failed to append message")
}
c.lastMessages[chat] = id
return nil
return id, nil
}
// onReceive handles lower level message receiving logic, such as requesting all previous message dependencies that we