Seen Service (#39)

* Seen Service

* Fixes for model

* Update import

* More fixes

* Complete seen service
This commit is contained in:
ben-toogood
2021-02-04 11:05:32 +00:00
committed by GitHub
parent bc30a8ad81
commit b02d4dacb5
12 changed files with 1302 additions and 1 deletions

119
seen/handler/handler.go Normal file
View File

@@ -0,0 +1,119 @@
package handler
import (
"context"
"time"
"github.com/google/uuid"
"github.com/micro/services/seen/domain"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
pb "github.com/micro/services/seen/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
var (
ErrMissingUserID = errors.BadRequest("MISSING_USER_ID", "Missing UserID")
ErrMissingResourceID = errors.BadRequest("MISSING_RESOURCE_ID", "Missing ResourceID")
ErrMissingResourceIDs = errors.BadRequest("MISSING_RESOURCE_IDS", "Missing ResourceIDs")
ErrMissingResourceType = errors.BadRequest("MISSING_RESOURCE_TYPE", "Missing ResourceType")
ErrStore = errors.InternalServerError("STORE_ERROR", "Error connecting to the store")
)
type Seen struct {
Domain *domain.Domain
}
// Set a resource as seen by a user. If no timestamp is provided, the current time is used.
func (s *Seen) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse) error {
// validate the request
if len(req.UserId) == 0 {
return ErrMissingUserID
}
if len(req.ResourceId) == 0 {
return ErrMissingResourceID
}
if len(req.ResourceType) == 0 {
return ErrMissingResourceType
}
// default the timestamp
if req.Timestamp == nil {
req.Timestamp = timestamppb.New(time.Now())
}
// write the object to the store
err := s.Domain.Create(domain.Seen{
ID: uuid.New().String(),
UserID: req.UserId,
ResourceID: req.ResourceId,
ResourceType: req.ResourceType,
Timestamp: req.Timestamp.AsTime(),
})
if err != nil {
logger.Errorf("Error with store: %v", err)
return ErrStore
}
return nil
}
// Unset a resource as seen, used in cases where a user viewed a resource but wants to override
// this so they remember to action it in the future, e.g. "Mark this as unread".
func (s *Seen) Unset(ctx context.Context, req *pb.UnsetRequest, rsp *pb.UnsetResponse) error {
// validate the request
if len(req.UserId) == 0 {
return ErrMissingUserID
}
if len(req.ResourceId) == 0 {
return ErrMissingResourceID
}
if len(req.ResourceType) == 0 {
return ErrMissingResourceType
}
// delete the object from the store
err := s.Domain.Delete(domain.Seen{
UserID: req.UserId,
ResourceID: req.ResourceId,
ResourceType: req.ResourceType,
})
if err != nil {
logger.Errorf("Error with store: %v", err)
return ErrStore
}
return nil
}
// Read returns the timestamps at which various resources were seen by a user. If no timestamp
// is returned for a given resource_id, it indicates that resource has not yet been seen by the
// user.
func (s *Seen) Read(ctx context.Context, req *pb.ReadRequest, rsp *pb.ReadResponse) error {
// validate the request
if len(req.UserId) == 0 {
return ErrMissingUserID
}
if len(req.ResourceIds) == 0 {
return ErrMissingResourceIDs
}
if len(req.ResourceType) == 0 {
return ErrMissingResourceType
}
// query the store
data, err := s.Domain.Read(req.UserId, req.ResourceType, req.ResourceIds)
if err != nil {
logger.Errorf("Error with store: %v", err)
return ErrStore
}
// serialize the response
rsp.Timestamps = make(map[string]*timestamppb.Timestamp, len(data))
for uid, ts := range data {
rsp.Timestamps[uid] = timestamppb.New(ts)
}
return nil
}

View File

@@ -0,0 +1,226 @@
package handler_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/google/uuid"
"github.com/micro/micro/v3/service/store/memory"
"github.com/micro/services/seen/domain"
"github.com/micro/services/seen/handler"
pb "github.com/micro/services/seen/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
func newHandler() *handler.Seen {
return &handler.Seen{
Domain: domain.New(memory.NewStore()),
}
}
func TestSet(t *testing.T) {
tt := []struct {
Name string
UserID string
ResourceType string
ResourceID string
Timestamp *timestamppb.Timestamp
Error error
}{
{
Name: "MissingUserID",
ResourceType: "message",
ResourceID: uuid.New().String(),
Error: handler.ErrMissingUserID,
},
{
Name: "MissingResourceID",
UserID: uuid.New().String(),
ResourceType: "message",
Error: handler.ErrMissingResourceID,
},
{
Name: "MissingResourceType",
UserID: uuid.New().String(),
ResourceID: uuid.New().String(),
Error: handler.ErrMissingResourceType,
},
{
Name: "WithTimetamp",
UserID: uuid.New().String(),
ResourceID: uuid.New().String(),
ResourceType: "message",
Timestamp: timestamppb.New(time.Now().Add(time.Minute * -5)),
},
{
Name: "WithoutTimetamp",
UserID: uuid.New().String(),
ResourceID: uuid.New().String(),
ResourceType: "message",
},
}
h := newHandler()
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
err := h.Set(context.TODO(), &pb.SetRequest{
UserId: tc.UserID,
ResourceId: tc.ResourceID,
ResourceType: tc.ResourceType,
Timestamp: tc.Timestamp,
}, &pb.SetResponse{})
assert.Equal(t, tc.Error, err)
})
}
}
func TestUnset(t *testing.T) {
// seed some test data
h := newHandler()
seed := &pb.SetRequest{
UserId: uuid.New().String(),
ResourceId: uuid.New().String(),
ResourceType: "message",
}
err := h.Set(context.TODO(), seed, &pb.SetResponse{})
assert.NoError(t, err)
tt := []struct {
Name string
UserID string
ResourceType string
ResourceID string
Error error
}{
{
Name: "MissingUserID",
ResourceType: "message",
ResourceID: uuid.New().String(),
Error: handler.ErrMissingUserID,
},
{
Name: "MissingResourceID",
UserID: uuid.New().String(),
ResourceType: "message",
Error: handler.ErrMissingResourceID,
},
{
Name: "MissingResourceType",
UserID: uuid.New().String(),
ResourceID: uuid.New().String(),
Error: handler.ErrMissingResourceType,
},
{
Name: "Exists",
UserID: seed.UserId,
ResourceID: seed.ResourceId,
ResourceType: seed.ResourceType,
},
{
Name: "Repeat",
UserID: seed.UserId,
ResourceID: seed.ResourceId,
ResourceType: seed.ResourceType,
},
}
for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
err := h.Unset(context.TODO(), &pb.UnsetRequest{
UserId: tc.UserID,
ResourceId: tc.ResourceID,
ResourceType: tc.ResourceType,
}, &pb.UnsetResponse{})
assert.Equal(t, tc.Error, err)
})
}
}
func TestRead(t *testing.T) {
tn := time.Now()
h := newHandler()
// seed some test data
td := []struct {
UserID string
ResourceID string
ResourceType string
Timestamp *timestamppb.Timestamp
}{
{
UserID: "user-1",
ResourceID: "message-1",
ResourceType: "message",
Timestamp: timestamppb.New(tn),
},
{
UserID: "user-1",
ResourceID: "message-2",
ResourceType: "message",
Timestamp: timestamppb.New(tn.Add(time.Minute * -10)),
},
{
UserID: "user-1",
ResourceID: "notification-1",
ResourceType: "notification",
Timestamp: timestamppb.New(tn.Add(time.Minute * -10)),
},
{
UserID: "user-2",
ResourceID: "message-3",
ResourceType: "message",
Timestamp: timestamppb.New(tn.Add(time.Minute * -10)),
},
}
for _, d := range td {
assert.NoError(t, h.Set(context.TODO(), &pb.SetRequest{
UserId: d.UserID,
ResourceId: d.ResourceID,
ResourceType: d.ResourceType,
Timestamp: d.Timestamp,
}, &pb.SetResponse{}))
}
// check only the requested values are returned
var rsp pb.ReadResponse
err := h.Read(context.TODO(), &pb.ReadRequest{
UserId: "user-1",
ResourceType: "message",
ResourceIds: []string{"message-1", "message-2", "message-3"},
}, &rsp)
assert.NoError(t, err)
assert.Len(t, rsp.Timestamps, 2)
if v := rsp.Timestamps["message-1"]; v != nil {
assert.True(t, v.AsTime().Equal(tn))
} else {
t.Errorf("Expected a timestamp for message-1")
}
if v := rsp.Timestamps["message-2"]; v != nil {
assert.True(t, v.AsTime().Equal(tn.Add(time.Minute*-10)))
} else {
t.Errorf("Expected a timestamp for message-2")
}
// unsetting a resource should remove it from the list
err = h.Unset(context.TODO(), &pb.UnsetRequest{
UserId: "user-1",
ResourceId: "message-2",
ResourceType: "message",
}, &pb.UnsetResponse{})
assert.NoError(t, err)
rsp = pb.ReadResponse{}
err = h.Read(context.TODO(), &pb.ReadRequest{
UserId: "user-1",
ResourceType: "message",
ResourceIds: []string{"message-1", "message-2", "message-3"},
}, &rsp)
assert.NoError(t, err)
assert.Len(t, rsp.Timestamps, 1)
}