110 lines
2.2 KiB
Go
110 lines
2.2 KiB
Go
package plugins
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/dgraph-io/dgo/v2"
|
|
"github.com/dgraph-io/dgo/v2/protos/api"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/viper"
|
|
"gitlab.dcso.lolcat/LABS/styx/models"
|
|
)
|
|
|
|
// PastebinPlugin defines the general PastebinPlugin structure.
|
|
type PastebinPlugin struct {
|
|
StopChan chan bool
|
|
StoppedChan chan bool
|
|
Running bool
|
|
}
|
|
|
|
// Initialize initialises the certstream configuration.
|
|
func (p *PastebinPlugin) Initialize() bool {
|
|
if !viper.GetBool("pastebin.activated") {
|
|
return false
|
|
}
|
|
logrus.Info("pastebin plugin is activated")
|
|
return true
|
|
}
|
|
|
|
// Run runs the Pastebin plugin.
|
|
func (p *PastebinPlugin) Run(wg *sync.WaitGroup, dgraphClient *dgo.Dgraph) {
|
|
if !p.Running {
|
|
p.StoppedChan = make(chan bool)
|
|
wg.Add(1)
|
|
go p.doRun(dgraphClient)
|
|
p.Running = true
|
|
}
|
|
}
|
|
|
|
// Stop stops the Pastebin plugin.
|
|
func (p *PastebinPlugin) Stop(wg *sync.WaitGroup) {
|
|
if p.Running {
|
|
p.StoppedChan = make(chan bool)
|
|
close(p.StopChan)
|
|
<-p.StopChan
|
|
wg.Done()
|
|
p.Running = false
|
|
}
|
|
}
|
|
|
|
func (p *PastebinPlugin) doRun(graphClient *dgo.Dgraph) {
|
|
for {
|
|
select {
|
|
default:
|
|
pastes, err := models.QueryPastes()
|
|
if err != nil {
|
|
logrus.Error(err)
|
|
}
|
|
for _, p := range pastes {
|
|
paste, err := models.FetchPaste(p)
|
|
if err != nil {
|
|
logrus.Error("cannot fetch paste", err)
|
|
}
|
|
fp := models.FullPaste{
|
|
Meta: p,
|
|
Full: paste,
|
|
NodeType: "fullPaste",
|
|
}
|
|
pasteNode := models.BuildPasteNode(&fp)
|
|
mainNode := models.BuildNode("node", "pastebin", pasteNode.ID)
|
|
// if elastic {
|
|
// e.StorePaste(fp)
|
|
// }
|
|
// models.SavePaste("paste_formatted.json", res)
|
|
|
|
e := models.Node{
|
|
ID: mainNode.ID,
|
|
NodeType: mainNode.NodeType,
|
|
NData: mainNode.NData,
|
|
Created: mainNode.Created,
|
|
Modified: mainNode.Modified,
|
|
PasteNode: *pasteNode,
|
|
}
|
|
|
|
ctx := context.Background()
|
|
mu := &api.Mutation{
|
|
CommitNow: true,
|
|
}
|
|
|
|
pb, err := json.Marshal(e)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
mu.SetJson = pb
|
|
|
|
_, err = graphClient.NewTxn().Mutate(ctx, mu)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
time.Sleep(3 * time.Second)
|
|
}
|
|
}
|
|
}
|