corso/src/pkg/services/m365/api/conversations.go
Keepers 5362137116
expose graph options to sdk clients (#4653)
1. moves the m365/graph package from internal to pkg/services/api so that options are accessible to sdk users.
2. exposes graph.Options in the api client.Service call.

---

#### Does this PR need a docs update or release note?

- [x]  No

#### Type of change

- [x] 🌻 Feature
2023-11-10 19:26:59 +00:00

131 lines
3.4 KiB
Go

package api
import (
"context"
"github.com/alcionai/clues"
"github.com/jaytaylor/html2text"
"github.com/microsoftgraph/msgraph-sdk-go/groups"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/alcionai/corso/src/internal/common/ptr"
"github.com/alcionai/corso/src/internal/common/str"
"github.com/alcionai/corso/src/pkg/backup/details"
"github.com/alcionai/corso/src/pkg/services/m365/api/graph"
)
// ---------------------------------------------------------------------------
// controller
// ---------------------------------------------------------------------------
func (c Client) Conversations() Conversations {
return Conversations{c}
}
// Conversations is an interface-compliant provider of the client.
type Conversations struct {
Client
}
// ---------------------------------------------------------------------------
// Item (conversation thread post)
// ---------------------------------------------------------------------------
func (c Conversations) GetConversationPost(
ctx context.Context,
groupID, conversationID, threadID, postID string,
cc CallConfig,
) (models.Postable, *details.GroupsInfo, error) {
config := &groups.ItemConversationsItemThreadsItemPostsPostItemRequestBuilderGetRequestConfiguration{
QueryParameters: &groups.ItemConversationsItemThreadsItemPostsPostItemRequestBuilderGetQueryParameters{},
}
if len(cc.Select) > 0 {
config.QueryParameters.Select = cc.Select
}
if len(cc.Expand) > 0 {
config.QueryParameters.Expand = append(config.QueryParameters.Expand, cc.Expand...)
}
post, err := c.Stable.
Client().
Groups().
ByGroupId(groupID).
Conversations().
ByConversationId(conversationID).
Threads().
ByConversationThreadId(threadID).
Posts().
ByPostId(postID).
Get(ctx, config)
if err != nil {
return nil, nil, graph.Stack(ctx, err)
}
return post, conversationPostInfo(post), graph.Stack(ctx, err).OrNil()
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
func conversationPostInfo(
post models.Postable,
) *details.GroupsInfo {
if post == nil {
return nil
}
preview, contentLen, err := getConversationPostContentPreview(post)
if err != nil {
preview = "malformed or unparseable html" + preview
}
var sender string
if post.GetSender() != nil && post.GetSender().GetEmailAddress() != nil {
sender = ptr.Val(post.GetSender().GetEmailAddress().GetAddress())
}
size := contentLen
for _, a := range post.GetAttachments() {
size += int64(ptr.Val(a.GetSize()))
}
cpi := details.ConversationPostInfo{
CreatedAt: ptr.Val(post.GetCreatedDateTime()),
Creator: sender,
Preview: preview,
Size: size,
}
return &details.GroupsInfo{
ItemType: details.GroupsConversationPost,
Modified: ptr.Val(post.GetLastModifiedDateTime()),
Post: cpi,
}
}
func getConversationPostContentPreview(post models.Postable) (string, int64, error) {
content, origSize, err := stripConversationPostHTML(post)
return str.Preview(content, 128), origSize, clues.Stack(err).OrNil()
}
func stripConversationPostHTML(post models.Postable) (string, int64, error) {
var (
content string
origSize int64
)
if post.GetBody() != nil {
content = ptr.Val(post.GetBody().GetContent())
}
origSize = int64(len(content))
content, err := html2text.FromString(content)
return content, origSize, clues.Stack(err).OrNil()
}