2016-10-05 21:38:18 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2016-10-16 00:04:19 +00:00
|
|
|
"errors"
|
|
|
|
protocol "github.com/s-rah/go-ricochet"
|
2016-10-17 04:26:35 +00:00
|
|
|
"github.com/ricochet-im/ricochet-go/core/utils"
|
|
|
|
"github.com/ricochet-im/ricochet-go/rpc"
|
2016-10-05 21:38:18 +00:00
|
|
|
"log"
|
2016-10-16 00:04:19 +00:00
|
|
|
"math/rand"
|
2016-10-05 21:38:18 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-10-16 00:04:19 +00:00
|
|
|
// XXX should have limits on backlog size/duration
|
2016-10-05 21:38:18 +00:00
|
|
|
|
|
|
|
type Conversation struct {
|
|
|
|
Contact *Contact
|
|
|
|
|
|
|
|
mutex sync.Mutex
|
|
|
|
|
2016-10-16 00:04:19 +00:00
|
|
|
localEntity *ricochet.Entity
|
|
|
|
remoteEntity *ricochet.Entity
|
|
|
|
messages []*ricochet.Message
|
|
|
|
lastSentMessageId uint32
|
|
|
|
|
|
|
|
events *utils.Publisher
|
2016-10-05 21:38:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 00:04:19 +00:00
|
|
|
func NewConversation(contact *Contact, remoteEntity *ricochet.Entity, eventStream *utils.Publisher) *Conversation {
|
2016-10-05 21:38:18 +00:00
|
|
|
return &Conversation{
|
|
|
|
Contact: contact,
|
|
|
|
localEntity: &ricochet.Entity{IsSelf: true},
|
|
|
|
remoteEntity: remoteEntity,
|
|
|
|
messages: make([]*ricochet.Message, 0),
|
2016-10-16 00:04:19 +00:00
|
|
|
events: eventStream,
|
2016-10-05 21:38:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 00:04:19 +00:00
|
|
|
func (c *Conversation) EventMonitor() utils.Subscribable {
|
|
|
|
return c.events
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conversation) Messages() []*ricochet.Message {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
re := make([]*ricochet.Message, 0, len(c.messages))
|
|
|
|
for _, message := range c.messages {
|
|
|
|
re = append(re, message)
|
|
|
|
}
|
|
|
|
return re
|
2016-10-05 21:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conversation) Receive(id uint64, timestamp int64, text string) {
|
|
|
|
message := &ricochet.Message{
|
|
|
|
Sender: c.remoteEntity,
|
|
|
|
Recipient: c.localEntity,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
Identifier: id,
|
2016-10-16 00:57:08 +00:00
|
|
|
Status: ricochet.Message_UNREAD,
|
2016-10-05 21:38:18 +00:00
|
|
|
Text: text,
|
|
|
|
}
|
2016-10-16 00:04:19 +00:00
|
|
|
|
2016-10-05 21:38:18 +00:00
|
|
|
c.mutex.Lock()
|
2016-10-16 00:04:19 +00:00
|
|
|
defer c.mutex.Unlock()
|
2016-10-05 21:38:18 +00:00
|
|
|
|
2016-10-16 00:04:19 +00:00
|
|
|
c.messages = append(c.messages, message)
|
2016-10-05 21:38:18 +00:00
|
|
|
event := ricochet.ConversationEvent{
|
|
|
|
Type: ricochet.ConversationEvent_RECEIVE,
|
|
|
|
Msg: message,
|
|
|
|
}
|
2016-10-16 00:04:19 +00:00
|
|
|
c.events.Publish(event)
|
2016-10-05 21:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Conversation) UpdateSentStatus(id uint64, success bool) {
|
|
|
|
c.mutex.Lock()
|
2016-10-16 00:04:19 +00:00
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
for i := len(c.messages) - 1; i >= 0; i-- {
|
|
|
|
message := c.messages[i]
|
2016-10-05 21:38:18 +00:00
|
|
|
if message.Status != ricochet.Message_SENDING || message.Identifier != id {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if success {
|
|
|
|
message.Status = ricochet.Message_DELIVERED
|
|
|
|
} else {
|
|
|
|
message.Status = ricochet.Message_ERROR
|
|
|
|
}
|
|
|
|
|
|
|
|
event := ricochet.ConversationEvent{
|
|
|
|
Type: ricochet.ConversationEvent_UPDATE,
|
|
|
|
Msg: message,
|
|
|
|
}
|
2016-10-16 00:04:19 +00:00
|
|
|
c.events.Publish(event)
|
2016-10-05 21:38:18 +00:00
|
|
|
return
|
|
|
|
}
|
2016-10-16 00:04:19 +00:00
|
|
|
|
|
|
|
log.Printf("Ignoring ack for unknown message id %d", id)
|
2016-10-05 21:38:18 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 23:50:07 +00:00
|
|
|
func (c *Conversation) Send(text string) (*ricochet.Message, error) {
|
2016-10-16 00:04:19 +00:00
|
|
|
if len(text) == 0 {
|
|
|
|
return nil, errors.New("Message text is empty")
|
|
|
|
} else if len(text) > 2000 {
|
|
|
|
return nil, errors.New("Message is too long")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
if c.lastSentMessageId == 0 {
|
|
|
|
// Rand is seeded by Ricochet.Init
|
|
|
|
c.lastSentMessageId = rand.Uint32()
|
|
|
|
}
|
|
|
|
if c.lastSentMessageId++; c.lastSentMessageId == 0 {
|
|
|
|
c.lastSentMessageId++
|
|
|
|
}
|
|
|
|
|
2016-10-05 21:38:18 +00:00
|
|
|
message := &ricochet.Message{
|
|
|
|
Sender: c.localEntity,
|
|
|
|
Recipient: c.remoteEntity,
|
|
|
|
Timestamp: time.Now().Unix(),
|
2016-10-16 00:04:19 +00:00
|
|
|
Identifier: uint64(c.lastSentMessageId),
|
2016-10-06 23:50:07 +00:00
|
|
|
Status: ricochet.Message_QUEUED,
|
2016-10-05 21:38:18 +00:00
|
|
|
Text: text,
|
|
|
|
}
|
2016-10-06 23:50:07 +00:00
|
|
|
|
2016-10-16 00:04:19 +00:00
|
|
|
// XXX threading mess, and probably deadlockable. Need better API for conn.
|
2016-10-06 23:50:07 +00:00
|
|
|
conn := c.Contact.Connection()
|
|
|
|
if conn != nil {
|
2016-10-16 00:04:19 +00:00
|
|
|
sendMessageToConnection(conn, message)
|
2016-10-06 23:50:07 +00:00
|
|
|
message.Status = ricochet.Message_SENDING
|
|
|
|
}
|
|
|
|
|
2016-10-05 21:38:18 +00:00
|
|
|
c.messages = append(c.messages, message)
|
|
|
|
event := ricochet.ConversationEvent{
|
|
|
|
Type: ricochet.ConversationEvent_SEND,
|
|
|
|
Msg: message,
|
|
|
|
}
|
2016-10-16 00:04:19 +00:00
|
|
|
c.events.Publish(event)
|
2016-10-06 23:50:07 +00:00
|
|
|
|
|
|
|
return message, nil
|
2016-10-05 21:38:18 +00:00
|
|
|
}
|
2016-10-16 00:04:19 +00:00
|
|
|
|
|
|
|
// Send all messages in the QUEUED state to the contact, if
|
|
|
|
// a connection is available. Should be called after a new
|
|
|
|
// connection is established.
|
|
|
|
func (c *Conversation) SendQueuedMessages() int {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
conn := c.Contact.Connection()
|
|
|
|
if conn == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
sent := 0
|
|
|
|
for _, message := range c.messages {
|
|
|
|
if message.Status != ricochet.Message_QUEUED {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessageToConnection(conn, message)
|
|
|
|
message.Status = ricochet.Message_SENDING
|
|
|
|
sent++
|
|
|
|
|
|
|
|
event := ricochet.ConversationEvent{
|
|
|
|
Type: ricochet.ConversationEvent_UPDATE,
|
|
|
|
Msg: message,
|
|
|
|
}
|
|
|
|
c.events.Publish(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sent
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendMessageToConnection(conn *protocol.OpenConnection, message *ricochet.Message) {
|
|
|
|
// 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
|
|
|
|
conn.SendMessage(channelId, message.Text)
|
|
|
|
}
|
2016-10-16 00:57:08 +00:00
|
|
|
|
|
|
|
// XXX This is inefficient -- it'll usually only be marking the last message
|
|
|
|
// or few messages. Need a better way to know what's unread.
|
|
|
|
func (c *Conversation) MarkReadBeforeMessage(msgId uint64) int {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
marked := 0
|
|
|
|
for _, message := range c.messages {
|
|
|
|
if message.Status == ricochet.Message_UNREAD {
|
|
|
|
message.Status = ricochet.Message_READ
|
|
|
|
marked++
|
|
|
|
|
|
|
|
event := ricochet.ConversationEvent{
|
|
|
|
Type: ricochet.ConversationEvent_UPDATE,
|
|
|
|
Msg: message,
|
|
|
|
}
|
|
|
|
c.events.Publish(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
if message.Identifier == msgId && message.Recipient.IsSelf {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return marked
|
|
|
|
}
|