🏡 index : quine-global/quintodrome.git

// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the ScrobbleRetriever host service.
// It is intended for use in Navidrome plugins built with TinyGo.
//
//go:build wasip1

package host

import (
	"encoding/json"
	"errors"

	"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
)

// ScrobbleCountOptions represents the ScrobbleCountOptions data structure.
// ScrobbleCountOptions carries optional parameters for counting user scrobbles
type ScrobbleCountOptions struct {
	FromTimestamp *int64 `json:"fromTimestamp"`
	ToTimestamp   *int64 `json:"toTimestamp"`
}

// ScrobbleOptions represents the ScrobbleOptions data structure.
// ScrobbleOptions carries optional parameters for retrieving user scrobbles
type ScrobbleOptions struct {
	FromTimestamp *int64 `json:"fromTimestamp"`
	ToTimestamp   *int64 `json:"toTimestamp"`
	Descending    bool   `json:"descending"`
	MaxItems      int    `json:"maxItems"`
	Offset        int    `json:"offset"`
}

// ScrobbleRef represents the ScrobbleRef data structure.
// ScrobbleRef represents one instance of a scrobble (instance id, file id, submission time)
type ScrobbleRef struct {
	ID             int64  `json:"id"`
	MediaFileID    string `json:"mediaFileId"`
	SubmissionTime int64  `json:"submissionTime"`
}

// scrobbleretriever_getfirsttimestamp is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user scrobbleretriever_getfirsttimestamp
func scrobbleretriever_getfirsttimestamp(uint64) uint64

// scrobbleretriever_getlasttimestamp is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user scrobbleretriever_getlasttimestamp
func scrobbleretriever_getlasttimestamp(uint64) uint64

// scrobbleretriever_getscrobbles is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user scrobbleretriever_getscrobbles
func scrobbleretriever_getscrobbles(uint64) uint64

// scrobbleretriever_getscrobblecount is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user scrobbleretriever_getscrobblecount
func scrobbleretriever_getscrobblecount(uint64) uint64

type scrobbleRetrieverGetFirstTimestampRequest struct {
	Username string `json:"username"`
}

type scrobbleRetrieverGetFirstTimestampResponse struct {
	Result *int64 `json:"result,omitempty"`
	Error  string `json:"error,omitempty"`
}

type scrobbleRetrieverGetLastTimestampRequest struct {
	Username string `json:"username"`
}

type scrobbleRetrieverGetLastTimestampResponse struct {
	Result *int64 `json:"result,omitempty"`
	Error  string `json:"error,omitempty"`
}

type scrobbleRetrieverGetScrobblesRequest struct {
	Username string          `json:"username"`
	Options  ScrobbleOptions `json:"options"`
}

type scrobbleRetrieverGetScrobblesResponse struct {
	Scrobbles []ScrobbleRef    `json:"scrobbles,omitempty"`
	Next      *ScrobbleOptions `json:"next,omitempty"`
	Error     string           `json:"error,omitempty"`
}

type scrobbleRetrieverGetScrobbleCountRequest struct {
	Username string               `json:"username"`
	Options  ScrobbleCountOptions `json:"options"`
}

type scrobbleRetrieverGetScrobbleCountResponse struct {
	Result int64  `json:"result,omitempty"`
	Error  string `json:"error,omitempty"`
}

// ScrobbleRetrieverGetFirstTimestamp calls the scrobbleretriever_getfirsttimestamp host function.
// GetFirstTimestamp returns the unix timestamp of the oldest scrobble for the user.
// If the user has no scrobbles, returns nil
func ScrobbleRetrieverGetFirstTimestamp(username string) (*int64, error) {
	// Marshal request to JSON
	req := scrobbleRetrieverGetFirstTimestampRequest{
		Username: username,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := scrobbleretriever_getfirsttimestamp(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response scrobbleRetrieverGetFirstTimestampResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return nil, err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return nil, errors.New(response.Error)
	}

	return response.Result, nil
}

// ScrobbleRetrieverGetLastTimestamp calls the scrobbleretriever_getlasttimestamp host function.
// GetLastTimestamp returns the unix timestamp of the most recent scrobble for the user
// If the user has no scrobbles, return nil
func ScrobbleRetrieverGetLastTimestamp(username string) (*int64, error) {
	// Marshal request to JSON
	req := scrobbleRetrieverGetLastTimestampRequest{
		Username: username,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := scrobbleretriever_getlasttimestamp(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response scrobbleRetrieverGetLastTimestampResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return nil, err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return nil, errors.New(response.Error)
	}

	return response.Result, nil
}

// ScrobbleRetrieverGetScrobbles calls the scrobbleretriever_getscrobbles host function.
// GetScrobbles returns one page of scrobbles for a user.
//
// Parameters:
//   - username: the user to query for scrobbles
//   - options.FromTimestamp: If specified, the first UNIX timestamp to start fetching scrobbles (inclusive). Otherwise, start from the first scrobble
//   - options.ToTimestamp: If specified, the last UNIX timestamp to fetch (inclusive). Otherwise, end at the last scrobble
//   - options.Descending: If true, order from newest to oldest. Otherwise, oldest to newest
//   - options.MaxItems: The maximum number of items to retrieve. The maximum value (and default) if not specified is 5000
//   - options.Offset: Pagination state; only valid as received on the options returned by a previous call. Never set it manually
//
// Returns:
//   - scrobbles: The scrobbles in the requested range, ordered by submission time
//     (ties broken by scrobble ID) in the direction given by options.Descending
//   - next: The options for the following page, or nil once no scrobbles remain.
//     Pass it back to GetScrobbles unchanged and repeat until it is nil. It carries an
//     adjusted FromTimestamp/ToTimestamp, so keep a copy if you still need the original range
func ScrobbleRetrieverGetScrobbles(username string, options ScrobbleOptions) ([]ScrobbleRef, *ScrobbleOptions, error) {
	// Marshal request to JSON
	req := scrobbleRetrieverGetScrobblesRequest{
		Username: username,
		Options:  options,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return nil, nil, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := scrobbleretriever_getscrobbles(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response scrobbleRetrieverGetScrobblesResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return nil, nil, err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return nil, nil, errors.New(response.Error)
	}

	return response.Scrobbles, response.Next, nil
}

// ScrobbleRetrieverGetScrobbleCount calls the scrobbleretriever_getscrobblecount host function.
// GetScrobbleCount returns the number of scrobbles for a user in a given range
//
// Parameters:
//   - username: the user to query for scrobbles
//   - options.FromTimestamp: If specified, the first UNIX timestamp to start fetching scrobbles (inclusive). Otherwise, start from the first scrobble
//   - options.ToTimestamp: If specified, the last UNIX timestamp to fetch (inclusive). Otherwise, end at the last scrobble
//
// Returns:
//   - the number of scrobbles in the given range, or 0
func ScrobbleRetrieverGetScrobbleCount(username string, options ScrobbleCountOptions) (int64, error) {
	// Marshal request to JSON
	req := scrobbleRetrieverGetScrobbleCountRequest{
		Username: username,
		Options:  options,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return 0, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := scrobbleretriever_getscrobblecount(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response scrobbleRetrieverGetScrobbleCountResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return 0, err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return 0, errors.New(response.Error)
	}

	return response.Result, nil
}