3
0

Refactor event sending + add user preloading

This commit is contained in:
Denis Arh 2018-09-27 19:11:01 +02:00
parent 6f2a270ece
commit 2715e5a3f7
3 changed files with 87 additions and 35 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/titpetric/factory"
authService "github.com/crusttech/crust/auth/service"
"github.com/crusttech/crust/internal/store"
"github.com/crusttech/crust/sam/repository"
"github.com/crusttech/crust/sam/types"
@ -20,11 +21,12 @@ type (
db *factory.DB
ctx context.Context
store store.Store
usr authService.UserService
evl EventService
attachment repository.AttachmentRepository
message repository.MessageRepository
store store.Store
evl EventService
}
AttachmentService interface {
@ -40,6 +42,7 @@ type (
func Attachment(store store.Store) AttachmentService {
return (&attachment{
store: store,
usr: authService.DefaultUser,
evl: DefaultEvent,
}).With(context.Background())
}
@ -50,11 +53,12 @@ func (svc *attachment) With(ctx context.Context) AttachmentService {
db: db,
ctx: ctx,
store: svc.store,
usr: svc.usr.With(ctx),
evl: svc.evl.With(ctx),
attachment: repository.Attachment(ctx, db),
message: repository.Message(ctx, db),
store: svc.store,
evl: svc.evl.With(ctx),
}
}
@ -135,9 +139,7 @@ func (svc *attachment) Create(channelId uint64, name string, size int64, fh io.R
log.Printf("File %s (id: %d) attached to message (id: %d)", att.Name, att.ID, msg.ID)
msg.Attachment = att
msg.Attachment.GenerateURLs()
return svc.evl.Message(msg)
return svc.sendEvent(msg, att)
})
}
@ -191,4 +193,21 @@ func (svc *attachment) makePreview(att *types.Attachment, original io.ReadSeeker
return svc.store.Save(att.PreviewUrl, original)
}
// Sends message to event loop
//
// It also preloads user
func (svc *attachment) sendEvent(msg *types.Message, att *types.Attachment) (err error) {
msg.Attachment = att
msg.Attachment.GenerateURLs()
if msg.User == nil {
// @todo pull user from cache
if msg.User, err = svc.usr.FindByID(msg.UserID); err != nil {
return
}
}
return svc.evl.Message(msg)
}
var _ AttachmentService = &attachment{}

View File

