From 27990e6174d8a29ec1b0b59c1247c189571c3058 Mon Sep 17 00:00:00 2001 From: neha-Gupta1 Date: Tue, 22 Aug 2023 12:48:11 +0530 Subject: [PATCH] item pager for channels --- src/pkg/services/m365/api/channels_pager.go | 74 +++++++++++++++++++ .../services/m365/api/channels_pager_test.go | 39 +++++----- 2 files changed, 96 insertions(+), 17 deletions(-) diff --git a/src/pkg/services/m365/api/channels_pager.go b/src/pkg/services/m365/api/channels_pager.go index ee1bb5f5c..55ed89f56 100644 --- a/src/pkg/services/m365/api/channels_pager.go +++ b/src/pkg/services/m365/api/channels_pager.go @@ -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()) +} diff --git a/src/pkg/services/m365/api/channels_pager_test.go b/src/pkg/services/m365/api/channels_pager_test.go index 4c8bc46e8..1b498c87f 100644 --- a/src/pkg/services/m365/api/channels_pager_test.go +++ b/src/pkg/services/m365/api/channels_pager_test.go @@ -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)) // }