diff --git a/notifications/pkg/channels/channels.go b/notifications/pkg/channels/channels.go new file mode 100644 index 0000000000..3350679507 --- /dev/null +++ b/notifications/pkg/channels/channels.go @@ -0,0 +1,51 @@ +// Package channels provides different communication channels to notify users. +package channels + +import ( + "context" + "net/smtp" + + gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" + "github.com/cs3org/reva/pkg/rgrpc/todo/pool" + "github.com/owncloud/ocis/ocis-pkg/log" +) + +type Channel interface { + SendMessage(receiver, msg string) error +} + +func NewMailChanel(logger log.Logger) (Channel, error) { + gc, err := pool.GetGatewayServiceClient("localhost:9142") + if err != nil { + logger.Error().Err(err).Msg("could not get gateway client") + return nil, err + } + return Mail{ + gatewayClient: gc, + }, nil +} + +type Mail struct { + gatewayClient gateway.GatewayAPIClient +} + +func (m Mail) SendMessage(receiver, msg string) error { + res, err := m.gatewayClient.Authenticate(context.Background(), &gateway.AuthenticateRequest{ + Type: "machine", + ClientId: "userid:" + receiver, + ClientSecret: "change-me-please", + }) + if err != nil { + return err + } + + from := "god" + password := "godisdead" + to := []string{res.User.Mail} + host := "localhost" + port := "1025" + body := []byte(msg) + auth := smtp.PlainAuth("", from, password, host) + + return smtp.SendMail(host+":"+port, auth, from, to, body) +} diff --git a/notifications/pkg/command/server.go b/notifications/pkg/command/server.go index 48ba224aac..af13805919 100644 --- a/notifications/pkg/command/server.go +++ b/notifications/pkg/command/server.go @@ -6,6 +6,7 @@ import ( "github.com/asim/go-micro/plugins/events/nats/v4" "github.com/cs3org/reva/pkg/events" "github.com/cs3org/reva/pkg/events/server" + "github.com/owncloud/ocis/notifications/pkg/channels" "github.com/owncloud/ocis/notifications/pkg/config" "github.com/owncloud/ocis/notifications/pkg/config/parser" "github.com/owncloud/ocis/notifications/pkg/logging" @@ -39,8 +40,11 @@ func Server(cfg *config.Config) *cli.Command { if err != nil { return err } - - svc := service.NewEventsNotifier(evts, logger) + channel, err := channels.NewMailChanel(logger) + if err != nil { + return err + } + svc := service.NewEventsNotifier(evts, channel, logger) return svc.Run() }, } diff --git a/notifications/pkg/service/service.go b/notifications/pkg/service/service.go index 07a46f0838..e220bb9448 100644 --- a/notifications/pkg/service/service.go +++ b/notifications/pkg/service/service.go @@ -1,11 +1,12 @@ package service import ( - "fmt" "os" "os/signal" "syscall" + "github.com/cs3org/reva/pkg/events" + "github.com/owncloud/ocis/notifications/pkg/channels" "github.com/owncloud/ocis/ocis-pkg/log" ) @@ -13,9 +14,10 @@ type Service interface { Run() error } -func NewEventsNotifier(events <-chan interface{}, logger log.Logger) Service { +func NewEventsNotifier(events <-chan interface{}, channel channels.Channel, logger log.Logger) Service { return eventsNotifier{ logger: logger, + channel: channel, events: events, signals: make(chan os.Signal, 1), } @@ -23,6 +25,7 @@ func NewEventsNotifier(events <-chan interface{}, logger log.Logger) Service { type eventsNotifier struct { logger log.Logger + channel channels.Channel events <-chan interface{} signals chan os.Signal } @@ -35,7 +38,14 @@ func (s eventsNotifier) Run() error { select { case evt := <-s.events: go func() { - fmt.Println(evt) + switch e := evt.(type) { + case events.ShareCreated: + if err := s.channel.SendMessage(e.GranteeUserID.OpaqueId, "You got a share"); err != nil { + s.logger.Error(). + Err(err). + Msg("failed to send a message") + } + } }() case <-s.signals: s.logger.Debug().