* feat: add plugin system * fix db docker build * fix hammerhead readme, add strava subscription news to docs * fixes and sdk improvements * fix: reduce Meilisearch load, debounce federation sync (#1012) * optimize meili trail index * several fixes --------- Co-authored-by: Flomp <Flomp@users.noreply.github.com> * Bump svelte from 5.55.5 to 5.56.0 in /docs (#1032) Bumps [svelte](https://github.com/sveltejs/svelte/tree/HEAD/packages/svelte) from 5.55.5 to 5.56.0. - [Release notes](https://github.com/sveltejs/svelte/releases) - [Changelog](https://github.com/sveltejs/svelte/blob/main/packages/svelte/CHANGELOG.md) - [Commits](https://github.com/sveltejs/svelte/commits/svelte@5.56.0/packages/svelte) --- updated-dependencies: - dependency-name: svelte dependency-version: 5.56.0 dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Flomp <Flomp@users.noreply.github.com> * Release v0.19.2 (#1035) * chore: release v0.19.2 * add changelog --------- Co-authored-by: Flomp <26000991+Flomp@users.noreply.github.com> Co-authored-by: Christian Beutel <> * speed up plugin sync and several small fixes * concepts for security improvements and process stability * improve concept * security concept implemented * remove insecure TLS * worker concept implemented * fixes and cleanup * fixes * docu * mermaid, namings * WASM plugin host improvements, plugin logging * fix db migration * Improve plugin config and category mapping UI * fixes * further fixes * remove manual test sync * fix db migration and strava mapping * type added, UI improvements * fix plugin card toggle clickable area * optimize synch status card layout * plugin type 'trails' instead of 'integration' * session auth validation in UI * fix komoot date and waypoints * improve category mapping * fix send to hammerhead: trail name * plugin setup error handling improved * fix review findings * re-mapping added * rename remote_category --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Flomp <Flomp@users.noreply.github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Flomp <26000991+Flomp@users.noreply.github.com>
164 lines
4.2 KiB
Go
164 lines
4.2 KiB
Go
//go:build tinygo
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/extism/go-pdk"
|
|
"github.com/open-wanderer/wanderer/plugins/sdk"
|
|
)
|
|
|
|
type hammerheadClient struct {
|
|
userID string
|
|
token string
|
|
}
|
|
|
|
func login(email string, password string) (string, error) {
|
|
spec := sdk.HostRequestSpec{
|
|
Method: "POST",
|
|
Target: sdk.RequestTarget{
|
|
Type: "connector",
|
|
Connector: "api",
|
|
Path: "/v1/auth/token",
|
|
},
|
|
Headers: map[string]string{
|
|
"Accept": "application/json",
|
|
},
|
|
Body: &sdk.HostRequestBody{
|
|
Type: sdk.HostRequestBodyTypeJSON,
|
|
JSON: map[string]string{
|
|
"grant_type": "password",
|
|
"username": email,
|
|
"password": password,
|
|
},
|
|
},
|
|
Expect: sdk.ResponseExpect{
|
|
ContentTypes: []string{"application/json"},
|
|
MaxBytes: 1048576,
|
|
},
|
|
}
|
|
response, body, err := sdk.HostRequest(spec)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if response.Status != 200 {
|
|
return "", fmt.Errorf("hammerhead login failed (%d): %s", response.Status, string(body))
|
|
}
|
|
|
|
var parsed loginResponse
|
|
if err := json.Unmarshal(body, &parsed); err != nil {
|
|
return "", err
|
|
}
|
|
if parsed.Token == "" {
|
|
return "", fmt.Errorf("hammerhead login returned no access token")
|
|
}
|
|
|
|
pdk.SetVar("hammerhead_access_token", []byte(parsed.Token))
|
|
return parsed.Token, nil
|
|
}
|
|
|
|
func loginClient(auth map[string]any) (hammerheadClient, error) {
|
|
email := sdk.StringField(auth, "email")
|
|
password := sdk.StringField(auth, "password")
|
|
if email == "" || password == "" {
|
|
return hammerheadClient{}, fmt.Errorf("email and password are required")
|
|
}
|
|
token, err := login(email, password)
|
|
if err != nil {
|
|
return hammerheadClient{}, err
|
|
}
|
|
userID, err := userIDFromJWT(token)
|
|
if err != nil {
|
|
return hammerheadClient{}, err
|
|
}
|
|
return hammerheadClient{userID: userID, token: token}, nil
|
|
}
|
|
|
|
func (c hammerheadClient) get(path string, query []sdk.QueryParam, out any) error {
|
|
response, body, err := sdk.HostRequest(sdk.HostRequestSpec{
|
|
Method: "GET",
|
|
Target: sdk.RequestTarget{
|
|
Type: "connector",
|
|
Connector: "api",
|
|
Path: "/v1/users/" + c.userID + path,
|
|
Query: query,
|
|
},
|
|
Headers: map[string]string{
|
|
sdk.AuthHeaderAuthorization: sdk.AuthSchemeBearer + " " + c.token,
|
|
"Accept": "application/json",
|
|
},
|
|
Expect: sdk.ResponseExpect{
|
|
ContentTypes: []string{"application/json"},
|
|
MaxBytes: 1048576,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if response.Status != 200 {
|
|
return fmt.Errorf("hammerhead request failed (%d): %s", response.Status, string(body))
|
|
}
|
|
return json.Unmarshal(body, out)
|
|
}
|
|
|
|
func (c hammerheadClient) activities(page int, perPage int) ([]activityResponse, int, error) {
|
|
var data activitiesResponse
|
|
err := c.get("/activities", hammerheadListQuery(page, perPage), &data)
|
|
return data.Data, data.TotalPages, err
|
|
}
|
|
|
|
func (c hammerheadClient) tours(page int, perPage int) ([]tourResponse, int, error) {
|
|
var data toursResponse
|
|
err := c.get("/routes", hammerheadListQuery(page, perPage), &data)
|
|
return data.Data, data.TotalPages, err
|
|
}
|
|
|
|
func (c hammerheadClient) activity(id string) (*activity, error) {
|
|
var data activity
|
|
err := c.get("/activities/"+id+"/details", nil, &data)
|
|
return &data, err
|
|
}
|
|
|
|
func (c hammerheadClient) tour(id string) (*tour, error) {
|
|
var data tour
|
|
err := c.get("/routes/"+id, nil, &data)
|
|
return &data, err
|
|
}
|
|
|
|
func hammerheadListQuery(page int, perPage int) []sdk.QueryParam {
|
|
return []sdk.QueryParam{
|
|
{Name: "page", Value: strconv.Itoa(page)},
|
|
{Name: "perPage", Value: strconv.Itoa(perPage)},
|
|
{Name: "orderBy", Value: "NEWEST"},
|
|
{Name: "ascending", Value: "true"},
|
|
}
|
|
}
|
|
|
|
func userIDForUpload(auth map[string]any) (string, error) {
|
|
token := string(pdk.GetVar("hammerhead_access_token"))
|
|
if token == "" {
|
|
email := sdk.StringField(auth, "email")
|
|
password := sdk.StringField(auth, "password")
|
|
if email == "" || password == "" {
|
|
return "", fmt.Errorf("email and password are required")
|
|
}
|
|
var err error
|
|
token, err = login(email, password)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
return userIDFromJWT(token)
|
|
}
|
|
|
|
func userIDFromSession() (string, error) {
|
|
token := string(pdk.GetVar("hammerhead_access_token"))
|
|
if token == "" {
|
|
return "", fmt.Errorf("session token is not available")
|
|
}
|
|
return userIDFromJWT(token)
|
|
}
|