refactor(quark): port message_query, message_search

#283
This commit is contained in:
Paul Makles
2024-04-07 23:13:45 +01:00
parent 301676fb54
commit 6bc74749d2
5 changed files with 136 additions and 132 deletions

View File

@@ -5,8 +5,8 @@ use iso8601_timestamp::Timestamp;
use revolt_config::config;
use revolt_models::{
v0::{
self, DataMessageSend, Embed, MessageAuthor, MessageSort, MessageWebhook, PushNotification,
ReplyIntent, SendableEmbed, Text, RE_MENTION,
self, BulkMessageResponse, DataMessageSend, Embed, MessageAuthor, MessageSort,
MessageWebhook, PushNotification, ReplyIntent, SendableEmbed, Text, RE_MENTION,
},
validator::Validate,
};
@@ -488,6 +488,50 @@ impl Message {
Ok(())
}
/// Helper function to fetch many messages with users
pub async fn fetch_with_users(
db: &Database,
query: MessageQuery,
perspective: &User,
include_users: Option<bool>,
server_id: Option<String>,
) -> Result<BulkMessageResponse> {
let messages: Vec<v0::Message> = db
.fetch_messages(query)
.await?
.into_iter()
.map(Into::into)
.collect();
if let Some(true) = include_users {
let user_ids = messages
.iter()
.map(|m| m.author.clone())
.collect::<HashSet<String>>()
.into_iter()
.collect::<Vec<String>>();
let users = User::fetch_many_ids_as_mutuals(db, perspective, &user_ids).await?;
Ok(BulkMessageResponse::MessagesAndUsers {
messages,
users,
members: if let Some(server_id) = server_id {
Some(
db.fetch_members(&server_id, &user_ids)
.await?
.into_iter()
.map(Into::into)
.collect(),
)
} else {
None
},
})
} else {
Ok(BulkMessageResponse::JustMessages(messages))
}
}
/// Append content to message
pub async fn append(
db: &Database,

View File

@@ -250,13 +250,13 @@ auto_derived!(
/// Maximum number of messages to fetch
///
/// For fetching nearby messages, this is \`(limit + 1)\`.
#[validate(range(min = 1, max = 100))]
#[cfg_attr(feature = "validator", validate(range(min = 1, max = 100)))]
pub limit: Option<i64>,
/// Message id before which messages should be fetched
#[validate(length(min = 26, max = 26))]
#[cfg_attr(feature = "validator", validate(length(min = 26, max = 26)))]
pub before: Option<String>,
/// Message id after which messages should be fetched
#[validate(length(min = 26, max = 26))]
#[cfg_attr(feature = "validator", validate(length(min = 26, max = 26)))]
pub after: Option<String>,
/// Message sort direction
pub sort: Option<MessageSort>,
@@ -265,33 +265,34 @@ auto_derived!(
/// Specifying 'nearby' ignores 'before', 'after' and 'sort'.
/// It will also take half of limit rounded as the limits to each side.
/// It also fetches the message ID specified.
#[validate(length(min = 26, max = 26))]
#[cfg_attr(feature = "validator", validate(length(min = 26, max = 26)))]
pub nearby: Option<String>,
/// Whether to include user (and member, if server channel) objects
pub include_users: Option<bool>,
}
/// Options for searching for messages
#[cfg_attr(feature = "validator", derive(Validate))]
pub struct DataMessageSearch {
/// Full-text search query
///
/// See [MongoDB documentation](https://docs.mongodb.com/manual/text-search/#-text-operator) for more information.
#[validate(length(min = 1, max = 64))]
#[cfg_attr(feature = "validator", validate(length(min = 1, max = 64)))]
pub query: String,
/// Maximum number of messages to fetch
#[validate(range(min = 1, max = 100))]
#[cfg_attr(feature = "validator", validate(range(min = 1, max = 100)))]
pub limit: Option<i64>,
/// Message id before which messages should be fetched
#[validate(length(min = 26, max = 26))]
#[cfg_attr(feature = "validator", validate(length(min = 26, max = 26)))]
pub before: Option<String>,
/// Message id after which messages should be fetched
#[validate(length(min = 26, max = 26))]
#[cfg_attr(feature = "validator", validate(length(min = 26, max = 26)))]
pub after: Option<String>,
/// Message sort direction
///
/// By default, it will be sorted by latest.
#[serde(default = "MessageSort::default")]
#[cfg_attr(feature = "serde", serde(default = "MessageSort::default"))]
pub sort: MessageSort,
/// Whether to include user (and member, if server channel) objects
pub include_users: Option<bool>,