2024-02-20 18:11:34 +00:00
|
|
|
package plug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-07-08 20:15:05 +00:00
|
|
|
"errors"
|
2024-02-20 18:11:34 +00:00
|
|
|
"fmt"
|
2024-07-09 20:16:43 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/v2/pkg/database"
|
2024-07-08 20:15:05 +00:00
|
|
|
otterError "git.dragse.it/anthrove/otter-space-sdk/v2/pkg/error"
|
2024-07-09 20:16:43 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/v2/pkg/models"
|
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
2024-07-08 12:33:06 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
|
2024-07-08 13:16:59 +00:00
|
|
|
pb "git.dragse.it/anthrove/plug-sdk/v2/pkg/grpc"
|
2024-02-20 18:11:34 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2024-07-09 20:16:43 +00:00
|
|
|
type Message struct {
|
|
|
|
title string
|
|
|
|
body string
|
|
|
|
createdAt *timestamp.Timestamp
|
|
|
|
}
|
|
|
|
|
2024-07-08 12:33:06 +00:00
|
|
|
type TaskExecution func(ctx context.Context, database database.OtterSpace, sourceUsername string, anthroveUser models.User, deepScrape bool, apiKey string, cancelFunction func()) error
|
2024-07-08 12:56:17 +00:00
|
|
|
type SendMessageExecution func(ctx context.Context, userSourceID string, message string) error
|
2024-07-09 20:16:43 +00:00
|
|
|
type GetMessageExecution func(ctx context.Context, userSourceID string, userSourceUsername string) ([]Message, error)
|
2024-02-20 20:12:49 +00:00
|
|
|
|
2024-07-08 12:33:06 +00:00
|
|
|
type Plug struct {
|
2024-02-20 20:12:49 +00:00
|
|
|
address string
|
|
|
|
port string
|
|
|
|
ctx context.Context
|
2024-07-06 22:26:24 +00:00
|
|
|
database database.OtterSpace
|
2024-02-20 20:12:49 +00:00
|
|
|
taskExecutionFunction TaskExecution
|
2024-07-08 12:33:06 +00:00
|
|
|
sendMessageExecution SendMessageExecution
|
2024-07-09 20:16:43 +00:00
|
|
|
getMessageExecution GetMessageExecution
|
2024-07-08 12:33:06 +00:00
|
|
|
source models.Source
|
2024-02-20 18:11:34 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 12:33:06 +00:00
|
|
|
func NewPlug(ctx context.Context, address string, port string, source models.Source) Plug {
|
|
|
|
return Plug{
|
2024-02-20 18:11:34 +00:00
|
|
|
ctx: ctx,
|
|
|
|
address: address,
|
|
|
|
port: port,
|
2024-07-08 12:33:06 +00:00
|
|
|
source: source,
|
2024-02-20 18:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-08 12:33:06 +00:00
|
|
|
func (p *Plug) Listen() error {
|
2024-02-20 18:11:34 +00:00
|
|
|
var err error
|
|
|
|
|
2024-07-08 19:08:37 +00:00
|
|
|
log.Printf("initilazing source!")
|
|
|
|
err = p.database.CreateSource(p.ctx, &p.source)
|
2024-07-08 12:33:06 +00:00
|
|
|
if err != nil {
|
2024-07-08 20:15:05 +00:00
|
|
|
if !errors.Is(err, &otterError.NoDataWritten{}) {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2024-07-08 12:33:06 +00:00
|
|
|
}
|
|
|
|
|
2024-07-08 13:16:59 +00:00
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", p.address, p.port))
|
2024-02-20 18:11:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer()
|
|
|
|
|
2024-07-09 20:16:43 +00:00
|
|
|
pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(p.database, p.taskExecutionFunction, p.sendMessageExecution, p.getMessageExecution))
|
2024-02-20 18:11:34 +00:00
|
|
|
|
|
|
|
err = grpcServer.Serve(lis)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-08 12:33:06 +00:00
|
|
|
func (p *Plug) WithOtterSpace(graph database.OtterSpace) {
|
|
|
|
p.database = graph
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plug) TaskExecutionFunction(function TaskExecution) {
|
|
|
|
p.taskExecutionFunction = function
|
2024-02-20 18:11:34 +00:00
|
|
|
}
|
2024-02-20 20:12:49 +00:00
|
|
|
|
2024-07-08 12:33:06 +00:00
|
|
|
func (p *Plug) SendMessageExecution(function SendMessageExecution) {
|
|
|
|
p.sendMessageExecution = function
|
2024-02-20 20:12:49 +00:00
|
|
|
}
|
2024-07-09 20:16:43 +00:00
|
|
|
|
|
|
|
func (p *Plug) GetMessageExecution(function GetMessageExecution) {
|
|
|
|
p.getMessageExecution = function
|
|
|
|
}
|