Skip to content

Commit 562f4ba

Browse files
committed
Add setting for matrix sdk media caching
1 parent cfa2f0a commit 562f4ba

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

docs/iamb.5

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ The room to show by default instead of the
133133
.Sy :welcome
134134
window.
135135

136+
.IT Sy cache_policy
137+
Configure how downloaded files and image previews are cached internally.
138+
Specifying this object will replace the upstream defaults and possibly lead to unconstrained cache growth.
139+
Possible keys and their effect are documented at:
140+
.br
141+
.Lk https://docs.rs/matrix-sdk/latest/matrix_sdk/media/struct.MediaRetentionPolicy.html#fields
142+
136143
.It Sy image_preview
137144
Enable image previews and configure it.
138145
An empty object will enable the feature with default settings, omitting it will disable the feature.

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::process;
1212

1313
use clap::Parser;
1414
use matrix_sdk::authentication::matrix::MatrixSession;
15+
use matrix_sdk::media::MediaRetentionPolicy;
1516
use matrix_sdk::ruma::{OwnedDeviceId, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, UserId};
1617
use ratatui::style::{Color, Modifier as StyleModifier, Style};
1718
use ratatui::text::Span;
@@ -584,6 +585,7 @@ pub struct TunableValues {
584585
pub user_gutter_width: usize,
585586
pub external_edit_file_suffix: String,
586587
pub tabstop: usize,
588+
pub cache_policy: MediaRetentionPolicy,
587589
}
588590

589591
#[derive(Clone, Default, Deserialize)]
@@ -612,6 +614,7 @@ pub struct Tunables {
612614
pub user_gutter_width: Option<usize>,
613615
pub external_edit_file_suffix: Option<String>,
614616
pub tabstop: Option<usize>,
617+
pub cache_policy: Option<MediaRetentionPolicy>,
615618
}
616619

617620
impl Tunables {
@@ -646,6 +649,7 @@ impl Tunables {
646649
.external_edit_file_suffix
647650
.or(other.external_edit_file_suffix),
648651
tabstop: self.tabstop.or(other.tabstop),
652+
cache_policy: self.cache_policy.or(other.cache_policy),
649653
}
650654
}
651655

@@ -676,6 +680,7 @@ impl Tunables {
676680
.external_edit_file_suffix
677681
.unwrap_or_else(|| ".md".to_string()),
678682
tabstop: self.tabstop.unwrap_or(4),
683+
cache_policy: self.cache_policy.unwrap_or_default(),
679684
}
680685
}
681686
}

src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub fn mock_tunables() -> TunableValues {
200200
image_preview: None,
201201
user_gutter_width: 30,
202202
tabstop: 4,
203+
cache_policy: Default::default(),
203204
}
204205
}
205206

src/worker.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::time::{Duration, Instant};
1313

1414
use futures::{stream::FuturesUnordered, StreamExt};
1515
use gethostname::gethostname;
16-
use matrix_sdk::media::MediaRetentionPolicy;
1716
use matrix_sdk::ruma::events::room::MediaSource;
1817
use ratatui_image::picker::Picker;
1918
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
@@ -748,13 +747,7 @@ pub async fn create_client(settings: &ApplicationSettings) -> Client {
748747

749748
client
750749
.media()
751-
.set_media_retention_policy(
752-
MediaRetentionPolicy::new()
753-
// 4 GiB.
754-
.with_max_cache_size(Some(4 * 1024 * 1024 * 1024))
755-
// 200 MiB.
756-
.with_max_file_size(Some(200 * 1024 * 1024)),
757-
)
750+
.set_media_retention_policy(settings.tunables.cache_policy)
758751
.await
759752
.expect("Failed to set cache policy");
760753

0 commit comments

Comments
 (0)