summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/messages.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/messages.go')
-rw-r--r--vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/messages.go54
1 files changed, 42 insertions, 12 deletions
diff --git a/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/messages.go b/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/messages.go
index 2c112155..89ad9178 100644
--- a/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/messages.go
+++ b/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/messages.go
@@ -2,6 +2,7 @@ package realtime
import (
"fmt"
+ "log"
"strconv"
"time"
@@ -16,27 +17,54 @@ const (
default_buffer_size = 100
)
+var messageListenerAdded = false
+
+// NewMessage creates basic message with an ID, a RoomID, and a Msg
+// Takes channel and text
+func (c *Client) NewMessage(channel *models.Channel, text string) *models.Message {
+ return &models.Message{
+ ID: c.newRandomId(),
+ RoomID: channel.ID,
+ Msg: text,
+ }
+}
+
// LoadHistory loads history
-// Takes roomId
+// Takes roomID
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/load-history
-func (c *Client) LoadHistory(roomId string) error {
- _, err := c.ddp.Call("loadHistory", roomId)
+func (c *Client) LoadHistory(roomID string) ([]models.Message, error) {
+ m, err := c.ddp.Call("loadHistory", roomID)
if err != nil {
- return err
+ return nil, err
}
- return nil
+ history := m.(map[string]interface{})
+
+ document, _ := gabs.Consume(history["messages"])
+ msgs, err := document.Children()
+ if err != nil {
+ log.Printf("response is in an unexpected format: %v", err)
+ return make([]models.Message, 0), nil
+ }
+
+ messages := make([]models.Message, len(msgs))
+
+ for i, arg := range msgs {
+ messages[i] = *getMessageFromDocument(arg)
+ }
+
+ // log.Println(messages)
+
+ return messages, nil
}
// SendMessage sends message to channel
-// takes channel and message
+// takes message
//
// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/send-message
-func (c *Client) SendMessage(m *models.Message) (*models.Message, error) {
- m.ID = c.newRandomId()
-
- rawResponse, err := c.ddp.Call("sendMessage", m)
+func (c *Client) SendMessage(message *models.Message) (*models.Message, error) {
+ rawResponse, err := c.ddp.Call("sendMessage", message)
if err != nil {
return nil, err
}
@@ -158,8 +186,10 @@ func (c *Client) SubscribeToMessageStream(channel *models.Channel, msgChannel ch
return err
}
- // msgChannel := make(chan models.Message, default_buffer_size)
- c.ddp.CollectionByName("stream-room-messages").AddUpdateListener(messageExtractor{msgChannel, "update"})
+ if !messageListenerAdded {
+ c.ddp.CollectionByName("stream-room-messages").AddUpdateListener(messageExtractor{msgChannel, "update"})
+ messageListenerAdded = true
+ }
return nil
}