core: Minimal message-sending functionality

This is broken in many ways.
This commit is contained in:
John Brooks 2016-10-06 16:50:07 -07:00
parent c50b1f29d4
commit ee0c19b28b
3 changed files with 52 additions and 4 deletions

View File

@ -170,5 +170,25 @@ func (s *RpcServer) MonitorConversations(req *rpc.MonitorConversationsRequest, s
}
func (s *RpcServer) SendMessage(ctx context.Context, req *rpc.Message) (*rpc.Message, error) {
return nil, NotImplementedError
if req.Sender == nil || !req.Sender.IsSelf {
return nil, errors.New("Invalid message sender")
} else if req.Recipient == nil || req.Recipient.IsSelf {
return nil, errors.New("Invalid message recipient")
}
contact := s.core.Identity.ContactList().ContactByAddress(req.Recipient.Address)
if contact == nil || (req.Recipient.ContactId != 0 && int32(contact.Id()) != req.Recipient.ContactId) {
return nil, errors.New("Unknown recipient")
}
// XXX timestamp
// XXX validate text
// XXX identifier
message, err := contact.Conversation().Send(req.Text)
if err != nil {
return nil, err
}
return message, nil
}

View File

@ -125,6 +125,13 @@ func (c *Contact) Conversation() *Conversation {
return c.conversation
}
// XXX Thread safety disaster
func (c *Contact) Connection() *protocol.OpenConnection {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.connection
}
// Goroutine to handle the protocol connection for a contact.
// Responsible for making outbound connections and taking over authenticated
// inbound connections, running protocol handlers on the active connection, and

View File

@ -88,7 +88,7 @@ func (c *Conversation) UpdateSentStatus(id uint64, success bool) {
c.mutex.Unlock()
}
func (c *Conversation) Send(text string) {
func (c *Conversation) Send(text string) (*ricochet.Message, error) {
// XXX protocol
// XXX check that text is ok, get identifier, etc
// XXX decide whether sending or queued based on state
@ -96,10 +96,29 @@ func (c *Conversation) Send(text string) {
Sender: c.localEntity,
Recipient: c.remoteEntity,
Timestamp: time.Now().Unix(),
Identifier: 0, // XXX
Status: ricochet.Message_SENDING, // XXX
Identifier: 0, // XXX
Status: ricochet.Message_QUEUED,
Text: text,
}
// XXX witness thread disaster
conn := c.Contact.Connection()
if conn != nil {
// XXX hardcoded channel IDs, also channel IDs shouldn't be exposed
channelId := int32(7)
if !conn.Client {
channelId++
}
// XXX no error handling
if conn.GetChannelType(channelId) != "im.ricochet.chat" {
conn.OpenChatChannel(channelId)
}
// XXX no message IDs, no acks
conn.SendMessage(channelId, text)
message.Status = ricochet.Message_SENDING
}
c.mutex.Lock()
c.messages = append(c.messages, message)
log.Printf("Conversation sent message: %v", message)
@ -110,4 +129,6 @@ func (c *Conversation) Send(text string) {
Msg: message,
}
conversationStream.Publish(event)
return message, nil
}