Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions chats/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ func (o *ops) CreateGroup(ctx context.Context, userIDs []string, topic string, i
return adapter.MapGraphChat(resp), nil
}

func (o *ops) GetOneOnOneChat(ctx context.Context, chatID string) (*models.Chat, error) {
resp, requestErr := o.chatAPI.GetOneOnOneChat(ctx, chatID)
if requestErr != nil {
return nil, snd.MapError(requestErr, snd.WithResource(resources.OneOnOneChat, chatID))
}
return adapter.MapGraphChat(resp), nil
}

func (o *ops) GetGroupChat(ctx context.Context, chatID string) (*models.Chat, error) {
resp, requestErr := o.chatAPI.GetGroupChat(ctx, chatID)
if requestErr != nil {
return nil, snd.MapError(requestErr, snd.WithResource(resources.GroupChat, chatID))
}
return adapter.MapGraphChat(resp), nil
}

func (o *ops) AddMemberToGroupChat(ctx context.Context, chatID, userID string) (*models.Member, error) {
resp, requestErr := o.chatAPI.AddMemberToGroupChat(ctx, chatID, userID)
if requestErr != nil {
Expand Down
2 changes: 2 additions & 0 deletions chats/ops_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
type chatOps interface {
CreateOneOnOne(ctx context.Context, userID string) (*models.Chat, error)
CreateGroup(ctx context.Context, userIDs []string, topic string, includeMe bool) (*models.Chat, error)
GetOneOnOneChat(ctx context.Context, chatID string) (*models.Chat, error)
GetGroupChat(ctx context.Context, chatID string) (*models.Chat, error)
AddMemberToGroupChat(ctx context.Context, chatID, userID string) (*models.Member, error)
RemoveMemberFromGroupChat(ctx context.Context, chatID, userID string) error
ListGroupChatMembers(ctx context.Context, chatID string) ([]*models.Member, error)
Expand Down
12 changes: 12 additions & 0 deletions chats/ops_with_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ func (o *opsWithCache) CreateGroup(ctx context.Context, userIDs []string, topic
return chat, nil
}

func (o *opsWithCache) GetOneOnOneChat(ctx context.Context, chatID string) (*models.Chat, error) {
return cacher.WithErrorClear(func() (*models.Chat, error) {
return o.chatOps.GetOneOnOneChat(ctx, chatID)
}, o.cacheHandler)
}

func (o *opsWithCache) GetGroupChat(ctx context.Context, chatID string) (*models.Chat, error) {
return cacher.WithErrorClear(func() (*models.Chat, error) {
return o.chatOps.GetGroupChat(ctx, chatID)
}, o.cacheHandler)
}

func (o *opsWithCache) AddMemberToGroupChat(ctx context.Context, chatID, userID string) (*models.Member, error) {
member, err := o.chatOps.AddMemberToGroupChat(ctx, chatID, userID)
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions chats/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ func (s *service) CreateGroup(ctx context.Context, recipientRefs []string, topic
return resp, nil
}

func (s *service) GetChat(ctx context.Context, chatRef ChatRef) (*models.Chat, error) {
chatID, err := s.resolveChatIDFromRef(ctx, chatRef)
if err != nil {
return nil, snd.Wrap("GetChat", err,
snd.NewParam(resources.ChatRef, chatRef.get()),
)
}

var resp *models.Chat
switch chatRef.(type) {
case OneOnOneChatRef:
resp, err = s.chatOps.GetOneOnOneChat(ctx, chatID)
case GroupChatRef:
resp, err = s.chatOps.GetGroupChat(ctx, chatID)
default:
return nil, snd.Wrap("GetChat", fmt.Errorf("unknown chat reference type"),
snd.NewParam(resources.ChatRef, chatRef.get()),
)
}
if err != nil {
return nil, snd.Wrap("GetChat", err,
snd.NewParam(resources.ChatRef, chatRef.get()),
)
}

return resp, nil
}

func (s *service) AddMemberToGroupChat(ctx context.Context, chatRef GroupChatRef, userRef string) (*models.Member, error) {
chatID, err := s.resolveChatIDFromRef(ctx, chatRef)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions chats/service_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Service interface {
// The authenticated user may be included by setting includeMe to true.
CreateGroup(ctx context.Context, recipientRefs []string, topic string, includeMe bool) (*models.Chat, error)

// GetChat retrieves a chat (one-on-one or group) by its reference.
GetChat(ctx context.Context, chatRef ChatRef) (*models.Chat, error)

// AddMemberToGroupChat adds a user to a group chat.
AddMemberToGroupChat(ctx context.Context, chatRef GroupChatRef, userRef string) (*models.Member, error)

Expand Down
60 changes: 60 additions & 0 deletions internal/api/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (

type OneOnOneChatAPI interface {
CreateOneOnOneChat(ctx context.Context, recipientRef string) (msmodels.Chatable, *sender.RequestError)
GetOneOnOneChat(ctx context.Context, chatID string) (msmodels.Chatable, *sender.RequestError)
}

type GroupChatAPI interface {
GetGroupChat(ctx context.Context, chatID string) (msmodels.Chatable, *sender.RequestError)
CreateGroupChat(ctx context.Context, recipientRefs []string, topic string, includeMe bool) (msmodels.Chatable, *sender.RequestError)
AddMemberToGroupChat(ctx context.Context, chatID, userRef string) (msmodels.ConversationMemberable, *sender.RequestError)
RemoveMemberFromGroupChat(ctx context.Context, chatID, memberID string) *sender.RequestError
Expand Down Expand Up @@ -52,6 +54,64 @@ func NewChat(client *graph.GraphServiceClient, senderCfg *config.SenderConfig, s
return &chatsAPI{client, senderCfg, searchAPI}
}

func (c *chatsAPI) GetOneOnOneChat(ctx context.Context, chatID string) (msmodels.Chatable, *sender.RequestError) {
me, err := GetMe(ctx, c.client, c.senderCfg)
if err != nil {
return nil, err
}
if me.GetId() == nil {
return nil, &sender.RequestError{Message: "cannot get current user ID"}
}

requestParameters := &graphusers.ItemChatsChatItemRequestBuilderGetQueryParameters{
Expand: []string{"members"},
}

configuration := &graphusers.ItemChatsChatItemRequestBuilderGetRequestConfiguration{
QueryParameters: requestParameters,
}

call := func(ctx context.Context) (sender.Response, error) {
return c.client.
Users().
ByUserId(*me.GetId()).
Chats().
ByChatId(chatID).
Get(ctx, configuration)
}

resp, err := sender.SendRequest(ctx, call, c.senderCfg)
if err != nil {
return nil, err
}

out, ok := resp.(msmodels.Chatable)
if !ok {
return nil, newTypeError("Chatable")
}
return out, nil
}

func (c *chatsAPI) GetGroupChat(ctx context.Context, chatID string) (msmodels.Chatable, *sender.RequestError) {
call := func(ctx context.Context) (sender.Response, error) {
return c.client.
Chats().
ByChatId(chatID).
Get(ctx, nil)
}

resp, err := sender.SendRequest(ctx, call, c.senderCfg)
if err != nil {
return nil, err
}

out, ok := resp.(msmodels.Chatable)
if !ok {
return nil, newTypeError("Chatable")
}
return out, nil
}

func (c *chatsAPI) CreateOneOnOneChat(ctx context.Context, userRef string) (msmodels.Chatable, *sender.RequestError) {
body := msmodels.NewChat()
chatType := msmodels.ONEONONE_CHATTYPE
Expand Down
60 changes: 60 additions & 0 deletions internal/testutil/mock_chat_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.