Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New api support #177

Merged
merged 9 commits into from
Jul 31, 2023
59 changes: 57 additions & 2 deletions core/main/src/firebolt/handlers/discovery_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use jsonrpsee::{
use ripple_sdk::api::{
device::entertainment_data::*,
firebolt::{
fb_discovery::{EVENT_ON_SIGN_IN, EVENT_ON_SIGN_OUT},
fb_general::{ListenRequest, ListenerResponse},
provider::ExternalProviderResponse,
},
Expand Down Expand Up @@ -90,6 +91,18 @@ pub trait Discovery {
async fn sign_in(&self, ctx: CallContext, sign_in_info: SignInInfo) -> RpcResult<bool>;
#[method(name = "discovery.signOut")]
async fn sign_out(&self, ctx: CallContext) -> RpcResult<bool>;
#[method(name = "discovery.onSignIn")]
async fn on_sign_in(
&self,
ctx: CallContext,
request: ListenRequest,
) -> RpcResult<ListenerResponse>;
#[method(name = "discovery.onSignOut")]
async fn on_sign_out(
&self,
ctx: CallContext,
request: ListenRequest,
) -> RpcResult<ListenerResponse>;
#[method(name = "discovery.watched")]
async fn watched(&self, ctx: CallContext, watched_info: WatchedInfo) -> RpcResult<bool>;
#[method(name = "discovery.watchNext")]
Expand Down Expand Up @@ -277,15 +290,30 @@ impl DiscoveryImpl {
ctx: CallContext,
is_signed_in: bool,
) -> RpcResult<bool> {
Ok(self
let app_id = ctx.app_id.to_owned();
let res = self
.state
.get_client()
.send_extn_request(match is_signed_in {
true => AccountLinkRequest::SignIn(ctx),
false => AccountLinkRequest::SignOut(ctx),
})
.await
.is_ok())
.is_ok();
if res {
AppEvents::emit(
&self.state,
if is_signed_in {
EVENT_ON_SIGN_IN
} else {
EVENT_ON_SIGN_OUT
},
&serde_json::json!({"appId": app_id,}),
)
.await;
return Ok(true);
}
return Ok(false);
}

async fn content_access(
Expand Down Expand Up @@ -404,6 +432,33 @@ impl DiscoveryServer for DiscoveryImpl {
self.process_sign_in_request(ctx.clone(), false).await
}

async fn on_sign_in(
&self,
ctx: CallContext,
request: ListenRequest,
) -> RpcResult<ListenerResponse> {
let listen = request.listen;
AppEvents::add_listener(&&self.state, EVENT_ON_SIGN_IN.to_string(), ctx, request);

Ok(ListenerResponse {
listening: listen,
event: EVENT_ON_SIGN_IN.to_owned(),
})
}
async fn on_sign_out(
&self,
ctx: CallContext,
request: ListenRequest,
) -> RpcResult<ListenerResponse> {
let listen = request.listen;
AppEvents::add_listener(&&self.state, EVENT_ON_SIGN_OUT.to_string(), ctx, request);

Ok(ListenerResponse {
listening: listen,
event: EVENT_ON_SIGN_OUT.to_owned(),
})
}

async fn watched(&self, ctx: CallContext, watched_info: WatchedInfo) -> RpcResult<bool> {
info!("Discovery.watched");
match self
Expand Down
56 changes: 56 additions & 0 deletions core/main/src/firebolt/handlers/localization_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ use crate::{
firebolt::rpc::RippleRPCProvider, processor::storage::storage_manager::StorageManager,
service::apps::provider_broker::ProviderBroker, state::platform_state::PlatformState,
};
use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct SetMapEntryProperty {
pub key: String,
pub value: String,
}
#[derive(Deserialize, Debug)]
pub struct RemoveMapEntryProperty {
pub key: String,
}
#[rpc(server)]
pub trait Localization {
#[method(name = "localization.locality")]
Expand Down Expand Up @@ -121,6 +131,19 @@ pub trait Localization {
ctx: CallContext,
set_request: SetStringProperty,
) -> RpcResult<()>;
#[method(name = "localization.addAdditionalInfo")]
async fn add_additional_info(
&self,
ctx: CallContext,
set_map_entry_property: SetMapEntryProperty,
) -> RpcResult<()>;
#[method(name = "localization.removeAdditionalInfo")]
async fn remove_additional_info(
&self,
ctx: CallContext,
remove_map_entry_property: RemoveMapEntryProperty,
) -> RpcResult<()>;

#[method(name = "localization.onAdditionalInfoChanged")]
async fn on_additional_info_changed(
&self,
Expand Down Expand Up @@ -402,6 +425,39 @@ impl LocalizationServer for LocalizationImpl {
.await
}

// #[instrument(skip(self))]
async fn add_additional_info(
&self,
_ctx: CallContext,
set_map_entry_property: SetMapEntryProperty,
) -> RpcResult<()> {
/*
Per FIRE-189, AdditionalInfo is now individually updatable, so read the entire map out, and update
value in place, and then write entire map out

*/
StorageManager::set_value_in_map(
&self.platform_state,
StorageProperty::AdditionalInfo,
set_map_entry_property.key,
set_map_entry_property.value,
)
.await
}
// #[instrument(skip(self))]
async fn remove_additional_info(
&self,
_ctx: CallContext,
remove_map_entry_property: RemoveMapEntryProperty,
) -> RpcResult<()> {
StorageManager::remove_value_in_map(
&self.platform_state,
StorageProperty::AdditionalInfo,
remove_map_entry_property.key,
)
.await
}

async fn on_additional_info_changed(
&self,
ctx: CallContext,
Expand Down
82 changes: 82 additions & 0 deletions core/main/src/processor/storage/storage_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use ripple_sdk::{
tokio,
utils::error::RippleError,
};
use std::collections::HashMap;

use crate::{
processor::storage::storage_manager_utils::{
Expand Down Expand Up @@ -112,6 +113,87 @@ impl StorageManager {
}
}

pub async fn get_map(
state: &PlatformState,
property: StorageProperty,
) -> RpcResult<HashMap<String, Value>> {
match StorageManager::get_string(state, property.clone()).await {
Ok(raw_value) => match serde_json::from_str(&raw_value) {
Ok(raw_map) => {
let the_map: HashMap<String, serde_json::Value> = raw_map;
Ok(the_map)
}
Err(_) => Err(StorageManager::get_firebolt_error(&property)),
},
Err(_) => Err(StorageManager::get_firebolt_error(&property)),
}
}

pub async fn set_value_in_map(
state: &PlatformState,
property: StorageProperty,
key: String,
value: String,
) -> RpcResult<()> {
match StorageManager::get_map(state, property.clone()).await {
Ok(the_map) => {
let mut mutant: HashMap<String, serde_json::Value> = the_map;
mutant.insert(key, serde_json::Value::String(value));
match StorageManager::set_string(
state,
property.clone(),
serde_json::to_string(&mutant).unwrap(),
None,
)
.await
{
Ok(_) => Ok(()),
Err(_) => Err(StorageManager::get_firebolt_error(&property)),
}
}
Err(_) => {
let mut map: HashMap<String, serde_json::Value> = Default::default();
map.insert(key, serde_json::Value::String(value));
match StorageManager::set_string(
state,
property.clone(),
serde_json::to_string(&map).unwrap(),
None,
)
.await
{
Ok(_) => Ok(()),
Err(_) => Err(StorageManager::get_firebolt_error(&property)),
}
}
}
}

pub async fn remove_value_in_map(
state: &PlatformState,
property: StorageProperty,
key: String,
) -> RpcResult<()> {
match StorageManager::get_map(state, property.clone()).await {
Ok(the_map) => {
let mut mutant: HashMap<String, serde_json::Value> = the_map;
mutant.remove(&key);
match StorageManager::set_string(
state,
property.clone(),
serde_json::to_string(&mutant).unwrap(),
None,
)
.await
{
Ok(_) => Ok(()),
Err(_) => Err(StorageManager::get_firebolt_error(&property)),
}
}
Err(_) => Err(StorageManager::get_firebolt_error(&property)),
}
}

pub async fn set_string(
state: &PlatformState,
property: StorageProperty,
Expand Down
Loading