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

add send_draft() to JSON-RPC API #4839

Merged
merged 2 commits into from
Oct 22, 2023
Merged

add send_draft() to JSON-RPC API #4839

merged 2 commits into from
Oct 22, 2023

Conversation

adbenitez
Copy link
Member

close #4643

@adbenitez adbenitez changed the base branch from master to stable October 20, 2023 00:51
@@ -1848,6 +1848,22 @@ impl CommandApi {
}
}

async fn send_draft(&self, account_id: u32, chat_id: u32) -> Result<u32> {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it this way because it was the simplest way to implement it with my limited Rust copy+paste skills, but probably it is more bug-free to pass the draft/msg id instead to be sure you are sending the draft you are expecting to send and not another draft that replaced the draft you think you are sending in the mean time

anyway for bots this is good enough, desktop is maybe more sensitive, feel free to pick up this pull request and improve

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be for providing just msg id of draft and the function tries to load it and if it can then it sends it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also if the api is not final you can make it start with misc_. BTW the current api for setting drafts is also not quite ready as it does not return the message id of the draft yet and has no option to keep the same msg id even when you change the text it will create a new message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the danger of an just send whatever draft function is that it could produce race conditions like:

  1. create draft
  2. send draft because user clicked on send
  3. send draft because user clicked on send multiple times (this one could be delayed for some reason
  4. user creates new draft for new message
  5. Unfinished message is send due to the delayed send draft call

Admittedly this is theoretically, but I still think a api where you create a draft then send it by it's id is better.
Anyways we should really think about this, also how to implement it nicely in desktop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it absolutely makes sense to pass msg id, but if no one will take care of this PR soon, I would like to have this api at least for bots, where that is not a problem, to at least have something to work with in bots

@adbenitez adbenitez changed the title add send_draft() add send_draft() to JSON-RPC API Oct 20, 2023
@r10s
Copy link
Member

r10s commented Oct 20, 2023

tbh, i do not really get why a send_draft() is needed at all for jsonrpc. would be nice to get some background here.

on low-level rust and cffi, a draft is send as every other message using send_msg() - and if you want to prepare a webxdc-message before sending, you call send_webxdc_status_update() etc. on the draft. is there some special handling of messages in send_msg()

sure, things can always be done differently, however, unless there are good reason, it makes sense to do things in a similar way, with similar functions and names

EDIT: answering myself: there is just no send_msg() in the jsonrpc. probably, there were reasons for deviating from the existing api. maybe take this as a chance to add a send_msg() (EDIT: taking msg_id, not pointer) that is closer to the core api? then the draft problem would be solved implicitly; however, i think this is quite similar to what @Simon-Laux suggested above with "providing just msg id and try to send it"

@link2xt
Copy link
Collaborator

link2xt commented Oct 20, 2023

probably, there were reasons for deviating from the existing api. maybe take this as a chance to add a send_msg() that is closer to the core api?

There is a send_msg API that accepts a MessageData. This needs to be a way to set updates in there.

There is currently no set_draft, only misc_set_draft. I think set_draft should be similar to send_msg and also accept a complete message description. Then UI will keep MessageData in memory, which fully describes the message except for the blobs (which are referenced by filename or some other blob identifier), and can update the message completely locally, possibly offloading it as a draft from time to time.

Sending a draft will then be no different from sending a normal message.

@adbenitez
Copy link
Member Author

Sending a draft will then be no different from sending a normal message.

sending preserving the message ID etc. without recreating a new message and re-setting all status updates is better IMHO, that is set_draft(), get msg-id returned by set_draft(), send status updates, send the original message draft, at least that is what android does

let ctx = self.get_context(account_id).await?;
if let Some(draft) = ChatId::new(chat_id).get_draft(&ctx).await? {
let mut draft = draft;
let msg_id = chat::send_msg(&ctx, ChatId::new(chat_id), &mut draft)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does sending the draft make it disappear from draft? or it most be manually removed after sending?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it keeps the msg_id, how can it be kept? Should be transformed into the same message.

Anyway, this PR needs a test checking that msg_id is kept and that no new message is created as a result.

@link2xt
Copy link
Collaborator

link2xt commented Oct 21, 2023

So how about send_draft(account_id, msg_id) that sends a draft preserving the msg_id? If the message is not a draft, return an error.

Copy link
Member

@Simon-Laux Simon-Laux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good for now, it is marked as experimental via the misc_ prefix.
We can discuss this at length next week, I also discussed a bit with @r10s personally already.
I vote for merging this so adb can do what he wants and then breaking it in the next update where we do everything "right"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

not possible to send webxdc app staged in draft with JSON-RPC API
4 participants