matrix_sdk/room/messages.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
// Copyright 2023 The Matrix.org Foundation C.I.C.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fmt;
use matrix_sdk_common::{debug::DebugStructExt as _, deserialized_responses::TimelineEvent};
use ruma::{
api::{
client::{filter::RoomEventFilter, message::get_message_events},
Direction,
},
assign,
events::AnyStateEvent,
serde::Raw,
uint, RoomId, UInt,
};
/// Options for [`messages`][super::Room::messages].
///
/// See that method and
/// <https://spec.matrix.org/v1.3/client-server-api/#get_matrixclientv3roomsroomidmessages>
/// for details.
#[non_exhaustive]
pub struct MessagesOptions {
/// The token to start returning events from.
///
/// This token can be obtained from a `prev_batch` token returned for each
/// room from the sync API, or from a start or end token returned by a
/// previous `messages` call.
///
/// If `from` isn't provided the homeserver shall return a list of messages
/// from the first or last (per the value of the dir parameter) visible
/// event in the room history for the requesting user.
pub from: Option<String>,
/// The token to stop returning events at.
///
/// This token can be obtained from a `prev_batch` token returned for each
/// room by the sync API, or from a start or end token returned by a
/// previous `messages` call.
pub to: Option<String>,
/// The direction to return events in.
pub dir: Direction,
/// The maximum number of events to return.
///
/// Default: 10.
pub limit: UInt,
/// A [`RoomEventFilter`] to filter returned events with.
pub filter: RoomEventFilter,
}
impl MessagesOptions {
/// Creates `MessagesOptions` with the given direction.
///
/// All other parameters will be defaulted.
pub fn new(dir: Direction) -> Self {
Self { from: None, to: None, dir, limit: uint!(10), filter: RoomEventFilter::default() }
}
/// Creates `MessagesOptions` with `dir` set to `Backward`.
///
/// If no `from` token is set afterwards, pagination will start at the
/// end of (the accessible part of) the room timeline.
pub fn backward() -> Self {
Self::new(Direction::Backward)
}
/// Creates `MessagesOptions` with `dir` set to `Forward`.
///
/// If no `from` token is set afterwards, pagination will start at the
/// beginning of (the accessible part of) the room timeline.
pub fn forward() -> Self {
Self::new(Direction::Forward)
}
/// Creates a new `MessagesOptions` from `self` with the `from` field set to
/// the given value.
///
/// Since the field is public, you can also assign to it directly. This
/// method merely acts as a shorthand for that, because it is very
/// common to set this field.
pub fn from<'a>(self, from: impl Into<Option<&'a str>>) -> Self {
Self { from: from.into().map(ToOwned::to_owned), ..self }
}
pub(super) fn into_request(self, room_id: &RoomId) -> get_message_events::v3::Request {
assign!(get_message_events::v3::Request::new(room_id.to_owned(), self.dir), {
from: self.from,
to: self.to,
limit: self.limit,
filter: self.filter,
})
}
}
#[cfg(not(tarpaulin_include))]
impl fmt::Debug for MessagesOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self { from, to, dir, limit, filter } = self;
let mut s = f.debug_struct("MessagesOptions");
s.maybe_field("from", from).maybe_field("to", to).field("dir", dir).field("limit", limit);
if !filter.is_empty() {
s.field("filter", filter);
}
s.finish()
}
}
/// The result of a [`super::Room::messages`] call.
///
/// In short, this is a possibly decrypted version of the response of a
/// `room/messages` api call.
#[derive(Debug, Default)]
pub struct Messages {
/// The token the pagination starts from.
pub start: String,
/// The token the pagination ends at.
pub end: Option<String>,
/// A list of room events.
pub chunk: Vec<TimelineEvent>,
/// A list of state events relevant to showing the `chunk`.
pub state: Vec<Raw<AnyStateEvent>>,
}
/// The result of a [`super::Room::event_with_context`] query.
///
/// This is a wrapper around
/// [`ruma::api::client::context::get_context::v3::Response`], with events
/// decrypted if needs be.
#[derive(Debug, Default)]
pub struct EventWithContextResponse {
/// The event targeted by the /context query.
pub event: Option<TimelineEvent>,
/// Events before the target event, if a non-zero context size was
/// requested.
///
/// Like the corresponding Ruma response, these are in reverse chronological
/// order.
pub events_before: Vec<TimelineEvent>,
/// Events after the target event, if a non-zero context size was requested.
///
/// Like the corresponding Ruma response, these are in chronological order.
pub events_after: Vec<TimelineEvent>,
/// Token to paginate backwards, aka "start" token.
pub prev_batch_token: Option<String>,
/// Token to paginate forwards, aka "end" token.
pub next_batch_token: Option<String>,
/// State events related to the request.
///
/// If lazy-loading of members was requested, this may contain room
/// membership events.
pub state: Vec<Raw<AnyStateEvent>>,
}