pub mod v3 {
use std::collections::BTreeMap;
use js_int::{uint, UInt};
use ruma_common::{
api::ruma_api,
events::{AnyStateEvent, AnyTimelineEvent},
serde::{Incoming, Raw, StringEnum},
OwnedEventId, OwnedMxcUri, OwnedRoomId, OwnedUserId,
};
use serde::{Deserialize, Serialize};
use crate::{
filter::{IncomingRoomEventFilter, RoomEventFilter},
PrivOwnedStr,
};
ruma_api! {
metadata: {
description: "Search events.",
method: POST,
name: "search",
r0_path: "/_matrix/client/r0/search",
stable_path: "/_matrix/client/v3/search",
rate_limited: true,
authentication: AccessToken,
added: 1.0,
}
request: {
#[ruma_api(query)]
pub next_batch: Option<&'a str>,
pub search_categories: Categories<'a>,
}
response: {
pub search_categories: ResultCategories,
}
error: crate::Error
}
impl<'a> Request<'a> {
pub fn new(search_categories: Categories<'a>) -> Self {
Self { next_batch: None, search_categories }
}
}
impl Response {
pub fn new(search_categories: ResultCategories) -> Self {
Self { search_categories }
}
}
#[derive(Clone, Debug, Default, Incoming, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Categories<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub room_events: Option<Criteria<'a>>,
}
impl Categories<'_> {
pub fn new() -> Self {
Default::default()
}
}
#[derive(Clone, Debug, Incoming, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Criteria<'a> {
pub search_term: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub keys: Option<&'a [SearchKeys]>,
#[serde(skip_serializing_if = "RoomEventFilter::is_empty")]
pub filter: RoomEventFilter<'a>,
#[serde(skip_serializing_if = "Option::is_none")]
pub order_by: Option<OrderBy>,
#[serde(default, skip_serializing_if = "EventContext::is_default")]
pub event_context: EventContext,
#[serde(skip_serializing_if = "Option::is_none")]
pub include_state: Option<bool>,
#[serde(default, skip_serializing_if = "Groupings::is_empty")]
pub groupings: Groupings<'a>,
}
impl<'a> Criteria<'a> {
pub fn new(search_term: &'a str) -> Self {
Self {
search_term,
keys: None,
filter: RoomEventFilter::default(),
order_by: None,
event_context: Default::default(),
include_state: None,
groupings: Default::default(),
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct EventContext {
#[serde(
default = "default_event_context_limit",
skip_serializing_if = "is_default_event_context_limit"
)]
pub before_limit: UInt,
#[serde(
default = "default_event_context_limit",
skip_serializing_if = "is_default_event_context_limit"
)]
pub after_limit: UInt,
#[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
pub include_profile: bool,
}
fn default_event_context_limit() -> UInt {
uint!(5)
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_default_event_context_limit(val: &UInt) -> bool {
*val == default_event_context_limit()
}
impl EventContext {
pub fn new() -> Self {
Self {
before_limit: default_event_context_limit(),
after_limit: default_event_context_limit(),
include_profile: false,
}
}
pub fn is_default(&self) -> bool {
self.before_limit == default_event_context_limit()
&& self.after_limit == default_event_context_limit()
&& !self.include_profile
}
}
impl Default for EventContext {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct EventContextResult {
#[serde(skip_serializing_if = "Option::is_none")]
pub end: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub events_after: Vec<Raw<AnyTimelineEvent>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub events_before: Vec<Raw<AnyTimelineEvent>>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub profile_info: BTreeMap<OwnedUserId, UserProfile>,
#[serde(skip_serializing_if = "Option::is_none")]
pub start: Option<String>,
}
impl EventContextResult {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.end.is_none()
&& self.events_after.is_empty()
&& self.events_before.is_empty()
&& self.profile_info.is_empty()
&& self.start.is_none()
}
}
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Grouping {
pub key: Option<GroupingKey>,
}
impl Grouping {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.key.is_none()
}
}
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, StringEnum)]
#[ruma_enum(rename_all = "snake_case")]
#[non_exhaustive]
pub enum GroupingKey {
RoomId,
Sender,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
#[derive(Clone, Default, Debug, Incoming, Serialize)]
#[incoming_derive(Default)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Groupings<'a> {
#[serde(default, skip_serializing_if = "<[_]>::is_empty")]
pub group_by: &'a [Grouping],
}
impl Groupings<'_> {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.group_by.is_empty()
}
}
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, Debug, PartialEq, Eq, StringEnum)]
#[non_exhaustive]
pub enum SearchKeys {
#[ruma_enum(rename = "content.body")]
ContentBody,
#[ruma_enum(rename = "content.name")]
ContentName,
#[ruma_enum(rename = "content.topic")]
ContentTopic,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
#[derive(Clone, Debug, PartialEq, Eq, StringEnum)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_enum(rename_all = "snake_case")]
pub enum OrderBy {
Recent,
Rank,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct ResultCategories {
#[serde(default, skip_serializing_if = "ResultRoomEvents::is_empty")]
pub room_events: ResultRoomEvents,
}
impl ResultCategories {
pub fn new() -> Self {
Default::default()
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct ResultRoomEvents {
#[serde(skip_serializing_if = "Option::is_none")]
pub count: Option<UInt>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub groups: BTreeMap<GroupingKey, BTreeMap<OwnedRoomIdOrUserId, ResultGroup>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub next_batch: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub results: Vec<SearchResult>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub state: BTreeMap<OwnedRoomId, Vec<Raw<AnyStateEvent>>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub highlights: Vec<String>,
}
impl ResultRoomEvents {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.count.is_none()
&& self.groups.is_empty()
&& self.next_batch.is_none()
&& self.results.is_empty()
&& self.state.is_empty()
&& self.highlights.is_empty()
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct ResultGroup {
#[serde(skip_serializing_if = "Option::is_none")]
pub next_batch: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub order: Option<UInt>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub results: Vec<OwnedEventId>,
}
impl ResultGroup {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.next_batch.is_none() && self.order.is_none() && self.results.is_empty()
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct SearchResult {
#[serde(skip_serializing_if = "EventContextResult::is_empty")]
pub context: EventContextResult,
#[serde(skip_serializing_if = "Option::is_none")]
pub rank: Option<UInt>,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Raw<AnyTimelineEvent>>,
}
impl SearchResult {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.context.is_empty() && self.rank.is_none() && self.result.is_none()
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct UserProfile {
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "compat",
serde(default, deserialize_with = "ruma_common::serde::empty_string_as_none")
)]
pub avatar_url: Option<OwnedMxcUri>,
#[serde(skip_serializing_if = "Option::is_none")]
pub displayname: Option<String>,
}
impl UserProfile {
pub fn new() -> Self {
Default::default()
}
pub fn is_empty(&self) -> bool {
self.avatar_url.is_none() && self.displayname.is_none()
}
}
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[allow(clippy::exhaustive_enums)]
pub enum OwnedRoomIdOrUserId {
RoomId(OwnedRoomId),
UserId(OwnedUserId),
}
}