From f48c8a4f4f55c5541b6ef3e5ffb891e6f5d02484 Mon Sep 17 00:00:00 2001 From: David Janowski Date: Mon, 8 Jul 2024 14:56:17 +0200 Subject: [PATCH] feat: implemented the new Message Function --- pkg/plug/grpc.go | 20 +++++++++++++++++--- pkg/plug/server.go | 4 ++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/pkg/plug/grpc.go b/pkg/plug/grpc.go index f6156c6..18fb5e0 100644 --- a/pkg/plug/grpc.go +++ b/pkg/plug/grpc.go @@ -2,10 +2,11 @@ package plug import ( "context" + "log" + "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/database" "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/models" "google.golang.org/protobuf/types/known/timestamppb" - "log" gRPC "git.dragse.it/anthrove/plug-sdk/pkg/grpc" gonanoid "github.com/matoous/go-nanoid/v2" @@ -16,13 +17,15 @@ type server struct { ctx map[string]context.CancelFunc database database.OtterSpace taskExecutionFunction TaskExecution + sendMessageExecution SendMessageExecution } -func NewGrpcServer(database database.OtterSpace, taskExecutionFunction TaskExecution) gRPC.PlugConnectorServer { +func NewGrpcServer(database database.OtterSpace, taskExecutionFunction TaskExecution, sendMessageExecution SendMessageExecution) gRPC.PlugConnectorServer { return &server{ ctx: make(map[string]context.CancelFunc), database: database, taskExecutionFunction: taskExecutionFunction, + sendMessageExecution: sendMessageExecution, } } @@ -49,7 +52,7 @@ func (s *server) TaskStart(ctx context.Context, creation *gRPC.PlugTaskCreation) go func() { // FIXME: better implement this methode, works for now but needs refactoring - err := s.taskExecutionFunction(ctx, s.database, creation.UserSourceName, anthroveUser, creation.DeepScrape, func() { + err := s.taskExecutionFunction(ctx, s.database, creation.UserSourceName, anthroveUser, creation.DeepScrape, creation.ApiKey, func() { s.removeTask(id) }) if err != nil { @@ -101,3 +104,14 @@ func (s *server) Ping(_ context.Context, request *gRPC.PingRequest) (*gRPC.PongR } return &response, nil } + +func (s *server) SendMessage(ctx context.Context, request *gRPC.SendMessageRequest) (*gRPC.SendMessageResponse, error) { + messageResponse := gRPC.SendMessageResponse{Success: true} + + err := s.sendMessageExecution(ctx, request.UserId, request.Message) + if err != nil { + return nil, err + } + + return &messageResponse, nil +} diff --git a/pkg/plug/server.go b/pkg/plug/server.go index d187d22..a7ddf55 100644 --- a/pkg/plug/server.go +++ b/pkg/plug/server.go @@ -14,7 +14,7 @@ import ( ) type TaskExecution func(ctx context.Context, database database.OtterSpace, sourceUsername string, anthroveUser models.User, deepScrape bool, apiKey string, cancelFunction func()) error -type SendMessageExecution func(ctx context.Context, userSourceID string, message string) +type SendMessageExecution func(ctx context.Context, userSourceID string, message string) error type Plug struct { address string @@ -58,7 +58,7 @@ func (p *Plug) Listen() error { grpcServer := grpc.NewServer() - pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(p.database, p.taskExecutionFunction)) + pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(p.database, p.taskExecutionFunction, p.sendMessageExecution)) err = grpcServer.Serve(lis) if err != nil {