groupme/groupmeext/client.go
2023-09-20 17:04:49 -05:00

99 lines
2.7 KiB
Go

package groupmeext
import (
"context"
"gitea.watsonlabs.net/watsonb8/groupme-lib"
)
type Client struct {
*groupme.Client
}
// NewClient creates a new GroupMe API Client
func NewClient() *Client {
n := Client{
Client: groupme.NewClient(),
}
return &n
}
func (c Client) IndexAllGroups(authToken string) ([]*groupme.Group, error) {
return c.IndexGroups(context.TODO(), &groupme.GroupsQuery{
// Omit: "memberships",
PerPage: 100, //TODO: Configurable and add multipage support
}, authToken)
}
func (c Client) IndexAllRelations(authToken string) ([]*groupme.User, error) {
return c.IndexRelations(context.TODO(), authToken)
}
func (c Client) IndexAllChats(authToken string) ([]*groupme.Chat, error) {
return c.IndexChats(context.TODO(), &groupme.IndexChatsQuery{
PerPage: 100, //TODO?
}, authToken)
}
func (c Client) LoadMessagesAfter(groupID groupme.ID, lastMessageID string, lastMessageFromMe bool, private bool, authToken string) ([]*groupme.Message, error) {
if private {
ans, e := c.IndexDirectMessages(context.TODO(), groupID.String(), &groupme.IndexDirectMessagesQuery{
SinceID: groupme.ID(lastMessageID),
//Limit: num,
}, authToken)
//fmt.Println(groupID, lastMessageID, num, i.Count, e)
if e != nil {
return nil, e
}
for i, j := 0, len(ans.Messages)-1; i < j; i, j = i+1, j-1 {
ans.Messages[i], ans.Messages[j] = ans.Messages[j], ans.Messages[i]
}
return ans.Messages, nil
} else {
i, e := c.IndexMessages(context.TODO(), groupID, &groupme.IndexMessagesQuery{
AfterID: groupme.ID(lastMessageID),
//20 for consistency with dms
Limit: 20,
}, authToken)
//fmt.Println(groupID, lastMessageID, num, i.Count, e)
if e != nil {
return nil, e
}
return i.Messages, nil
}
}
func (c Client) LoadMessagesBefore(groupID, lastMessageID string, private bool, authToken string) ([]*groupme.Message, error) {
if private {
i, e := c.IndexDirectMessages(context.TODO(), groupID, &groupme.IndexDirectMessagesQuery{
BeforeID: groupme.ID(lastMessageID),
//Limit: num,
}, authToken)
//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,
}, authToken)
//fmt.Println(groupID, lastMessageID, num, i.Count, e)
if e != nil {
return nil, e
}
return i.Messages, nil
}
}
func (c *Client) RemoveFromGroup(uid, groupID groupme.ID, authToken string) error {
group, err := c.ShowGroup(context.TODO(), groupID, authToken)
if err != nil {
return err
}
return c.RemoveMember(context.TODO(), groupID, group.GetMemberByUserID(uid).ID, authToken)
}