@ -8,6 +8,7 @@ import (
"github.com/pkg/errors"
"github.com/titpetric/factory"
authService "github.com/crusttech/crust/auth/service"
"github.com/crusttech/crust/sam/repository"
"github.com/crusttech/crust/sam/types"
)
@ -17,6 +18,7 @@ type (
db *factory.DB
ctx context.Context
usr authService.UserService
evl EventService
channel repository.ChannelRepository
@ -45,6 +47,7 @@ type (
func Channel() ChannelService {
return (&channel{
usr: authService.DefaultUser,
evl: DefaultEvent,
}).With(context.Background())
}
@ -55,6 +58,7 @@ func (svc *channel) With(ctx context.Context) ChannelService {
db: db,
ctx: ctx,
usr: svc.usr.With(ctx),
evl: svc.evl.With(ctx),
channel: repository.Channel(ctx, db),
@ -198,7 +202,7 @@ func (svc *channel) Create(in *types.Channel) (out *types.Channel, err error) {
return
}
svc.evl.Message(msg)
svc.sendMessageEvent(msg)
return svc.evl.Channel(out)
})
@ -219,6 +223,23 @@ func (svc *channel) Update(in *types.Channel) (out *types.Channel, err error) {
return errors.New("Not allowed to edit deleted channels")
}
if out.Type != in.Type {
// @todo [SECURITY] check if user can create public channels
if in.Type == types.ChannelTypePublic && false {
return errors.New("Not allowed to change type of this channel to public")
}
// @todo [SECURITY] check if user can create private channels
if in.Type == types.ChannelTypePrivate && false {
return errors.New("Not allowed to change type of this channel to private")
}
// @todo [SECURITY] check if user can create group channels
if in.Type == types.ChannelTypeGroup && false {
return errors.New("Not allowed to change type of this channel to group")
}
}
var chUpdatorId = repository.Identity(svc.ctx)
// Copy values
@ -245,23 +266,6 @@ func (svc *channel) Update(in *types.Channel) (out *types.Channel, err error) {
out.Topic = in.Topic
}
if out.Type != in.Type {
// @todo [SECURITY] check if user can create public channels
if in.Type == types.ChannelTypePublic && false {
return errors.New("Not allowed to change type of this channel to public")
}
// @todo [SECURITY] check if user can create private channels
if in.Type == types.ChannelTypePrivate && false {
return errors.New("Not allowed to change type of this channel to private")
}
// @todo [SECURITY] check if user can create group channels
if in.Type == types.ChannelTypeGroup && false {
return errors.New("Not allowed to change type of this channel to group")
}
}
// Save the updated channel
if out, err = svc.channel.UpdateChannel(in); err != nil {
return
@ -274,7 +278,7 @@ func (svc *channel) Update(in *types.Channel) (out *types.Channel, err error) {
return err
}
svc.evl.Message(msg)
svc.sendMessageEvent(msg)
}
if err != nil {
@ -306,7 +310,7 @@ func (svc *channel) Delete(id uint64) error {
if err != nil {
return
}
svc.evl.Message(msg)
svc.sendMessageEvent(msg)
if err = svc.channel.DeleteChannelByID(id); err != nil {
return
@ -336,7 +340,7 @@ func (svc *channel) Recover(id uint64) error {
if err != nil {
return
}
svc.evl.Message(msg)
svc.sendMessageEvent(msg)
err = svc.channel.UnarchiveChannelByID(id)
if err != nil {
@ -368,7 +372,7 @@ func (svc *channel) Archive(id uint64) error {
if err != nil {
return
}
svc.evl.Message(msg)
svc.sendMessageEvent(msg)
err = svc.channel.ArchiveChannelByID(id)
if err != nil {
@ -399,7 +403,7 @@ func (svc *channel) Unarchive(id uint64) error {
if err != nil {
return
}
svc.evl.Message(msg)
svc.sendMessageEvent(msg)
err = svc.channel.ArchiveChannelByID(id)
if err != nil {
@ -419,6 +423,20 @@ func (svc *channel) makeSystemMessage(ch *types.Channel, format string, a ...int
}
}
// Sends message to event loop
//
// It also preloads user
func (svc *channel) sendMessageEvent(msg *types.Message) (err error) {
if msg.User == nil {
// @todo pull user from cache
if msg.User, err = svc.usr.FindByID(msg.UserID); err != nil {
return
}
}
return svc.evl.Message(msg)
}
//// @todo temp location, move this somewhere else
//type (
// nativeChannelSec struct {

View File

@ -60,6 +60,7 @@ func (svc *message) With(ctx context.Context) MessageService {
return &message{
db: db,
ctx: ctx,
usr: svc.usr.With(ctx),
evl: svc.evl.With(ctx),
@ -167,7 +168,7 @@ func (svc *message) Direct(recipientID uint64, in *types.Message) (out *types.Me
return
}
return svc.evl.Message(out)
return svc.sendEvent(out)
})
}
@ -185,7 +186,7 @@ func (svc *message) Create(mod *types.Message) (*types.Message, error) {
return nil, err
}
return message, svc.evl.Message(message)
return message, svc.sendEvent(message)
}
func (svc *message) Update(mod *types.Message) (*types.Message, error) {
@ -205,7 +206,7 @@ func (svc *message) Update(mod *types.Message) (*types.Message, error) {
return nil, err
}
return message, svc.evl.Message(message)
return message, svc.sendEvent(message)
}
func (svc *message) Delete(id uint64) error {
@ -303,4 +304,18 @@ func (svc *message) Unflag(messageID uint64) error {
return nil
}
// Sends message to event loop
//
// It also preloads user
func (svc *message) sendEvent(msg *types.Message) (err error) {
if msg.User == nil {
// @todo pull user from cache
if msg.User, err = svc.usr.FindByID(msg.UserID); err != nil {
return
}
}
return svc.evl.Message(msg)
}
var _ MessageService = &message{}