Skip to content

Commit

Permalink
get/set metadata wired up
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkplug0025 committed Sep 18, 2024
1 parent 61deb12 commit 1f80b25
Show file tree
Hide file tree
Showing 16 changed files with 390 additions and 82 deletions.
36 changes: 26 additions & 10 deletions services/user/Workshop/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions services/user/Workshop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
resolver = "2"
members = ["service", "query", "plugin"]

[package]
name = "workshop_package"
version = "0.1.0"
edition = "2021"

[workspace.package]
version = "0.1.0"
rust-version = "1.64"
Expand All @@ -15,6 +10,9 @@ publish = false
repository = "https://github.com/gofractally/psibase"
homepage = "https://psibase.io"

[package]
name = "workshop_package"

[package.metadata.psibase]
package-name = "workshop"
description = "Workshop for psibase apps"
Expand All @@ -27,6 +25,10 @@ debug = false
strip = true
lto = true

[lib]
crate-type = ["rlib"]

[dependencies]
workshop = { path = "service" }
r-workshop = { path = "query" }
workshop-plugin = { path = "plugin" }
17 changes: 13 additions & 4 deletions services/user/Workshop/plugin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "plugin"
name = "workshop-plugin"
version.workspace = true
rust-version.workspace = true
repository.workspace = true
Expand All @@ -10,16 +10,25 @@ publish = false
[dependencies]
wit-bindgen-rt = { version = "0.30.0", features = ["bitflags"] }
psibase = { path="../../../../rust/psibase" }
fracpack = { path = "../../../../rust/fracpack" }
workshop = { path="../service" }
serde = "1.0"
serde_json = "1.0"

[lib]
crate-type = ["cdylib"]

[package.metadata.component]
package = "workshop:plugin"
package = "workshop:psibase-plugin"

[package.metadata.component.target]
world = "impl"
[package.metadata.component.dependencies]

[package.metadata.component.target.dependencies]
"host:common" = { path = "../../CommonApi/common/packages/wit/host-common.wit" }
"transact:plugin" = { path = "../../../system/Transact/plugin/wit/world.wit" }

[package.metadata.component.target]
world = "impl"

[dev-dependencies]
workshop_package = { path = ".." }
25 changes: 25 additions & 0 deletions services/user/Workshop/plugin/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::bindings::host::common::types::{Error, PluginId};

#[derive(PartialEq, Eq, Hash)]
pub enum ErrorType {
QueryError,
}

fn my_plugin_id() -> PluginId {
return PluginId {
service: "workshop".to_string(),
plugin: "plugin".to_string(),
};
}

impl ErrorType {
pub fn err(self, msg: &str) -> Error {
match self {
ErrorType::QueryError => Error {
code: self as u32,
producer: my_plugin_id(),
message: format!("Failed to post graphql query: {}", msg),
},
}
}
}
101 changes: 93 additions & 8 deletions services/user/Workshop/plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,66 @@
#[allow(warnings)]
mod bindings;

use bindings::exports::workshop::plugin::consumer::Guest as Consumer;
use bindings::exports::workshop::plugin::consumer::{
AppMetadata as ConsumerAppMetadata, Guest as Consumer,
};
use bindings::exports::workshop::plugin::developer::Guest as Developer;
use bindings::host::common::{client as Client, server as Server, types as CommonTypes};
use bindings::workshop::plugin::types::{AccountId};
use bindings::transact::plugin::intf as Transact;
use bindings::workshop::plugin::types::AccountId;
use psibase::fracpack::Pack;
use psibase::AccountNumber;
use workshop as WorkshopService;
use workshop::service::AppMetadata;

use serde::{Deserialize, Serialize};

mod errors;
use errors::ErrorType::*;

struct WorkshopPlugin;

trait TryParseGqlResponse: Sized {
fn from_gql(s: String) -> Result<Self, CommonTypes::Error>;
}

#[derive(Deserialize, Debug)]
struct ResponseRoot<T> {
data: T,
}

#[allow(non_snake_case)]
#[derive(Deserialize, Debug)]
struct AppMetadataResponseData {
name: String,
shortDescription: String,
longDescription: String,
icon: Option<String>,
tosSubpage: String,
privacyPolicySubpage: String,
appHomepageSubpage: String,
status: String,
tags: Vec<String>,
}

