Skip to content

Commit

Permalink
Replace media_events_timeline with a more generic `message_filtered…
Browse files Browse the repository at this point in the history
…_timeline` that can take any of the available RoomMessage message types
  • Loading branch information
stefanceriu committed Dec 3, 2024
1 parent 07f6614 commit 545fd53
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 29 deletions.
38 changes: 37 additions & 1 deletion bindings/matrix-sdk-ffi/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use matrix_sdk::IdParseError;
use matrix_sdk_ui::timeline::TimelineEventItemId;
use ruma::{
events::{
room::{message::Relation, redaction::SyncRoomRedactionEvent},
room::{
message::MessageType as RumaMessageType, message::Relation,
redaction::SyncRoomRedactionEvent,
},
AnySyncMessageLikeEvent, AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent,
MessageLikeEventContent as RumaMessageLikeEventContent, RedactContent,
RedactedStateEventContent, StaticStateEventContent, SyncMessageLikeEvent, SyncStateEvent,
Expand Down Expand Up @@ -356,6 +359,39 @@ impl From<MessageLikeEventType> for ruma::events::MessageLikeEventType {
}
}

#[derive(Debug, PartialEq, Clone, uniffi::Enum)]
pub enum RoomMessageEventMessageType {
Audio,
Emote,
File,
Image,
Location,
Notice,
ServerNotice,
Text,
Video,
VerificationRequest,
Other,
}

impl From<RumaMessageType> for RoomMessageEventMessageType {
fn from(val: ruma::events::room::message::MessageType) -> Self {
match val {
RumaMessageType::Audio { .. } => Self::Audio,
RumaMessageType::Emote { .. } => Self::Emote,
RumaMessageType::File { .. } => Self::File,
RumaMessageType::Image { .. } => Self::Image,
RumaMessageType::Location { .. } => Self::Location,
RumaMessageType::Notice { .. } => Self::Notice,
RumaMessageType::ServerNotice { .. } => Self::ServerNotice,
RumaMessageType::Text { .. } => Self::Text,
RumaMessageType::Video { .. } => Self::Video,
RumaMessageType::VerificationRequest { .. } => Self::VerificationRequest,
_ => Self::Other,
}
}
}

/// Contains the 2 possible identifiers of an event, either it has a remote
/// event id or a local transaction id, never both or none.
#[derive(Clone, uniffi::Enum)]
Expand Down
55 changes: 27 additions & 28 deletions bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ use ruma::{
call::notify,
room::{
avatar::ImageInfo as RumaAvatarImageInfo,
message::{MessageType, RoomMessageEventContentWithoutRelation},
power_levels::RoomPowerLevels as RumaPowerLevels,
MediaSource,
message::RoomMessageEventContentWithoutRelation,
power_levels::RoomPowerLevels as RumaPowerLevels, MediaSource,
},
AnyMessageLikeEventContent, AnySyncTimelineEvent, TimelineEventType,
},
Expand All @@ -35,7 +34,7 @@ use super::RUNTIME;
use crate::{
chunk_iterator::ChunkIterator,
error::{ClientError, MediaInfoError, RoomError},
event::{MessageLikeEventType, StateEventType},
event::{MessageLikeEventType, RoomMessageEventMessageType, StateEventType},
identity_status_change::IdentityStatusChange,
room_info::RoomInfo,
room_member::RoomMember,
Expand All @@ -53,12 +52,6 @@ pub enum Membership {
Knocked,
}

#[derive(Debug, Clone, uniffi::Enum)]
pub enum MediaEventsTimelineFilter {
ImageAndVideo,
FileAndAudio,
}

impl From<RoomState> for Membership {
fn from(value: RoomState) -> Self {
match value {
Expand Down Expand Up @@ -267,32 +260,38 @@ impl Room {
Ok(Timeline::new(timeline))
}

pub async fn media_events_timeline(
/// A timeline instance that can be configured to only include RoomMessage type events
/// and filter those further based on their message type.
///
/// Virtual timeline items will still be provided and the `default_event_filter` will
/// be applied before everything else.
///
/// # Arguments
///
/// * `internal_id_prefix` - An optional String that will be prepended to
/// all the timeline item's internal IDs, making it possible to
/// distinguish different timeline instances from each other.
///
/// * `allowed_message_types` - A list of `RoomMessageEventMessageType` that will
/// be allowed to appear in the timeline
pub async fn message_filtered_timeline(
&self,
filter: MediaEventsTimelineFilter,
internal_id_prefix: Option<String>,
allowed_message_types: Vec<RoomMessageEventMessageType>,
) -> Result<Arc<Timeline>, ClientError> {
let room = &self.inner;
let mut builder = matrix_sdk_ui::timeline::Timeline::builder(&self.inner);

let mut builder = matrix_sdk_ui::timeline::Timeline::builder(room);
if let Some(internal_id_prefix) = internal_id_prefix {
builder = builder.with_internal_id_prefix(internal_id_prefix);
}

builder = builder.event_filter(move |event, room_version_id| {
default_event_filter(event, room_version_id)
&& match event {
AnySyncTimelineEvent::MessageLike(msg) => match msg.original_content() {
Some(AnyMessageLikeEventContent::RoomMessage(content)) => match filter {
MediaEventsTimelineFilter::ImageAndVideo => {
matches!(
content.msgtype,
MessageType::Image(_) | MessageType::Video(_)
)
}
MediaEventsTimelineFilter::FileAndAudio => {
matches!(
content.msgtype,
MessageType::Audio(_) | MessageType::File(_)
)
}
},
Some(AnyMessageLikeEventContent::RoomMessage(content)) => {
allowed_message_types.contains(&content.msgtype.into())
}
_ => false,
},
_ => false,
Expand Down

0 comments on commit 545fd53

Please sign in to comment.