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

Return effective content type in JSON API. #3289

Merged
merged 6 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub struct Inscription {
pub children: Vec<InscriptionId>,
pub content_length: Option<usize>,
pub content_type: Option<String>,
pub effective_content_type: Option<String>,
pub fee: u64,
pub height: u32,
pub id: InscriptionId,
Expand Down
12 changes: 12 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,17 @@ impl Server {
let info = Index::inscription_info(&index, query)?
.ok_or_not_found(|| format!("inscription {query}"))?;

let effective_mime_type = if let Some(delegate_id) = info.inscription.delegate() {
let delegate_result = index.get_inscription_by_id(delegate_id);
if let Ok(Some(delegate)) = delegate_result {
delegate.content_type().map(str::to_string)
} else {
info.inscription.content_type().map(str::to_string)
}
} else {
info.inscription.content_type().map(str::to_string)
};

Ok(if accept_json {
Json(api::Inscription {
address: info
Expand All @@ -1535,6 +1546,7 @@ impl Server {
children: info.children,
content_length: info.inscription.content_length(),
content_type: info.inscription.content_type().map(|s| s.to_string()),
effective_content_type: effective_mime_type,
fee: info.entry.fee,
height: info.entry.height,
id: info.entry.id,
Expand Down
1 change: 1 addition & 0 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ fn get_inscription() {
children: Vec::new(),
content_length: Some(3),
content_type: Some("text/plain;charset=utf-8".to_string()),
effective_content_type: Some("text/plain;charset=utf-8".to_string()),
fee: 138,
height: 2,
id: inscription_id,
Expand Down
33 changes: 33 additions & 0 deletions tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2185,6 +2185,39 @@ fn file_inscribe_with_delegate_inscription() {
ord_rpc_server.assert_response(format!("/content/{}", inscribe.inscriptions[0].id), "FOO");
}

#[test]
fn inscription_with_delegate_returns_effective_content_type() {
let bitcoin_rpc_server = test_bitcoincore_rpc::spawn();
let ord_rpc_server = TestServer::spawn_with_server_args(&bitcoin_rpc_server, &[], &[]);
create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

bitcoin_rpc_server.mine_blocks(1);
let (delegate, _) = inscribe(&bitcoin_rpc_server, &ord_rpc_server);

let inscribe = CommandBuilder::new(format!(
"wallet inscribe --fee-rate 1.0 --delegate {delegate} --file meow.wav"
))
.write("meow.wav", [0; 2048])
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<Inscribe>();

bitcoin_rpc_server.mine_blocks(1);

let inscription_id = inscribe.inscriptions[0].id;
let json_response = ord_rpc_server.json_request(format!("/inscription/{}", inscription_id));

let inscription_json: api::Inscription =
serde_json::from_str(&json_response.text().unwrap()).unwrap();
assert_regex_match!(inscription_json.address.unwrap(), r"bc1p.*");

assert_eq!(inscription_json.content_type, Some("audio/wav".to_string()));
assert_eq!(
inscription_json.effective_content_type,
Some("text/plain;charset=utf-8".to_string())
);
}

#[test]
fn file_inscribe_with_non_existent_delegate_inscription() {
let bitcoin_rpc_server = test_bitcoincore_rpc::spawn();
Expand Down
Loading