diff --git a/server/main.go b/server/main.go index 3edf29a..fccffe6 100644 --- a/server/main.go +++ b/server/main.go @@ -2,8 +2,6 @@ package main import ( "log" - "net/http" - "net/url" "os" _ "github.com/joho/godotenv/autoload" @@ -14,6 +12,7 @@ import ( // uncomment once you have at least one .go migration file in the "migrations" directory _ "github.com/Dan6erbond/share-me/migrations" "github.com/Dan6erbond/share-me/pkg/meilisearch" + "github.com/Dan6erbond/share-me/pkg/tags" ) func main() { @@ -54,48 +53,9 @@ func main() { return nil }) - app.OnRecordBeforeCreateRequest("files/posts", "create", "defaults").Add(func(e *core.RecordCreateEvent) error { - if e.Record.Collection().Name == "files" { - e.Record.Set("tags", "[]") - e.Record.Set("tagsSuggestions", "[]") - - return nil - } - - if e.Record.Collection().Name == "posts" { - e.Record.Set("tags", "[]") - - return nil - } - - return nil - }) - if os.Getenv("TAGGER_HOST") != "" { - app.OnRecordAfterCreateRequest("files", "create", "tag").Add(func(e *core.RecordCreateEvent) error { - if e.Record.Collection().Name != "files" { - return nil - } - - url, err := url.Parse(os.Getenv("TAGGER_HOST")) - - if err != nil { - log.Println(err) - return err - } - - url.Path = "files/" + e.Record.Id - - resp, err := http.Post(url.String(), "application/json", nil) - resp.Body.Close() - - if err != nil { - log.Println(err) - return err - } - - return err - }) + tags.RegisterCommands(app, os.Getenv("TAGGER_HOST")) + tags.RegisterHooks(app, os.Getenv("TAGGER_HOST")) } if err := app.Start(); err != nil { diff --git a/server/pkg/tags/commands.go b/server/pkg/tags/commands.go new file mode 100644 index 0000000..251756b --- /dev/null +++ b/server/pkg/tags/commands.go @@ -0,0 +1,56 @@ +package tags + +import ( + "log" + "net/http" + "net/url" + + "github.com/pocketbase/pocketbase" + "github.com/pocketbase/pocketbase/tools/types" + "github.com/spf13/cobra" +) + +func RegisterCommands(app *pocketbase.PocketBase, taggerHost string) { + tagsCmd := &cobra.Command{ + Use: "tags", + Run: func(command *cobra.Command, args []string) { + print("Runs commands to interact with the tagger") + }, + } + + tagsCmd.AddCommand(&cobra.Command{ + Use: "generate", + Run: func(command *cobra.Command, args []string) { + files, err := app.Dao().FindRecordsByExpr("files") + + if err != nil { + log.Println(err) + } + + for _, file := range files { + if file.Get("tagsSuggestions").(types.JsonRaw).String() != "" { + continue + } + + url, err := url.Parse(taggerHost) + + if err != nil { + log.Println(err) + } + + url.Path = "files/" + file.Id + + resp, err := http.Post(url.String(), "application/json", nil) + + if err != nil { + log.Println(err) + continue + } + + defer resp.Body.Close() + } + }, + }) + + app.RootCmd.AddCommand(tagsCmd) +} diff --git a/server/pkg/tags/hooks.go b/server/pkg/tags/hooks.go new file mode 100644 index 0000000..a507e9a --- /dev/null +++ b/server/pkg/tags/hooks.go @@ -0,0 +1,55 @@ +package tags + +import ( + "log" + "net/http" + "net/url" + + "github.com/pocketbase/pocketbase" + "github.com/pocketbase/pocketbase/core" +) + +func RegisterHooks(app *pocketbase.PocketBase, taggerHost string) { + app.OnRecordBeforeCreateRequest("files/posts", "create", "defaults").Add(func(e *core.RecordCreateEvent) error { + if e.Record.Collection().Name == "files" { + e.Record.Set("tags", "[]") + e.Record.Set("tagsSuggestions", "[]") + + return nil + } + + if e.Record.Collection().Name == "posts" { + e.Record.Set("tags", "[]") + + return nil + } + + return nil + }) + + app.OnRecordAfterCreateRequest("files", "create", "tag").Add(func(e *core.RecordCreateEvent) error { + if e.Record.Collection().Name != "files" { + return nil + } + + url, err := url.Parse(taggerHost) + + if err != nil { + log.Println(err) + return err + } + + url.Path = "files/" + e.Record.Id + + resp, err := http.Post(url.String(), "application/json", nil) + + if err != nil { + log.Println(err) + return err + } + + defer resp.Body.Close() + + return nil + }) +}