From 79873bdd8c7f855ce20b9aca23ec94ee1ac63d06 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 9 Jul 2024 22:16:43 +0200 Subject: [PATCH] feat: added support for the new get message function --- pkg/plug/grpc.go | 25 ++++++++++++++++++++++++- pkg/plug/server.go | 20 ++++++++++++++++---- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/pkg/plug/grpc.go b/pkg/plug/grpc.go index 0772010..45eff05 100644 --- a/pkg/plug/grpc.go +++ b/pkg/plug/grpc.go @@ -18,14 +18,16 @@ type server struct { database database.OtterSpace taskExecutionFunction TaskExecution sendMessageExecution SendMessageExecution + getMessageExecution GetMessageExecution } -func NewGrpcServer(database database.OtterSpace, taskExecutionFunction TaskExecution, sendMessageExecution SendMessageExecution) gRPC.PlugConnectorServer { +func NewGrpcServer(database database.OtterSpace, taskExecutionFunction TaskExecution, sendMessageExecution SendMessageExecution, getMessageExecution GetMessageExecution) gRPC.PlugConnectorServer { return &server{ ctx: make(map[string]context.CancelFunc), database: database, taskExecutionFunction: taskExecutionFunction, sendMessageExecution: sendMessageExecution, + getMessageExecution: getMessageExecution, } } @@ -115,3 +117,24 @@ func (s *server) SendMessage(ctx context.Context, request *gRPC.SendMessageReque return &messageResponse, nil } + +func (s *server) GetUserMessages(ctx context.Context, request *gRPC.GetMessagesRequest) (*gRPC.GetMessagesResponse, error) { + messageResponse := gRPC.GetMessagesResponse{} + + messages, err := s.getMessageExecution(ctx, request.UserId, request.UserName) + if err != nil { + return nil, err + } + + for _, message := range messages { + messageResponse.Messages = append(messageResponse.Messages, &gRPC.Message{ + FromUserId: request.UserId, + FromUserName: request.UserName, + CreatedAt: message.createdAt, + Body: message.body, + Title: message.title, + }) + } + + return &messageResponse, nil +} diff --git a/pkg/plug/server.go b/pkg/plug/server.go index c416159..71d99b6 100644 --- a/pkg/plug/server.go +++ b/pkg/plug/server.go @@ -4,19 +4,26 @@ import ( "context" "errors" "fmt" + "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/database" otterError "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/error" + "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/models" + "github.com/golang/protobuf/ptypes/timestamp" "log" "net" - "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/database" - "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/models" - pb "git.dragse.it/anthrove/plug-sdk/v2/pkg/grpc" "google.golang.org/grpc" ) +type Message struct { + title string + body string + createdAt *timestamp.Timestamp +} + 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) error +type GetMessageExecution func(ctx context.Context, userSourceID string, userSourceUsername string) ([]Message, error) type Plug struct { address string @@ -25,6 +32,7 @@ type Plug struct { database database.OtterSpace taskExecutionFunction TaskExecution sendMessageExecution SendMessageExecution + getMessageExecution GetMessageExecution source models.Source } @@ -55,7 +63,7 @@ func (p *Plug) Listen() error { grpcServer := grpc.NewServer() - pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(p.database, p.taskExecutionFunction, p.sendMessageExecution)) + pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(p.database, p.taskExecutionFunction, p.sendMessageExecution, p.getMessageExecution)) err = grpcServer.Serve(lis) if err != nil { @@ -76,3 +84,7 @@ func (p *Plug) TaskExecutionFunction(function TaskExecution) { func (p *Plug) SendMessageExecution(function SendMessageExecution) { p.sendMessageExecution = function } + +func (p *Plug) GetMessageExecution(function GetMessageExecution) { + p.getMessageExecution = function +}