Skip to content

Commit

Permalink
Bump matrix-sdk dependency to 0.8.
Browse files Browse the repository at this point in the history
This accommodates recent protocol changes and addresses #378.
  • Loading branch information
dstu committed Dec 9, 2024
1 parent 9a9bdb4 commit d8b8f23
Show file tree
Hide file tree
Showing 8 changed files with 576 additions and 382 deletions.
857 changes: 513 additions & 344 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ version = "0.0.20"
#rev = "24f3ec11c7f634005a27b26878d0fbbdcc08f272"

[dependencies.matrix-sdk]
version = "0.7.1"
version = "0.8.0"
default-features = false
features = ["e2e-encryption", "sqlite", "sso-login"]

Expand Down
4 changes: 2 additions & 2 deletions src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,9 +1096,9 @@ impl Message {
let padding = user_gutter - 2 - width;

let sender = if align_right {
space(padding) + &truncated + " "
format!("{}{} ", space(padding), truncated)
} else {
truncated.into_owned() + &space(padding) + " "
format!("{}{} ", truncated, space(padding))
};

Span::styled(sender, style).into()
Expand Down
44 changes: 26 additions & 18 deletions src/notifications.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::time::SystemTime;

use matrix_sdk::{
deserialized_responses::RawAnySyncOrStrippedTimelineEvent,
notification_settings::{IsEncrypted, IsOneToOne, NotificationSettings, RoomNotificationMode},
room::Room as MatrixRoom,
ruma::{
api::client::push::get_notifications::v3::Notification,
events::{room::message::MessageType, AnyMessageLikeEventContent, AnySyncTimelineEvent},
serde::Raw,
MilliSecondsSinceUnixEpoch,
RoomId,
},
Expand Down Expand Up @@ -53,21 +54,28 @@ pub async fn register_notifications(
return;
}

match parse_notification(notification, room, show_message).await {
Ok((summary, body, server_ts)) => {
if server_ts < startup_ts {
return;
}
match notification.event {
RawAnySyncOrStrippedTimelineEvent::Sync(e) =>
match parse_full_notification(e, room, show_message).await {
Ok((summary, body, server_ts)) => {
if server_ts < startup_ts {
return;
}

if is_missing_mention(&body, mode, &client) {
return;
}
if is_missing_mention(&body, mode, &client) {
return;
}

send_notification(&notify_via, &store, &summary, body.as_deref()).await;
},
Err(err) => {
tracing::error!("Failed to extract notification data: {err}")
},
send_notification(&notify_via, &store, &summary, body.as_deref()).await;
},
Err(err) => {
tracing::error!("Failed to extract notification data: {err}")
}
}
// Stripped events may be dropped silently because they're
// only relevant if we're not in a room, and we presumably
// don't want notifications for rooms we're not in.
RawAnySyncOrStrippedTimelineEvent::Stripped(_) => (),
}
}
})
Expand Down Expand Up @@ -171,12 +179,12 @@ async fn is_visible_room(store: &AsyncProgramStore, room_id: &RoomId) -> bool {
is_focused(&locked) && is_open(&mut locked, room_id)
}

