Simplify & unify service initialization, calling, handling
This commit is contained in:
@@ -44,11 +44,10 @@ const (
|
||||
attachmentPreviewURL = "/attachment/%d/%s/preview"
|
||||
)
|
||||
|
||||
func Attachment(store store.Store) *attachment {
|
||||
svc := (&attachment{
|
||||
func Attachment(store store.Store) AttachmentService {
|
||||
return (&attachment{
|
||||
store: store,
|
||||
}).With(context.Background()).(*attachment)
|
||||
return svc
|
||||
}).With(context.Background())
|
||||
}
|
||||
|
||||
func (svc *attachment) With(ctx context.Context) AttachmentService {
|
||||
@@ -72,7 +71,6 @@ func (svc *attachment) OpenOriginal(att *types.Attachment) (io.ReadSeeker, error
|
||||
|
||||
func (svc *attachment) OpenPreview(att *types.Attachment) (io.ReadSeeker, error) {
|
||||
return svc.store.Open(att.PreviewUrl)
|
||||
|
||||
}
|
||||
|
||||
func (svc *attachment) LoadFromMessages(mm types.MessageSet) (err error) {
|
||||
|
||||
@@ -30,8 +30,9 @@ type (
|
||||
Create(channel *types.Channel) (*types.Channel, error)
|
||||
Update(channel *types.Channel) (*types.Channel, error)
|
||||
|
||||
deleter
|
||||
archiver
|
||||
Archive(ID uint64) error
|
||||
Unarchive(ID uint64) error
|
||||
Delete(ID uint64) error
|
||||
}
|
||||
|
||||
//channelSecurity interface {
|
||||
@@ -39,10 +40,8 @@ type (
|
||||
//}
|
||||
)
|
||||
|
||||
func Channel() *channel {
|
||||
svc := (&channel{}).With(context.Background()).(*channel)
|
||||
//svc.sec.ch = ChannelSecurity(svc.channel)
|
||||
return svc
|
||||
func Channel() ChannelService {
|
||||
return (&channel{}).With(context.Background())
|
||||
}
|
||||
|
||||
func (svc *channel) With(ctx context.Context) ChannelService {
|
||||
|
||||
@@ -41,15 +41,12 @@ type (
|
||||
|
||||
Direct(recipientID uint64, in *types.Message) (out *types.Message, err error)
|
||||
|
||||
deleter
|
||||
Delete(ID uint64) error
|
||||
}
|
||||
)
|
||||
|
||||
func Message(attSvc AttachmentService) *message {
|
||||
m := (&message{
|
||||
att: attSvc,
|
||||
}).With(context.Background()).(*message)
|
||||
return m
|
||||
func Message() MessageService {
|
||||
return (&message{}).With(context.Background())
|
||||
}
|
||||
|
||||
func (svc *message) With(ctx context.Context) MessageService {
|
||||
@@ -57,7 +54,7 @@ func (svc *message) With(ctx context.Context) MessageService {
|
||||
return &message{
|
||||
db: db,
|
||||
ctx: ctx,
|
||||
att: svc.att,
|
||||
att: DefaultAttachment.With(ctx),
|
||||
channel: repository.Channel(ctx, db),
|
||||
message: repository.Message(ctx, db),
|
||||
reaction: repository.Reaction(ctx, db),
|
||||
|
||||
@@ -26,13 +26,14 @@ type (
|
||||
Create(organisation *types.Organisation) (*types.Organisation, error)
|
||||
Update(organisation *types.Organisation) (*types.Organisation, error)
|
||||
|
||||
deleter
|
||||
archiver
|
||||
Archive(ID uint64) error
|
||||
Unarchive(ID uint64) error
|
||||
Delete(ID uint64) error
|
||||
}
|
||||
)
|
||||
|
||||
func Organisation() *organisation {
|
||||
return (&organisation{}).With(context.Background()).(*organisation)
|
||||
func Organisation() OrganisationService {
|
||||
return (&organisation{}).With(context.Background())
|
||||
}
|
||||
|
||||
func (svc *organisation) With(ctx context.Context) OrganisationService {
|
||||
|
||||
@@ -26,7 +26,7 @@ func Init() {
|
||||
|
||||
DefaultAttachment = Attachment(fs)
|
||||
DefaultChannel = Channel()
|
||||
DefaultMessage = Message(DefaultAttachment)
|
||||
DefaultMessage = Message()
|
||||
DefaultOrganisation = Organisation()
|
||||
DefaultPubSub = PubSub()
|
||||
DefaultTeam = Team()
|
||||
|
||||
+5
-4
@@ -28,13 +28,14 @@ type (
|
||||
Merge(teamID, targetTeamID uint64) error
|
||||
Move(teamID, organisationID uint64) error
|
||||
|
||||
deleter
|
||||
archiver
|
||||
Archive(ID uint64) error
|
||||
Unarchive(ID uint64) error
|
||||
Delete(ID uint64) error
|
||||
}
|
||||
)
|
||||
|
||||
func Team() *team {
|
||||
return (&team{}).With(context.Background()).(*team)
|
||||
func Team() TeamService {
|
||||
return (&team{}).With(context.Background())
|
||||
}
|
||||
|
||||
func (svc *team) With(ctx context.Context) TeamService {
|
||||
|
||||
@@ -36,6 +36,7 @@ type (
|
||||
svc struct {
|
||||
user authService.UserService
|
||||
ch samService.ChannelService
|
||||
msg samService.MessageService
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -52,6 +53,7 @@ func (Session) New(ctx context.Context, config *repository.Flags, conn *websocke
|
||||
|
||||
s.svc.user = authService.DefaultUser
|
||||
s.svc.ch = samService.DefaultChannel
|
||||
s.svc.msg = samService.DefaultMessage
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -2,17 +2,13 @@ package websocket
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/crusttech/crust/internal/auth"
|
||||
"github.com/crusttech/crust/sam/service"
|
||||
"github.com/crusttech/crust/sam/types"
|
||||
"github.com/crusttech/crust/sam/websocket/incoming"
|
||||
"github.com/crusttech/crust/sam/websocket/outgoing"
|
||||
)
|
||||
|
||||
func channelService(ctx context.Context) service.ChannelService {
|
||||
return service.Channel().With(ctx)
|
||||
}
|
||||
|
||||
func (s *Session) channelJoin(ctx context.Context, p *incoming.ChannelJoin) error {
|
||||
// @todo: check access / can we join this channel (should be done by service layer)
|
||||
|
||||
@@ -48,7 +44,7 @@ func (s *Session) channelPart(ctx context.Context, p *incoming.ChannelPart) erro
|
||||
}
|
||||
|
||||
func (s *Session) channelList(ctx context.Context, p *incoming.Channels) error {
|
||||
channels, err := channelService(ctx).Find(&types.ChannelFilter{IncludeMembers: true})
|
||||
channels, err := s.svc.ch.With(ctx).Find(&types.ChannelFilter{IncludeMembers: true})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -75,7 +71,7 @@ func (s *Session) channelCreate(ctx context.Context, p *incoming.ChannelCreate)
|
||||
ch.Type = types.ChannelType(*p.Type)
|
||||
}
|
||||
|
||||
ch, err = channelService(ctx).Create(ch)
|
||||
ch, err = s.svc.ch.With(ctx).Create(ch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -98,7 +94,7 @@ func (s *Session) channelCreate(ctx context.Context, p *incoming.ChannelCreate)
|
||||
}
|
||||
|
||||
func (s *Session) channelDelete(ctx context.Context, p *incoming.ChannelDelete) (err error) {
|
||||
err = channelService(ctx).Delete(parseUInt64(p.ChannelID))
|
||||
err = s.svc.ch.With(ctx).Delete(parseUInt64(p.ChannelID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -110,7 +106,7 @@ func (s *Session) channelDelete(ctx context.Context, p *incoming.ChannelDelete)
|
||||
}
|
||||
|
||||
func (s *Session) channelUpdate(ctx context.Context, p *incoming.ChannelUpdate) error {
|
||||
ch, err := channelService(ctx).FindByID(parseUInt64(p.ID))
|
||||
ch, err := s.svc.ch.With(ctx).FindByID(parseUInt64(p.ID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -127,7 +123,7 @@ func (s *Session) channelUpdate(ctx context.Context, p *incoming.ChannelUpdate)
|
||||
ch.Type = types.ChannelType(*p.Type)
|
||||
}
|
||||
|
||||
ch, err = channelService(ctx).Update(ch)
|
||||
ch, err = s.svc.ch.With(ctx).Update(ch)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -3,22 +3,11 @@ package websocket
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/crusttech/crust/sam/service"
|
||||
"github.com/crusttech/crust/sam/types"
|
||||
"github.com/crusttech/crust/sam/websocket/incoming"
|
||||
"github.com/crusttech/crust/sam/websocket/outgoing"
|
||||
|
||||
fstore "github.com/crusttech/crust/internal/store"
|
||||
)
|
||||
|
||||
func messageService(ctx context.Context) service.MessageService {
|
||||
// @todo refactor, optimize this
|
||||
store, _ := fstore.New("var/store")
|
||||
attSvc := service.Attachment(store)
|
||||
msgSvc := service.Message(attSvc)
|
||||
return msgSvc.With(ctx)
|
||||
}
|
||||
|
||||
func (s *Session) messageCreate(ctx context.Context, p *incoming.MessageCreate) error {
|
||||
var (
|
||||
msg = &types.Message{
|
||||
@@ -27,7 +16,7 @@ func (s *Session) messageCreate(ctx context.Context, p *incoming.MessageCreate)
|
||||
}
|
||||
)
|
||||
|
||||
msg, err := messageService(ctx).Create(msg)
|
||||
msg, err := s.svc.msg.With(ctx).Create(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -42,7 +31,7 @@ func (s *Session) messageUpdate(ctx context.Context, p *incoming.MessageUpdate)
|
||||
Message: p.Message,
|
||||
}
|
||||
)
|
||||
msg, err := messageService(ctx).Update(msg)
|
||||
msg, err := s.svc.msg.With(ctx).Update(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -61,7 +50,7 @@ func (s *Session) messageDelete(ctx context.Context, p *incoming.MessageDelete)
|
||||
id = parseUInt64(p.ID)
|
||||
)
|
||||
|
||||
if err := messageService(ctx).Delete(id); err != nil {
|
||||
if err := s.svc.msg.With(ctx).Delete(id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -80,7 +69,7 @@ func (s *Session) messageHistory(ctx context.Context, p *incoming.Messages) erro
|
||||
}
|
||||
)
|
||||
|
||||
messages, err := messageService(ctx).Find(filter)
|
||||
messages, err := s.svc.msg.With(ctx).Find(filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user