Skip to content

Commit

Permalink
feat(server): ✨ implement tags generate command to generate tags fo…
Browse files Browse the repository at this point in the history
…r old files
  • Loading branch information
Dan6erbond committed May 14, 2023
1 parent 8dee322 commit f187a42
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 43 deletions.
46 changes: 3 additions & 43 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package main

import (
"log"
"net/http"
"net/url"
"os"

_ "github.com/joho/godotenv/autoload"
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
56 changes: 56 additions & 0 deletions server/pkg/tags/commands.go
Original file line number Diff line number Diff line change
@@ -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)
}
55 changes: 55 additions & 0 deletions server/pkg/tags/hooks.go
Original file line number Diff line number Diff line change
@@ -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
})
}

0 comments on commit f187a42

Please sign in to comment.