pub async fn parse_notification(
notification: Notification,
pub async fn parse_full_notification(
event: Raw<AnySyncTimelineEvent>,
room: MatrixRoom,
show_body: bool,
) -> IambResult<(String, Option<String>, MilliSecondsSinceUnixEpoch)> {
let event = notification.event.deserialize().map_err(IambError::from)?;
let event = event.deserialize().map_err(IambError::from)?;

let server_ts = event.origin_server_ts();

Expand All @@ -188,7 +196,7 @@ pub async fn parse_notification(
.and_then(|m| m.display_name())
.unwrap_or_else(|| sender_id.localpart());

let summary = if let Ok(room_name) = room.display_name().await {
let summary = if let Ok(room_name) = room.compute_display_name().await {
format!("{sender_name} in {room_name}")
} else {
sender_name.to_string()
Expand Down
4 changes: 2 additions & 2 deletions src/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use matrix_sdk::{
media::{MediaFormat, MediaRequest},
media::{MediaFormat, MediaRequestParameters},
ruma::{
events::{
room::{
Expand Down Expand Up @@ -157,7 +157,7 @@ async fn download_or_load(
},
Err(_) => {
media
.get_media_content(&MediaRequest { source, format: MediaFormat::File }, true)
.get_media_content(&MediaRequestParameters { source, format: MediaFormat::File }, true)
.await
.and_then(|buffer| {
if let Err(err) =
Expand Down
4 changes: 2 additions & 2 deletions src/windows/room/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use url::Url;

use matrix_sdk::{
attachment::AttachmentConfig,
media::{MediaFormat, MediaRequest},
media::{MediaFormat, MediaRequestParameters},
room::Room as MatrixRoom,
ruma::{
events::reaction::ReactionEventContent,
Expand Down Expand Up @@ -276,7 +276,7 @@ impl ChatState {
}

if !filename.exists() || flags.contains(DownloadFlags::FORCE) {
let req = MediaRequest { source, format: MediaFormat::File };
let req = MediaRequestParameters { source, format: MediaFormat::File };

let bytes =
media.get_media_content(&req, true).await.map_err(IambError::from)?;
Expand Down
4 changes: 2 additions & 2 deletions src/windows/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use matrix_sdk::{
OwnedUserId,
RoomId,
},
DisplayName,
RoomState as MatrixRoomState,
RoomDisplayName,
};

use ratatui::{
Expand Down Expand Up @@ -139,7 +139,7 @@ impl RoomState {
pub fn new(
room: MatrixRoom,
thread: Option<OwnedEventId>,
name: DisplayName,
name: RoomDisplayName,
tags: Option<Tags>,
store: &mut ProgramStore,
) -> Self {
Expand Down
39 changes: 28 additions & 11 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::time::{Duration, Instant};

use futures::{stream::FuturesUnordered, StreamExt};
use gethostname::gethostname;
use matrix_sdk::deserialized_responses::{DecryptedRoomEvent, TimelineEventKind};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio::sync::Semaphore;
use tokio::task::JoinHandle;
Expand All @@ -24,6 +25,7 @@ use matrix_sdk::{
encryption::verification::{SasVerification, Verification},
encryption::{BackupDownloadStrategy, EncryptionSettings},
event_handler::Ctx,
deserialized_responses::DisplayName,
matrix_auth::MatrixSession,
reqwest,
room::{Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
Expand Down Expand Up @@ -78,9 +80,9 @@ use matrix_sdk::{
},
Client,
ClientBuildError,
DisplayName,
Error as MatrixError,
RoomMemberships,
RoomDisplayName,
};

use modalkit::errors::UIError;
Expand Down Expand Up @@ -293,10 +295,25 @@ async fn load_older_one(
let mut msgs = vec![];

for ev in chunk.into_iter() {
let msg = match ev.event.deserialize() {
Ok(AnyTimelineEvent::MessageLike(msg)) => msg,
Ok(AnyTimelineEvent::State(_)) => continue,
Err(_) => continue,
let msg: AnyMessageLikeEvent = match ev.kind {
TimelineEventKind::Decrypted(DecryptedRoomEvent { event, .. }) => match event.deserialize() {
Ok(e) => e,
_ => continue,
}
TimelineEventKind::PlainText { event } => match event.deserialize() {
Ok(e) => match e.into_full_event(room_id.to_owned()) {
AnyTimelineEvent::MessageLike(e) => e,
_ => continue,
}
_ => continue,
}
TimelineEventKind::UnableToDecrypt { event, .. } => match event.deserialize() {
Ok(e) => match e.into_full_event(room_id.to_owned()) {
AnyTimelineEvent::MessageLike(e) => e,
_ => continue,
}
_ => continue,
}
};

let event_id = msg.event_id();
Expand Down Expand Up @@ -440,7 +457,7 @@ async fn refresh_rooms(client: &Client, store: &AsyncProgramStore) {
let mut dms = vec![];

for room in client.invited_rooms().into_iter() {
let name = room.display_name().await.unwrap_or(DisplayName::Empty).to_string();
let name = room.compute_display_name().await.unwrap_or(RoomDisplayName::Empty).to_string();
let tags = room.tags().await.unwrap_or_default();

names.push((room.room_id().to_owned(), name));
Expand All @@ -455,7 +472,7 @@ async fn refresh_rooms(client: &Client, store: &AsyncProgramStore) {
}

for room in client.joined_rooms().into_iter() {
let name = room.display_name().await.unwrap_or(DisplayName::Empty).to_string();
let name = room.compute_display_name().await.unwrap_or(RoomDisplayName::Empty).to_string();
let tags = room.tags().await.unwrap_or_default();

names.push((room.room_id().to_owned(), name));
Expand Down Expand Up @@ -603,7 +620,7 @@ fn oneshot<T>() -> (ClientReply<T>, ClientResponse<T>) {
return (reply, response);
}

pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>);
pub type FetchedRoom = (MatrixRoom, RoomDisplayName, Option<Tags>);

pub enum WorkerTask {
Init(AsyncProgramStore, ClientReply<()>),
Expand Down Expand Up @@ -1077,10 +1094,10 @@ impl ClientWorker {
let user_id = ev.state_key;

let ambiguous_name =
ev.content.displayname.as_deref().unwrap_or_else(|| user_id.localpart());
DisplayName::new(ev.content.displayname.as_deref().unwrap_or_else(|| user_id.as_str()));
let ambiguous = client
.store()
.get_users_with_display_name(room_id, ambiguous_name)
.get_users_with_display_name(room_id, &ambiguous_name)
.await
.map(|users| users.len() > 1)
.unwrap_or_default();
Expand Down Expand Up @@ -1346,7 +1363,7 @@ impl ClientWorker {

async fn get_room(&mut self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> {
if let Some(room) = self.client.get_room(&room_id) {
let name = room.display_name().await.map_err(IambError::from)?;
let name = room.compute_display_name().await.map_err(IambError::from)?;
let tags = room.tags().await.map_err(IambError::from)?;

Ok((room, name, tags))
Expand Down

0 comments on commit d8b8f23

Please sign in to comment.