core: Minimal message-sending functionality
This is broken in many ways.
This commit is contained in:
parent
c50b1f29d4
commit
ee0c19b28b
|
@ -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) {
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,6 +125,13 @@ func (c *Contact) Conversation() *Conversation {
|
||||||
return c.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.
|
// Goroutine to handle the protocol connection for a contact.
|
||||||
// Responsible for making outbound connections and taking over authenticated
|
// Responsible for making outbound connections and taking over authenticated
|
||||||
// inbound connections, running protocol handlers on the active connection, and
|
// inbound connections, running protocol handlers on the active connection, and
|
||||||
|
|
|
@ -88,7 +88,7 @@ func (c *Conversation) UpdateSentStatus(id uint64, success bool) {
|
||||||
c.mutex.Unlock()
|
c.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conversation) Send(text string) {
|
func (c *Conversation) Send(text string) (*ricochet.Message, error) {
|
||||||
// XXX protocol
|
// XXX protocol
|
||||||
// XXX check that text is ok, get identifier, etc
|
// XXX check that text is ok, get identifier, etc
|
||||||
// XXX decide whether sending or queued based on state
|
// XXX decide whether sending or queued based on state
|
||||||
|
@ -97,9 +97,28 @@ func (c *Conversation) Send(text string) {
|
||||||
Recipient: c.remoteEntity,
|
Recipient: c.remoteEntity,
|
||||||
Timestamp: time.Now().Unix(),
|
Timestamp: time.Now().Unix(),
|
||||||
Identifier: 0, // XXX
|
Identifier: 0, // XXX
|
||||||
Status: ricochet.Message_SENDING, // XXX
|
Status: ricochet.Message_QUEUED,
|
||||||
Text: text,
|
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.mutex.Lock()
|
||||||
c.messages = append(c.messages, message)
|
c.messages = append(c.messages, message)
|
||||||
log.Printf("Conversation sent message: %v", message)
|
log.Printf("Conversation sent message: %v", message)
|
||||||
|
@ -110,4 +129,6 @@ func (c *Conversation) Send(text string) {
|
||||||
Msg: message,
|
Msg: message,
|
||||||
}
|
}
|
||||||
conversationStream.Publish(event)
|
conversationStream.Publish(event)
|
||||||
|
|
||||||
|
return message, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue