Skip to content

Add API to get next fresh message #3777

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

Closed
wants to merge 4 commits into from
Closed

Conversation

link2xt
Copy link
Collaborator

@link2xt link2xt commented Nov 27, 2022

Rust: Context.get_next_fresh_msg()
C: dc_get_next_fresh_msg()
JSON-RPC: get_next_fresh_msg method

Rust: Context.get_next_fresh_msg()
C: dc_get_next_fresh_msg()
JSON-RPC: get_next_fresh_msg method
*
* @memberof dc_context_t
* @param context The context object as returned from dc_context_new().
* @return Message ID of the next fresh message. Returns 0 on error.
Copy link
Contributor

Choose a reason for hiding this comment

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

what if there is no next fresh message?

@@ -751,6 +760,19 @@ impl Context {
Ok(list)
}

/// Returns oldest fresh message in unmuted and unblocked chats.
///
/// If there are no such messages, waits until there is one.
Copy link
Contributor

Choose a reason for hiding this comment

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

waits until there is one.

is this also true for cffi/jsonprc? if so, this information should be added to the docs there as well.

maybe also add a line for the usecase. though i am sure, there is concrete need somewhere, the uscase is not clear to at least to me :)

Copy link
Contributor

Choose a reason for hiding this comment

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

or maybe better rename the function to wait_for_next_fresh_message(), i would not expect a get-function to block for maybe hours.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I named it similarly to get_next_event() for consistency. We can call it something with wait, but then there can be an impression that get_next_event() is somehow different and non-blocking.

Copy link
Contributor

Choose a reason for hiding this comment

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

hm, get_next_event() is called on an explicit event emitter object that does not have many other usages, so that seems less confusing than for the context where all the other get() work differently.

but i agree, wait() would have been better for the context emitter as well.

i still suggest to not repeat the "mistake" use sth. with "wait" in the name here. maybe we can rename the other usages accordingly when we iterate over or so.

@link2xt
Copy link
Collaborator Author

link2xt commented Nov 27, 2022

@r10s The use case is to make it easier to write bots that have only a single thread and loop for the next message to process in infinite loop, then process them, then mark as read, like this:

if not configured:
    configure
start
loop:
    msg = get_next_fresh_msg()
    process msg, maybe break the loop
    mark msg as read

Then you can be sure all messages are processed in the order they arrive, bot processes old messages after starting and never forgets about incoming message because it was busy processing another message already.

It makes easier to quick start with bots without using any frameworks. There is a PR with a quickstart deltachat-bot/bot-pages#45 but example code is currently busy looping on get_fresh_messages().

Users can then switch to advanced techniques like writing an event loop that, processing events and so on, but then there is a need to think about how message processing (initial processing of old messages and new messages that arrive while the bot works) works together with event processing, use some synchronization primitives available in the language bot is written in etc. With this simple API synchronization is done in the core.

Copy link
Contributor

@r10s r10s left a comment

Choose a reason for hiding this comment

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

lgtm, i'd modify the name and maybe add a line "this is mainly useful for bot usage" or so to the comment.

but these are no blockers.

@link2xt
Copy link
Collaborator Author

link2xt commented Nov 27, 2022

I still don't like that there is no way to stop this loop. With events loop you call stop_io, receive an INFO event and then break out of the loop, but this function will not return at all.

Maybe it really makes more sense to have a function that only waits and may be interrupted, but you have to check if there are actually any messages yourself.

@link2xt link2xt marked this pull request as draft November 27, 2022 18:04
@link2xt
Copy link
Collaborator Author

link2xt commented Nov 29, 2022

I found a better way to do a simple bot without IPC and all this stuff. An echo bot in #3734 is running an event loop in the main task and has a separate function that processes all fresh messages. Will try to get it into quickstart guide then.

@link2xt link2xt closed this Nov 29, 2022
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.

3 participants