Kuska-ssb / ssb

Secure Scuttlebut library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Supporting options for getSubset query

mycognosist opened this issue · comments

commented

When making a partialReplication.getSubset RPC call, it is possible to specify (optional) options to the query: including pageLimit and descending.

Here is the implementation of subset options in go-sbot and in JS.

It is not currently possible to supply these optional parameters when making a getSubset RPC call from kuska (for the code in #18).

send_request() serializes args as an array with a single element. However, options for the getSubset RPC call are expected to be the 2nd element in the args array (they are not included together with the main query object). So the JSON string should look something like this:

[{"op":"type","string":"post"},{"descending":false,"keys":true,"pageLimit":2}]
pub async fn send_request<T: serde::Serialize>(
    &mut self,
    name: &[&str],
    rpc_type: RpcType,
    args: &T,
) -> Result<RequestNo> {
    self.req_no += 1;

    let body_str = serde_json::to_string(&BodyRef {
        name,
        rpc_type,
        args: &[&args],
    })?;

I am wondering what the best way is to support options as a second argument. One way would be to make args a tuple, like this:

#[derive(Serialize)]
pub struct BodyRef<'a, T: serde::Serialize, U: serde::Serialize> {
    pub name: &'a [&'a str],
    #[serde(rename = "type")]
    pub rpc_type: RpcType,
    pub args: (&'a T, &'a Option<U>),
}

// ...

pub async fn send_request<T: serde::Serialize, U: serde::Serialize>(
    &mut self,
    name: &[&str],
    rpc_type: RpcType,
    args: &T,
    opts: &Option<U>,
) -> Result<RequestNo> {
    self.req_no += 1;

    let body_str = serde_json::to_string(&BodyRef {
        name,
        rpc_type,
        args: (&args, &opts),
    })?;

I have confirmed that this approach works when interacting with go-sbot. If the opts is None, the JSON output looks like this:

[{"op":"type","string":"post"},null]

@adria0 I wonder what you think about this? Can you think of a more elegant approach?

commented

Solved in #20 :)