groupme/groupmeExt/client.go

93 lines
2.4 KiB
Go
Raw Normal View History

2021-02-21 05:58:50 +00:00
package groupmeExt
import (
"context"
"github.com/karmanyaahm/groupme"
2021-03-03 00:46:53 +00:00
"github.com/karmanyaahm/matrix-groupme-go/types"
2021-02-21 05:58:50 +00:00
)
type Client struct {
*groupme.Client
}
// NewClient creates a new GroupMe API Client
func NewClient(authToken string) *Client {
n := Client{
Client: groupme.NewClient(authToken),
}
return &n
}
func (c Client) IndexAllGroups() ([]*groupme.Group, error) {
return c.IndexGroups(context.TODO(), &groupme.GroupsQuery{
2021-02-27 22:38:47 +00:00
// Omit: "memberships",
2021-02-21 05:58:50 +00:00
PerPage: 100, //TODO: Configurable and add multipage support
})
}
2021-04-28 02:49:01 +00:00
func (c Client) IndexAllChats() ([]*groupme.Chat, error) {
return c.IndexChats(context.TODO(), &groupme.IndexChatsQuery{
PerPage: 100, //TODO?
2021-02-21 05:58:50 +00:00
})
2021-04-28 02:49:01 +00:00
}
2021-02-21 05:58:50 +00:00
2021-04-28 02:49:01 +00:00
func (c Client) LoadMessagesAfter(groupID, lastMessageID string, lastMessageFromMe bool, private bool) ([]*groupme.Message, error) {
if private {
i, e := c.IndexDirectMessages(context.TODO(), groupID, &groupme.IndexDirectMessagesQuery{
SinceID: groupme.ID(lastMessageID),
//Limit: num,
})
//fmt.Println(groupID, lastMessageID, num, i.Count, e)
if e != nil {
return nil, e
}
return i.Messages, nil
} else {
i, e := c.IndexMessages(context.TODO(), groupme.ID(groupID), &groupme.IndexMessagesQuery{
AfterID: groupme.ID(lastMessageID),
//20 for consistency with dms
Limit: 20,
})
//fmt.Println(groupID, lastMessageID, num, i.Count, e)
if e != nil {
return nil, e
}
return i.Messages, nil
2021-02-21 05:58:50 +00:00
}
}
2021-04-28 02:49:01 +00:00
func (c Client) LoadMessagesBefore(groupID, lastMessageID string, private bool) ([]*groupme.Message, error) {
if private {
i, e := c.IndexDirectMessages(context.TODO(), groupID, &groupme.IndexDirectMessagesQuery{
BeforeID: groupme.ID(lastMessageID),
//Limit: num,
})
//fmt.Println(groupID, lastMessageID, num, i.Count, e)
if e != nil {
return nil, e
}
return i.Messages, nil
} else {
//TODO: limit max 100
i, e := c.IndexMessages(context.TODO(), groupme.ID(groupID), &groupme.IndexMessagesQuery{
BeforeID: groupme.ID(lastMessageID),
//20 for consistency with dms
Limit: 20,
})
//fmt.Println(groupID, lastMessageID, num, i.Count, e)
if e != nil {
return nil, e
}
return i.Messages, nil
2021-02-21 05:58:50 +00:00
}
}
2021-02-28 00:57:42 +00:00
func (c *Client) RemoveFromGroup(uid, groupID types.GroupMeID) error {
group, err := c.ShowGroup(context.TODO(), groupme.ID(groupID))
if err != nil {
return err
}
return c.RemoveMember(context.TODO(), groupme.ID(groupID), group.GetMemberByUserID(groupme.ID(uid)).ID)
}