-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
135 lines (110 loc) · 3.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"log"
"github.com/fourjuaneight/rivendell/helpers"
"github.com/fourjuaneight/rivendell/utils"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
)
func archive(name string, url string, typeName string) (string, error) {
media, getcontentErr := helpers.GetContent(name, url, typeName)
if getcontentErr != nil {
return "", fmt.Errorf("[archive][GetContent]: %w", getcontentErr)
}
typeOps := utils.GetFileType(typeName, url)
list := utils.ToCapitalized(typeName)
path := fmt.Sprintf("Bookmarks/%s/%s.%s", list, utils.FileNameFmt(name), typeOps.File)
archiveUrl, uploadtob2Err := helpers.UploadToB2(media, path, typeOps.MIME)
if uploadtob2Err != nil {
return "", fmt.Errorf("[archive][UploadToB2]: %w", uploadtob2Err)
}
return archiveUrl, nil
}
func main() {
app := pocketbase.New()
collections := []*models.Collection{
bookmarksCollection(),
feedsCollection(),
mediaCollection(),
musicCollection(),
mtgCollection(),
recordsCollection(),
githubCollection(),
stackExchangeCollection(),
metaCollection(),
}
// manually declare schemas
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
for _, collection := range collections {
existing, _ := app.Dao().FindCollectionByNameOrId(collection.Name)
if existing == nil {
if err := app.Dao().SaveCollection(collection); err != nil {
log.Fatal("[OnBeforeServe]: %w", err)
}
}
}
return nil
})
// setup migrations
migratecmd.MustRegister(app, app.RootCmd, &migratecmd.Options{
Automigrate: true,
})
// set default values
app.OnRecordBeforeCreateRequest().Add(func(e *core.RecordCreateEvent) error {
record := e.Record
collection := record.Collection().Name
if collection == "bookmarks" || collection == "feeds" {
record.Set("dead", false)
record.Set("shared", false)
}
return nil
})
// archive bookmarks
app.OnRecordAfterCreateRequest().Add(func(e *core.RecordCreateEvent) error {
record := e.Record
collection := record.Collection().Name
switch collection {
case "bookmarks":
// archive bookmarks
name := record.SchemaData()["title"].(string)
url := record.SchemaData()["url"].(string)
typeName := record.SchemaData()["type"].(string)
archive, archiveErr := archive(name, url, typeName)
if archiveErr != nil {
return fmt.Errorf("[OnRecordAfterCreateRequest][archiveErr]: %w", archiveErr)
}
record.Set("archive", archive)
case "github":
// query repository info
url := record.SchemaData()["url"].(string)
repo, repoErr := helpers.GetRepoInfo(url)
if repoErr != nil {
return fmt.Errorf("[OnRecordAfterCreateRequest][repoErr]: %w", repoErr)
}
record.Set("name", repo.Name)
record.Set("owner", repo.Owner)
record.Set("description", repo.Description)
record.Set("language", repo.Language)
case "stack_exchange":
// query repository info
question := record.SchemaData()["question"].(string)
questionInfo, questionInfoErr := helpers.GetQuestionInfo(question)
if questionInfoErr != nil {
return fmt.Errorf("[OnRecordAfterCreateRequest][questionInfoErr]: %w", questionInfoErr)
}
record.Set("title", questionInfo.Title)
record.Set("question", questionInfo.Question)
record.Set("answers", questionInfo.Answer)
record.Set("tags", questionInfo.Tags)
default:
}
app.Dao().SaveRecord(record)
return nil
})
if err := app.Start(); err != nil {
log.Fatal("[Start]: %w", err)
}
}