Skip to content

Commit

Permalink
Revert "Fix/ad policy events (#270)" (#297)
Browse files Browse the repository at this point in the history
This reverts commit 5783cb0.
  • Loading branch information
brendanobra authored Nov 3, 2023
1 parent 5783cb0 commit aeeab03
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 294 deletions.
213 changes: 86 additions & 127 deletions core/main/src/firebolt/handlers/advertising_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
// SPDX-License-Identifier: Apache-2.0
//

use crate::{
service::apps::app_events::{AppEventDecorationError, AppEventDecorator, AppEvents},
utils::rpc_utils::rpc_add_event_listener_with_decorator,
};
use crate::service::apps::app_events::{AppEventDecorationError, AppEventDecorator, AppEvents};
use jsonrpsee::{
core::{async_trait, Error, RpcResult},
proc_macros::rpc,
Expand All @@ -40,18 +37,24 @@ use ripple_sdk::{
EVENT_ADVERTISING_SKIP_RESTRICTION_CHANGED,
},
},
extn::extn_client_message::ExtnResponse,
log::error,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

use crate::{
firebolt::rpc::RippleRPCProvider, processor::storage::storage_manager::StorageManager,
state::platform_state::PlatformState, utils::rpc_utils::rpc_err,
firebolt::rpc::RippleRPCProvider,
processor::storage::storage_manager::StorageManager,
state::platform_state::PlatformState,
utils::rpc_utils::{rpc_add_event_listener, rpc_err},
};

use super::{capabilities_rpc::is_permitted, privacy_rpc};
use super::{
capabilities_rpc::is_permitted,
privacy_rpc::{self, PrivacyImpl},
};

const ADVERTISING_APP_BUNDLE_ID_SUFFIX: &str = "Comcast";

Expand All @@ -70,7 +73,7 @@ pub struct AdvertisingPolicy {
pub limit_ad_tracking: bool,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum SkipRestriction {
None,
Expand All @@ -82,15 +85,15 @@ pub enum SkipRestriction {
impl SkipRestriction {
pub fn as_string(&self) -> &'static str {
match self {
SkipRestriction::None => NONE,
SkipRestriction::None => "none",
SkipRestriction::AdsUnwatched => "adsUnwatched",
SkipRestriction::AdsAll => "adsAll",
SkipRestriction::All => "all",
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Deserialize, Debug, Clone)]
pub struct SetSkipRestrictionRequest {
pub value: SkipRestriction,
}
Expand Down Expand Up @@ -134,21 +137,7 @@ pub trait Advertising {
#[method(name = "advertising.resetIdentifier")]
async fn reset_identifier(&self, ctx: CallContext) -> RpcResult<()>;
}
const NONE: &str = "none";
async fn get_advertisting_policy(platform_state: &PlatformState) -> AdvertisingPolicy {
AdvertisingPolicy {
skip_restriction: StorageManager::get_string(
platform_state,
StorageProperty::SkipRestriction,
)
.await
.unwrap_or_else(|_| String::from(NONE)),
limit_ad_tracking: !privacy_rpc::PrivacyImpl::get_allow_app_content_ad_targeting(
platform_state,
)
.await,
}
}

#[derive(Clone)]
struct AdvertisingPolicyEventDecorator {}
#[async_trait]
Expand All @@ -160,7 +149,14 @@ impl AppEventDecorator for AdvertisingPolicyEventDecorator {
_event_name: &str,
_val_in: &Value,
) -> Result<Value, AppEventDecorationError> {
Ok(serde_json::to_value(get_advertisting_policy(ps).await)?)
let skip_restriction =
StorageManager::get_string(ps, StorageProperty::SkipRestriction).await;

let sr = match skip_restriction {
Err(_) => String::from("none"),
Ok(_) => skip_restriction.unwrap(),
};
Ok(serde_json::to_value(sr).unwrap())
}
fn dec_clone(&self) -> Box<dyn AppEventDecorator + Send + Sync> {
Box::new(self.clone())
Expand All @@ -169,7 +165,7 @@ impl AppEventDecorator for AdvertisingPolicyEventDecorator {

#[derive(Clone)]
struct AdvertisingSetRestrictionEventDecorator {}
use std::convert::From;

#[async_trait]
impl AppEventDecorator for AdvertisingSetRestrictionEventDecorator {
async fn decorate(
Expand All @@ -179,11 +175,14 @@ impl AppEventDecorator for AdvertisingSetRestrictionEventDecorator {
_event_name: &str,
_val_in: &Value,
) -> Result<Value, AppEventDecorationError> {
Ok(serde_json::to_value(
StorageManager::get_string(ps, StorageProperty::SkipRestriction)
.await
.unwrap_or_else(|_| String::from(NONE)),
)?)
let skip_restriction =
StorageManager::get_string(ps, StorageProperty::SkipRestriction).await;

let sr = match skip_restriction {
Err(_) => String::from("none"),
Ok(_) => skip_restriction.unwrap(),
};
Ok(serde_json::to_value(sr).unwrap())
}
fn dec_clone(&self) -> Box<dyn AppEventDecorator + Send + Sync> {
Box::new(self.clone())
Expand All @@ -197,17 +196,28 @@ pub struct AdvertisingImpl {
#[async_trait]
impl AdvertisingServer for AdvertisingImpl {
async fn reset_identifier(&self, _ctx: CallContext) -> RpcResult<()> {
self.state
let session = self.state.session_state.get_account_session().unwrap();

let resp = self
.state
.get_client()
.send_extn_request(AdvertisingRequest::ResetAdIdentifier(
self.state
.session_state
.get_account_session()
.ok_or_else(|| Error::Custom(String::from("no session available")))?,
))
.await
.map(|_ok| ())
.map_err(|err| err.into())
.send_extn_request(AdvertisingRequest::ResetAdIdentifier(session))
.await;

if resp.is_err() {
error!("Error resetting ad identifier {:?}", resp);
return Err(rpc_err("Could not reset ad identifier for the device"));
}

match resp {
Ok(payload) => match payload.payload.extract().unwrap() {
ExtnResponse::None(()) => Ok(()),
_ => Err(rpc_err("Device returned Unable to reset ad identifier")),
},
Err(_e) => Err(jsonrpsee::core::Error::Custom(String::from(
"Unable to reset ad identifier",
))),
}
}

async fn advertising_id(&self, ctx: CallContext) -> RpcResult<AdvertisingId> {
Expand Down Expand Up @@ -265,6 +275,7 @@ impl AdvertisingServer for AdvertisingImpl {
capability: FireboltCap::short("advertising:identifier".to_string()),
role: Some(CapabilityRole::Use),
};
let ad_id_authorised = is_permitted(self.state.clone(), ctx, params).await;

let payload = AdvertisingRequest::GetAdInitObject(AdInitObjectRequestParams {
privacy_data: privacy_rpc::get_allow_app_content_ad_targeting_settings(&self.state)
Expand All @@ -276,11 +287,17 @@ impl AdvertisingServer for AdvertisingImpl {
device_ad_attributes: HashMap::new(),
coppa: config.options.coppa.unwrap_or(false),
authentication_entity: config.options.authentication_entity.unwrap_or_default(),
dist_session: session
.ok_or_else(|| Error::Custom(String::from("no session available")))?,
dist_session: session.unwrap(),
});

match self.state.get_client().send_extn_request(payload).await {
let resp = self.state.get_client().send_extn_request(payload).await;

if resp.is_err() {
error!("Error getting ad init object: {:?}", resp);
return Err(rpc_err("Could not get ad init object from the device"));
}

match resp {
Ok(payload) => match payload.payload.extract().unwrap() {
AdvertisingResponse::AdInitObject(obj) => {
let ad_init_object = AdvertisingFrameworkConfig {
Expand All @@ -291,10 +308,7 @@ impl AdvertisingServer for AdvertisingImpl {
ad_site_section_id: "".to_string(),
ad_opt_out: obj.ad_opt_out,
privacy_data: obj.privacy_data,
ifa: if is_permitted(self.state.clone(), ctx, params)
.await
.unwrap_or(false)
{
ifa: if ad_id_authorised.unwrap() {
obj.ifa
} else {
"0".repeat(obj.ifa.len())
Expand All @@ -304,7 +318,7 @@ impl AdvertisingServer for AdvertisingImpl {
app_bundle_id: obj.app_bundle_id,
distributor_app_id: obj.distributor_app_id,
device_ad_attributes: obj.device_ad_attributes,
coppa: obj.coppa.to_string().parse::<u32>().unwrap_or(0),
coppa: obj.coppa.to_string().parse::<u32>().unwrap(),
authentication_entity: obj.authentication_entity,
};
Ok(ad_init_object)
Expand Down Expand Up @@ -345,22 +359,27 @@ impl AdvertisingServer for AdvertisingImpl {
}

async fn policy(&self, _ctx: CallContext) -> RpcResult<AdvertisingPolicy> {
Ok(get_advertisting_policy(&self.state).await)
let limit_ad_tracking = PrivacyImpl::get_allow_app_content_ad_targeting(&self.state).await;
let skip_restriction =
StorageManager::get_string(&self.state, StorageProperty::SkipRestriction).await;

let sr = match skip_restriction {
Err(_) => String::from("none"),
Ok(_) => skip_restriction.unwrap(),
};

Ok(AdvertisingPolicy {
skip_restriction: sr,
limit_ad_tracking,
})
}

async fn advertising_on_policy_changed(
&self,
ctx: CallContext,
request: ListenRequest,
) -> RpcResult<ListenerResponse> {
rpc_add_event_listener_with_decorator(
&self.state,
ctx,
request,
EVENT_ADVERTISING_POLICY_CHANGED,
Some(Box::new(AdvertisingPolicyEventDecorator {})),
)
.await
rpc_add_event_listener(&self.state, ctx, request, EVENT_ADVERTISING_POLICY_CHANGED).await
}

async fn advertising_set_skip_restriction(
Expand All @@ -378,11 +397,14 @@ impl AdvertisingServer for AdvertisingImpl {
}

async fn advertising_skip_restriction(&self, _ctx: CallContext) -> RpcResult<String> {
Ok(
StorageManager::get_string(&self.state, StorageProperty::SkipRestriction)
.await
.unwrap_or_else(|_| String::from(NONE)),
)
let skip_restriction =
StorageManager::get_string(&self.state, StorageProperty::SkipRestriction).await;

let sr = match skip_restriction {
Err(_) => String::from("none"),
Ok(_) => skip_restriction.unwrap(),
};
Ok(sr)
}

async fn advertising_on_skip_restriction_changed(
Expand Down Expand Up @@ -411,66 +433,3 @@ impl RippleRPCProvider<AdvertisingImpl> for AdvertisingRPCProvider {
(AdvertisingImpl { state }).into_rpc()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::firebolt::handlers::advertising_rpc::AdvertisingImpl;
use ripple_sdk::{api::gateway::rpc_gateway_api::JsonRpcApiRequest, tokio};
use ripple_tdk::utils::test_utils::Mockable;
use serde::{Deserialize, Serialize};
use serde_json::json;

#[derive(Serialize, Deserialize, Clone, Debug)]
struct CallContextContainer {
pub ctx: Option<CallContext>,
}

fn merge(a: &mut Value, b: &Value) {
match (a, b) {
(&mut Value::Object(ref mut a), Value::Object(ref b)) => {
for (k, v) in b {
merge(a.entry(k.clone()).or_insert(Value::Null), v);
}
}
(a, b) => {
*a = b.clone();
}
}
}

fn test_request(
method_name: String,
call_context: Option<CallContext>,
params: Option<Value>,
) -> String {
let mut the_map = params.map_or(json!({}), |v| v);
merge(
&mut the_map,
&serde_json::json!(CallContextContainer { ctx: call_context }),
);
let v = serde_json::to_value(JsonRpcApiRequest {
jsonrpc: "2.0".to_string(),
id: Some(1),
method: method_name,
params: Some(the_map),
})
.unwrap();
serde_json::to_string(&v).unwrap()
}

#[tokio::test]
pub async fn test_app_bundle_id() {
let ad_module = (AdvertisingImpl {
state: PlatformState::mock(),
})
.into_rpc();

let request = test_request(
"advertising.appBundleId".to_string(),
Some(CallContext::mock()),
None,
);

assert!(ad_module.raw_json_request(&request).await.is_ok());
}
}
Loading

0 comments on commit aeeab03

Please sign in to comment.