This makes it so that files will be deduplicated when using the JsonRPC API. @nicodh and @WofWca you know the Desktop code and how it is using the API, so, you can probably tell me whether this is a good way of changing the JsonRPC code - feel free to push changes directly to this PR here! This PR here changes the existing functions instead of creating new ones; we can alternatively create new ones if it allows for a smoother transition. This brings a few changes: - If you pass a file that is already in the blobdir, it will be renamed to `<hash>.<extension>` immediately (previously, the filename on the disk stayed the same) - If you pass a file that's not in the blobdir yet, it will be copied to the blobdir immediately (previously, it was copied to the blobdir later, when sending) - If you create a file and then pass it to `create_message()`, it's better to directly create it in the blobdir, since it doesn't need to be copied. - You must not write to the files after they were passed to core, because otherwise, the hash will be wrong. So, if Desktop recodes videos or so, then the video file mustn't just be overwritten. What you can do instead is write the recoded video to a file with a random name in the blobdir and then create a new message with the new attachment. If needed, we can also create a JsonRPC for `set_file_and_deduplicate()` that replaces the file on an existing message. In order to test whether everything still works, the desktop issue has a list of things to test: https://github.com/deltachat/deltachat-desktop/issues/4498 Core issue: #6265 --------- Co-authored-by: l <link2xt@testrun.org>
deltachat-jsonrpc
This crate provides a JSON-RPC 2.0 interface to DeltaChat.
The JSON-RPC API is exposed in two fashions:
- A executable that exposes the JSON-RPC API through a WebSocket server running on localhost.
- The JSON-RPC API can also be called through the C FFI. The C FFI needs to be built with the
jsonrpcfeature. It will then expose the functionsdc_jsonrpc_init,dc_jsonrpc_request,dc_jsonrpc_next_responseanddc_jsonrpc_unref. See the docs in the header file for details.
We also include a JavaScript and TypeScript client for the JSON-RPC API. The source for this is in the typescript folder. The client can easily be used with the WebSocket server to build DeltaChat apps for web browsers or Node.js. See the examples for details.
Usage
Running the WebSocket server
From within this folder, you can start the WebSocket server with the following command:
cargo run --features webserver
If you want to use the server in a production setup, first build it in release mode:
cargo build --features webserver --release
You will then find the deltachat-jsonrpc-server executable in your target/release folder.
The executable currently does not support any command-line arguments. By default, once started it will accept WebSocket connections on ws://localhost:20808/ws. It will store the persistent configuration and databases in a ./accounts folder relative to the directory from where it is started.
The server can be configured with environment variables:
| variable | default | description |
|---|---|---|
DC_PORT |
20808 |
port to listen on |
DC_ACCOUNTS_PATH |
./accounts |
path to storage directory |
If you are targeting other architectures (like KaiOS or Android), the webserver binary can be cross-compiled easily with rust-cross:
cross build --features=webserver --target armv7-linux-androideabi --release
Using the TypeScript/JavaScript client
The package includes a JavaScript/TypeScript client which is partially auto-generated through the JSON-RPC library used by this crate (yerpc). Find the source in the typescript folder.
To use it locally, first install the dependencies and compile the TypeScript code to JavaScript:
cd typescript
npm install
npm run build
The JavaScript client is not yet published on NPM (but will likely be soon). Currently, it is recommended to vendor the bundled build. After running npm run build as documented above, there will be a file dist/deltachat.bundle.js. This is an ESM module containing all dependencies. Copy this file to your project and import the DeltaChat class.
import { DeltaChat } from './deltachat.bundle.js'
const dc = new DeltaChat('ws://localhost:20808/ws')
const accounts = await dc.rpc.getAllAccounts()
console.log('accounts', accounts)
A script is included to build autogenerated documentation, which includes all RPC methods:
cd typescript
npm run docs
Then open the typescript/docs folder in a web browser.
Development
Running the example app
We include a small demo web application that talks to the WebSocket server. It can be used for testing. Feel invited to expand this.
cd typescript
npm run build
npm run example:build
npm run example:start
Then, open http://localhost:8080/example.html in a web browser.
Run npm run example:dev to live-rebuild the example app when files changes.
Testing
The crate includes both a basic Rust smoke test and more featureful integration tests that use the TypeScript client.
Rust tests
To run the Rust test, use this command:
cargo test
TypeScript tests
cd typescript
npm run test
This will build the deltachat-jsonrpc-server binary and then run a test suite against the WebSocket server.
The test suite includes some tests that need online connectivity and a way to create test email accounts. To run these tests, talk to DeltaChat developers to get a token for the testrun.org service, or use a local instance of mailadm.
Then, set the CHATMAIL_DOMAIN environment variable to your testing email server domain.
CHATMAIL_DOMAIN=chat.example.org npm run test
Test Coverage
Running npm run test will report test coverage. For the coverage to be accurate the online tests need to be run.
If you are offline and want to see the coverage results anyway (even though they are inaccurate), you can bypass the errors of the online tests by setting the
COVERAGE_OFFLINE=1environment variable.
A summary of the coverage will be reported in the terminal after the test run. Open coverage/index.html in a web browser for a detailed report.