#[allow(non_snake_case)]
#[derive(Deserialize, Debug)]
struct AppMetadataResponse {
appMetadata: Option<AppMetadataResponseData>,
}
impl TryParseGqlResponse for AppMetadataResponseData {
fn from_gql(response: String) -> Result<Self, CommonTypes::Error> {
println!("response: {:?}", response);
let response_root: ResponseRoot<AppMetadataResponse> =
serde_json::from_str(&response).map_err(|e| QueryError.err(&e.to_string()))?;
println!("response_root: {:?}", response_root);
let appMetadata = response_root.data.appMetadata.unwrap();
// TODO: handle 404 gracefully as not found error
println!("app_metadata: {:?}", appMetadata);
Ok(appMetadata)
}
}

impl Developer for WorkshopPlugin {
fn set_app_metadata(
name: String,
Expand All @@ -23,10 +73,10 @@ impl Developer for WorkshopPlugin {
status: String,
tags: Vec<String>,
) -> Result<(), CommonTypes::Error> {
// convert js blob bytes to base64
Server::add_action_to_transaction(
"setAppMetadata",
&WorkshopService::action_structs::setAppMetadata {
// todo: convert js blob bytes to base64
Transact::add_action_to_transaction(
"setMetadata",
&WorkshopService::action_structs::setMetadata {
name: name.to_owned(),
short_description: short_description.to_owned(),
long_description: long_description.to_owned(),
Expand All @@ -44,9 +94,44 @@ impl Developer for WorkshopPlugin {
}

impl Consumer for WorkshopPlugin {
fn get_app_metadata(account: AccountId) -> Result<(), CommonTypes::Error> {
fn get_app_metadata(account: AccountId) -> Result<ConsumerAppMetadata, CommonTypes::Error> {
let query = format!(
r#"query {{
appMetadata(accountId: "{account}") {{
name,
shortDescription,
longDescription,
icon,
tosSubpage,
privacyPolicySubpage,
appHomepageSubpage,
status,
tags
}}
}}"#,
account = account
);
println!("query: {}", query);
// todo: handle 404 gracefully
let metadata = AppMetadataResponseData::from_gql(Server::post_graphql_get_json(&query)?)?;

println!("metadata: {:?}", metadata);

// todo: fix fields
let x = ConsumerAppMetadata {
name: metadata.name,
short_description: metadata.shortDescription,
long_description: metadata.longDescription,
icon: metadata.icon.unwrap_or_default(),
tos_subpage: metadata.tosSubpage,
privacy_policy_subpage: metadata.privacyPolicySubpage,
app_homepage_subpage: metadata.appHomepageSubpage,
status: metadata.status,
tags: vec![],
};

// convert timestamps from unix to iso strings
Ok(())
Ok(x)
}
}

Expand Down
1 change: 1 addition & 0 deletions services/user/Workshop/plugin/wit/impl.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package workshop:plugin;

world impl {
include host:common/imports;
include transact:plugin/imports;

export developer;
export consumer;
Expand Down
16 changes: 15 additions & 1 deletion services/user/Workshop/plugin/wit/world.wit
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,21 @@ interface developer {
interface consumer {
use host:common/types.{error};
use types.{account-id};
get-app-metadata: func(account: account-id) -> result<_, error>;


record app-metadata {
name: string,
short-description: string,
long-description: string,
icon: string,
tos-subpage: string,
privacy-policy-subpage: string,
app-homepage-subpage: string,
status: string,
tags: list<string>
}

get-app-metadata: func(account: account-id) -> result<app-metadata, error>;
}

world imports {
Expand Down
1 change: 1 addition & 0 deletions services/user/Workshop/query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ publish = false

[package.metadata.psibase]
plugin = "workshop-plugin"
data = [{src = "../ui/dist", dst = "/"}]

[dependencies]
psibase = { path = "../../../../rust/psibase" }
Expand Down
2 changes: 1 addition & 1 deletion services/user/Workshop/query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod service {
&self,
account_id: AccountNumber,
) -> Option<workshop::service::AppMetadata> {
let x = workshop::Wrapper::call().getAppMetadata(account_id);
let x = workshop::Wrapper::call().getMetadata(account_id);
x
}
}
Expand Down
Loading

0 comments on commit 1f80b25

Please sign in to comment.