diff --git a/chatIn.go b/chatIn.go index a770e68..f05d8c7 100644 --- a/chatIn.go +++ b/chatIn.go @@ -109,3 +109,74 @@ func heartbeat(c chan<- ChatAPI, freq time.Duration) { count++ } } + +// Read fetches chat messages from a conversation. By default, 10 messages will +// be fetched at a time. However, if count is passed, then that is the number of +// messages that will be fetched. +func (c Chat) Read(count ...int) (*ChatAPI, error) { + m := ChatAPI{} + m.Method = "read" + m.Params.Options.Channel = c.Channel + if len(count) == 0 { + m.Params.Options.Pagination.Num = 10 + } else { + m.Params.Options.Pagination.Num = count[0] + } + + r, err := chatAPIOut(c.keybase.Path, m) + if err != nil { + return &ChatAPI{}, err + } + r.keybase = *c.keybase + return &r, nil +} + +// Next fetches the next page of chat messages that were fetched with Read. By +// default, Next will fetch the same amount of messages that were originally +// fetched with Read. However, if count is passed, then that is the number of +// messages that will be fetched. +func (c *ChatAPI) Next(count ...int) (*ChatAPI, error) { + m := ChatAPI{} + m.Method = "read" + m.Params.Options.Channel = c.Result.Messages[0].Msg.Channel + if len(count) == 0 { + m.Params.Options.Pagination.Num = c.Result.Pagination.Num + } else { + m.Params.Options.Pagination.Num = count[0] + } + m.Params.Options.Pagination.Next = c.Result.Pagination.Next + + result, err := chatAPIOut(c.keybase.Path, m) + if err != nil { + return &ChatAPI{}, err + } + k := c.keybase + *c = result + c.keybase = k + return c, nil +} + +// Previous fetches the previous page of chat messages that were fetched with Read. +// By default, Previous will fetch the same amount of messages that were +// originally fetched with Read. However, if count is passed, then that is the +// number of messages that will be fetched. +func (c *ChatAPI) Previous(count ...int) (*ChatAPI, error) { + m := ChatAPI{} + m.Method = "read" + m.Params.Options.Channel = c.Result.Messages[0].Msg.Channel + if len(count) == 0 { + m.Params.Options.Pagination.Num = c.Result.Pagination.Num + } else { + m.Params.Options.Pagination.Num = count[0] + } + m.Params.Options.Pagination.Previous = c.Result.Pagination.Previous + + result, err := chatAPIOut(c.keybase.Path, m) + if err != nil { + return &ChatAPI{}, err + } + k := c.keybase + *c = result + c.keybase = k + return c, nil +} diff --git a/types.go b/types.go index 9e17cfd..7142b43 100644 --- a/types.go +++ b/types.go @@ -13,6 +13,7 @@ type ChatAPI struct { Conversations []conversation `json:"conversations,omitempty"` Offline bool `json:"offline,omitempty"` Result result `json:"result,omitempty"` + keybase Keybase // Some methods will need this, so I'm passing it but keeping it unexported } type sender struct { UID string `json:"uid"` @@ -141,9 +142,11 @@ type params struct { Options options `json:"options"` } type pagination struct { - Next string `json:"next"` - Previous string `json:"previous"` - Num int `json:"num"` + Next string `json:"next"` + Previous string `json:"previous"` + Num int `json:"num"` + Last bool `json:"last,omitempty"` + ForceFirstPage bool `json:"forceFirstPage,omitempty"` } type result struct { Messages []messages `json:"messages,omitempty"` @@ -188,6 +191,11 @@ type chat interface { Delete(messageID int) (ChatAPI, error) } +type chatAPI interface { + Next(count ...int) (*ChatAPI, error) + Previous(count ...int) (*ChatAPI, error) +} + type keybase interface { NewChat(channel Channel) Chat Run(handler func(ChatAPI), options ...RunOptions)