-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ use std::time::{Duration, Instant, SystemTime}; | |
|
||
use anyhow::{ensure, Result}; | ||
use async_channel::{self as channel, Receiver, Sender}; | ||
use tokio::sync::{Mutex, RwLock}; | ||
use tokio::sync::{Mutex, Notify, RwLock}; | ||
|
||
use crate::chat::{get_chat_cnt, ChatId}; | ||
use crate::config::Config; | ||
|
@@ -205,6 +205,11 @@ pub struct InnerContext { | |
pub(crate) translated_stockstrings: StockStrings, | ||
pub(crate) events: Events, | ||
|
||
/// Notify about fresh messages. | ||
/// | ||
/// This notification is used only internally for [`InternalContext.get_next_fresh_msg()`]. | ||
pub(crate) notify_fresh_message: Notify, | ||
|
||
pub(crate) scheduler: RwLock<Option<Scheduler>>, | ||
pub(crate) ratelimit: RwLock<Ratelimit>, | ||
|
||
|
@@ -354,6 +359,7 @@ impl Context { | |
wrong_pw_warning_mutex: Mutex::new(()), | ||
translated_stockstrings: stockstrings, | ||
events, | ||
notify_fresh_message: Notify::new(), | ||
scheduler: RwLock::new(None), | ||
ratelimit: RwLock::new(Ratelimit::new(Duration::new(60, 0), 6.0)), // Allow to send 6 messages immediately, no more than once every 10 seconds. | ||
quota: RwLock::new(None), | ||
|
@@ -451,9 +457,12 @@ impl Context { | |
self.emit_event(EventType::MsgsChanged { chat_id, msg_id }); | ||
} | ||
|
||
/// Emits an IncomingMsg event with specified chat and message ids | ||
/// Emits an IncomingMsg event with specified chat and message ids. | ||
/// | ||
/// Notifies `get_next_fresh_msg()` that there might be a new fresh message. | ||
pub fn emit_incoming_msg(&self, chat_id: ChatId, msg_id: MsgId) { | ||
self.emit_event(EventType::IncomingMsg { chat_id, msg_id }); | ||
self.notify_fresh_message.notify_one(); | ||
} | ||
|
||
/// Returns a receiver for emitted events. | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. or maybe better rename the function to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I named it similarly to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
pub async fn get_next_fresh_msg(&self) -> Result<MsgId> { | ||
loop { | ||
if let Some(msg_id) = self.get_fresh_msgs().await?.last() { | ||
return Ok(*msg_id); | ||
} else { | ||
self.notify_fresh_message.notified().await; | ||
} | ||
} | ||
} | ||
|
||
/// Searches for messages containing the query string. | ||
/// | ||
/// If `chat_id` is provided this searches only for messages in this chat, if `chat_id` | ||
|
There was a problem hiding this comment.
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?