cache public meilisearch key
This commit is contained in:
162
db/main.go
162
db/main.go
@@ -1308,73 +1308,143 @@ func bootstrapCategories(app core.App) error {
|
||||
}
|
||||
|
||||
func bootstrapMeilisearchDocuments(app core.App, client meilisearch.ServiceManager) error {
|
||||
query := app.RecordQuery("trails")
|
||||
// --- Trails ---
|
||||
const pageSize = 100
|
||||
var page int64 = 0
|
||||
|
||||
// Clear index before re-indexing
|
||||
if _, err := client.Index("trails").DeleteAllDocuments(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for {
|
||||
trails := []*core.Record{}
|
||||
|
||||
if err := query.All(&trails); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := client.Index("trails").DeleteAllDocuments()
|
||||
err := app.RecordQuery("trails").
|
||||
Limit(pageSize).
|
||||
Offset(page * int64(pageSize)).
|
||||
All(&trails)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(trails) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// Collect author IDs to fetch in batch
|
||||
authorIDs := make([]string, 0, len(trails))
|
||||
for _, t := range trails {
|
||||
if id := t.GetString("author"); id != "" {
|
||||
authorIDs = append(authorIDs, id)
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch all authors in one go
|
||||
authorArgs := make([]interface{}, len(authorIDs))
|
||||
for i, v := range authorIDs {
|
||||
authorArgs[i] = v
|
||||
}
|
||||
|
||||
authors, err := app.FindAllRecords("activitypub_actors",
|
||||
dbx.In("id", authorArgs...),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
authorMap := make(map[string]*core.Record, len(authors))
|
||||
for _, a := range authors {
|
||||
authorMap[a.Id] = a
|
||||
}
|
||||
|
||||
for _, trail := range trails {
|
||||
author, err := app.FindRecordById("activitypub_actors", trail.GetString(("author")))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
author := authorMap[trail.GetString("author")]
|
||||
|
||||
if err := util.IndexTrail(app, trail, author, client); err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to index trail '%s': %v", trail.GetString("name"), err))
|
||||
continue
|
||||
}
|
||||
|
||||
// Shares
|
||||
shares, err := app.FindAllRecords("trail_share",
|
||||
dbx.NewExp("trail = {:trailId}", dbx.Params{"trailId": trail.Id}),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
actorIds := make([]string, len(shares))
|
||||
for i, r := range shares {
|
||||
actorIds[i] = r.GetString("actor")
|
||||
}
|
||||
err = util.UpdateTrailShares(trail.Id, actorIds, client)
|
||||
if err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to update trail shares '%s': %v", trail.GetString("name"), err))
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to fetch shares for trail '%s': %v", trail.GetString("name"), err))
|
||||
continue
|
||||
}
|
||||
shareActors := make([]string, len(shares))
|
||||
for i, r := range shares {
|
||||
shareActors[i] = r.GetString("actor")
|
||||
}
|
||||
if err := util.UpdateTrailShares(trail.Id, shareActors, client); err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to update trail shares '%s': %v", trail.GetString("name"), err))
|
||||
}
|
||||
|
||||
// Likes
|
||||
likes, err := app.FindAllRecords("trail_like",
|
||||
dbx.NewExp("trail = {:trailId}", dbx.Params{"trailId": trail.Id}),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
actorIds = make([]string, len(likes))
|
||||
for i, r := range likes {
|
||||
actorIds[i] = r.GetString("actor")
|
||||
}
|
||||
err = util.UpdateTrailLikes(trail.Id, actorIds, client)
|
||||
if err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to update trail likes '%s': %v", trail.GetString("name"), err))
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to fetch likes for trail '%s': %v", trail.GetString("name"), err))
|
||||
continue
|
||||
}
|
||||
likeActors := make([]string, len(likes))
|
||||
for i, r := range likes {
|
||||
likeActors[i] = r.GetString("actor")
|
||||
}
|
||||
if err := util.UpdateTrailLikes(trail.Id, likeActors, client); err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to update trail likes '%s': %v", trail.GetString("name"), err))
|
||||
}
|
||||
}
|
||||
|
||||
lists, err := app.FindAllRecords("lists")
|
||||
page++
|
||||
}
|
||||
|
||||
// --- Lists ---
|
||||
if _, err := client.Index("lists").DeleteAllDocuments(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
page = 0
|
||||
for {
|
||||
lists := []*core.Record{}
|
||||
err := app.RecordQuery("lists").
|
||||
Limit(pageSize).
|
||||
Offset(page * pageSize).
|
||||
All(&lists)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = client.Index("lists").DeleteAllDocuments()
|
||||
if len(lists) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// Collect author IDs in batch
|
||||
authorIDs := make([]string, 0, len(lists))
|
||||
for _, l := range lists {
|
||||
if id := l.GetString("author"); id != "" {
|
||||
authorIDs = append(authorIDs, id)
|
||||
}
|
||||
}
|
||||
// Fetch all authors in one go
|
||||
authorArgs := make([]interface{}, len(authorIDs))
|
||||
for i, v := range authorIDs {
|
||||
authorArgs[i] = v
|
||||
}
|
||||
|
||||
authors, err := app.FindAllRecords("activitypub_actors",
|
||||
dbx.In("id", authorArgs...),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
authorMap := make(map[string]*core.Record, len(authors))
|
||||
for _, a := range authors {
|
||||
authorMap[a.Id] = a
|
||||
}
|
||||
|
||||
for _, list := range lists {
|
||||
author, err := app.FindRecordById("activitypub_actors", list.GetString(("author")))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
author := authorMap[list.GetString("author")]
|
||||
|
||||
if err := util.IndexList(app, list, author, client); err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to index list '%s': %v", list.GetString("name"), err))
|
||||
continue
|
||||
@@ -1384,18 +1454,20 @@ func bootstrapMeilisearchDocuments(app core.App, client meilisearch.ServiceManag
|
||||
dbx.NewExp("list = {:listId}", dbx.Params{"listId": list.Id}),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
actorIds := make([]string, len(shares))
|
||||
for i, r := range shares {
|
||||
actorIds[i] = r.GetString("actor")
|
||||
}
|
||||
err = util.UpdateListShares(list.Id, actorIds, client)
|
||||
|
||||
if err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to update list shares '%s': %v", list.GetString("name"), err))
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to fetch list shares '%s': %v", list.GetString("name"), err))
|
||||
continue
|
||||
}
|
||||
shareActors := make([]string, len(shares))
|
||||
for i, r := range shares {
|
||||
shareActors[i] = r.GetString("actor")
|
||||
}
|
||||
if err := util.UpdateListShares(list.Id, shareActors, client); err != nil {
|
||||
app.Logger().Warn(fmt.Sprintf("Unable to update list shares '%s': %v", list.GetString("name"), err))
|
||||
}
|
||||
}
|
||||
|
||||
page++
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ function isFormContentType(request: Request) {
|
||||
);
|
||||
}
|
||||
|
||||
let publicMeilisearchKey: string | undefined = undefined;
|
||||
|
||||
const auth: Handle = async ({ event, resolve }) => {
|
||||
const pb = new PocketBase(envPub.PUBLIC_POCKETBASE_URL)
|
||||
@@ -84,8 +85,12 @@ const auth: Handle = async ({ event, resolve }) => {
|
||||
settings = await pb.collection('settings').getFirstListItem<Settings>(`user="${pb.authStore.record.id}"`, { requestKey: null })
|
||||
actor = await pb.collection("activitypub_actors").getFirstListItem(`user='${pb.authStore.record.id}'`)
|
||||
} else {
|
||||
if (!publicMeilisearchKey) {
|
||||
const response = await pb.send("/public/search/token", { method: "GET", fetch: event.fetch });
|
||||
meiliApiKey = response.token;
|
||||
publicMeilisearchKey = response.token;
|
||||
}
|
||||
|
||||
meiliApiKey = publicMeilisearchKey!;
|
||||
}
|
||||
const ms = new MeiliSearch({ host: env.MEILI_URL, apiKey: meiliApiKey });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user