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

api: cffi: add dc_event_get_json which gives the jsonrpc representation of an event #6326

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions deltachat-ffi/deltachat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://js.jsonrpc.delta.chat/types/T.Event.html>.
*
* 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().
*/
char* dc_event_get_json(dc_event_t* event);

/**
* Free memory used by an event object.
Expand Down
18 changes: 18 additions & 0 deletions deltachat-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading