2024-07-19 13:28:48 +00:00
|
|
|
package plug
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
|
2024-08-14 14:06:53 +00:00
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/database"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v3/pkg/models"
|
|
|
|
|
2024-07-19 13:28:48 +00:00
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
|
|
|
|
2024-08-14 14:16:42 +00:00
|
|
|
pb "git.anthrove.art/Anthrove/plug-sdk/v3/pkg/grpc"
|
2024-07-19 13:28:48 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Title string
|
|
|
|
Body string
|
|
|
|
CreatedAt *timestamp.Timestamp
|
|
|
|
}
|
|
|
|
|
2024-08-17 18:23:11 +00:00
|
|
|
type TaskExecution func(ctx context.Context, userSource models.UserSource, deepScrape bool, apiKey string, cancelFunction func()) error
|
|
|
|
type SendMessageExecution func(ctx context.Context, userSource models.UserSource, message string) error
|
|
|
|
type GetMessageExecution func(ctx context.Context, userSource models.UserSource) ([]Message, error)
|
2024-07-19 13:28:48 +00:00
|
|
|
|
|
|
|
type Plug struct {
|
|
|
|
address string
|
|
|
|
port string
|
|
|
|
ctx context.Context
|
|
|
|
taskExecutionFunction TaskExecution
|
|
|
|
sendMessageExecution SendMessageExecution
|
|
|
|
getMessageExecution GetMessageExecution
|
|
|
|
source models.Source
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPlug(ctx context.Context, address string, port string, source models.Source) Plug {
|
|
|
|
return Plug{
|
|
|
|
ctx: ctx,
|
|
|
|
address: address,
|
|
|
|
port: port,
|
|
|
|
source: source,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plug) Listen() error {
|
|
|
|
var err error
|
2024-08-15 07:56:40 +00:00
|
|
|
var source models.Source
|
2024-07-19 13:28:48 +00:00
|
|
|
|
2024-08-15 07:56:40 +00:00
|
|
|
source, err = database.GetSourceByDomain(p.ctx, p.source.Domain)
|
2024-07-19 13:28:48 +00:00
|
|
|
if err != nil {
|
2024-08-24 22:09:21 +00:00
|
|
|
if err.Error() == "Database error: NoDataFound" {
|
2024-08-15 07:56:40 +00:00
|
|
|
log.Printf("Initalizing source!")
|
2024-08-24 21:42:38 +00:00
|
|
|
source, err = database.CreateSource(p.ctx, p.source)
|
|
|
|
if err != nil {
|
2024-08-14 14:06:53 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(err)
|
2024-07-19 13:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-15 07:56:40 +00:00
|
|
|
p.source = source
|
|
|
|
|
2024-08-14 14:06:53 +00:00
|
|
|
log.Print("Source exists")
|
|
|
|
|
2024-08-17 18:23:11 +00:00
|
|
|
// Start the Server
|
|
|
|
|
2024-07-19 13:28:48 +00:00
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", p.address, p.port))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
grpcServer := grpc.NewServer()
|
|
|
|
|
2024-08-15 07:56:40 +00:00
|
|
|
pb.RegisterPlugConnectorServer(grpcServer, NewGrpcServer(p.source, p.taskExecutionFunction, p.sendMessageExecution, p.getMessageExecution))
|
2024-07-19 13:28:48 +00:00
|
|
|
|
|
|
|
err = grpcServer.Serve(lis)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-15 07:56:40 +00:00
|
|
|
func (p *Plug) GetSource() models.Source {
|
|
|
|
return p.source
|
|
|
|
}
|
|
|
|
|
2024-07-19 13:28:48 +00:00
|
|
|
func (p *Plug) TaskExecutionFunction(function TaskExecution) {
|
|
|
|
p.taskExecutionFunction = function
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plug) SendMessageExecution(function SendMessageExecution) {
|
|
|
|
p.sendMessageExecution = function
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plug) GetMessageExecution(function GetMessageExecution) {
|
|
|
|
p.getMessageExecution = function
|
|
|
|
}
|