diff options
author | Wim <wim@42.be> | 2020-12-06 23:16:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-06 23:16:02 +0100 |
commit | 0d7315249d20bf9856605068074a7b6c6bcce835 (patch) | |
tree | f8ab7e0f3e96491e439eb49beebf3fae658215c4 /vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/messages.go | |
parent | 4913766d58cd1fe204b27dc93172c5dd4a95a88a (diff) | |
download | matterbridge-msglm-0d7315249d20bf9856605068074a7b6c6bcce835.tar.gz matterbridge-msglm-0d7315249d20bf9856605068074a7b6c6bcce835.tar.bz2 matterbridge-msglm-0d7315249d20bf9856605068074a7b6c6bcce835.zip |
Update vendor (#1330)
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.go | 54 |
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 } |