Updated to latest Protobuf #3

Merged
fenpaws merged 5 commits from develop/protobuf into main 2024-07-09 20:34:10 +00:00
2 changed files with 40 additions and 5 deletions
Showing only changes of commit 79873bdd8c - Show all commits

View File

@ -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
}

View File

@ -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
}