From e919b73cbe289a44a20208c95a0e1f4223607501 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Tue, 10 Dec 2024 01:54:19 +0100 Subject: [PATCH 1/2] api: cffi: add `dc_event_get_json` which gives the jsonrpc representation of an event This has the advantage that fields are named/labled and there can be potentially more than 2 (data1 & data2) fields on the events. This removes the need for the (potentialy confusing) overloading data1 with `dc_event_get_data1_str` to add more information to events. And also allows gradual/partial moving to the jsonrpc api, when needed or wanted. --- deltachat-ffi/deltachat.h | 13 +++++++++++++ deltachat-ffi/src/lib.rs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index 19bbdeb89e..4c82d9eeee 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -5951,6 +5951,19 @@ char* dc_event_get_data2_str(dc_event_t* event); */ uint32_t dc_event_get_account_id(dc_event_t* event); +/** + * Get the json representation of the event as the jsonrpc api would return it. + * For documentation on this json object see . + * + * The difference to the cffi is that the fields are named, + * and can also contain more fields than the cffi api (which only has data1 and data2). + * + * @memberof dc_event_t + * @param event The event object as returned from dc_get_next_event(). + * @return The json representation of the event as string or NULL. + * Must be freed using dc_str_unref(). + */ +uint32_t dc_event_get_json(dc_event_t* event); /** * Free memory used by an event object. diff --git a/deltachat-ffi/src/lib.rs b/deltachat-ffi/src/lib.rs index 694654e365..4c515e2b4d 100644 --- a/deltachat-ffi/src/lib.rs +++ b/deltachat-ffi/src/lib.rs @@ -816,6 +816,24 @@ pub unsafe extern "C" fn dc_event_get_account_id(event: *mut dc_event_t) -> u32 (*event).id } +#[no_mangle] +pub unsafe extern "C" fn dc_event_get_json(event: *mut dc_event_t) -> *mut libc::c_char { + if event.is_null() { + eprintln!("ignoring careless call to dc_event_get_json()"); + return ptr::null_mut(); + } + + match serde_json::to_string(&deltachat_jsonrpc::api::types::events::Event::from( + (*event).clone(), + )) { + Ok(string) => string.strdup(), + Err(error) => { + eprintln!("dc_event_get_json() failed to serialise to json: {error:#}"); + ptr::null_mut() + } + } +} + pub type dc_event_emitter_t = EventEmitter; #[no_mangle] From 61598dcdddc7644ebac9092bc2b86be7840f7671 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Thu, 12 Dec 2024 03:09:40 +0100 Subject: [PATCH 2/2] fix method return type --- deltachat-ffi/deltachat.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deltachat-ffi/deltachat.h b/deltachat-ffi/deltachat.h index 4c82d9eeee..c9fcb9db67 100644 --- a/deltachat-ffi/deltachat.h +++ b/deltachat-ffi/deltachat.h @@ -5963,7 +5963,7 @@ uint32_t dc_event_get_account_id(dc_event_t* event); * @return The json representation of the event as string or NULL. * Must be freed using dc_str_unref(). */ -uint32_t dc_event_get_json(dc_event_t* event); +char* dc_event_get_json(dc_event_t* event); /** * Free memory used by an event object.