item pager for channels

This commit is contained in:
neha-Gupta1 2023-08-22 12:48:11 +05:30
parent 7de04b98ab
commit 27990e6174
2 changed files with 96 additions and 17 deletions

View File

@ -3,7 +3,10 @@ package api
import (
"context"
"github.com/alcionai/clues"
"github.com/alcionai/corso/src/internal/common/ptr"
"github.com/alcionai/corso/src/internal/m365/graph"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/microsoftgraph/msgraph-sdk-go/teams"
)
@ -68,3 +71,74 @@ func (p *messagePageCtrl) GetPage(ctx context.Context) (PageLinker, error) {
return resp, nil
}
type MessageItemIDType struct {
ItemID string
}
type channelItemPageCtrl struct {
gs graph.Servicer
builder *teams.ItemChannelsItemMessagesRequestBuilder
options *teams.ItemChannelsItemMessagesRequestBuilderGetRequestConfiguration
}
func (c Channels) GetItemIDsInContainer(
ctx context.Context,
teamID, channelID string,
) (map[string]MessageItemIDType, error) {
ctx = clues.Add(ctx, "channel_id", channelID)
pager := c.NewChannelItemPager(teamID, channelID)
items, err := enumerateItems(ctx, pager)
if err != nil {
return nil, graph.Wrap(ctx, err, "enumerating contacts")
}
m := map[string]MessageItemIDType{}
for _, item := range items {
m[ptr.Val(item.GetId())] = MessageItemIDType{
ItemID: ptr.Val(item.GetId()),
}
}
return m, nil
}
func (c Channels) NewChannelItemPager(
teamID, containerID string,
selectProps ...string,
) itemPager[models.ChatMessageable] {
options := &teams.ItemChannelsItemMessagesRequestBuilderGetRequestConfiguration{
QueryParameters: &teams.ItemChannelsItemMessagesRequestBuilderGetQueryParameters{},
}
if len(selectProps) > 0 {
options.QueryParameters.Select = selectProps
}
builder := c.Stable.
Client().
Teams().
ByTeamId(teamID).
Channels().
ByChannelId(containerID).
Messages()
return &channelItemPageCtrl{c.Stable, builder, options}
}
//lint:ignore U1000 False Positive
func (p *channelItemPageCtrl) getPage(ctx context.Context) (PageLinkValuer[models.ChatMessageable], error) {
page, err := p.builder.Get(ctx, p.options)
if err != nil {
return nil, graph.Stack(ctx, err)
}
return EmptyDeltaLinker[models.ChatMessageable]{PageLinkValuer: page}, nil
}
//lint:ignore U1000 False Positive
func (p *channelItemPageCtrl) setNext(nextLink string) {
p.builder = teams.NewItemChannelsItemMessagesRequestBuilder(nextLink, p.gs.Adapter())
}

View File

@ -139,24 +139,29 @@ func deleteChannel(ctx context.Context, credentials account.M365Config, teamID,
}
// func (suite *ChannelPagerIntgSuite) TestMessages_CreateGetAndDelete() {
// t := suite.T()
// ctx, flush := tester.NewContext(t)
// defer flush()
// t := suite.T()
// ctx, flush := tester.NewContext(t)
// defer flush()
// var (
// teamID = tconfig.M365TeamsID(t)
// channelID = tconfig.M365ChannelID(t)
// credentials = suite.its.ac.Credentials
// chanClient = suite.its.ac.Channels()
// )
// var (
// teamID = tconfig.M365TeamsID(t)
// channelID = tconfig.M365ChannelID(t)
// credentials = suite.its.ac.Credentials
// chanClient = suite.its.ac.Channels()
// )
// POST channel
// patchBody := models.NewChatMessage()
// body := models.NewItemBody()
// content := "Hello World"
// body.SetContent(&content)
// patchBody.SetBody(body)
// // GET channel - should be not found
// message, _, err := chanClient.GetMessage(ctx, teamID, channelID, "", "")
// assert.Error(t, err, clues.ToCore(err))
// // POST channel
// // patchBody := models.NewChatMessage()
// // body := models.NewItemBody()
// // content := "Hello World"
// // body.SetContent(&content)
// // patchBody.SetBody(body)
// // _, := suite.its.ac.Channels().PostMessage(ctx, teamID, channelID, patchBody)
// // assert.NoError(t, err, clues.ToCore(err))
// _, := suite.its.ac.Channels().PostMessage(ctx, teamID, channelID, patchBody)
// assert.NoError(t, err, clues.ToCore(err))
// }