1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::sync::Arc;
use adex_primitives::{
sentry::{units_for_slot, IMPRESSION},
targeting::{input::Global, Input},
test_util::{DUMMY_CAMPAIGN, DUMMY_IPFS, DUMMY_VALIDATOR_FOLLOWER, DUMMY_VALIDATOR_LEADER},
util::ApiUrl,
ToHex,
};
use adview_manager::{
get_unit_html_with_events, manager::Size, manager::DEFAULT_TOKENS, Manager, Options,
};
use axum::{response::Html, Extension};
use chrono::Utc;
use log::debug;
use wiremock::{
matchers::{method, path, query_param},
Mock, MockServer, ResponseTemplate,
};
use tera::Context;
use crate::app::State;
pub async fn get_index(Extension(state): Extension<Arc<State>>) -> Html<String> {
let html = state
.tera
.render("index.html", &Default::default())
.expect("Should render");
Html(html)
}
pub async fn get_preview_ad(Extension(state): Extension<Arc<State>>) -> Html<String> {
let mock_server = MockServer::start().await;
let market_url = mock_server.uri().parse().unwrap();
let whitelisted_tokens = DEFAULT_TOKENS.clone();
let disabled_video = false;
let publisher_addr = "0x0000000000000000626f62627973686d75726461"
.parse()
.unwrap();
let options = Options {
market_url,
market_slot: DUMMY_IPFS[0],
publisher_addr,
whitelisted_tokens,
size: Some(Size::new(300, 100)),
navigator_language: Some("bg".into()),
disabled_video,
disabled_sticky: false,
validators: vec![
ApiUrl::parse(&DUMMY_VALIDATOR_LEADER.url).expect("should parse"),
ApiUrl::parse(&DUMMY_VALIDATOR_FOLLOWER.url).expect("should parse"),
],
};
let manager =
Manager::new(options.clone(), Default::default()).expect("Failed to create AdView Manager");
let pub_prefix = publisher_addr.to_hex();
let units_for_slot_resp = units_for_slot::response::Response {
targeting_input_base: Input {
ad_view: None,
global: Global {
ad_slot_id: options.market_slot,
ad_slot_type: "".into(),
publisher_id: publisher_addr,
country: Some("Bulgaria".into()),
event_type: IMPRESSION,
seconds_since_epoch: Utc::now(),
user_agent_os: None,
user_agent_browser_family: None,
},
campaign: None,
balances: None,
ad_unit_id: None,
ad_slot: None,
},
accepted_referrers: vec![],
fallback_unit: None,
campaigns: vec![],
};
let mock_call = Mock::given(method("GET"))
.and(path(format!("units-for-slot/{}", options.market_slot)))
.and(query_param("pubPrefix", pub_prefix))
.and(query_param(
"depositAssets[]",
"0x6B175474E89094C44Da98b954EedeAC495271d0F",
))
.respond_with(ResponseTemplate::new(200).set_body_json(units_for_slot_resp))
.expect(1)
.named("get_units_for_slot_resp");
mock_call.mount(&mock_server).await;
let demand_resp = manager
.get_units_for_slot_resp()
.await
.expect("Should return Mocked response");
debug!("Mocked response: {demand_resp:?}");
let supermarket_ad_unit = adex_primitives::sentry::units_for_slot::response::AdUnit {
ipfs: DUMMY_IPFS[1],
media_url: "ipfs://QmcUVX7fvoLMM93uN2bD3wGTH8MXSxeL8hojYfL2Lhp7mR".to_string(),
media_mime: "image/jpeg".to_string(),
target_url: "https://www.adex.network/?stremio-test-banner-1".to_string(),
};
let code = get_unit_html_with_events(
&options,
&supermarket_ad_unit,
"localhost",
DUMMY_CAMPAIGN.id,
&DUMMY_CAMPAIGN.validators,
false,
);
let html = {
let mut context = Context::new();
context.insert("ad_code", &code);
state
.tera
.render("ad.html", &context)
.expect("Should render")
};
Html(html)
}
pub async fn get_preview_video(Extension(state): Extension<Arc<State>>) -> Html<String> {
let whitelisted_tokens = DEFAULT_TOKENS.clone();
let disabled_video = false;
let publisher_addr = "0x0000000000000000626f62627973686d75726461"
.parse()
.unwrap();
let options = Options {
market_url: "http://placeholder.com".parse().unwrap(),
market_slot: DUMMY_IPFS[0],
publisher_addr,
whitelisted_tokens,
size: Some(Size::new(728, 90)),
navigator_language: Some("bg".into()),
disabled_video,
disabled_sticky: false,
validators: vec![
ApiUrl::parse(&DUMMY_VALIDATOR_LEADER.url).expect("should parse"),
ApiUrl::parse(&DUMMY_VALIDATOR_FOLLOWER.url).expect("should parse"),
],
};
let video_ad_unit = adex_primitives::sentry::units_for_slot::response::AdUnit {
ipfs: "QmShJ6tsJ7LHLiYGX4EAmPyCPWJuCnvm6AKjweQPnw9qfh"
.parse()
.expect("Valid IPFS"),
media_url: "ipfs://Qmevmms1AZgYXS27A9ghSjHn4DSaHMfgYcD2SoGW14RYGy".to_string(),
media_mime: "video/mp4".to_string(),
target_url: "https://www.stremio.com/downloads".to_string(),
};
let video_html = get_unit_html_with_events(
&options,
&video_ad_unit,
"localhost",
DUMMY_CAMPAIGN.id,
&DUMMY_CAMPAIGN.validators,
false,
);
let html = {
let mut context = Context::new();
context.insert("ad_code", &video_html);
state
.tera
.render("ad.html", &context)
.expect("Should render")
};
Html(html)
}