mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 11:15:12 +00:00
37
streams/handler/create_conversation.go
Normal file
37
streams/handler/create_conversation.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
)
|
||||
|
||||
// Create a conversation
|
||||
func (s *Streams) CreateConversation(ctx context.Context, req *pb.CreateConversationRequest, rsp *pb.CreateConversationResponse) error {
|
||||
// validate the request
|
||||
if len(req.GroupId) == 0 {
|
||||
return ErrMissingGroupID
|
||||
}
|
||||
if len(req.Topic) == 0 {
|
||||
return ErrMissingTopic
|
||||
}
|
||||
|
||||
// write the conversation to the database
|
||||
conv := &Conversation{
|
||||
ID: uuid.New().String(),
|
||||
Topic: req.Topic,
|
||||
GroupID: req.GroupId,
|
||||
CreatedAt: s.Time(),
|
||||
}
|
||||
if err := s.DB.Create(conv).Error; err != nil {
|
||||
logger.Errorf("Error creating conversation: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// serialize the response
|
||||
rsp.Conversation = conv.Serialize()
|
||||
return nil
|
||||
}
|
||||
60
streams/handler/create_conversation_test.go
Normal file
60
streams/handler/create_conversation_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/micro/services/streams/handler"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCreateConversation(t *testing.T) {
|
||||
tt := []struct {
|
||||
Name string
|
||||
GroupID string
|
||||
Topic string
|
||||
Error error
|
||||
}{
|
||||
{
|
||||
Name: "MissingGroupID",
|
||||
Topic: "HelloWorld",
|
||||
Error: handler.ErrMissingGroupID,
|
||||
},
|
||||
{
|
||||
Name: "MissingTopic",
|
||||
GroupID: uuid.New().String(),
|
||||
Error: handler.ErrMissingTopic,
|
||||
},
|
||||
{
|
||||
Name: "Valid",
|
||||
GroupID: uuid.New().String(),
|
||||
Topic: "HelloWorld",
|
||||
},
|
||||
}
|
||||
|
||||
h := testHandler(t)
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
var rsp pb.CreateConversationResponse
|
||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
||||
Topic: tc.Topic, GroupId: tc.GroupID,
|
||||
}, &rsp)
|
||||
|
||||
assert.Equal(t, tc.Error, err)
|
||||
if tc.Error != nil {
|
||||
assert.Nil(t, rsp.Conversation)
|
||||
return
|
||||
}
|
||||
|
||||
assertConversationsMatch(t, &pb.Conversation{
|
||||
CreatedAt: timestamppb.New(h.Time()),
|
||||
GroupId: tc.GroupID,
|
||||
Topic: tc.Topic,
|
||||
}, rsp.Conversation)
|
||||
})
|
||||
}
|
||||
}
|
||||
53
streams/handler/create_message.go
Normal file
53
streams/handler/create_message.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Create a message within a conversation
|
||||
func (s *Streams) CreateMessage(ctx context.Context, req *pb.CreateMessageRequest, rsp *pb.CreateMessageResponse) error {
|
||||
// validate the request
|
||||
if len(req.AuthorId) == 0 {
|
||||
return ErrMissingAuthorID
|
||||
}
|
||||
if len(req.ConversationId) == 0 {
|
||||
return ErrMissingConversationID
|
||||
}
|
||||
if len(req.Text) == 0 {
|
||||
return ErrMissingText
|
||||
}
|
||||
|
||||
return s.DB.Transaction(func(tx *gorm.DB) error {
|
||||
// lookup the conversation
|
||||
var conv Conversation
|
||||
if err := s.DB.Where(&Conversation{ID: req.ConversationId}).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
||||
return ErrNotFound
|
||||
} else if err != nil {
|
||||
logger.Errorf("Error reading conversation: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// create the message
|
||||
msg := &Message{
|
||||
ID: uuid.New().String(),
|
||||
SentAt: s.Time(),
|
||||
Text: req.Text,
|
||||
AuthorID: req.AuthorId,
|
||||
ConversationID: req.ConversationId,
|
||||
}
|
||||
if err := s.DB.Create(msg).Error; err != nil {
|
||||
logger.Errorf("Error creating message: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// serialize the response
|
||||
rsp.Message = msg.Serialize()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
89
streams/handler/create_message_test.go
Normal file
89
streams/handler/create_message_test.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/micro/services/streams/handler"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCreateMessage(t *testing.T) {
|
||||
h := testHandler(t)
|
||||
|
||||
// seed some data
|
||||
var cRsp pb.CreateConversationResponse
|
||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
||||
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
||||
}, &cRsp)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating conversation: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
tt := []struct {
|
||||
Name string
|
||||
AuthorID string
|
||||
ConversationID string
|
||||
Text string
|
||||
Error error
|
||||
}{
|
||||
{
|
||||
Name: "MissingConversationID",
|
||||
Text: "HelloWorld",
|
||||
AuthorID: uuid.New().String(),
|
||||
Error: handler.ErrMissingConversationID,
|
||||
},
|
||||
{
|
||||
Name: "MissingAuthorID",
|
||||
ConversationID: uuid.New().String(),
|
||||
Text: "HelloWorld",
|
||||
Error: handler.ErrMissingAuthorID,
|
||||
},
|
||||
{
|
||||
Name: "MissingText",
|
||||
ConversationID: uuid.New().String(),
|
||||
AuthorID: uuid.New().String(),
|
||||
Error: handler.ErrMissingText,
|
||||
},
|
||||
{
|
||||
Name: "ConversationNotFound",
|
||||
ConversationID: uuid.New().String(),
|
||||
AuthorID: uuid.New().String(),
|
||||
Text: "HelloWorld",
|
||||
Error: handler.ErrNotFound,
|
||||
},
|
||||
{
|
||||
Name: "Valid",
|
||||
ConversationID: cRsp.Conversation.Id,
|
||||
AuthorID: uuid.New().String(),
|
||||
Text: "HelloWorld",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
var rsp pb.CreateMessageResponse
|
||||
err := h.CreateMessage(context.TODO(), &pb.CreateMessageRequest{
|
||||
Text: tc.Text, ConversationId: tc.ConversationID, AuthorId: tc.AuthorID,
|
||||
}, &rsp)
|
||||
|
||||
assert.Equal(t, tc.Error, err)
|
||||
if tc.Error != nil {
|
||||
assert.Nil(t, rsp.Message)
|
||||
return
|
||||
}
|
||||
|
||||
assertMessagesMatch(t, &pb.Message{
|
||||
AuthorId: tc.AuthorID,
|
||||
ConversationId: tc.ConversationID,
|
||||
SentAt: timestamppb.New(h.Time()),
|
||||
Text: tc.Text,
|
||||
}, rsp.Message)
|
||||
})
|
||||
}
|
||||
}
|
||||
34
streams/handler/delete_conversation.go
Normal file
34
streams/handler/delete_conversation.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Delete a conversation and all the messages within
|
||||
func (s *Streams) DeleteConversation(ctx context.Context, req *pb.DeleteConversationRequest, rsp *pb.DeleteConversationResponse) error {
|
||||
// validate the request
|
||||
if len(req.Id) == 0 {
|
||||
return ErrMissingID
|
||||
}
|
||||
|
||||
return s.DB.Transaction(func(tx *gorm.DB) error {
|
||||
// delete all the messages
|
||||
if err := tx.Where(&Message{ConversationID: req.Id}).Delete(&Message{}).Error; err != nil {
|
||||
logger.Errorf("Error deleting messages: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// delete the conversation
|
||||
if err := tx.Where(&Conversation{ID: req.Id}).Delete(&Conversation{}).Error; err != nil {
|
||||
logger.Errorf("Error deleting conversation: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
50
streams/handler/delete_conversation_test.go
Normal file
50
streams/handler/delete_conversation_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/micro/services/streams/handler"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDeleteConversation(t *testing.T) {
|
||||
h := testHandler(t)
|
||||
|
||||
// seed some data
|
||||
var cRsp pb.CreateConversationResponse
|
||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
||||
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
||||
}, &cRsp)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating conversation: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Run("MissingID", func(t *testing.T) {
|
||||
err := h.DeleteConversation(context.TODO(), &pb.DeleteConversationRequest{}, &pb.DeleteConversationResponse{})
|
||||
assert.Equal(t, handler.ErrMissingID, err)
|
||||
})
|
||||
|
||||
t.Run("Valid", func(t *testing.T) {
|
||||
err := h.DeleteConversation(context.TODO(), &pb.DeleteConversationRequest{
|
||||
Id: cRsp.Conversation.Id,
|
||||
}, &pb.DeleteConversationResponse{})
|
||||
assert.NoError(t, err)
|
||||
|
||||
err = h.ReadConversation(context.TODO(), &pb.ReadConversationRequest{
|
||||
Id: cRsp.Conversation.Id,
|
||||
}, &pb.ReadConversationResponse{})
|
||||
assert.Equal(t, handler.ErrNotFound, err)
|
||||
})
|
||||
|
||||
t.Run("Retry", func(t *testing.T) {
|
||||
err := h.DeleteConversation(context.TODO(), &pb.DeleteConversationRequest{
|
||||
Id: cRsp.Conversation.Id,
|
||||
}, &pb.DeleteConversationResponse{})
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
}
|
||||
31
streams/handler/list_conversations.go
Normal file
31
streams/handler/list_conversations.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
)
|
||||
|
||||
// List all the conversations for a group
|
||||
func (s *Streams) ListConversations(ctx context.Context, req *pb.ListConversationsRequest, rsp *pb.ListConversationsResponse) error {
|
||||
// validate the request
|
||||
if len(req.GroupId) == 0 {
|
||||
return ErrMissingGroupID
|
||||
}
|
||||
|
||||
// query the database
|
||||
var convs []Conversation
|
||||
if err := s.DB.Where(&Conversation{GroupID: req.GroupId}).Find(&convs).Error; err != nil {
|
||||
logger.Errorf("Error reading conversation: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// serialize the response
|
||||
rsp.Conversations = make([]*pb.Conversation, len(convs))
|
||||
for i, c := range convs {
|
||||
rsp.Conversations[i] = c.Serialize()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
55
streams/handler/list_conversations_test.go
Normal file
55
streams/handler/list_conversations_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/services/streams/handler"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestListConversations(t *testing.T) {
|
||||
h := testHandler(t)
|
||||
|
||||
// seed some data
|
||||
var cRsp1 pb.CreateConversationResponse
|
||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
||||
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
||||
}, &cRsp1)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating conversation: %v", err)
|
||||
return
|
||||
}
|
||||
var cRsp2 pb.CreateConversationResponse
|
||||
err = h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
||||
Topic: "FooBar", GroupId: uuid.New().String(),
|
||||
}, &cRsp2)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating conversation: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Run("MissingGroupID", func(t *testing.T) {
|
||||
var rsp pb.ListConversationsResponse
|
||||
err := h.ListConversations(context.TODO(), &pb.ListConversationsRequest{}, &rsp)
|
||||
assert.Equal(t, handler.ErrMissingGroupID, err)
|
||||
assert.Nil(t, rsp.Conversations)
|
||||
})
|
||||
|
||||
t.Run("Valid", func(t *testing.T) {
|
||||
var rsp pb.ListConversationsResponse
|
||||
err := h.ListConversations(context.TODO(), &pb.ListConversationsRequest{
|
||||
GroupId: cRsp1.Conversation.GroupId,
|
||||
}, &rsp)
|
||||
|
||||
assert.NoError(t, err)
|
||||
if len(rsp.Conversations) != 1 {
|
||||
t.Fatalf("Expected 1 conversation to be returned, got %v", len(rsp.Conversations))
|
||||
return
|
||||
}
|
||||
|
||||
assertConversationsMatch(t, cRsp1.Conversation, rsp.Conversations[0])
|
||||
})
|
||||
}
|
||||
45
streams/handler/list_messages.go
Normal file
45
streams/handler/list_messages.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
)
|
||||
|
||||
const DefaultLimit = 25
|
||||
|
||||
// List the messages within a conversation in reverse chronological order, using sent_before to
|
||||
// offset as older messages need to be loaded
|
||||
func (s *Streams) ListMessages(ctx context.Context, req *pb.ListMessagesRequest, rsp *pb.ListMessagesResponse) error {
|
||||
// validate the request
|
||||
if len(req.ConversationId) == 0 {
|
||||
return ErrMissingConversationID
|
||||
}
|
||||
|
||||
// construct the query
|
||||
q := s.DB.Where(&Message{ConversationID: req.ConversationId}).Order("sent_at DESC")
|
||||
if req.SentBefore != nil {
|
||||
q = q.Where("sent_at < ?", req.SentBefore.AsTime())
|
||||
}
|
||||
if req.Limit != nil {
|
||||
q.Limit(int(req.Limit.Value))
|
||||
} else {
|
||||
q.Limit(DefaultLimit)
|
||||
}
|
||||
|
||||
// execute the query
|
||||
var msgs []Message
|
||||
if err := q.Find(&msgs).Error; err != nil {
|
||||
logger.Errorf("Error reading messages: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// serialize the response
|
||||
rsp.Messages = make([]*pb.Message, len(msgs))
|
||||
for i, m := range msgs {
|
||||
rsp.Messages[i] = m.Serialize()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
116
streams/handler/list_messages_test.go
Normal file
116
streams/handler/list_messages_test.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/services/streams/handler"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
func TestListMessages(t *testing.T) {
|
||||
h := testHandler(t)
|
||||
h.Time = time.Now
|
||||
|
||||
// seed some data
|
||||
var convRsp pb.CreateConversationResponse
|
||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
||||
Topic: "TestListMessages", GroupId: uuid.New().String(),
|
||||
}, &convRsp)
|
||||
assert.NoError(t, err)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
msgs := make([]*pb.Message, 50)
|
||||
for i := 0; i < len(msgs); i++ {
|
||||
var rsp pb.CreateMessageResponse
|
||||
err := h.CreateMessage(context.TODO(), &pb.CreateMessageRequest{
|
||||
ConversationId: convRsp.Conversation.Id,
|
||||
AuthorId: uuid.New().String(),
|
||||
Text: strconv.Itoa(i),
|
||||
}, &rsp)
|
||||
assert.NoError(t, err)
|
||||
msgs[i] = rsp.Message
|
||||
}
|
||||
|
||||
t.Run("MissingConversationID", func(t *testing.T) {
|
||||
var rsp pb.ListMessagesResponse
|
||||
err := h.ListMessages(context.TODO(), &pb.ListMessagesRequest{}, &rsp)
|
||||
assert.Equal(t, handler.ErrMissingConversationID, err)
|
||||
assert.Nil(t, rsp.Messages)
|
||||
})
|
||||
|
||||
t.Run("NoOffset", func(t *testing.T) {
|
||||
var rsp pb.ListMessagesResponse
|
||||
err := h.ListMessages(context.TODO(), &pb.ListMessagesRequest{
|
||||
ConversationId: convRsp.Conversation.Id,
|
||||
}, &rsp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if len(rsp.Messages) != handler.DefaultLimit {
|
||||
t.Fatalf("Expected %v messages but got %v", handler.DefaultLimit, len(rsp.Messages))
|
||||
return
|
||||
}
|
||||
expected := msgs[25:]
|
||||
sortMessages(rsp.Messages)
|
||||
for i, msg := range rsp.Messages {
|
||||
assertMessagesMatch(t, expected[i], msg)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("LimitSet", func(t *testing.T) {
|
||||
var rsp pb.ListMessagesResponse
|
||||
err := h.ListMessages(context.TODO(), &pb.ListMessagesRequest{
|
||||
ConversationId: convRsp.Conversation.Id,
|
||||
Limit: &wrapperspb.Int32Value{Value: 10},
|
||||
}, &rsp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if len(rsp.Messages) != 10 {
|
||||
t.Fatalf("Expected %v messages but got %v", 10, len(rsp.Messages))
|
||||
return
|
||||
}
|
||||
expected := msgs[40:]
|
||||
sortMessages(rsp.Messages)
|
||||
for i, msg := range rsp.Messages {
|
||||
assertMessagesMatch(t, expected[i], msg)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("OffsetAndLimit", func(t *testing.T) {
|
||||
var rsp pb.ListMessagesResponse
|
||||
err := h.ListMessages(context.TODO(), &pb.ListMessagesRequest{
|
||||
ConversationId: convRsp.Conversation.Id,
|
||||
Limit: &wrapperspb.Int32Value{Value: 5},
|
||||
SentBefore: msgs[20].SentAt,
|
||||
}, &rsp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if len(rsp.Messages) != 5 {
|
||||
t.Fatalf("Expected %v messages but got %v", 5, len(rsp.Messages))
|
||||
return
|
||||
}
|
||||
expected := msgs[15:20]
|
||||
sortMessages(rsp.Messages)
|
||||
for i, msg := range rsp.Messages {
|
||||
assertMessagesMatch(t, expected[i], msg)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// sortMessages by the time they were sent
|
||||
func sortMessages(msgs []*pb.Message) {
|
||||
sort.Slice(msgs, func(i, j int) bool {
|
||||
if msgs[i].SentAt == nil || msgs[j].SentAt == nil {
|
||||
return true
|
||||
}
|
||||
return msgs[i].SentAt.AsTime().Before(msgs[j].SentAt.AsTime())
|
||||
})
|
||||
}
|
||||
38
streams/handler/read_conversation.go
Normal file
38
streams/handler/read_conversation.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
)
|
||||
|
||||
// Read a conversation using its ID, can filter using group ID if provided
|
||||
func (s *Streams) ReadConversation(ctx context.Context, req *pb.ReadConversationRequest, rsp *pb.ReadConversationResponse) error {
|
||||
// validate the request
|
||||
if len(req.Id) == 0 {
|
||||
return ErrMissingID
|
||||
}
|
||||
|
||||
// construct the query
|
||||
q := Conversation{ID: req.Id}
|
||||
if req.GroupId != nil {
|
||||
q.GroupID = req.GroupId.Value
|
||||
}
|
||||
|
||||
// execute the query
|
||||
var conv Conversation
|
||||
if err := s.DB.Where(&q).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
||||
return ErrNotFound
|
||||
} else if err != nil {
|
||||
logger.Errorf("Error reading conversation: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// serialize the response
|
||||
rsp.Conversation = conv.Serialize()
|
||||
return nil
|
||||
}
|
||||
70
streams/handler/read_conversation_test.go
Normal file
70
streams/handler/read_conversation_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/services/streams/handler"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
func TestReadConversation(t *testing.T) {
|
||||
h := testHandler(t)
|
||||
|
||||
// seed some data
|
||||
var cRsp pb.CreateConversationResponse
|
||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
||||
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
||||
}, &cRsp)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating conversation: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
tt := []struct {
|
||||
Name string
|
||||
ID string
|
||||
GroupID *wrapperspb.StringValue
|
||||
Error error
|
||||
Result *pb.Conversation
|
||||
}{
|
||||
{
|
||||
Name: "MissingID",
|
||||
Error: handler.ErrMissingID,
|
||||
},
|
||||
{
|
||||
Name: "IncorrectID",
|
||||
ID: uuid.New().String(),
|
||||
Error: handler.ErrNotFound,
|
||||
},
|
||||
{
|
||||
Name: "FoundUsingIDOnly",
|
||||
ID: cRsp.Conversation.Id,
|
||||
Result: cRsp.Conversation,
|
||||
},
|
||||
{
|
||||
Name: "IncorrectGroupID",
|
||||
ID: cRsp.Conversation.Id,
|
||||
Error: handler.ErrNotFound,
|
||||
GroupID: &wrapperspb.StringValue{Value: uuid.New().String()},
|
||||
},
|
||||
}
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
var rsp pb.ReadConversationResponse
|
||||
err := h.ReadConversation(context.TODO(), &pb.ReadConversationRequest{
|
||||
Id: tc.ID, GroupId: tc.GroupID,
|
||||
}, &rsp)
|
||||
assert.Equal(t, tc.Error, err)
|
||||
|
||||
if tc.Result == nil {
|
||||
assert.Nil(t, rsp.Conversation)
|
||||
} else {
|
||||
assertConversationsMatch(t, tc.Result, rsp.Conversation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
49
streams/handler/recent_messages.go
Normal file
49
streams/handler/recent_messages.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RecentMessages returns the most recent messages in a group of conversations. By default the
|
||||
// most messages retrieved per conversation is 25, however this can be overriden using the
|
||||
// limit_per_conversation option
|
||||
func (s *Streams) RecentMessages(ctx context.Context, req *pb.RecentMessagesRequest, rsp *pb.RecentMessagesResponse) error {
|
||||
// validate the request
|
||||
if len(req.ConversationIds) == 0 {
|
||||
return ErrMissingConversationIDs
|
||||
}
|
||||
|
||||
limit := DefaultLimit
|
||||
if req.LimitPerConversation != nil {
|
||||
limit = int(req.LimitPerConversation.Value)
|
||||
}
|
||||
|
||||
// query the database
|
||||
var msgs []Message
|
||||
err := s.DB.Transaction(func(tx *gorm.DB) error {
|
||||
for _, id := range req.ConversationIds {
|
||||
var cms []Message
|
||||
if err := tx.Where(&Message{ConversationID: id}).Order("sent_at DESC").Limit(limit).Find(&cms).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
msgs = append(msgs, cms...)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
logger.Errorf("Error reading messages: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// serialize the response
|
||||
rsp.Messages = make([]*pb.Message, len(msgs))
|
||||
for i, m := range msgs {
|
||||
rsp.Messages[i] = m.Serialize()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
101
streams/handler/recent_messages_test.go
Normal file
101
streams/handler/recent_messages_test.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/services/streams/handler"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
func TestRecentMessages(t *testing.T) {
|
||||
h := testHandler(t)
|
||||
h.Time = time.Now
|
||||
|
||||
// seed some data
|
||||
ids := make([]string, 3)
|
||||
convos := make(map[string][]*pb.Message, 3)
|
||||
for i := 0; i < 3; i++ {
|
||||
var convRsp pb.CreateConversationResponse
|
||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
||||
Topic: "TestRecentMessages", GroupId: uuid.New().String(),
|
||||
}, &convRsp)
|
||||
assert.NoError(t, err)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
convos[convRsp.Conversation.Id] = make([]*pb.Message, 50)
|
||||
ids[i] = convRsp.Conversation.Id
|
||||
|
||||
for j := 0; j < 50; j++ {
|
||||
var rsp pb.CreateMessageResponse
|
||||
err := h.CreateMessage(context.TODO(), &pb.CreateMessageRequest{
|
||||
ConversationId: convRsp.Conversation.Id,
|
||||
AuthorId: uuid.New().String(),
|
||||
Text: fmt.Sprintf("Conversation %v, Message %v", i, j),
|
||||
}, &rsp)
|
||||
assert.NoError(t, err)
|
||||
convos[convRsp.Conversation.Id][j] = rsp.Message
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("MissingConversationIDs", func(t *testing.T) {
|
||||
var rsp pb.RecentMessagesResponse
|
||||
err := h.RecentMessages(context.TODO(), &pb.RecentMessagesRequest{}, &rsp)
|
||||
assert.Equal(t, handler.ErrMissingConversationIDs, err)
|
||||
assert.Nil(t, rsp.Messages)
|
||||
})
|
||||
|
||||
t.Run("LimitSet", func(t *testing.T) {
|
||||
var rsp pb.RecentMessagesResponse
|
||||
err := h.RecentMessages(context.TODO(), &pb.RecentMessagesRequest{
|
||||
ConversationIds: ids,
|
||||
LimitPerConversation: &wrapperspb.Int32Value{Value: 10},
|
||||
}, &rsp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if len(rsp.Messages) != 30 {
|
||||
t.Fatalf("Expected %v messages but got %v", 30, len(rsp.Messages))
|
||||
return
|
||||
}
|
||||
var expected []*pb.Message
|
||||
for _, msgs := range convos {
|
||||
expected = append(expected, msgs[40:]...)
|
||||
}
|
||||
sortMessages(expected)
|
||||
sortMessages(rsp.Messages)
|
||||
for i, msg := range rsp.Messages {
|
||||
assertMessagesMatch(t, expected[i], msg)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("NoLimitSet", func(t *testing.T) {
|
||||
reducedIDs := ids[:2]
|
||||
|
||||
var rsp pb.RecentMessagesResponse
|
||||
err := h.RecentMessages(context.TODO(), &pb.RecentMessagesRequest{
|
||||
ConversationIds: reducedIDs,
|
||||
}, &rsp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
if len(rsp.Messages) != 50 {
|
||||
t.Fatalf("Expected %v messages but got %v", 50, len(rsp.Messages))
|
||||
return
|
||||
}
|
||||
var expected []*pb.Message
|
||||
for _, id := range reducedIDs {
|
||||
expected = append(expected, convos[id][25:]...)
|
||||
}
|
||||
sortMessages(expected)
|
||||
sortMessages(rsp.Messages)
|
||||
for i, msg := range rsp.Messages {
|
||||
assertMessagesMatch(t, expected[i], msg)
|
||||
}
|
||||
})
|
||||
}
|
||||
60
streams/handler/streams.go
Normal file
60
streams/handler/streams.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrMissingID = errors.BadRequest("MISSING_ID", "Missing ID")
|
||||
ErrMissingGroupID = errors.BadRequest("MISSING_GROUP_ID", "Missing GroupID")
|
||||
ErrMissingTopic = errors.BadRequest("MISSING_TOPIC", "Missing Topic")
|
||||
ErrMissingAuthorID = errors.BadRequest("MISSING_AUTHOR_ID", "Missing Author ID")
|
||||
ErrMissingText = errors.BadRequest("MISSING_TEXT", "Missing text")
|
||||
ErrMissingConversationID = errors.BadRequest("MISSING_CONVERSATION_ID", "Missing Conversation ID")
|
||||
ErrMissingConversationIDs = errors.BadRequest("MISSING_CONVERSATION_IDS", "One or more Conversation IDs are required")
|
||||
ErrNotFound = errors.NotFound("NOT_FOUND", "Conversation not found")
|
||||
)
|
||||
|
||||
type Streams struct {
|
||||
DB *gorm.DB
|
||||
Time func() time.Time
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID string
|
||||
AuthorID string
|
||||
ConversationID string
|
||||
Text string
|
||||
SentAt time.Time
|
||||
}
|
||||
|
||||
func (m *Message) Serialize() *pb.Message {
|
||||
return &pb.Message{
|
||||
Id: m.ID,
|
||||
AuthorId: m.AuthorID,
|
||||
ConversationId: m.ConversationID,
|
||||
Text: m.Text,
|
||||
SentAt: timestamppb.New(m.SentAt),
|
||||
}
|
||||
}
|
||||
|
||||
type Conversation struct {
|
||||
ID string
|
||||
GroupID string
|
||||
Topic string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (c *Conversation) Serialize() *pb.Conversation {
|
||||
return &pb.Conversation{
|
||||
Id: c.ID,
|
||||
GroupId: c.GroupID,
|
||||
Topic: c.Topic,
|
||||
CreatedAt: timestamppb.New(c.CreatedAt),
|
||||
}
|
||||
}
|
||||
84
streams/handler/streams_test.go
Normal file
84
streams/handler/streams_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/micro/services/streams/handler"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func testHandler(t *testing.T) *handler.Streams {
|
||||
// connect to the database
|
||||
db, err := gorm.Open(postgres.Open("postgresql://postgres@localhost:5432/streams?sslmode=disable"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("Error connecting to database: %v", err)
|
||||
}
|
||||
|
||||
// migrate the database
|
||||
if err := db.AutoMigrate(&handler.Conversation{}, &handler.Message{}); err != nil {
|
||||
t.Fatalf("Error migrating database: %v", err)
|
||||
}
|
||||
|
||||
// clean any data from a previous run
|
||||
if err := db.Exec("TRUNCATE TABLE conversations, messages CASCADE").Error; err != nil {
|
||||
t.Fatalf("Error cleaning database: %v", err)
|
||||
}
|
||||
|
||||
return &handler.Streams{DB: db, Time: func() time.Time { return time.Unix(1611327673, 0) }}
|
||||
}
|
||||
|
||||
func assertConversationsMatch(t *testing.T, exp, act *pb.Conversation) {
|
||||
if act == nil {
|
||||
t.Errorf("Conversation not returned")
|
||||
return
|
||||
}
|
||||
|
||||
// adapt this check so we can reuse the func in testing create, where we don't know the exact id
|
||||
// which will be generated
|
||||
if len(exp.Id) > 0 {
|
||||
assert.Equal(t, exp.Id, act.Id)
|
||||
} else {
|
||||
assert.NotEmpty(t, act.Id)
|
||||
}
|
||||
|
||||
assert.Equal(t, exp.Topic, act.Topic)
|
||||
assert.Equal(t, exp.GroupId, act.GroupId)
|
||||
|
||||
if act.CreatedAt == nil {
|
||||
t.Errorf("CreatedAt not set")
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, exp.CreatedAt.AsTime().Equal(act.CreatedAt.AsTime()))
|
||||
}
|
||||
|
||||
func assertMessagesMatch(t *testing.T, exp, act *pb.Message) {
|
||||
if act == nil {
|
||||
t.Errorf("Message not returned")
|
||||
return
|
||||
}
|
||||
|
||||
// adapt this check so we can reuse the func in testing create, where we don't know the exact id
|
||||
// which will be generated
|
||||
if len(exp.Id) > 0 {
|
||||
assert.Equal(t, exp.Id, act.Id)
|
||||
} else {
|
||||
assert.NotEmpty(t, act.Id)
|
||||
}
|
||||
|
||||
assert.Equal(t, exp.Text, act.Text)
|
||||
assert.Equal(t, exp.AuthorId, act.AuthorId)
|
||||
assert.Equal(t, exp.ConversationId, act.ConversationId)
|
||||
|
||||
if act.SentAt == nil {
|
||||
t.Errorf("SentAt not set")
|
||||
return
|
||||
}
|
||||
|
||||
assert.True(t, exp.SentAt.AsTime().Equal(act.SentAt.AsTime()))
|
||||
}
|
||||
41
streams/handler/update_conversation.go
Normal file
41
streams/handler/update_conversation.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Update a conversations topic
|
||||
func (s *Streams) UpdateConversation(ctx context.Context, req *pb.UpdateConversationRequest, rsp *pb.UpdateConversationResponse) error {
|
||||
// validate the request
|
||||
if len(req.Id) == 0 {
|
||||
return ErrMissingID
|
||||
}
|
||||
if len(req.Topic) == 0 {
|
||||
return ErrMissingTopic
|
||||
}
|
||||
|
||||
// lookup the conversation
|
||||
var conv Conversation
|
||||
if err := s.DB.Where(&Conversation{ID: req.Id}).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
||||
return ErrNotFound
|
||||
} else if err != nil {
|
||||
logger.Errorf("Error reading conversation: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// update the conversation
|
||||
conv.Topic = req.Topic
|
||||
if err := s.DB.Save(&conv).Error; err != nil {
|
||||
logger.Errorf("Error updating conversation: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// serialize the result
|
||||
rsp.Conversation = conv.Serialize()
|
||||
return nil
|
||||
}
|
||||
66
streams/handler/update_conversation_test.go
Normal file
66
streams/handler/update_conversation_test.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package handler_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/services/streams/handler"
|
||||
pb "github.com/micro/services/streams/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestUpdateConversation(t *testing.T) {
|
||||
h := testHandler(t)
|
||||
|
||||
// seed some data
|
||||
var cRsp pb.CreateConversationResponse
|
||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
||||
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
||||
}, &cRsp)
|
||||
if err != nil {
|
||||
t.Fatalf("Error creating conversation: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Run("MissingID", func(t *testing.T) {
|
||||
err := h.UpdateConversation(context.TODO(), &pb.UpdateConversationRequest{
|
||||
Topic: "NewTopic",
|
||||
}, &pb.UpdateConversationResponse{})
|
||||
assert.Equal(t, handler.ErrMissingID, err)
|
||||
})
|
||||
|
||||
t.Run("MissingTopic", func(t *testing.T) {
|
||||
err := h.UpdateConversation(context.TODO(), &pb.UpdateConversationRequest{
|
||||
Id: uuid.New().String(),
|
||||
}, &pb.UpdateConversationResponse{})
|
||||
assert.Equal(t, handler.ErrMissingTopic, err)
|
||||
})
|
||||
|
||||
t.Run("InvalidID", func(t *testing.T) {
|
||||
err := h.UpdateConversation(context.TODO(), &pb.UpdateConversationRequest{
|
||||
Id: uuid.New().String(),
|
||||
Topic: "NewTopic",
|
||||
}, &pb.UpdateConversationResponse{})
|
||||
assert.Equal(t, handler.ErrNotFound, err)
|
||||
})
|
||||
|
||||
t.Run("Valid", func(t *testing.T) {
|
||||
err := h.UpdateConversation(context.TODO(), &pb.UpdateConversationRequest{
|
||||
Id: cRsp.Conversation.Id,
|
||||
Topic: "NewTopic",
|
||||
}, &pb.UpdateConversationResponse{})
|
||||
assert.NoError(t, err)
|
||||
|
||||
var rsp pb.ReadConversationResponse
|
||||
err = h.ReadConversation(context.TODO(), &pb.ReadConversationRequest{
|
||||
Id: cRsp.Conversation.Id,
|
||||
}, &rsp)
|
||||
assert.NoError(t, err)
|
||||
if rsp.Conversation == nil {
|
||||
t.Fatal("No conversation returned")
|
||||
return
|
||||
}
|
||||
assert.Equal(t, "NewTopic", rsp.Conversation.Topic)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user