Compare commits

...

594 Commits

Author SHA1 Message Date
Damir Jelić 4c46e42201 chore: Release matrix-sdk version 0.10.0 2025-02-04 16:32:55 +01:00
Damir Jelić 0d4bc65e28 chore: Enable releases for the test crates 2025-02-04 16:32:55 +01:00
Jorge Martín 5e1bae02fe feat(ffi): Add RoomPreview::forget action in the FFI layer 2025-02-04 16:26:15 +01:00
Ivan Enderlin 77a67de7df fix(ui): Fix performance of ReadReceiptTimelineUpdate::apply.
This patch improves the performance of
`ReadReceiptTimelineUpdate::apply`, which does 2 things: it calls
`remove_old_receipt` and `add_new_receipt`. Both of them need an
timeline item position. Until this patch, `rfind_event_by_id` was used
and was the bottleneck. The improvement is twofold as is as follows.

First off, when collecting data to create `ReadReceiptTimelineUpdate`,
the timeline item position can be known ahead of time by using
`EventMeta::timeline_item_index`. This data is not always available, for
example if the timeline item isn't created yet. But let's try to collect
these data if there are some.

Second, inside `ReadReceiptTimelineUpdate::remove_old_receipt`, we use
the timeline item position collected from `EventMeta` if it exists.
Otherwise, let's fallback to a similar `rfind_event_by_id` pattern,
without using intermediate types. It's more straightforward here: we
don't need an `EventTimelineItemWithId`, we only need the position.
Once the position is known, it is stored in `Self` (!), this is the
biggest improvement here. Le't see why.

Finally, inside `ReadReceiptTimelineUpdate::add_new_receipt`, we use
the timeline item position collected from `EventMeta` if it exists,
similarly to what `remove_old_receipt` does. Otherwise, let's fallback
to an iterator to find the position. However, instead of iterating over
**all** items, we can skip the first ones, up to the position of the
timeline item holding the old receipt, so up to the position found by
`remove_old_receipt`.

I'm testing this patch with the `test_lazy_back_pagination` test in
https://github.com/matrix-org/matrix-rust-sdk/pull/4594. With 10_000
events in the sync, the `ReadReceipts::maybe_update_read_receipt` method
was taking 52% of the whole execution time. With this patch, it takes
8.1%.
2025-02-04 16:02:29 +01:00
JoFrost f27eb4d1c8 fix[oidc]: fix docstring in oidc module 2025-02-04 15:33:28 +01:00
Jorge Martín 05814c5559 refactor(ffi): Map client API errors to ClientError::MatrixApi, containing the error kind, their error code and the associated message 2025-02-04 12:25:51 +01:00
Kévin Commaille d5d9898fb4 feat: Upgrade Ruma to 0.12.1
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-02-04 12:00:40 +01:00
Ivan Enderlin 3f71d9a379 fix(sdk): Improve performance of RoomEvents::maybe_apply_new_redaction.
This patch improves the performance of
`RoomEvents::maybe_apply_new_redaction`. This method deserialises
all the events it receives, entirely. If the event is not an
`m.room.redaction`, then the method returns early. Most of the time,
the event is deserialised for nothing because most events aren't of kind
`m.room.redaction`!

This patch first uses `Raw::get_field("type")` to detect the type of
the event. If it's a `m.room.redaction`, then the event is entirely
deserialized, otherwise the method returns.

When running the `test_lazy_back_pagination` from
https://github.com/matrix-org/matrix-rust-sdk/pull/4594 with 10'000
events, prior to this patch, this method takes 11% of the execution
time. With this patch, this method takes 2.5%.
2025-02-04 09:49:58 +01:00
Jorge Martín b077f45e78 test(room_preview): Add tests for where get_room_preview gets its data from for each room state 2025-02-04 09:33:31 +01:00
Jorge Martín 648d527f2f fix(room_preview): Return room preview info based on local data for banned rooms too
Any remote endpoint would just return a `403` status code so we have no other choice than trusting the local room info we already have.
2025-02-04 09:33:31 +01:00
Jorge Martín 8513547e92 feat(ffi): Add FFI bindings for Room::forget.
Also make sure rooms the user has been banned from can also be forgotten, not only left ones.
2025-02-03 19:48:27 +01:00
dependabot[bot] d18669e8d9 chore(deps): bump crate-ci/typos from 1.29.4 to 1.29.5
Bumps [crate-ci/typos](https://github.com/crate-ci/typos) from 1.29.4 to 1.29.5.
- [Release notes](https://github.com/crate-ci/typos/releases)
- [Changelog](https://github.com/crate-ci/typos/blob/master/CHANGELOG.md)
- [Commits](https://github.com/crate-ci/typos/compare/v1.29.4...v1.29.5)

---
updated-dependencies:
- dependency-name: crate-ci/typos
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-02-03 16:52:27 +01:00
Benjamin Bouvier a0426251a3 test(timeline): test that editing a replied-to doesn't lose the latest edit JSON 2025-02-03 16:52:12 +01:00
Benjamin Bouvier 2739c5bf27 test(timeline): test that adding a response or ending a poll doesn't clear the latest edit JSON 2025-02-03 16:52:12 +01:00
Benjamin Bouvier 381f4d419f fix(timeline): don't clear the latest_edit_json under certain conditions 2025-02-03 16:52:12 +01:00
Ivan Enderlin 9ab5547065 fix(ui): Fix performance of AllRemoteEvents::(in|de)crement_all_timeline_item_index_after.
This patch fixes the performance of
`AllRemoteEvents::increment_all_timeline_item_index_after` and
`decrment_all_timeline_item_index_after`.

It appears that the code was previously iterating over all items. This
is a waste of time. This patch updates the code to iterate over all
items in reverse order:

- if `new_timeline_item_index` is 0, we need to shift all items anyways,
  so all items must be traversed, the iterator direction doesn't matter,
- otherwise, it's unlikely we want to traverse all items: the item has
  been either inserted or pushed back, so there is no need to traverse
  the first items; we can also break the iteration as soon as all
  timeline item index after `new_timeline_item_index` has been updated.

I'm testing this patch with the `test_lazy_back_pagination` test in
https://github.com/matrix-org/matrix-rust-sdk/pull/4594. With 10_000
events in the sync, the `ObservableItems::push_back` method (that uses
`AllRemoteEvents::increment_all_timeline_item_index_after`) was taking
7% of the whole execution time. With this patch, it takes 0.7%.
2025-02-03 11:25:48 +01:00
Kévin Commaille df3cb002a5 chore: Add changelog entries
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-02-03 11:22:23 +01:00
Kévin Commaille 6ebd4295b9 feat(sqlite): Limit size of WAL file
The WAL file can grow depending on the transactions that are run. A
critical case is VACUUM which basically writes the content of the DB
file to the WAL file before writing it back to the DB file.

SQLite doesn't try to reduce the size of the file after that unless we
set an explicit limit,
so we could end up taking twice the size of the database on the
filesystem.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-02-03 11:22:23 +01:00
Kévin Commaille c5104d68fd feat(sqlite): Run PRAGMA optimize regularly
As recommended by the SQLite docs.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-02-03 11:22:23 +01:00
Kévin Commaille 0064839283 fix(sqlite): Vaccum the SqliteStateStore
It should have been done in the migration of version 7, to reduce the
size of the database on the filesystem after the media cache was moved
to the SqliteEventCacheStore. Better late than never.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-02-03 11:22:23 +01:00
Kévin Commaille 2727d72916 fix(timeline): Do not filter out own receipts in load_read_receipts_for_event
Fixes #4517.

It turns out that the bugs found in that test were due to 2 causes:

- First commit: `TestRoomDataProvider` didn't use `initial_user_receipts` but returned hardcoded values.
- Second commit: Our own read receipts were ignored in `TimelineStateTransaction::load_read_receipts_for_event`, although we need to process all read receipts via `ReadReceipts::maybe_update_read_receipt` because it knows how to filter out our own read receipts were needed.
2025-02-03 10:15:25 +00:00
Ivan Enderlin 33a2cc3031 chore(cargo): Bump the minimum stable rust version (MSRV). 2025-02-03 10:27:45 +01:00
Ivan Enderlin 38097f90b2 fix(ui): Fix performance of TimelineEventHandler::deduplicate_local_timeline_item.
This patch drastically improves the performance of
`TimelineEventHandler::deduplicate_local_timeline_item`.

Before this patch, `rfind_event_item` was used to iterate over all
timeline items: for each item in reverse order, if it was an event
timeline item, and if it was a local event timeline item, and if it was
matching the event ID or transaction ID, then a duplicate was found.

The problem is the following: all items are traversed.

However, local event timeline items are always at the back of the items.
Even virtual timeline items are before local event timeline items. Thus,
it is not necessary to traverse all items. It is possible to stop the
iteration as soon as (i) a non event timeline item is met, or (ii) a non
local event timeline item is met.

This patch updates
`TimelineEventHandler::deduplicate_local_timeline_item` to replace to
use of `rfind_event_item` by a custom iterator that stops as soon as a
non event timeline item, or a non local event timeline item, is met, or
—of course— when a local event timeline item is a duplicate.

To do so, [`Iterator::try_fold`] is probably the best companion.
[`Iterator::try_find`] would have been nice, but it is available on
nightlies, not on stable versions of Rust. However, many methods in
`Iterator` are using `try_fold`, like `find` or any other methods that
need to do a “short-circuit”. Anyway, `try_fold` works pretty nice here,
and does exactly what we need.

Our use of `try_fold` is to return a `ControlFlow<Option<(usize,
TimelineItem)>, ()>`. After `try_fold`, we call
`ControlFlow::break_value`, which returns an `Option`. Hence the need
to call `Option::flatten` at the end to get a single `Option` instead of
having an `Option<Option<(usize, TimelineItem)>>`.

I'm testing this patch with the `test_lazy_back_pagination` test in
https://github.com/matrix-org/matrix-rust-sdk/pull/4594. With 10_000
events in the sync, the test was taking 13s to run on my machine. With
this patch, it takes 10s to run. It's a 23% improvement. This
`deduplicate_local_timeline_item` method was taking a large part of the
computation according to the profiler. With this patch, this method is
barely visible in the profiler it is so small.

[`Iterator::try_fold`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.try_fold
[`Iterator::try_find`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.try_find
2025-02-03 10:27:45 +01:00
Ivan Enderlin 47d08683a2 fix(security): Update OpenSSL.
See this note https://rustsec.org/advisories/RUSTSEC-2025-0004.

This patch updates OpenSSL to 0.10.70.
2025-02-03 09:42:58 +01:00
Damir Jelić 57919f5480 chore: Bump most of our deps 2025-01-31 17:14:37 +01:00
Damir Jelić b8949cfe26 chore: Bump vodozemac 2025-01-31 17:14:37 +01:00
Damir Jelić 8d27b0c811 test: Simplify some tests using the assert_next_with_timeout macro 2025-01-31 14:15:18 +01:00
Damir Jelić 3707d2fb81 test: Make the timeout parameter in the assert_next_with_timeout macro optional 2025-01-31 14:15:18 +01:00
Damir Jelić eaaa5e17a0 chore: Fix a doc example in the MatrixMockServer 2025-01-31 14:15:18 +01:00
Ivan Enderlin e3958b754c chore(crypto-ffi): Done is a unit type, no need for { .. }. 2025-01-31 14:07:43 +01:00
Ivan Enderlin 78d9e1292f chore(sdk): Do not iterate over the entire iterator when we can reach back.
This patch uses `next_back()` instead of `last()`, which is equivalent
but `last()` requires to iterate over the entire iterator, while
`next_back()` is a single operation.
2025-01-31 14:07:43 +01:00
Ivan Enderlin d594b4dad7 chore(sdk): Remove a useless type conversion.
This patch removes a useless type conversion. The `Room::event()` method
returns a `TimelineEvent`, so calling `Into::into` is useless: we map
`TimelineEvent` to `TimelineEvent`.
2025-01-31 14:07:43 +01:00
Ivan Enderlin 3f40ad83a5 chore(sdk): Remove a useless type conversion.
This patch removes a useless type conversion. The iterator produces
`TimelineEvent`, so mapping to `TimelineEvent::from` is useless: we map
`TimelineEvent` to `TimelineEvent`.
2025-01-31 14:07:43 +01:00
Ivan Enderlin 5049d1a3b6 chore(sqlite): Use repeat_n(…, n) instead of repeat(…).take(n).
Thanks Clippy!
2025-01-31 14:07:43 +01:00
Damir Jelić 29862fc9bd refactor: Add the assert_next_eq_with_timeout test helper
This test helper is the same as the assert_next_eq helper, but it waits
for the stream to be ready for a certain amount of time instead of
expecting it to be ready right away.
2025-01-31 09:58:55 +01:00
Damir Jelić 585224b2fa chore(ui): Replace the unit type with an empty block 2025-01-31 09:58:55 +01:00
Damir Jelić 0dc5e69ace fix: Retry the sync even in case of network errors 2025-01-31 09:58:55 +01:00
Damir Jelić b323802ab0 fix(ui): Enable retries for network failures of the /versions check in the SyncService 2025-01-31 09:58:55 +01:00
Damir Jelić 252786d2ef refactor(ui): Make SyncService::stop infallible
The `SyncService::stop()` method could fail for the following reasons:

1. The supervisor was not properly started up, this is a programmer error.
2. The supervisor task wouldn't shut down and instead it returns a JoinError.
3. We couldn't notify the supervisor task that it should shutdown due the channel being closed.

All of those cases shouldn't ever happen and the supervisor task will be
stopped in all of them.

1. Since there is no supervisor to be stopped, we can safely just log an
   error, our tests ensure that a `SyncService::start()` does create a
   supervisor.

2. A JoinError can be returned if the task has been cancelled or if the
   supervisor task has panicked. Since we never cancel the task, nor
   have any panics in the supervisor task, we can assume that this won't
   happen.

3. The supervisor task holds on to a reference to the receiving end of
   the channel, as long as the task is alive the channel can not be
   closed.

In conclusion, it doesn't seem to be useful to forward these error cases
to the user.
2025-01-31 09:58:55 +01:00
Damir Jelić 97cbe57d3f docs: Document the offline mode for the SyncService 2025-01-31 09:58:55 +01:00
Damir Jelić 0d8ad159c3 test(ui): Write tests for the SyncService offline mode 2025-01-31 09:58:55 +01:00
Damir Jelić 9d732395ce feat(ui): Introduce a "offline" mode for the SyncService 2025-01-31 09:58:55 +01:00
Damir Jelić 6a772d1c56 test: Add a method to mock the /versions endpoint to the MatrixMockServer 2025-01-31 09:58:55 +01:00
Damir Jelić b71499ffe6 refactor(ui): Move the start/stop implementations under the inner SyncService
This will allow us to more easily implement a restart method.
2025-01-31 09:58:55 +01:00
Damir Jelić 4dadf8581a refactor(ui): Move the creation of the child tasks in the SyncService around
This patch moves the creations of the child tasks of the SyncService
into the supervisor tasks itself. This should make it easier to let the
supervisor recreate tasks.

This will become useful once we introduce a offline mode where the
supervisor task becomes responsible to restart syncing once we notice
that the server is back online.
2025-01-31 09:58:55 +01:00
Benjamin Bouvier 4d4cd61363 chore: update copyright years for files newly introduced 2025-01-31 08:50:41 +01:00
Richard van der Hoff 6f42b0a67b crypto: withhold outgoing messages to unsigned dehydrated devices
Per https://github.com/matrix-org/matrix-rust-sdk/issues/4313, we should not
send outgoing messages to dehydrated devices that are not signed by the current
pinned/verified identity.
2025-01-29 17:37:36 +00:00
Richard van der Hoff e3b348761e test: test helpers in share_strategy tests
Split the existing `set_up_test_machine` into two parts, so we can set up
the test OlmMachine without importing data for other users
2025-01-29 17:37:36 +00:00
Ivan Enderlin d755a8a3aa chore(ui): Move EventMeta inside the metadata module.
This patch moves the `EventMeta` type from `state.rs` to `metadata.rs`.
2025-01-29 11:44:27 +01:00
Ivan Enderlin 66e3ddec47 chore(ui): Move TimelineMetadata into its own module.
This patch moves `TimelineMetadata`, its implementation and companion
types (like `RelativePosition`) into its own module. The idea is to
reduce the size of the `state.rs` module.
2025-01-29 11:44:27 +01:00
Ivan Enderlin 720d443452 chore(ui): Move TimelineStateTransaction into its own module.
This patch moves `TimelineStateTransaction` and its implementation into
its own module. The idea is to reduce the size of the `state.rs` module.
2025-01-29 11:44:27 +01:00
Ivan Enderlin 33d691a58e Merge pull request #4571 from zecakeh/media-retention-policy
feat: Add MediaRetentionPolicy to the EventCacheStore, take 2
2025-01-28 17:27:02 +01:00
Kévin Commaille c2f39c1086 Merge branch 'main' into media-retention-policy 2025-01-28 16:53:52 +01:00
Kévin Commaille df9c355aed Merge branch 'main' into media-retention-policy
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:46:55 +01:00
Kévin Commaille 0334ff3f64 chore(sdk): Add changelog entry for MediaRetentionPolicy
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:44:44 +01:00
Kévin Commaille 8262726369 chore(sqlite): Add changelog entry for EventCacheStore changes
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:44:44 +01:00
Kévin Commaille a4a6bf540d chore(base): Add changelog entry for MediaRetentionPolicy and associated changes
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:44:44 +01:00
Kévin Commaille 9b38f38aea fix(base): Organize changelog the same way as other crates
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:44:44 +01:00
Kévin Commaille 2e05cc74bf feat(sdk): Add methods to Media to interact with MediaRetentionPolicy
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:44:44 +01:00
Kévin Commaille eb9b86971a feat(base): Add methods for MediaRetentionPolicy to EventCacheStore
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:44:42 +01:00
Ivan Enderlin 31e7ec182c chore(ui): Remove TimelineNewItemPosition.
This patch removes the `TimelineNewItemPosition` type a sit is no longer
used.
2025-01-28 15:43:06 +01:00
Ivan Enderlin 5e8f3b2bc8 chore(ui): Remove HandleManyEventsResult.
This patch removes the `HandleManyEventsResult` type as it is no longer
used.
2025-01-28 15:43:06 +01:00
Ivan Enderlin c0b91c4b0e chore(ui): Remove TimelineState(Transaction)::add_remote_events_at.
This patch removes `TimelineState::add_remote_events_at` and
`TimelineStateTransaction::add_remote_events_at` as they are no longer
used.
2025-01-28 15:43:06 +01:00
Ivan Enderlin 46d90afa9c refactor(ui): Use handle_remote_events_with_diffs instead of add_remote_events.
This patch replaces a call to `add_remote_events` by
`handle_remote_events_with_diffs`.

The idea is to remove all calls to `add_remote_events`.
2025-01-28 15:43:06 +01:00
Ivan Enderlin 29e19b729b chore(ui): Remove TimelineController::add_events_at.
This patch removes `TimelineController::add_events_at` since it's a
non-public method and it's unused.
2025-01-28 15:43:06 +01:00
Ivan Enderlin 20c1eff391 refactor(ui): The Timeline no longer use add_events_at.
This patch replaces all uses of `TimelineController::add_events_at` by
`TimelineController::handle_remote_events_with_diffs` in the `Timeline`
itself.

The idea is to remove `add_events_at`, as we currently have 2 ways to
add or to handle remote events in the `Timeline`. We want a single one,
because it's simpler!
2025-01-28 15:43:06 +01:00
Ivan Enderlin 3e40db3d7f test(ui): Tests no longer use add_events_at.
This patch replaces all uses of `TimelineController::add_events_at` by
`TimelineController::handle_remote_events_with_diffs` in the tests.

The idea is to remove `add_events_at`, as we currently have 2 ways to
add or to handle remote events in the `Timeline`. We want a single one,
because it's simpler!
2025-01-28 15:43:06 +01:00
Kévin Commaille 8ca5983093 feat(sqlite): Implement EventCacheStoreMedia for SqliteEventCacheStore
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:38:18 +01:00
Kévin Commaille 144f568a5c feat(base): Implement EventCacheStoreMedia for MemoryStore
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:37:30 +01:00
Kévin Commaille 34c7dd48ae refactor(base): Use struct for media content in the MemoryStore
It is already a 3-tuple and we want to add more data so it will be clearer to use a stuct with named fields.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:36:16 +01:00
Kévin Commaille 6c7d8c16bb feat(base): Add macro for integration tests of EventCacheStoreMedia
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:36:16 +01:00
Kévin Commaille 2c930df8aa feat(base): Add MediaService
This is an API to handle the MediaRetentionPolicy with a lower level
EventCacheStoreMedia trait.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:36:06 +01:00
Kévin Commaille 834bed2b1a feat(base): Add MediaRetentionPolicy
This will be used as a configuration to decide whether or not to keep
media in the cache, allowing to do periodic cleanups to avoid to have
the size of the media cache grow indefinitely.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-28 15:35:26 +01:00
Valere 8d530ef220 Merge pull request #4558 from matrix-org/valere/memory_store_consistent_with_other_stores
refactor(crypto): Make memory store behave more like other stores
2025-01-28 15:27:03 +01:00
Valere 542d68dcda fix(test): Properly set up test crypto memory store by saving own device 2025-01-28 15:02:30 +01:00
Valere 50696a0d74 refact(mem_store) Add a global save change lock similar to other stores 2025-01-28 15:02:30 +01:00
Valere 182fc6fd8f refact(mem_store): Ser/Deser inbound group sessions 2025-01-28 15:02:30 +01:00
Valere fe85cddf88 refact(mem_store): Serialize/Deserialise olm sessions 2025-01-28 15:02:30 +01:00
Valere 9ff3761cac refact(mem_store): Serialize account and cache static account data 2025-01-28 15:02:30 +01:00
Valere a311dcbd3e refact(mem_store): Replace unwrap with expect 2025-01-28 15:02:30 +01:00
Andy Balaam 447bd67fe1 feat(crypto): Ignore to-device messages from dehydrated devices 2025-01-28 12:57:30 +00:00
Andy Balaam d8ba2b521c refactor(crypto): extract a method from a test that I will re-use later 2025-01-28 12:57:30 +00:00
Damir Jelić 8de15429fb chore: Fix a typo 2025-01-28 12:48:55 +01:00
Damir Jelić 3f398d8934 chore(ui): Move the SyncService stop logic out of the State::Running branch 2025-01-28 12:48:55 +01:00
Damir Jelić f8ec957193 doc(ui): Reword the doc comment for the is_supervisor_task_running method 2025-01-28 12:48:55 +01:00
Damir Jelić 7cc121ab38 docs(ui): Clarify that the supervisor encapsulates the child tasks in its own task 2025-01-28 12:48:55 +01:00
Damir Jelić 8a4918309a refactor(ui): Rename the abortion sender in the SyncService
Termination aligns better with the existing terminology.
2025-01-28 12:48:55 +01:00
Damir Jelić 30d7fac927 refactor(ui): Remove some unneeded references from the SyncServiceInner 2025-01-28 12:48:55 +01:00
Damir Jelić be71c6df56 docs: Document that the SyncService requires MSC4186 2025-01-28 12:48:55 +01:00
Damir Jelić 06ad67f99c docs(ui): Polish the documentation of the SyncService a bit 2025-01-28 12:48:55 +01:00
Damir Jelić 28fb6f7c27 fix(ui): Shutdown the child tasks if the channel got closed in the supervisor 2025-01-28 12:48:55 +01:00
Damir Jelić 842d32d41b refactor(ui): Prettify the two sync tasks a bit 2025-01-28 12:48:55 +01:00
Damir Jelić b52cf8327a refactor(ui): Remove some early returns from the sync service
Now that the various match branches in the start and stop method of the
SyncService are minimized we can remove the early returns.

This should allow us to more easily add new branches.
2025-01-28 12:48:55 +01:00
Damir Jelić d14526f161 refactor(ui): Move the task spawning functions under the supervisor 2025-01-28 12:48:55 +01:00
Damir Jelić 1b8a6b705c refactor(ui): Move the expiration of the sync services closer to the action 2025-01-28 12:48:55 +01:00
Damir Jelić 7c2b15fe86 refactor(ui): Move the spawning of the child tasks into the supervisor 2025-01-28 12:48:55 +01:00
Damir Jelić 3085f05d51 refactor(ui): Create a Supervisor for the SyncService
The supervisor is defined as two optional fields that are set and
removed at the same time.

This patch converts the two optional fields into a single optional
struct. The fields inside the struct now aren't anymore optional. This
ensures that they are always set and destroyed at the same time.
2025-01-28 12:48:55 +01:00
Damir Jelić 4344e06707 refactor(ui): Rename the scheduler task to supervisor task
From cambridge a scheduler is defined as:
    > someone whose job is to create or work with schedules

While supervisor is defined as:
    > a person whose job is to supervise someone or something

Well ok, that doesn't tell us much, supervise is defined as:
    > to watch a person or activity to make certain that everything is done correctly, safely, etc.:

In conclusion, supervising a task is the more common and better
understood terminology here I would say.
2025-01-28 12:48:55 +01:00
Damir Jelić 173ec75bb3 refactor(ui): Move common data of the SyncService under a lock
Previously we had a lock protecting an empty value, but the logic wants
to protect a bunch of data in the SyncService.

Let's do the usual thing and create a SyncServiceInner which holds the
data and protect that with a lock.
2025-01-28 12:48:55 +01:00
Ivan Enderlin 1d3f8bf898 doc(ui): Update the CHANGELOG. 2025-01-28 09:54:31 +01:00
Ivan Enderlin 5b3b87d3e2 chore(ui): Rename Timeline::subscribe_batched to ::subscribe.
This patch renames `Timeline::subscribe_batched` to
`Timeline::subscribe`. Since the `Timeline::subscribe` method has been
removed because unused, it no longer makes sense to have a “batched”
variant here. Let's simplify things!
2025-01-28 09:54:31 +01:00
Ivan Enderlin 6dc5b33d87 chore(ui): Remove useless trace!.
This patch removes useless `trace!` calls.
2025-01-28 09:54:31 +01:00
Richard van der Hoff 408b843156 test: fix cross-signing in legacy dehydrated device test
We missed a call to `sign_device_keys`. Pull out a test helper to make it more
obvious where this code is coming from.
2025-01-27 17:49:47 +00:00
Ivan Enderlin 0820170261 doc(ui): Update the CHANGELOG. 2025-01-27 17:02:09 +01:00
Ivan Enderlin 254ac6f2ce refactor(ui): Unify the Timeline pagination API.
This patch simplifies the `Timeline` pagination API as follows:

- a unique `paginate_backwards` method (no more
  `focused_paginate_backwards` and `live_paginate_backwards`),
- a unique `paginate_forwards` method (no more
  `focused_paginate_forwards`, the `live` variant was absent).

The idea is to unify pagination by hiding the `live` and `focused` mode.
It was already partially the case with `paginate_backards`, but the
`live` and `focused` variants were also present. I believe it creates
an unnecessary confusion.
2025-01-27 17:02:09 +01:00
Ivan Enderlin 468a7ac883 doc(ffi): Remove useless #[cfg(doc)] imports.
This patch removes 2 useless imports that are behind a `#[cfg(doc)]` but
never used.
2025-01-27 17:02:09 +01:00
Richard van der Hoff 3e610c80e1 Merge pull request #4581 from matrix-org/rav/refactor_share_keys
crypto: refactor the room key sharing strategies
2025-01-27 15:51:04 +00:00
Richard van der Hoff f43edbd31f refactor(crypto): split up split_devices_for_user_for_error_on_verified_user_problem_strategy
to make it easier to grok, I hope
2025-01-27 15:34:43 +00:00
Richard van der Hoff 7c57f2cee4 crypto: split out new device collection strategies
Rather than a bunch of flags on `DeviceBasedStrategy`, separate the strategies
properly.
2025-01-27 15:34:43 +00:00
Richard van der Hoff 8d612eca46 crypto: break up split_devices_for_user
handle the separate flags with separate methods.
2025-01-27 15:34:43 +00:00
Richard van der Hoff 98f4d55aa0 test: check serialization format of DeviceBasedStrategy 2025-01-27 15:34:43 +00:00
Richard van der Hoff 709b09c4ec test: factor out test helpers in share_strategy tests
Some helpers for creating common `EncryptionSettings`
2025-01-27 15:34:43 +00:00
Richard van der Hoff 818876a22e crypto: factor out common code between device collection cases
Firstly, build a `CollectRecipientsResult` as we go, rather than building its
components separately and then assembling it at the end.

Then, factor the common code between the two code paths into a method to update
the `CollectRecipientsResult`.
2025-01-27 15:34:43 +00:00
Stefan Ceriu 2657eb7866 feat(ui): expose a method for checking whether a message contains only emojis and should be boosted (use a bigger font size) (#4577)
- supports only text room message types
- enumerates through their body's grapheme clusters and check that every
single one of them is an emoji
- part of the `LazyTimelineItemProvider` so that it can be opt in
2025-01-27 14:00:01 +00:00
torrybr aaecbf07f2 refactor: dont panic if beacon_info is not found 2025-01-27 11:05:24 +01:00
torrybr f336638a17 refactor: move subscribe into arc 2025-01-27 11:05:24 +01:00
torrybr 839fbe477c feat(beacons): expose ffi functions to start, stop and subscribe 2025-01-27 11:05:24 +01:00
Richard van der Hoff 35ad5441d3 crypto: split common struct out of device collection results
`split_devices_for_user` returns a superset of the results of
`split_recipients_withhelds_for_user_based_on_identity`: let's reflect that in
the return types so we can start to share code.

Also, rename `split_recipients_withhelds_for_user_based_on_identity` to
`split_devices_for_user_for_identity_based_strategy` while we are here.
2025-01-26 22:50:15 +00:00
Kevin Boos 756dec264d Upgrade imbl, eyeball-im, eyeball-im-util to fix bounds check
A bounds check was recently relaxed in `imbl`'s `Focus::narrow()`
function: https://github.com/jneem/imbl/pull/89,
which fixed a bug that would cause a panic if the downstream user
of `matrix-sdk-ui` attempted to narrow a focus of Timeline items
using a range that included the last item in the Timeline.
Example: https://github.com/project-robius/robrix/issues/330

This fix has been incorporated in `eyeball-im` and `eyeball-im-util`
and has been tested by me to no longer trigger upon the aforementioned
conditions.
2025-01-25 02:13:22 -05:00
Doug 87983ab610 chore: Remove an old todo
This was already done by moving the methods into Client.
2025-01-24 18:06:05 +01:00
Neil Johnson 66ffc3448e Update README.md
style

Signed-off-by: Neil Johnson <neil@matrix.org>
2025-01-24 11:20:20 +01:00
Neil Johnson c6e308717d update maturity and contribution call to action 2025-01-24 10:56:05 +01:00
maan2003 4c4dd03411 fix(wasm): don't use tokio::time::{timeout,sleep} (#4573)
Tokio timeout and sleep don't work on wasm so provide alternative versions

---------

Signed-off-by: Manmeet Singh <manmeetmann2003@gmail.com>
Signed-off-by: Andy Balaam <andy.balaam@matrix.org>
Co-authored-by: Andy Balaam <andy.balaam@matrix.org>
2025-01-23 08:57:11 +00:00
Benjamin Bouvier 2d0f873342 refactor: send respects to multiple automated lints and checks 2025-01-22 20:24:48 +01:00
Benjamin Bouvier 041627ec4a test: rename EventFactory::into_sync to into_event 2025-01-22 20:24:48 +01:00
Benjamin Bouvier da4b8004f2 docs: add changelog entries 2025-01-22 20:24:48 +01:00
Benjamin Bouvier 3428494468 refactor: rename SyncTimelineEvent to TimelineEvent 2025-01-22 20:24:48 +01:00
Benjamin Bouvier 0c2046f93b refactor: have SyncTimelineEvent::push_actions be optional 2025-01-22 20:24:48 +01:00
Benjamin Bouvier eb31f035e6 refactor: turn TimelineEvent into SyncTimelineEvent
As the comment noted, they're essentially doing the same thing. A
`TimelineEvent` may not have computed push actions, and in that regard
it seemed more correct than `SyncTimelineEvent`, so another commit will
make the field optional.
2025-01-22 20:24:48 +01:00
Kévin Commaille df51404a14 chore(sdk): Add changelog for move of matrix_auth and oidc
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-22 20:22:13 +01:00
Kévin Commaille 3e78e441d4 refactor(sdk): Move oidc module to authentication::oidc
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-22 20:22:13 +01:00
Kévin Commaille 02c2e55855 refactor(sdk): Move matrix_auth module to authentication::matrix
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-22 20:22:13 +01:00
Stefan Ceriu a528624274 chore(ffi): replace all the different timeline builder methods with one taking a configuration (#4561) 2025-01-22 13:56:53 +02:00
Ivan Enderlin 1d83d42e9f chore(ui): Remove Timeline::subscribe.
This patch removes `Timeline::subscribe`. There is
`Timeline::subscribe_batched`, which is the only useful API.
`subscribe` was only used by our tests, and `matrix-sdk-ffi` uses
`subscribe_batched` only.
2025-01-22 11:55:23 +01:00
Ivan Enderlin 4684cfb780 chore: Replace Timeline::subscribe by Timeline::subscribe_batched.
This patch changes all calls to `Timeline::subscribe` to replace them by
`Timeline::subscribe_batched`. Most of them are in tests. It's the first
step of a plan to remove `Timeline::subscribe`.

The rest of the patch updates all the tests to use
`Timeline::subscribe_batched`.
2025-01-22 11:55:23 +01:00
Stefan Ceriu 991c9ad610 chore(ci): simplify formatting checks by using xtask instead 2025-01-22 12:20:47 +02:00
Hubert Chathi e826c54a42 Use the dehydrated device format implemented by vodozemac (#4421)
Signed-off-by: Hubert Chathi <hubertc@matrix.org>
2025-01-22 09:38:48 +01:00
Kévin Commaille 9ae658c1b9 feat(sdk): Enable HTTP/2 support
It became an optional default feature in reqwest 0.12, and we disable
the default features,
so I don't think it was meant to be disabled when the crate was
upgraded.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-22 09:08:00 +01:00
Benjamin Bouvier 4341aaf65c test: remove some uses of sync_timeline_event! in the base and sdk crates (#4565)
Part of #3716.
2025-01-21 14:33:22 +00:00
Jorge Martín ad847a82c8 refactor: Remove TestHelper in pinned_events.rs
Use helper functions instead.
2025-01-21 12:56:19 +01:00
Jorge Martín dad3e6839f test: update the code in pinned_events integration tests
This is done so the tests there use the new APIs based on `MatrixMockServer`.
2025-01-21 12:56:19 +01:00
Jorge Martín d078ef6155 fix: fix mock_room_event not being able to properly filter out event ids since the regex was incorrectly parsed by wiremock 2025-01-21 12:56:19 +01:00
Benjamin Bouvier 210c5749f1 test: minimize usage of EventFactory::state_key
It was used in places where we could make use of other helpers, in some
cases. Also introduces the `room_avatar` helper to create the room
avatar state event.
2025-01-21 10:50:29 +01:00
Benjamin Bouvier 0c74abbc50 test: get rid of EventBuilder (#4560)
This gets rid of `EventBuilder`, and makes more usage of the
`EventFactory`, which is more ergonomic to create test events.

A large part of
https://github.com/matrix-org/matrix-rust-sdk/issues/3716.
2025-01-21 09:23:03 +00:00
Jorge Martín dbadfe19b0 fix(timeline): avoid adding non-pinned events to the pinned events timeline when back paginating 2025-01-20 17:38:50 +01:00
dependabot[bot] f3e43dbfa4 chore(deps): bump malinskiy/action-android/install-sdk@release/0.1.4
Bumps [malinskiy/action-android/install-sdk@release/0.1.4](https://github.com/malinskiy/action-android) from 0.1.4 to 0.1.7.
- [Release notes](https://github.com/malinskiy/action-android/releases)
- [Commits](https://github.com/malinskiy/action-android/compare/release/0.1.4...release/0.1.7)

---
updated-dependencies:
- dependency-name: malinskiy/action-android/install-sdk@release/0.1.4
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-01-20 16:22:38 +01:00
dependabot[bot] b846a6dd81 chore(deps): bump crate-ci/typos from 1.28.3 to 1.29.4
Bumps [crate-ci/typos](https://github.com/crate-ci/typos) from 1.28.3 to 1.29.4.
- [Release notes](https://github.com/crate-ci/typos/releases)
- [Changelog](https://github.com/crate-ci/typos/blob/master/CHANGELOG.md)
- [Commits](https://github.com/crate-ci/typos/compare/v1.28.3...v1.29.4)

---
updated-dependencies:
- dependency-name: crate-ci/typos
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-01-20 16:22:31 +01:00
Ivan Enderlin c82e469fc3 chore(cargo): Update Ruma to include https://github.com/ruma/ruma/pull/1995. 2025-01-20 15:02:53 +01:00
Kévin Commaille f2c9a8f723 fix(ui): Fix latest_edit_json for live edits
This was a regression introduced in
f0d98602a9.

`latest_edit_json` was first set by the call to
`EventTimelineItem::with_content()`.
It was overwritten in the next section because of the
`if let EventTimelineItemKind::Remote(remote_event)`
that uses `item` instead of `new_item`.
It means that the updated `RemoteEventTimelineItem`
inside`new_item` was replaced by the outdated one inside `item`,
so `latest_edit_json` goes back to its previous value.

I believe that part of why that went unnoticed is that the code looks
more
complicated due to the need to set an inner field, so I decided to
change the API and
move `with_encryption_info` to `EventTimelineItem`, which makes the code
look cleaner.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-20 13:07:13 +01:00
torrybr 47fc073b70 refactor(live_location_share): exclude live location events of own user (#4535)
This change ensures that the user's own live location events are
excluded from the location sharing stream. Since the user's location is
already represented on the map by the blue dot, processing their own
events is redundant and unnecessary.
2025-01-17 14:56:10 +00:00
Jonas Platte 160600e8c0 chore(ui): Copy some attributes from matrix-sdk
The others should likely be copied at some point as well, but including
the readme as crate documentation is not currently useful since the
readme for the ui crate is empty, and warning about missing docs or
debug implementations would make CI fail without substantial extra work.
2025-01-17 15:35:28 +01:00
Jonas Platte 993c103270 ci: Add wasm job for matrix-sdk-ui 2025-01-17 15:35:28 +01:00
Jonas Platte e077980ba2 ci: Shorten job name
The extra 'wasm-flags' does not seem to add clarity.
2025-01-17 15:35:28 +01:00
Jonas Platte 63d14b798b ci: Reorder workflow matrix items to match ci.rs 2025-01-17 15:35:28 +01:00
Jonas Platte 077d63a9fc chore(ui): Re-export matrix-sdk's js feature 2025-01-17 15:35:28 +01:00
Ivan Enderlin 453c4e12db doc(sdk): Simplify documentation of RoomEventCache::subscribe.
This patch removes a `XXX` (which I believe is a TODO) in the
documentation of `RoomEventCache::subscribe`. We are not going to change
this API anytime soon.
2025-01-17 15:32:00 +01:00
Jonas Platte f7db52e069 Use Send-less BoxFuture for HttpClient Service impl on wasm 2025-01-17 12:21:21 +01:00
Richard van der Hoff 2bd8c56e64 crypto: add some more documentation to DeviceKeys
This confused me for a while, so I thought more documentation might help.
2025-01-16 15:13:25 +00:00
Richard van der Hoff f231c74314 test: simplify examples for KeyQueryResponseTemplate
Generating keys from slices rather than base64 is easier.

Also, s/builder/template/.
2025-01-16 15:13:11 +00:00
Stefan Ceriu 2cb6ee8e6d chore(ffi): silence useless logs coming out of the ffi crate
Setting the default log level to `debug` results in logs like:

```
log: log_event
log: latest_event
log: log_event
log: log_event
log: room_info
log: latest_event
log: log_event
log: room_info
```

Presumably they're coming out of the custom tracing configuration and we definitely don't need them.
2025-01-16 15:17:24 +02:00
Richard van der Hoff c24770a774 test: add support for dehydrated devices to KeyQueryResponseTemplate (#4540)
#4476 added some test helpers to generate `/keys/query` responses. We're
going to need to test dehydrated devices, so this PR adds support for
that.
2025-01-16 11:26:52 +00:00
Benjamin Bouvier 7fa06cb028 refactor(timeline): rename TimelineItemPosition::UpdateDecrypted to UpdateAt 2025-01-16 12:26:32 +01:00
Benjamin Bouvier 50383098ff feat(event cache): redact events in the database whenever they're redacted 2025-01-16 12:26:32 +01:00
Benjamin Bouvier 6f780a499c test(timeline): use assert_let_timeout more in the timeline's code 2025-01-16 12:26:32 +01:00
Benjamin Bouvier 425e48a46d feat(linked chunk): add LinkedChunk::replace_item_at to replace an item from a given position 2025-01-16 12:26:32 +01:00
Richard van der Hoff 3dd81fbe2c test: rename snapshots not to contain :
Windows doens't allow you to have `:` in its filenames
2025-01-16 11:11:38 +00:00
Benjamin Bouvier 6a0333e812 test: replace Option::default() by None 2025-01-16 11:34:42 +01:00
Benjamin Bouvier b3a789af90 test: get rid of the synced_client helper
Not running a large sync with many events make for simpler test cases,
with a more focused scope.
2025-01-16 11:34:42 +01:00
Benjamin Bouvier 560e582e41 test: get rid of mock_redaction and replace it with the holy MatrixMockServer 2025-01-16 11:34:42 +01:00
Benjamin Bouvier de7397a20e feat(event cache): handle redacted redactions in the AllEventsCache
This is unlikely that it will affect us, so not worth adding a test IMO,
but for the sake of completeness: this handles redacted redactions in
the `AllEventsCache` too.
2025-01-16 10:05:45 +01:00
Andy Balaam 8bd94318c0 fix(tests): Fix a flaky test by marking a room's members as synced.
This is intended to prevent the test
`test_when_user_in_verification_violation_becomes_verified_we_report_it`
flaking. I found that sometimes when it called `Room::members` the
result was empty due to it trying to fetch the answer from the server.
This change prevents that behaviour. I don't know why the behaviour was
inconsistent before.
2025-01-15 15:24:49 +00:00
Richard van der Hoff fe3cc09ae0 test: add examples for the new builder 2025-01-15 10:42:21 +00:00
Richard van der Hoff 3a3cc54067 test: generate dan's data dynamically 2025-01-15 10:42:21 +00:00
Richard van der Hoff 47f8b32ea1 test: give Dan new keys
Regenerate Dan's data with new cross-signing and device keys, for which I know
the private keys.

The signatures are manually calculated for now; this will be improved in a
later commit.
2025-01-15 10:42:21 +00:00
Richard van der Hoff 49748dbd4b test: factor out common parts of dan_keys_query_response{_loggedout} 2025-01-15 10:42:21 +00:00
Richard van der Hoff 25ea5fdd73 test: use builder for some more test data 2025-01-15 10:42:21 +00:00
Richard van der Hoff 5fadde5a6d test: implement test user data builder type
... and use it for some simple data
2025-01-15 10:42:21 +00:00
Richard van der Hoff b6be4d5170 test: remove redundant sig on master key
Our test helper won't do this, and it's redundant
2025-01-15 10:42:21 +00:00
Richard van der Hoff c9bac4ff2b test: snapshot the generated object rather than the JSON
We're going to be switching away from JSON-twiddling, so let's snapshot the
real object rather than the JSON.
2025-01-15 10:42:21 +00:00
Richard van der Hoff fedf7d214f test: add some snapshot tests before we change anything 2025-01-15 10:42:21 +00:00
Valere c969f903b7 Merge pull request #4526 from matrix-org/valere/test_encrypted_crypto_sql_snapshot
tests: Add an encrypted snapshot of a SQLite db for regression tests
2025-01-15 09:37:27 +01:00
Jorge Martín bd5d7aafee feat(ffi): Add FFI bindings for fn Room::own_membership_details.
Also add `membership_change_reason` field to `ffi::RoomMember`.
2025-01-14 16:23:51 +01:00
Jorge Martín e015a531da feat(room): Add fn Room::own_membership_details
This will retrieve the room member info of both the current user and the info for the sender of the current user's room member event.
2025-01-14 16:23:51 +01:00
Benjamin Bouvier b9014a5e2a test: keep a single sync in test_delayed_invite_response_and_sent_message_decryption()
This removes one sync that happens in the background, because it's
likely spurious and may be confusing the server about what's been seen
by the current client.
2025-01-14 15:07:10 +01:00
Jorge Martín e9487b0851 fix(timeline): Add UTDs to the timeline conditionally 2025-01-14 12:25:49 +01:00
Damir Jelić c60bfb877a chore: Add some missing links to the changelog 2025-01-13 19:27:44 +01:00
Valere ee32b1f600 tests: Add an encrypted snapshot of a SQLite db for regression tests 2025-01-13 17:50:50 +01:00
Daniel Salinas 9641aa9082 feat(send queue): Add an enqueued time to to-be-sent events (#4385)
Add a new created_at to the send_queue_events and
dependent_send_queue_events stored records. This will allow clients to
understand how stale a pending message might be in the event that the
queue encounters and error and becomes wedged.

This change is exposed through the FFI on the `EventTimelineItem` struct
as a new optional field named `local_created_at`. It will be `None` for
any Remote event, and `Some` for Local events (except for those that
were enqueued before the migrations were run).

Signed-off-by: Daniel Salinas

---------

Signed-off-by: Daniel Salinas <zzorba@users.noreply.github.com>
Co-authored-by: Daniel Salinas <danielsalinas@daniels-mbp-2.myfiosgateway.com>
Co-authored-by: Benjamin Bouvier <benjamin@bouvier.cc>
Co-authored-by: Daniel Salinas <danielsalinas@Daniels-MBP-2.attlocal.net>
2025-01-13 16:41:05 +00:00
Benjamin Bouvier a8ca77f4fc feat(base): remove cached events when forgetting about a room 2025-01-13 17:36:33 +01:00
Benjamin Bouvier e647ff935e feat(event cache store): allow removing an entire room at once 2025-01-13 17:36:33 +01:00
Benjamin Bouvier 7f04a9a18b fix(memory chunk): only remove a given room's events when clearing a roo 2025-01-13 17:36:33 +01:00
Damir Jelić 67d2cb790d chore: Fix a couple of typos 2025-01-13 17:25:00 +01:00
Benjamin Bouvier 279c78b3e2 chore!(encryption): rename are_we_the_last_man_standing to is_last_device
While the former name is arguably more fun, the latter is more
descriptive of what the function does.
2025-01-13 16:51:33 +01:00
Benjamin Bouvier 9514388108 tests: rename RoomMessagesResponse to RoomMessagesResponseTemplate 2025-01-13 14:50:21 +01:00
Benjamin Bouvier e6dc10933c tests: add helper to delay a room /messages response
This removes a few manual uses of `ResponseTemplate`, which is sweet and
guarantees some better typing for those responses overall.
2025-01-13 14:50:21 +01:00
Benjamin Bouvier c456356424 tests: add a helper to create a room /messages response 2025-01-13 14:50:21 +01:00
Benjamin Bouvier 5af326b36e fix(event cache): keep the previous-batch token when we haven't enabled storage 2025-01-13 14:50:21 +01:00
Jorge Martín 5548f38393 feat(ffi): Add FFI bindings for the new room privacy settings feature. 2025-01-13 11:29:10 +01:00
Jorge Martín d9c1188f87 test(room): Add integration tests for publishing and removing room aliases 2025-01-13 11:29:10 +01:00
Jorge Martín 588702756b feat(room): Add fn RoomPrivacySettings::remove_room_alias_in_room_directory. 2025-01-13 11:29:10 +01:00
Jorge Martín d6a74d389d feat(room): Add fn RoomPrivacySettings::publish_room_alias_in_room_directory.
This also needs some new mocks for resolving room aliases.
2025-01-13 11:29:10 +01:00
Jorge Martín d807d71e22 feat(room): Add fn RoomPrivacySettings::update_room_visibility. 2025-01-13 11:29:10 +01:00
Jorge Martín 587545ae82 feat(room): Add fn RoomPrivacySettings::get_room_visibility. 2025-01-13 11:29:10 +01:00
Jorge Martín 49985e5476 feat(room): Add fn RoomPrivacySettings::update_join_rule. 2025-01-13 11:29:10 +01:00
Jorge Martín 4fbe79a27d feat(room): Add fn RoomPrivacySettings::update_room_history_visibility. 2025-01-13 11:29:10 +01:00
Jorge Martín f61ad19ae6 feat(room): Add RoomPrivacySettings helper struct.
This can be accessed through `fn Room::privacy_settings` and will wrap the functionality related to a room's access and privacy settings.

This commit includes the `fn RoomPrivacySettings::update_canonical_alias` to modify the canonical alias of a room.
2025-01-13 11:29:10 +01:00
Benjamin Bouvier c9a49006f6 chore(xtask): tweak the TWiM report to include only merged PRs, not created PRs
As an outsider, I am mostly interested in features and new developments
that have happened, not those that *may* happen. An open-but-not-merged
PR may not get merged in the end, or it may not get merged any time
soon, creating false expectations. Merged PRs, on the other hand, have
definitely happened (even if they get undone, that happens via other PRs
that will get merged later). As such, I think it brings more value to
outsiders.
2025-01-13 11:03:08 +01:00
Kévin Commaille ca9eb70db5 Add PR link to changelog
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-13 08:46:10 +01:00
Kévin Commaille f173aea6e4 feat(sdk): Expose Client::server_versions publicly
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-13 08:46:10 +01:00
Jonas Platte e37ad11b47 refactor(ui): Use RPITIT / AFIT for RoomDataProvider 2025-01-11 13:19:16 -05:00
Damir Jelić d6c2a63f5c refactor: Use the simplified locks in the encryption tasks 2025-01-11 09:33:33 +01:00
Damir Jelić 4ebf5056be chore: Remove our ancient upgrade guide 2025-01-10 20:22:27 +01:00
Valere a79d409f9d task(bindings): Expose withdraw_verification in UserIdentity 2025-01-10 15:18:12 +01:00
Kévin Commaille 5941495e68 feat(sdk): Implement Default for AttachmentInfo types
Since all of their fields are optional, it simplifies their
construction.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-10 14:37:56 +01:00
Kévin Commaille b3491582d0 feat(sdk): Allow to set and check whether an image is animated
Using MSC4230.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-10 14:37:56 +01:00
Damir Jelić def4bbbed2 fix(store-encryption): Remove an unwrap that snuck in (#4506) 2025-01-10 14:13:10 +01:00
Valere 1dd2b2c9e8 test: Test the KnownSenderData migration with optimised [u8] serialization 2025-01-10 14:12:32 +01:00
Damir Jelić e4b269e0de fix: Implement visit_bytes for the Ed25519PublicKey deserialization
This fixes the deserialization of the SenderData since it switched to
the base64 encoding for serialization of the master key in one of its
variants.

The issue was introduced in 5ff556f6c3.
2025-01-10 14:12:32 +01:00
Kévin Commaille cb72d4375f chore: Upgrade Ruma
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-10 11:31:56 +01:00
Andy Balaam 7ec384c61a fix: Fix incorrect debug_struct calls in several places 2025-01-10 09:27:31 +01:00
Jonas Platte 7466f77eae refactor(ui): Replace tokio::spawn with matrix_sdk::executor::spawn 2025-01-10 03:02:37 -05:00
Jonas Platte 526b5c4630 refactor(ui): Relax some Send constraints on WASM 2025-01-10 03:02:37 -05:00
Jonas Platte 4043f9bf5d refactor(sdk): Un-cfg SendAttachment::with_send_progress_observable
It (now) compiles on WASM just fine.
2025-01-10 03:02:37 -05:00
Jonas Platte ff5dcbf631 refactor(common): Warn if LinkedChunk::updates() return value is not used 2025-01-09 16:20:51 -05:00
Jonas Platte 6c053a86bf chore: Fix new nightly warnings 2025-01-09 16:20:51 -05:00
Benjamin Bouvier 0cae54cc3f chore(ui): rename "utils" to "algorithms"
It only contains functions used to search items in the timeline now \o/
2025-01-09 17:27:53 +01:00
Benjamin Bouvier 692aceba50 chore(ui): move RelativePosition in timeline/controller 2025-01-09 17:27:53 +01:00
Benjamin Bouvier c4a86a3d0a chore(ui): move timeline/read_receipts to timeline/controller/read_receipts
Read receipts only make sense in the context of the timeline controller.
2025-01-09 17:27:53 +01:00
Benjamin Bouvier 5f5aa81174 chore(ui): move date and timestamp functionality to timeline/date_dividers.rs
`util.rs` files are… not the best thing. These types and functions were
only used by the date dividers file, so let's move them there.
2025-01-09 17:27:53 +01:00
Benjamin Bouvier 6e0f258a39 chore(sdk): move send_queue.rs to send_queue/mod.rs 2025-01-09 17:27:53 +01:00
Stefan Ceriu c4bfbd0f44 feat(ffi): move tracing setup from the final client to the ffi layer (#4492)
Having the final clients define the tracing filters / log levels proved
to be tricky to keep in check resulting missing logs (e.g. recently
introduced modules like the event cache) or unexpected behaviors (e.g.
missing panics because we don't set a global filter). As such we decided
to move the tracing setup and default definitions over to the rust side
and let rust developers have full control over them.

We will now take a general log level and optional extra targets and 
apply them on top of the rust side defined defaults. Targets that log
more than the requested by default will remain unchanged while the
others will increase their log levels to match. Certain targets like
`hyper` will be ignored in this step as they're too verbose others 
like `matrix_sdk` because they're too generic.
2025-01-09 18:08:44 +02:00
Benjamin Bouvier 8e0ee47637 refactor(event cache): eliminate intermediate function append_events_locked
and replace it with an inlined call to `append_events_locked_impl`,
that's then renamed `append_events_locked`.
2025-01-09 15:36:36 +01:00
Benjamin Bouvier 0915eeed51 chore(event cache): simplify and add logs to RoomEventCacheState::propagate_changes 2025-01-09 15:36:36 +01:00
Benjamin Bouvier fb54e869e9 chore(event cache): add more logs when the event cache tasks are shutting down 2025-01-09 15:36:36 +01:00
Benjamin Bouvier 9e97ed3134 test(event cache): add a regression test for not deleting a gap that wasn't inserted 2025-01-09 12:12:12 +01:00
Benjamin Bouvier b926c4287a refactor(event cache): use a more fine-grained check for the gap removal 2025-01-09 12:12:12 +01:00
Ivan Enderlin ddf4d575b7 fix(sdk): Ensure a gap has been inserted before removing it.
This patch fixes a bug where the code assumes a gap has been inserted,
and thus, is always present. But this isn't the case. If `prev_batch`
is `None`, a gap is not inserted, and so we cannot remove it. This patch
checks that `prev_batch` is `Some(_)`, which means the invariant is
correct, and the code can remove the gap.
2025-01-09 12:12:12 +01:00
Damir Jelić 2a954e3ce3 fix(base): Correctly name the LeftRoomUpdate in its debug implementation (#4487)
Signed-off-by: Damir Jelić <poljar@termina.org.uk>
Co-authored-by: Benjamin Bouvier <benjamin@bouvier.cc>
2025-01-09 11:10:33 +00:00
Jonas Platte b837865226 refactor(ui): Inherit Send / Sync bounds on RoomDataProvider from super traits 2025-01-09 09:26:55 +01:00
Jonas Platte eac5a5eb35 refactor(ui): Fix unused import on wasm 2025-01-09 09:26:55 +01:00
Jonas Platte d6c64027f6 refactor(sdk): Un-cfg Client::rooms_stream
It compiles on WASM too.
2025-01-09 09:26:55 +01:00
Ivan Enderlin df4b69666c chore: Make Clippy and wasm-pack happy. 2025-01-08 21:30:41 +01:00
Ivan Enderlin 5675ac7f46 refactor(sdk): Remove SlidingSyncRoomInner::client.
This patch removes `SlidingSyncRoomInner::client` because, first
off, it's not `Send`, and second, it's useless. Nobody uses it, it's
basically dead code… annoying dead code… bad dead code!
2025-01-08 21:30:41 +01:00
Ivan Enderlin 6b2233f8c4 fix(sdk): Use spawn from matrix_sdk_common to make it compatible with wasm32-u-u. 2025-01-08 21:30:41 +01:00
Ivan Enderlin 61dd560499 feat: Remove the experimental-sliding-sync feature flag.
Sliding sync is no longer experimental. It has a solid MSC4186, along
with a solid implementation inside Synapse. It's time to consider it
mature.

The SDK continues to support the old MSC3575 in addition to MSC4186.
This patch only removes the `experimental-sliding-sync` feature flag.
2025-01-08 21:30:41 +01:00
Damir Jelić 62567ca6eb refactor(crypto): Use the simplified locks across the crypto crate 2025-01-08 18:59:22 +01:00
Damir Jelić 46dc2a9c5e refactor: Use the simplified locks in the failures cache 2025-01-08 18:59:22 +01:00
Damir Jelić 891583b70e refactor: Add Mutex and RwLock wrappers that panic on poison 2025-01-08 18:59:22 +01:00
Ivan Enderlin e19bdbfd59 test(ui): Adjust tests according to the new Timeline behaviour. 2025-01-08 17:04:58 +01:00
Ivan Enderlin 14d0cc1935 refactor(ui): Remove TimelineSettings::vectordiffs_as_inputs.
From now on, this patch considers that `VectorDiff`s are the official
input type for the `Timeline`, via `RoomEventCacheUpdate` (notably
`::UpdateTimelineEvents`).

This patch removes `TimelineSettings::vectordiffs_as_inputs`. It thus
removes all deduplication logics, as it is supposed to be managed by the
`EventCache` itself.
2025-01-08 17:04:58 +01:00
Ivan Enderlin b8d0384da7 refactor: Remove RoomEventCacheUpdate::AddTimelineEvents.
This patch removes the `AddTimelineEvents` variant from
`RoomEventCacheUpdate` since it is replaced by `UpdateTimelineEvents`
which shares `VectorDiff`.

This patch also tests all uses of `UpdateTimelineEvents` in existing
tests.
2025-01-08 17:04:58 +01:00
Ivan Enderlin 4e0a6d15ca chore(sdk): Merge imports for the sake of clarity. 2025-01-08 17:04:58 +01:00
Ivan Enderlin 251433382f chore(test): Remove a warning.
`owned_user_id` is only used by a test behind the
`experimental-sliding-sync` feature flag.
2025-01-08 17:04:58 +01:00
Benjamin Bouvier 34e993435d fix(ffi): ensure the log level for panic is always set (#4485)
If it's present, we just let it untouched. Otherwise, we set it to
`error` if it's missing. See code comment explaining why we need this.

This makes sure we log panics at the FFI layer, since the `log-panics`
crate will use the `panic` target at the error level.
2025-01-08 15:51:14 +00:00
Benjamin Bouvier dc2775e194 chore!(ffi): rename thumbnail_url to thumbnail_path
This is a breaking change because uniffi may use foreign-language named
parameters based on the Rust parameter name.
2025-01-08 14:20:06 +01:00
Benjamin Bouvier 45c3752cae refactor!(ffi): common out more code in send_attachment and distinguish early from late errors
Some errors can be handled immediately and don't need a request to be
spawned, e.g. invalid mimetype and so on. The returned task handle still
deals about "late" errors about the upload failing (for sync uploads) or
the send queue failing to push the media upload (for async uploads).
2025-01-08 14:20:06 +01:00
Benjamin Bouvier ed178602d7 chore!(ffi): group parameters to upload in UploadParameters
Note: `Box<dyn ProgressWatcher>` couldn't be put in a `Record`, so
doesn't belong in `UploadParameters` as a result.
2025-01-08 14:20:06 +01:00
Benjamin Bouvier 35a03278c3 chore(ffi): rename url to filename in the FFI methods for sending attachments 2025-01-08 14:20:06 +01:00
Valere 9e69b631ee Merge pull request #4450 from matrix-org/valere/serialize_known_sender_data_b64
fix(crypto): Serialize sender data msk in base64 instead of numbers
2025-01-08 14:06:13 +01:00
Valere 5ff556f6c3 fix(crypto): Serialize sender data msk in base64 instead of numbers 2025-01-08 13:22:37 +01:00
Ivan Enderlin d64960679f refactor(sdk): Rename RoomEvents::filter_duplicated_events.
This patch renames `RoomEvents::filter_duplicated_events` to
`collect_valid_and_duplicated_events` as I believe it improves the
understanding of the code. The variables named `unique_events` are
renamed `events` as all (valid) events are returned, not only the unique
ones.
2025-01-08 13:01:59 +01:00
Ivan Enderlin 7ff1170681 chore: Re-indent. 2025-01-08 11:47:24 +01:00
Ivan Enderlin 55e25a3717 feat(sdk,ui): Add EventsOrigin::Pagination.
This patch adds the `Pagination` variant to the `EventsOrigin` enum.
Not something really mandatory and that is likely to fix a bug, but it's
now correct.
2025-01-08 11:43:03 +01:00
Joe Groocock 3f977b79fa feat(timeline): allow sending mentions along with media
Since 8205da898e it has been possible to
attach (intentional) mentions to _edited_ media captions, but the
send_$mediatype() timeline APIs provided no way to send them with the
initial event. This fixes that.

Signed-off-by: Joe Groocock <me@frebib.net>
2025-01-08 10:43:43 +01:00
Benjamin Bouvier aca8c8b8ee chore: remove some allow(dead_code) annotations and associated dead code (#4472)
We have quite a few `allow(dead_code)` annotations. While it's OK to use
in situations where the Cargo-feature combination explodes and makes it
hard to reason about when something is actually used or not, in other
situations it can be avoided, and show actual, dead code.
2025-01-08 10:37:18 +01:00
Kévin Commaille 47c24b9a17 fix(sdk): Fix test now that Ruma is fixed
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-08 10:35:28 +01:00
Kévin Commaille 47445b10f1 chore: Upgrade Ruma
This is using the ruma-0.12 branch where non-breaking changes are backported.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-08 10:35:28 +01:00
Jonas Platte c5a9a1e215 Clean up some imports
With experimental-sliding-sync enabled and e2e-encryption disabled,
there were a bunch of warnings about unused imports. This fixes them
(but a few warnings about other unused items remain).
2025-01-08 09:18:56 +01:00
Benjamin Bouvier 2ef14ded41 refactor(event cache): a few AllEventsCache refactorings (#4471)
I was investigating a potential deadlock with the event cache storage,
and only found a few places where to make the code a bit more idiomatic
and more readable.
2025-01-07 17:25:52 +01:00
Benjamin Bouvier 8205da898e feat(send queue): allow setting intentional mentions in media captions edits
Fixes #4302.
2025-01-07 16:52:53 +01:00
Benjamin Bouvier 618e47250d feat!(base): reintroduce Room::display_name
`compute_display_name` is made private again, and used only within the
base crate. A new public counterpart `Room::display_name` is introduced,
which returns a cached value for, or computes (and fills in cache) the
display name. This is simpler to use, and likely what most users expect
anyways.
2025-01-07 15:25:32 +01:00
Benjamin Bouvier 5110aa64aa doc(base): update lying doc comment of compute_display_name
It claimed that it would immediately return when the cached display name
value was computed, but that's absolutely not the case.

Spotted while reviewing a PR updating `iamb` to the latest version of
the SDK.
2025-01-07 14:39:07 +01:00
Benjamin Bouvier bcad0a3059 test(timeline): rewrite a test to use the MatrixMockServer instead 2025-01-07 11:58:34 +01:00
Benjamin Bouvier b7b88f58d2 feat!(send queue): make unrecoverable errors stop the sending queue
Instead of keeping on handling unwedged events from the sending queue,
it's now required to re-enable the send queue manually for the room that
encountered the sending error, all the time. This is more consistent,
and avoids weird behavior when a user would 1. send an event for which
sending fails, in an unrecoverable manner, 2. send an event that's
actually sendable.
2025-01-07 11:58:34 +01:00
Damir Jelić 412fcab4dc test: Await the device creation in the notification client redecryption test 2025-01-07 11:06:28 +01:00
Jonas Platte 8e75a940f7 Use Instant from web-time in more places (via ruma re-export)
web-time's Instant type is already used elsewhere in the project. It is
an alias for std's Instant type on most targets, but tries to call into
JavaScript on wasm32-unknown-unknown (assuming that the wasm blob is
used in from a browser context). Its Duration type is a plain re-export
of std's Duration, even on wasm32-unknown-unknown.
2025-01-07 09:35:52 +01:00
Kévin Commaille 70fb7899e6 feat!(timeline): Allow to send attachments from bytes (#4451)
Sometimes we can get the bytes directly, e.g. in Fractal we can get an
image from the clipboard. It avoids to have to write the data to a
temporary file only to have the data loaded back in memory by the SDK
right after.

The first commit to accept any type that implements `Into<String>` for
the filename is grouped here because it simplifies slightly the second
commit.

Note that we could also use `AttachmentSource` in the other
`send_attachment` APIs, on `Room` and `RoomSendQueue`, for consistency.

---------

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2025-01-06 15:44:29 +01:00
Benjamin Bouvier 1480fada6e refactor(event cache): make it clearer that vecdiff updates must be handled with with_events_mut
Every caller to `with_events_mut` must propagate the vector diff
updates, otherwise updates would be missing to the room event cache's
observers. This slightly tweaks the signature to make this a bit
clearer, and adjusts the code comment as well.
2025-01-06 13:14:31 +01:00
Kévin Commaille c50358366f refactor!(sdk): Set thumbnail in AttachmentConfig with builder method instead of constructor
`AttachmentConfig::with_thumbnail()` is replaced by
`AttachmentConfig::new().thumbnail()`.

Simplifies the use of `AttachmentConfig`, by avoiding code like:

```rust
let config = if let Some(thumbnail) = thumbnail {
  AttachmentConfig::with_thumbnail(thumbnail)
} else {
  AttachmentConfig::new()
};
```

---------

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-12-22 17:45:04 +00:00
Valere adb4428a69 test(crypto): Add some basic snapshot testing in crypto crate 2024-12-20 19:52:37 +01:00
Damir Jelić 667a8e684c chore: Fix a typo in the changelog 2024-12-20 13:59:54 +01:00
Ivan Enderlin f4b50db972 test: Increase timeout for codecoverage. 2024-12-20 13:57:45 +01:00
Ivan Enderlin 1abb2efc51 refactor(sdk): Rename two variables. 2024-12-20 13:57:45 +01:00
Ivan Enderlin c4132252d3 feat(ui): Enable TimelineSettings::vectordiffs_as_inputs if event cache storage is enabled.
This patch automatically enables
`TimelineSettings::vectordiffs_as_inputs` if and only if the event cache
storage is enabled.
2024-12-20 13:57:45 +01:00
Ivan Enderlin 51c76a15ad chore(ui): Make Clippy happy. 2024-12-20 13:57:45 +01:00
Ivan Enderlin f1842ba5d0 refactor(ui): Timeline receives pagination events as VectorDiffs!
This patch allows the paginated events of a `Timeline` to be received
via `RoomEventCacheUpdate::UpdateTimelineEvents` as `VectorDiff`s.
2024-12-20 13:57:45 +01:00
Ivan Enderlin d8dd72fd9c refactor(ui): Deduplicate timeline items conditionnally.
A previous patch deduplicates the remote events conditionnally. This
patch does the same but for timeline items.

The `Timeline` has its own deduplication algorithm (for remote
events, and for timeline items). The `Timeline` is about to receive
its updates via the `EventCache` which has its own deduplication
mechanism (`matrix_sdk::event_cache::Deduplicator`). To avoid conflicts
between the two, we conditionnally deduplicate timeline items based on
`TimelineSettings::vectordiffs_as_inputs`.

This patch takes the liberty to refactor the deduplication mechanism of
the timeline items to make it explicit with its own methods, so
that it can be re-used for `TimelineItemPosition::At`. A specific
short-circuit was present before, which is no more possible with the
rewrite to a generic mechanism. Consequently, when a local timeline
item becomes a remote timeline item, it was previously updated (via
`ObservableItems::replace`), but now the local timeline item is removed
(via `ObservableItems::remove`), and then the remote timeline item is
inserted (via `ObservableItems::insert`). Depending of whether a virtual
timeline item like a date divider is around, the position of the removal
and the insertion might not be the same (!), which is perfectly fine as
the date divider will be re-computed anyway. The result is exactly the
same, but the `VectorDiff` updates emitted by the `Timeline` are a bit
different (different paths, same result).

This is why this patch needs to update a couple of tests.
2024-12-20 13:57:45 +01:00
Ivan Enderlin 054f5e28f6 fix(common): Use a trick to avoid hitting the recursion_limit too quickly.
This patch adds a trick around `SyncTimelineEvent` to avoid reaching the
`recursion_limit` too quickly. Read the documentation in this patch to
learn more.
2024-12-20 13:57:45 +01:00
Ivan Enderlin 38e35b99d0 test(ui): Increase the recursion_limit.
Since we have added a new variant to `RoomEventCacheUpdate`, a macro
hits the recursion limit. It needs to be updated in order for tests to
run again.
2024-12-20 13:57:45 +01:00
Ivan Enderlin 39afb531ef task(ui): DayDivider has been renamed DateDivider.
This patch updates this branch to `main` where `DayDivider` has been
renamed `DateDivider`.
2024-12-20 13:57:45 +01:00
Ivan Enderlin c1ff5ff49f refactor(ui): Deduplicate remote events conditionnally.
The `Timeline` has its own remote event deduplication mechanism. But
we are transitioning to receive updates from the `EventCache` via
`VectorDiff`, which are emitted via `RoomEvents`, which already runs its
own deduplication mechanism (`matrix_sdk::event_cache::Deduplicator`).
Deduplication from the `EventCache` will generate `VectorDiff::Remove`
for example. It can create a conflict with the `Timeline` deduplication
mechanism.

This patch updates the deduplication mechanism from the `Timeline`
when adding or updating remote events to be conditionnal: when
`TimelineSettings::vectordiffs_as_inputs` is enabled, the deduplication
mechanism of the `Timeline` is silent, it does nothing, otherwise it
runs.
2024-12-20 13:57:45 +01:00
Ivan Enderlin 2358e4c32f task(ui): Support VectorDiff::Remove, in TimelineStateTransaction::handle_remote_events_with_diffs.
This patch updates
`TimelineStateTransaction::handle_remote_events_with_diffs` to support
`VectorDiff::Remove,`.
2024-12-20 13:57:45 +01:00
Ivan Enderlin 409fccb709 task(ui): Support VectorDiff::Insert in TimelineStateTransaction::handle_remote_events_with_diffs.
This patch updates
`TimelineStateTransaction::handle_remote_events_with_diffs` to support
`VectorDiff::Insert`.
2024-12-20 13:57:45 +01:00
Ivan Enderlin b25fd830ec task(ui): Add AllRemoteEvents::range.
This patch adds the `AllRemoteEvents::range` method. This
is going to be useful to support `VectorDiff::Insert` inside
`TimelineStateTransaction::handle_remote_events_with_diffs`.
2024-12-20 13:57:45 +01:00
Ivan Enderlin 02ab57870a task(ui): Add ObservableItems::insert_remote_event.
This patch adds the `ObservavbleItems::insert_remote_event` method.
This is going to be useful to implement `VectorDiff::Insert` inside
`TimelineStateTransaction::handle_remote_events_with_diffs`.
2024-12-20 13:57:45 +01:00
Ivan Enderlin eca3749b28 task(ui): Support VectorDiff::Clear in TimelineStateTransaction::handle_remote_events_with_diffs.
This patch updates
`TimelineStateTransaction::handle_remote_events_with_diffs` to support
`VectorDiff::Clear`.
2024-12-20 13:57:45 +01:00
Ivan Enderlin 3f17325bac task(ui): Support VectorDiff::PushBack in TimelineStateTransaction::handle_remote_events_with_diffs.
This patch updates
`TimelineStateTransaction::handle_remote_events_with_diffs` to support
`VectorDiff::PushBack`.
2024-12-20 13:57:45 +01:00
Ivan Enderlin 23c09b2c9d task(ui): Support VectorDiff::PushFront in TimelineStateTransaction::handle_remote_events_with_diffs.
This patch updates
`TimelineStateTransaction::handle_remote_events_with_diffs` to support
`VectorDiff::PushFront`.
2024-12-20 13:57:45 +01:00
Ivan Enderlin c1f8232450 task(ui): Support VectorDiff::Append in TimelineStateTransaction::handle_remote_events_with_diffs.
This patch updates
`TimelineStateTransaction::handle_remote_events_with_diffs` to support
`VectorDiff::Append`.
2024-12-20 13:57:45 +01:00
Ivan Enderlin e28073361d feat(ui): Add blank handle_remote_events_with_diffs. 2024-12-20 13:57:45 +01:00
Ivan Enderlin 1c2fb1ab72 refactor(sdk): Add RoomEventCacheUpdate::UpdateTimelineEvents.
This patch adds a new variant to `RoomEventCacheUpdate`, namely
`UpdateTimelineEvents. It's going to replace `AddTimelineEvents` soon
once it's stable enough. This is a transition. They are read by the
`Timeline` if and only if `TimelineSettings::vectordiffs_as_inputs` is
turned on.
2024-12-20 13:57:45 +01:00
Ivan Enderlin be89e3aacb feat(ui): Add TimelineBuilder::with_vectordiffs_as_inputs.
This patch adds `with_vectordiffs_as_inputs` on `TimelineBuilder` and
`vectordiffs_as_inputs` on `TimelineSettings`. This new flag allows to
transition from one system to another for the `Timeline`, when enabled,
the `Timeline` will accept `VectorDiff<SyncTimelineEvent>` for the
inputs instead of `Vec<SyncTimelineEvent>`.
2024-12-20 13:57:45 +01:00
Damir Jelić 36427b0e12 fix(ui): Consider banned rooms as rooms we left in the non-left rooms matcher
Recently we started to differentiate between rooms we've been banned
from from rooms we have left on our own.

Sadly the non-left rooms matcher only checked if the room state is not
equal to the Left state. This then accidentally moved all the banned
rooms to be considered as non-left.

We replace the single if expression with a match and list all the
states, this way we're going to be notified by the compiler that we need
to consider any new states we add in the future.
2024-12-20 12:35:34 +01:00
Daniel Salinas f8a9d12c88 Use a type alias to allow bindings to take advantage of custom types 2024-12-20 10:46:13 +01:00
Benjamin Bouvier 5f5e979e16 refactor!: Put the RequestConfig argument of Client::send() into a builder method
Instead of `Client::send(request, request_config)`, consumers can now do
`Client::send(request).with_request_config(request_config)`.
2024-12-20 10:35:18 +01:00
Valere 519f281844 Merge pull request #4428 from matrix-org/valere/insta_rs_snapshot_testing
test(snapshot): Use snapshot testing in sdk-common
2024-12-19 18:30:49 +01:00
Valere 3b31bbec0c test(snapshot): Use snapshot testing in sdk-common 2024-12-19 18:11:55 +01:00
Benjamin Bouvier f2942db316 refactor: avoid use of async_trait for RoomIdentityProvider
This is an 8 seconds (out of 22) decrease of the matrix-sdk compile
times.
2024-12-19 17:38:59 +01:00
Andy Balaam e4712be946 task(crypto): Support receiving stable identifier for MSC4147 2024-12-19 15:27:34 +00:00
Benjamin Bouvier bc8c4f5e58 fix(event cache): don't touch the linked chunk if an operation wouldn't cause meaningful changes
See comment on top of `deduplicated_all_new_events`.
2024-12-19 14:19:55 +01:00
Benjamin Bouvier fe9354a886 test: make test_room_keys_received_on_notification_client_trigger_redecryption more stable
When starting to back-paginate, in this test, we:

- either have a previous-batch token, that points to the first event
*before* the message was sent,
- or have no previous-batch token, because we stopped sync before
receiving the first sync result.

Because of the behavior introduced in 944a9220, we don't restart
back-paginating from the end, if we've reached the start. Now, if we are
in the case described by the first bullet item, then we may backpaginate
until the start of the room, and stop then, because we've back-paginated
all events. And so we'll never see the message sent by Alice after we
stopped sync'ing.

One solution to get to the desired state is to clear the internal state
of the room event cache, thus deleting the previous-batch token, thus
causing the situation described in the second bullet item. This achieves
what we want, that is, back-paginating from the end of the timeline.
2024-12-19 14:19:55 +01:00
Benjamin Bouvier d00ff8fa1f refactor(event cache): remove duplicated method RoomEventCacheState::clear() 2024-12-19 14:19:55 +01:00
Benjamin Bouvier 60f521cc23 feat(event cache): don't add a previous gap if all events were deduplicated, after back-pagination 2024-12-19 14:19:55 +01:00
Benjamin Bouvier bcb9a86a00 feat(event cache): don't add a previous gap if all events were deduplicated, after sync 2024-12-19 14:19:55 +01:00
Benjamin Bouvier 3f0712010f refactor(event cache): add a way to know if we deduplicated all events (at least one) 2024-12-19 14:19:55 +01:00
Benjamin Bouvier d89194f071 refactor: display source pagination error in PaginatorError::SdkError 2024-12-19 14:19:55 +01:00
Benjamin Bouvier a20ad728b5 feat(event cache): don't restart back-pagination from the end if we had no prev-batch token 2024-12-19 14:19:55 +01:00
Benjamin Bouvier 0d546dce5f refactor(event cache): move PaginationToken from the pagination to room/mod file 2024-12-19 14:19:55 +01:00
Jorge Martín 38cc9fb7c8 test(room): Improve Room::room_member_updates_sender tests 2024-12-19 14:14:05 +01:00
Jorge Martín 616c193a30 feat(room): create a cleanup task in Room::subscribe_to_knock_requests
This cleanup task will run while the knock request subscription runs and will use the `Room::room_member_updates_sender` notification to call `Room::remove_outdated_seen_knock_requests_ids` and remove outdated seen knock request ids automatically.
2024-12-19 14:14:05 +01:00
Jorge Martín 4a88e7cfee feat(room): add BaseRoom::remove_outdated_seen_knock_requests_ids fn
This will check the current seen knock request ids against the room members related to them and will remove those seen ids for members which are no longer in knock state or come from an outdated knock member event.
2024-12-19 14:14:05 +01:00
Jorge Martín 5d0fed5e53 feat(room): add helper methods to BaseRoom to get and write the current seen knock request ids while keeping them thread-safe with a lock around them 2024-12-19 14:14:05 +01:00
Jorge Martín 9975365a1e feat(room): add Room::room_member_updates_sender
This sender will notify receivers when new room members are received: this can happen either when reloading the full room member list from the HS or when new member events arrive during a sync.

The sender will emit a `RoomMembersUpdate`, which can be either a full reload or a partial one, including the user ids of the members that changed.
2024-12-19 14:14:05 +01:00
Integral f18e0b18a1 Replace PathBuf/Utf8PathBuf with Path/Utf8Path when ownership not needed 2024-12-19 13:29:09 +01:00
Jorge Martín b18100228e test(room): add test to verify how Room::observe_events will behave when several events are received in a short while 2024-12-19 12:16:49 +01:00
Benjamin Bouvier de568837fb fix(linked chunk): fix order handling of initial chunks in UpdateToVectorDiff::new()
The code would use a chunk iterator that moves forward, but call
`push_front()` repetitively on each chunk, semantically storing the
lengths in *reverse* order.

This could result in subsequent panics, when a new chunk was added,
because the links would not match what's expected (e.g. the last chunk
must have no successor, etc.).
2024-12-18 19:50:25 +01:00
Ivan Enderlin bc582ae101 doc(common): Update documentation of AsVector.
This patch updates the documentation of `AsVector`.
2024-12-18 19:47:25 +01:00
Damir Jelić bb573117e1 chore: Release matrix-sdk version 0.9.0 2024-12-18 15:12:23 +01:00
Andy Balaam ff7077b742 doc(crypto): Add changelog entry for #4424 2024-12-18 13:51:00 +00:00
Ivan Enderlin bb70229dd8 chore: Make Clippy happy. 2024-12-18 13:24:55 +01:00
Andy Balaam 03947618ff Merge branch 'release-for-crypto-wasm-11'
This brings in the fix for #4424 that we did on a release branch to
allow a quick release of crypto-wasm
2024-12-18 11:25:53 +00:00
Andy Balaam b18e7d71ed fix(crypto): Fix error when reading VerifiedStateOrBool with old PreviouslyVerifiedButNoLonger value 2024-12-18 11:22:53 +00:00
Andy Balaam 612ba6fa29 task(crypto): Accept old PreviouslyVerified value of ShieldStateCode when deserializing 2024-12-18 11:22:53 +00:00
Andy Balaam db39c6bea6 task(crypto): Accept old PreviouslyVerified value of VerificationLevel when deserializing 2024-12-18 11:22:53 +00:00
Andy Balaam 5f3b56a987 task(crypto): Accept old PreviouslyVerified value of SenderData when deserializing 2024-12-18 11:22:53 +00:00
Benjamin Bouvier 373709fb38 feat(event cache): don't replace a gap chunk by an empty items chunks 2024-12-17 18:30:57 +01:00
Benjamin Bouvier 5d8ad3a4a9 fix(linked chunk): in LinkedChunk::ritems_from, skip as long as we're on the right chunk
The previous code would skip based on the position's index, but not the
position's chunk. It could be that the position's chunk is different
from the first items chunk, as shown in the example, where the linked
chunk ends with a gap; in this case, the position's index would be 0,
while the first chunk found while iterating backwards had 3 items. As a
result, items 'd' and 'e' would be skipped incorrectly.

The fix is to take into account the chunk id when skipping over items.
2024-12-17 17:48:10 +01:00
Damir Jelić 0ca35d6c4a test: Test that room keys received by notification clients trigger redecryptions 2024-12-17 16:07:21 +01:00
Damir Jelić daeffc07b3 feat: Derive PartialEq and Eq for RoomListLoadingState 2024-12-17 16:07:21 +01:00
Damir Jelić bd15f4ecbe feat(timeline): Listen to the room keys stream to retry decryptions 2024-12-17 16:07:21 +01:00
Damir Jelić f17f4e2bf6 feat: Add a stream to listen to room keys being inserted to the store 2024-12-17 16:07:21 +01:00
Damir Jelić 177ec1216f feat(crypto)!: Don't ignore the error if the room_keys_received stream lags 2024-12-17 16:07:21 +01:00
Valere 512a2d2662 Merge pull request #4399 from matrix-org/valere/ffi_save_store_dehydration_pickle_key
feat(crypto-ffi-bindings): Save/Load dehydrated pickle key
2024-12-17 10:07:54 +01:00
Valere 95582a6c3c feat(crypto-bindings): Save/Load dehydrated pickle key
review: better tests
2024-12-17 09:51:28 +01:00
Jorge Martín 866b5fea40 feat(room): Separate RoomState::Banned from RoomState::Left.
This is needed to tell apart rooms in left and banned state in places like `RoomInfo` or `RoomPreview`.

The banned rooms will still count as left rooms in the sync processes.
2024-12-16 19:19:56 +01:00
Benjamin Bouvier 34ea42aec0 feat(ffi): expose the linked chunk debug string function at the FFI layer 2024-12-16 16:41:03 +01:00
Benjamin Bouvier cae7e43b91 feat(multiverse): add linked chunk debug screen in multiverse 2024-12-16 16:41:03 +01:00
Benjamin Bouvier 34d15a4d37 feat(event cache): propose a debug representation for the linked chunk in RoomEvents too 2024-12-16 16:41:03 +01:00
Benjamin Bouvier f6cb8186c6 task(event cache): limit the display of event ids to 8 chars in the raw chunk debug string 2024-12-16 16:41:03 +01:00
dependabot[bot] 47044b1a23 chore(deps): bump crate-ci/typos from 1.28.2 to 1.28.3
Bumps [crate-ci/typos](https://github.com/crate-ci/typos) from 1.28.2 to 1.28.3.
- [Release notes](https://github.com/crate-ci/typos/releases)
- [Changelog](https://github.com/crate-ci/typos/blob/master/CHANGELOG.md)
- [Commits](https://github.com/crate-ci/typos/compare/v1.28.2...v1.28.3)

---
updated-dependencies:
- dependency-name: crate-ci/typos
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-12-16 17:29:27 +02:00
Jorge Martín 05d46e6027 Rename JoinRequest in the SDK crates to KnockRequest, make Room::mark_knock_requests_as_seen thread safe and pass user_ids instead of event_ids: the user ids will be used to get the related member state events and they'll only be marked as seen if they're in a knock state.
Also, add extra checks to the integration tests.
2024-12-16 14:08:09 +01:00
Jorge Martín 338769508e feat(ffi): add bindings for subscribing to the join requests 2024-12-16 14:08:09 +01:00
Jorge Martín 93ebae6601 feat(room): allow subscribing to requests to join a room
This subscription will combine 3 streams: one notifying the members in the room have changed, another notifying the seen join requests have changed, and finally a third one notifying when the room members are no longer synced.

With this info we can track when we need to generate a new list of join requests to be emitted so the client can always have an up to date list.
2024-12-16 14:08:09 +01:00
Jorge Martín 780c264e59 feat(room): add JoinRequest abstraction
This struct is an abstraction over a room member or state event with knock membership.
2024-12-16 14:08:09 +01:00
Jorge Martín 9a899c1cb1 feat(room): add 'seen request to join ids' to the stores
This will allow us to keep track of which join room requests are marked as 'seen' by the current user and return them as such.

Also, add some methods to `Room` to mark new join requests as seen and to get the current ids for the seen join requests.
2024-12-16 14:08:09 +01:00
Richard van der Hoff 2703f7f7d4 crypto: extra logging in OtherUserIdentity
Add some extra logging in these two methids, to try to narrow down a bug report
we received.
2024-12-16 12:59:16 +00:00
Kévin Commaille 8d2e672996 feat!: Upgrade Ruma to 0.12.0
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-12-16 11:56:44 +01:00
Benjamin Bouvier 5a25e65da3 test(event cache): use the MatrixMockServer 2024-12-16 11:56:22 +01:00
Benjamin Bouvier c197808b42 task(event cache): get rid of one level of indent 2024-12-16 11:56:22 +01:00
Benjamin Bouvier ed34719295 task(event cache): simplify handling a back-pagination result 2024-12-16 11:56:22 +01:00
Benjamin Bouvier a052a79aaf fix(event cache): store the gap *before* events, after back-paginating
The conditions required to cause the bug might have been impossible to
reach in the real world, because it assumes a mix of:

- events present in the linked chunk
- no prev-batch token

However: now that we have storage, we could end up in this situation,
when reaching the start of the timeline (since there'll be no previous
gap in that case). We need to handle that better in the linked chunk
representation itself, but in the meanwhile, we should insert the gap
and the events in a relative correct order.
2024-12-16 11:56:22 +01:00
Benjamin Bouvier b6542477bb task(event cache): make the code more concise in back-pagination 2024-12-16 11:56:22 +01:00
Kévin Commaille a573b650c9 chore(sdk): Remove image-rayon cargo feature check from build.rs
The cargo feature was removed, but the build script was forgotten.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-12-15 18:07:42 +01:00
Valere 789bd317b3 Merge pull request #4383 from matrix-org/valere/cache_dehydration_pickle_key
feat(crypto): Support storing the dehydrated device pickle key
2024-12-13 14:42:44 +01:00
Valere 2b39476d9b feat(crypto): Support storing the dehydrated device pickle key 2024-12-13 13:05:19 +01:00
Andy Balaam 6dcefe49c2 feat(utds): Provide the reason why an event was an expected UTD 2024-12-13 10:46:10 +00:00
Benjamin Bouvier 150d9e4b05 fix(event cache store): always use immediate mode when handling linked chunk updates
If a linked chunk update starts with a RemoveChunk update, then the
transaction may start with a SELECT query and be considered a read
transaction. Soon enough, it will be upgraded into a write transaction,
because of the next UPDATE/DELETE operations that happen thereafter. If
there's another write transaction already happening, this may result in
a SQLITE_BUSY error, according to
https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions

One solution is to always start the transaction in immediate mode. This
may also fail with SQLITE_BUSY according to the documentation, but it's
unclear whether it will happen in general, since we're using WAL mode
too. Let's try it out.
2024-12-12 17:59:42 +01:00
Damir Jelić 54bd1d7931 refactor(base): Move the joined member count logic into its respective sub-functions 2024-12-12 14:44:21 +01:00
Damir Jelić 7ae31d0cb1 fix(base): Subtract the number of service members from the number joined members
This patch fixes an edge case where the member is alone in the room with
a service member. We already subtracted the number of service members
in the case we calculated the room summary ourselves, but we did not do
so when the server provided the room summary.

This lead to the room, instead of being called `Empty`, being called
`Foo and N others`.
2024-12-12 14:44:21 +01:00
Damir Jelić f7f58dfd71 feat(ui): Add the MemberHints state event type to the required state
This state event allows us to correctly calculate the room display name
according to MSC4171.

MSC: https://github.com/matrix-org/matrix-spec-proposals/pull/4171
2024-12-12 14:44:21 +01:00
Richard van der Hoff 780a4630e4 chore(ffi): avoid hardcoding clang version
Update the workaround for https://github.com/rust-lang/rust/issues/109717 to
avoid hardcoding the clang version; instead, run `clang -dumpversion` to figure
it out.

While we're there, use the `CC_x86_64-linux-android` env var, which should
point to clang, rather than relying on `ANDROID_NDK_HOME` to be set.
2024-12-12 12:54:00 +00:00
Benjamin Bouvier 3356e0cc82 refactor(state store): use a single lock for all memory store accesses
The `MemoryStore` implementation of the `StateStore` has grown into a
monster, with one lock per field. It's probably overkill, as individual
fields don't need fine-grained locks like this; after all, accesses to
the store shouldn't be reentrant in general.

Fixes #3720.
2024-12-12 10:04:09 +01:00
Richard van der Hoff fda374ee81 feat(ffi): Add new properties to UnableToDecryptInfo
Followup to https://github.com/matrix-org/matrix-rust-sdk/pull/4360: expose
the new properties via FFI
2024-12-11 18:42:28 +00:00
Benjamin Bouvier 0264e49968 task(event cache): rename a few things
- rename RawLinkedChunk -> RawChunk
- rename RawChunk::id -> RawChunk::identifier
- precise that a `RawChunk` is mostly a `Chunk` with different
previous/next links.
2024-12-11 12:10:24 +01:00
Benjamin Bouvier d42c449612 refactor(event cache): only store a prev-batch token if the timeline was limited 2024-12-11 12:10:24 +01:00
Benjamin Bouvier 925d10f2ff task(event cache store): include the number of added items in one log 2024-12-11 12:10:24 +01:00
Benjamin Bouvier 4402f59e74 refactor(event cache): spawn a task to handle updates to the event cache store 2024-12-11 12:10:24 +01:00
Benjamin Bouvier 20184552a8 feat(event cache): start with an empty linked chunk if reloading failed 2024-12-11 12:10:24 +01:00
Benjamin Bouvier 832fedb05e feat(event cache): display raw linked chunks from storage when they fail to be rebuilt 2024-12-11 12:10:24 +01:00
Benjamin Bouvier eeb14f6cbe refactor!(event cache store): have the event cache store return raw linked chunks, not the full linked chunk
And let the caller rebuild the linked chunk. This is slightly nicer in
that it allows us to display the raw representation of a reloaded linked
chunk, before checking its internal state is consistent; this will allow
for better debug of issues related to the linked chunk internal state.

No functional changes.
2024-12-11 12:10:24 +01:00
Kévin Commaille a562f73b1e doc(timeline): Document media caching of send_attachment
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-12-11 10:45:39 +01:00
Kévin Commaille 7295f29055 doc(send_queue): Document media caching of send_attachment
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-12-11 10:45:39 +01:00
Kévin Commaille 723d7973d5 fix(send_queue): Use MediaFormat::File when caching attachment thumbnail
The `MediaFormat` reflects only the request that would be made to the homeserver.
There is no link between the format of the files stored in the media cache and their purpose in an event.

`MediaFormat::Tumbnail` means that we request a server-generated thumbnail of a file in the media repository.

Since the thumbnail is its own file in the media repository, it makes more sense to use `MediaFormat::File`.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-12-11 10:45:39 +01:00
Stefan Ceriu d5e7a9c949 chore(ui): rename date divider is_same_date_as to is_same_date_divider_group_as 2024-12-10 14:11:48 +02:00
Stefan Ceriu 8f064581d6 chore(ui): rename the day_dividers module to date_dividers 2024-12-10 14:11:48 +02:00
Stefan Ceriu 634edf2b65 chore(ui): rename all timeline "day dividers" to "date deviders" following the introduction of montly divider mode 2024-12-10 14:11:48 +02:00
Stefan Ceriu 935e4df927 feat(ui): make the timeline date separators configurable; have them appear either when the day changes or when the month changes. 2024-12-10 14:11:48 +02:00
Richard van der Hoff 1d72d2774f feat(ui): Add more properties to UnableToDecryptInfo 2024-12-10 11:36:04 +00:00
Richard van der Hoff 1e72131e7f feat(ui) Add UnableToDecryptInfo::user_trusts_own_identity 2024-12-10 11:36:04 +00:00
Richard van der Hoff e8b3949db3 feat(ui): Add UnableToDecryptInfo::event_local_age_millis 2024-12-10 11:36:04 +00:00
Richard van der Hoff c501a39ad4 refactor(sdk): Add Encryption::device_creation_timestamp
... so that we can use it in more places
2024-12-10 11:36:04 +00:00
Richard van der Hoff a04f9187f8 refactor(ui): store UTD info within PendingUtdReport
... making it easier to report late decryptions.
2024-12-10 11:36:04 +00:00
Benjamin Bouvier 32e2070f56 refactor(ffi): use a bool instead of an option to make the API less awkward
By default, the event cache store will be disabled. If disabled, it will
clean all the events in the cache store; most of the time this will do
nothing, since the store will not even be filled with any event data, so
it would be cheap to do. If some data was filled in the cache store
before, then it would be cleared after the cache store has been
disabled.

This makes a less awkward API than the previous one, where `None` and
`Some(false)` carried different semantics.
2024-12-10 12:05:29 +01:00
Benjamin Bouvier 4ee96aaffc feat(event cache): add a way to clear a single room's persistent storage 2024-12-10 12:05:29 +01:00
Benjamin Bouvier 0783cf89ba feat(ffi): add a feature flag to enable persistent storage for the event cache 2024-12-10 12:05:29 +01:00
Benjamin Bouvier cf02e694f2 feat(event cache store): add a method to clear all rooms' linked chunks 2024-12-10 12:05:29 +01:00
Ivan Enderlin cf178d603c doc(ui): Fix typos. 2024-12-10 11:36:05 +01:00
Ivan Enderlin ee94c86164 doc(ui): Fix a typo. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 3526761580 doc(ui): Unfold a Self in the doc. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 9a08975c8e doc(ui): Explain why ObservableItemsEntries does not implement Iterator. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 6b56c9efd8 doc(ui): Explain why Deref is fine, but DerefMut is not. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 0f2ada0958 refactor(ui): Rename ObservableItems::clone to clone_items. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 0d17ea353f refactor(ui): Replace a panic by a sensible value + error!. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 13e26b13e7 doc(ui): Rephrase the documentation of ObservableItems::all_remote_events. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 72f1bd6180 doc(ui): Document ObservableItems::items more and TimelineItemKind. 2024-12-10 11:36:05 +01:00
Ivan Enderlin e32ea1627e doc(ui): Fix typos. 2024-12-10 11:36:05 +01:00
Ivan Enderlin ed1f2e29ed refactor(ui): ObservableItemsTransactionEntry::remove is no longer unsafe 2024-12-10 11:36:05 +01:00
Ivan Enderlin 92cb18207e test(ui): Write test suite for ObservableItems. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 80f6b8d2cd test(ui): Write test suite for AllRemoteEvents.
This patch adds test suite for `AllRemoteEvents`.
2024-12-10 11:36:05 +01:00
Ivan Enderlin 05969fefde chore: Make Clippy happy. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 81c962238a doc(ui): Add more documentation for AllRemoteEvents. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 56218ee5d7 refactor(ui): Create ObservableItemsTransactionEntry.
This patch creates `ObservableItemsTransactionEntry` that mimics
`ObservableVectorTransactionEntry`. The differences are `set` is
renamed `replace`, and `remove` is unsafe (because I failed to update
`AllRemoteEvents` in this method due to the borrow checker).
2024-12-10 11:36:05 +01:00
Ivan Enderlin aa9138b281 doc(ui): Add more documentation for ObservableItemsTransaction. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 6f231523b3 refactor(ui): Create ObservableItemsEntries and ObservableItemsEntry.
This patch creates `ObservableItemsEntries` and particularly
`ObservableItemsEntry` that wraps the equivalent
`ObservableVectorEntries` and `ObservableVectorEntry` with the
noticeable difference that `ObservableItemsEntry` does **not** expose
the `remove` method. It only exposes `replace` (which is a renaming
of `set`).
2024-12-10 11:36:05 +01:00
Ivan Enderlin 943b3fbd91 refactor(ui): Rename ObservableItems::set to replace.
This patch renames `ObservableItems(Transaction)::set` to `replace`, it
conveys the semantics a bit better for new comers.
2024-12-10 11:36:05 +01:00
Ivan Enderlin 40ff880597 doc(ui): Add more documentation. 2024-12-10 11:36:05 +01:00
Ivan Enderlin 0647be1bc3 refactor(ui): Move AllRemoteEvents inside observable_items.
This patch moves `AllRemoteEvents` inside `observable_items` so that
more methods can be made private, which reduces the risk of misuses
of this API. In particular,  the following methods are now strictly
private:

- `clear`
- `push_front`
- `push_back`
- `remove`
- `timeline_item_has_been_inserted_at`
- `timeline_item_has_been_removed_at`

In fact, now, all `&mut self` method (except `get_by_event_id_mut`) are
now strictly private!
2024-12-10 11:36:05 +01:00
Ivan Enderlin b069b20e18 refactor(ui): Create ObservableItems(Transaction). 2024-12-10 11:36:05 +01:00
Ivan Enderlin 91b73a2b16 refactor(ui): Maintain timeline_item_index when timeline items are inserted or removed.
This patch maintains the `timeline_item_index` when timeline items are
inserted or removed.
2024-12-10 11:36:05 +01:00
Ivan Enderlin 14d0f6877a refactor(ui): Maintain timeline_item_index when remote events are manipulated.
This patch maintains the `timeline_item_index` when a new remote events
is added or removed.
2024-12-10 11:36:05 +01:00
Ivan Enderlin a2210bce48 refactor(ui): Add EventMeta::timeline_item_index.
This is the foundation for the mapping between remote events and
timeline items.
2024-12-10 11:36:05 +01:00
Benjamin Bouvier 68cb85a2b2 refactor(event cache store): use a single transaction to handle all linked chunk updates at once
Instead of one transaction per update. This ensures that if a single
update fails, then none is taken into account.
2024-12-10 11:32:30 +01:00
Jonas Richard Richter 72fcc50f80 feat(ffi): Expose the method to send custom events with JSON content (#4390)
This patch adds the Room::send_raw method to the bindings, making it usable from
e.g. Swift.

Signed-off-by: Jonas Richard Richter <jonas-richard.richter@telekom.de>
2024-12-10 11:03:31 +01:00
Andy Balaam 5721c3622d refactor(key_backups): Rename fast_exists_on_server to exists_on_server 2024-12-09 16:33:56 +00:00
Andy Balaam 50eb46dc82 refactor(key_backups): Rename exists_on_server to fetch_exists_on_server 2024-12-09 16:33:56 +00:00
Andy Balaam 8aae16ffd7 feat(crypto) Provide a method to check whether server backup exists without hitting the server every time 2024-12-09 16:33:56 +00:00
Benjamin Bouvier e402ed4ce8 refactor(event cache): get the *most recent* pagination token, not the *oldest* one
Whenever it needs to back-paginate, the event cache should start with
the *most recent* backpagination token, not the oldest one.

This isn't a functional change, until the persistent storage is enabled.
The reason is that, currently, there is one previous-batch token alive;
after it's used, it's replaced with another gap and the events it served
to request from the server.

When persistent storage will be enabled, we'll have situations like the
one shown in the test code, where we can have multiple previous-batch
token alive at the same time. In that case, we'll need to back-paginate
from the most recent events to the least recent events, and not the
other way around, or we'll have holes in the timeline that won't be
filled until we got to the start of the timeline.
2024-12-09 15:57:14 +01:00
Kévin Commaille a1a04ee513 chore: Remove MSRV from READMEs
It can be found in Cargo.toml.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-12-09 16:22:00 +02:00
Benjamin Bouvier affdc25256 refactor(linked chunk): rename len() to num_items()
This makes it clearer that it's only concerned about the number of
items, not the number of chunks.
2024-12-09 14:46:21 +01:00
Benjamin Bouvier 8db78efbbc fix(event cache): use a correcter heuristic to decide whether to add initial events or not
Thanks Hywan for spotting the issue.
2024-12-09 14:46:21 +01:00
Kévin Commaille d8184e72eb fix(media): Make sure that local MXC URIs only try to get media from the cache and ignore requested dimensions (#4387)
Extracted from #4329. This does not change the `MediaFormat` of the
request used in the media cache by the send queue.

---------

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-12-09 13:43:49 +01:00
torrybr 3bd57d4307 feat(sdk): support for observing m.beacon events 2024-12-09 10:33:37 +01:00
Kévin Commaille 42193f1b06 chore(xtask): Remove unnecessary lifetime
`const` variables are always `'static`. Detected by clippy.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-12-08 16:57:22 +01:00
Kévin Commaille a277e6d37f chore(xtask): Disable unexpected_cfgs lint
It is triggered by the `xshell::cmd!` macro, and is fixed in xshell 0.2.7, which we cannot upgrade to.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-12-08 16:57:22 +01:00
Benjamin Bouvier bf6fa4cd55 fix(event cache): don't fill initial items if the room already had events (#4381)
The test requires subtle conditions to trigger:

- initialize a timeline from a room-list-service's room
- start a backpagination with that timeline (so the room event cache's
paginator is busy)
- try to initialize another timeline with the same room-list-service's
room (e.g. because the first room has been closed, and the app using it
doesn't have a room cache)

This would fail, because initializing a timeline calls
`EventCache::add_initial_events()` all the time, which tries to reset
the paginator's state, which assumes the paginator's not paginating at
this point. In a soon future, we'll get rid of the
`add_initial_events()` function because the event cache will handle its
own persistent storage; in the meantime, a correct fix is to skip
`add_initial_events()` if there was already something in the linked
chunk. After all, we're likely to fill the initial events with the same
events all the time, or a subset of more recent events. By doing that,
we're likely keeping *more* events in the linked chunk, instead.

Thanks to @stefanceriu for reporting the issue and confirming the fix
works!
2024-12-06 12:37:34 +01:00
Damir Jelić 6501a44e6a feat: Add support for MSC4171
Introduce support for MSC4171, enabling the designation of certain users
as service members. These flagged users are excluded from the room
display name calculation.

MSC: https://github.com/matrix-org/matrix-spec-proposals/pull/4171
2024-12-05 14:23:36 +01:00
Mathieu Velten ee30008f38 feat: Accept any string as a key for m.direct account data (#4228)
This is the follow up of this [Ruma
PR](https://github.com/ruma/ruma/pull/1946) for the SDK.

---------

Signed-off-by: Mathieu Velten <mathieu@velten.xyz>
Co-authored-by: Benjamin Bouvier <benjamin@bouvier.cc>
2024-12-05 12:37:16 +00:00
Damir Jelić 22cb8a1878 chore: Bump the pprof version to fix a security issue 2024-12-05 12:08:25 +01:00
Timo 111f916a78 feat(WidgetDriver): Pass Matrix API errors to the widget (#4241)
Currently the WidgetDriver just returns unspecified error strings to the
widget that can be used to display an issue description to the user. It
is not helpful to run code like a retry or other error mitigation logic.

Here it is proposed to add standardized errors for issues that every
widget driver implementation can run into (all matrix cs api errors):
https://github.com/matrix-org/matrix-spec-proposals/pull/2762#discussion_r1838804895

This PR forwards the errors that occur during the widget processing to
the widget in the correct format.

NOTE:
It does not include request Url and http Headers. See also:
https://github.com/matrix-org/matrix-spec-proposals/pull/2762#discussion_r1839802292

Co-authored-by: Benjamin Bouvier <benjamin@bouvier.cc>
2024-12-04 16:52:02 +00:00
Benjamin Bouvier a6e1f05957 test(event cache): use the MatrixMockServer for integration testing 2024-12-04 17:18:45 +01:00
Benjamin Bouvier 0b64c68191 test(event cache): make use of macros to avoid manual timeouts 2024-12-04 17:18:45 +01:00
Benjamin Bouvier 713039279c Enable persistent storage in multiverse
And fix an issue that would cause a crash because a timeline wasn't
initialized and we tried to unwrap it later.
2024-12-04 17:18:45 +01:00
Benjamin Bouvier d317e5d73c feat(event cache): don't react specifically to limited timelines, when storage's enabled 2024-12-04 17:18:45 +01:00
Damir Jelić ee93c278df chore: Update the hashbrown version we're using 2024-12-04 16:31:15 +01:00
Valere 1009ea86ae Merge pull request #4305 from matrix-org/valere/support_for_withheld_reason
feat(crypto): Add optional withheld reason to `UnableToDecryptReason`
2024-12-04 15:57:22 +01:00
Damir Jelić 7d8e7af308 Revert "chore(ui): Unify the logic for timeline item insertions"
This reverts commit d2ecd745f6.
2024-12-04 15:55:49 +01:00
Damir Jelić 136522c694 Revert "doc(timeline): tweak comments when inserting a new item"
This reverts commit 197da2c585.
2024-12-04 15:55:49 +01:00
Valere 6801811226 feat(crypto): Supports new UtdCause variants for withheld keys
Adds new UtdCause variants for withheld keys, enabling applications to display customised messages when an Unable-To-Decrypt message is expected.

refactor(crypto): Move WithheldCode from crypto to common crate
2024-12-04 15:33:23 +01:00
Benjamin Bouvier a4434d79c9 feat(event cache): strip bundled relations before persisting events 2024-12-04 12:35:58 +01:00
Benjamin Bouvier e0b1b5dc05 test: don't use the event cache storage but regular syncs instead 2024-12-04 12:35:46 +01:00
Benjamin Bouvier 1a63d8f0b7 task(event cache): ignore add_initial_events() when the event cache storage has been enabled
Not worth testing IMO, since this is about the "temporary" API we're
going to remove in subsequent patches.
2024-12-04 12:35:46 +01:00
Benjamin Bouvier 5bf3b11edf test: rewrite test_send_edit_when_timeline_is_clear to not use add_initial_items
Moar MatrixMockServer \o/
2024-12-04 12:35:46 +01:00
Benjamin Bouvier 8f1722f2a8 test: have the PendingEdit test helper use the matrix mock server and event cache storage 2024-12-04 12:35:46 +01:00
Benjamin Bouvier 5d95387935 test: remove unused adding of initial events in sliding sync test helper 2024-12-04 12:35:46 +01:00
Benjamin Bouvier bd93a9a40e test: remvoe use of add_initial_events in test_add_initial_events
And make it a smoke test that the event cache correctly gets events it
retrieves from sync.
2024-12-04 12:35:46 +01:00
Benjamin Bouvier 5cde4a6630 test: remove use of add_initial_events in test_ignored_unignored 2024-12-04 12:35:46 +01:00
Benjamin Bouvier de5511f009 test: rewrite test_ignored_unignored so it makes use of the MatrixMockServer 2024-12-04 12:35:46 +01:00
Damir Jelić 9bdd9fa831 chore: Update the RELEASING file so it doesn't mention git-cliff anymore 2024-12-04 11:25:00 +01:00
Damir Jelić 48bb3dbbe7 chore: Update our contributing guide for the manual changelog entries 2024-12-04 11:25:00 +01:00
Damir Jelić b8bf847fc1 chore: Set up cargo-release to update the versions in the changelog 2024-12-04 11:25:00 +01:00
Damir Jelić 17812b6949 chore: Remove our cliff config and don't use cliff to generate changelogs 2024-12-04 11:25:00 +01:00
Damir Jelić bab979aaf4 chore: Update our changelogs 2024-12-04 11:25:00 +01:00
Damir Jelić 42778dc79d chore: Replace git-cliff in the weekly-report command 2024-12-04 11:25:00 +01:00
Damir Jelić a948be9c85 chore: Downgrade xshell due to broken stdin interactions
Since xshell 0.2.3 the behavior of the run() function has changed in a
incompatible manner. Namely the stdin for the run() function no longer
inherits stdin from the shell. This makes it impossible for commands
that are executed by the run() function to accept input from the shell.

We don't use this functionality in many places but the `xtask release
prepare` command is now broken.

Let's just pin xshell to a working version while we wait for this to be
resolved upstream.

Upstream-issue: https://github.com/matklad/xshell/issues/63
2024-12-04 11:22:43 +01:00
Stefan Ceriu 9c381c1022 feat(ffi): expose a generic message_filtered_timeline 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.
Instances of these timelines will be used to power the 2 different tabs shown on the new media browser. The client will be responsible for interacting with it similar to a normal timeline and transforming its data into something renderable e.g. section by date separators (which will be made configurable in a follow up PR)
2024-12-04 11:41:25 +02:00
Andy Balaam 9002f82659 task(backup_tests): Move mock helpers into MatrixMockServer 2024-12-03 14:55:41 +00:00
Andy Balaam 5f7fb4699a task(backup_tests): Split exists_on_server test into separate tests 2024-12-03 14:55:41 +00:00
Andy Balaam 5907104e0e task(backup_tests): Use helper functions to shorten exists_on_server tests 2024-12-03 14:55:41 +00:00
Ivan Enderlin d7dff5b026 refactor(ui): Add AllRemoteEvents::get_by_event_id_mut.
Having a mutable iterator can be dangerous and is probably too generic
regarding the safety we want to add around the `AllRemoteEvents` type.

This patch removes `iter_mut` and replaces it by its only use case:
`get_by_event_id_mut`.
2024-12-03 13:52:42 +01:00
Ivan Enderlin cabde8ed11 refactor(ui): AllRemoteEvents::back becomes last to add semantics.
This patch renames `AllRemoteEvents::back` to `last` so that it now gets
a specific semantics instead of being generic.
2024-12-03 13:52:42 +01:00
Ivan Enderlin b02fd92ad0 refactor(ui): Introduce the AllRemoteEvents type.
This patch replaces `VecDeque<EventMeta>` by `AllRemoteEvents` which
is a wrapper type around `VecDeque<EventMeta>`, but this new type aims
at adding semantics API rather than a generic API. It also helps to
isolate the use of these values and to know precisely when and how they
are used.

As a first step, `AllRemoteEvents` implements a generic API to not break
the existing code. Next patches will revisit that a little bit step
by step.
2024-12-03 13:52:42 +01:00
Ivan Enderlin 9be8578aff doc(ui): Explain why all_remote_events is necessary. 2024-12-03 13:26:26 +01:00
Ivan Enderlin 4f28dd85bf refactor: TimelineStateTransaction::add_or_update_remote_event always return true.
The `add_or_update_remote_event` method always returns `true`. This
patch updates the method to return nothing, and cleans up the call sites
accordingly. This patch also adds comments to clarify the code flow.

The bool value returned by `add_or_update_remote_event` was supposed
to be `false` if the event was duplicated. First off, as soon as the
`Timeline` receives its events from the `EventCache` via `VectorDiff`,
the `event_cache::Deduplicator` will take care of deduplication,
so the `Timeline` won't have to handle that itself. Second,
`add_or_update_remote_event` was sometimes removing an event, but it
was re-inserting a new one immediately without returning `false`: it was
never returning `false` because a new event was always added.
2024-12-03 13:26:26 +01:00
Damir Jelić 74119e8861 Revert "chore: Remove Ruma from the cargo-deny git dep allow list"
This reverts commit f256fe4b24.

As discussed, we want to prioritize the testing of new Ruma features
over stability.
2024-12-03 12:58:37 +01:00
Timo e76b8f7e15 tests: Refactor the widget tests to use MockMatrixServer (#4236)
This is a follow up PR on https://github.com/matrix-org/matrix-rust-sdk/pull/3987.

And tries to use the `MockMatrixServer` wherever reasonable in the
widget integration tests.

---------

Co-authored-by: Benjamin Bouvier <benjamin@bouvier.cc>
2024-12-03 08:36:24 +00:00
Integral 31bd5c6790 refactor: replace static with const for global constants
Signed-off-by: Integral <integral@member.fsf.org>
2024-12-03 09:13:57 +01:00
Andy Balaam 50f036d283 task(crypto_tests): Fix test that will fail when we handle backups correctly
This test was checking that a new device which has access to backups
returned an unknown UTD when it was given empty JSON for the event. It
was only passing because we currently have incorrect behaviour when
backups are enabled.

The fix is to make the device old and without access to backups, so that
we still consider this UTD unexpected.
2024-12-02 17:34:44 +00:00
Andy Balaam 8c73f0c655 task(crypto_tests): Remove duplicate test 2024-12-02 17:34:44 +00:00
Andy Balaam 8de76deb1b task(crypto_tests): Re-arrange and reword utd_cause tests 2024-12-02 17:34:44 +00:00
Andy Balaam b65728d46f task(crypto_tests): Shorten utd_cause tests with helper functions for devices 2024-12-02 17:34:44 +00:00
Andy Balaam 0b4b4ea791 task(crypto_tests): Shorten utd_cause tests with utility functions for UnableToDecryptInfos 2024-12-02 17:34:44 +00:00
Andy Balaam 552ab81739 task(crypto_tests): Add comments and clarify tests for determining UTD causes 2024-12-02 17:34:44 +00:00
dependabot[bot] d49d12249a build(deps): bump crate-ci/typos from 1.27.3 to 1.28.2
Bumps [crate-ci/typos](https://github.com/crate-ci/typos) from 1.27.3 to 1.28.2.
- [Release notes](https://github.com/crate-ci/typos/releases)
- [Changelog](https://github.com/crate-ci/typos/blob/master/CHANGELOG.md)
- [Commits](https://github.com/crate-ci/typos/compare/v1.27.3...v1.28.2)

---
updated-dependencies:
- dependency-name: crate-ci/typos
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-12-02 17:19:21 +01:00
Gabriel Féron ed1d406b72 fix(store): fix indexing issue in ObservableMap (#4346)
Any `.remove` operation called on a `ObservableMap` did not re-index
`values` _after_ the removed position, which means any later operation
on elements inserted after the previously removed key would either be
fetched wrongly, or panic when using `.get_or_create`.

This PR fixes these two related bugs and adds extra test cases.

---------

Signed-off-by: g@leirbag.net
Co-authored-by: Benjamin Bouvier <benjamin@bouvier.cc>
2024-12-02 16:00:19 +00:00
Ivan Enderlin 80a48f53ad refactor: Rename add_or_update_event to add_or_update_remote_event. 2024-12-02 16:45:35 +01:00
Ivan Enderlin 51cfaaacee refactor: Rename TimelineMetadata::all_events to all_remote_events.
This patch renames the `all_events` field to `all_remote_events` in
`TimelineMetada` for the sake of clarity.
2024-12-02 16:45:35 +01:00
Benjamin Bouvier 2f9866cf04 test(event cache): test that the event cache correctly reads updates 2024-12-02 16:27:05 +01:00
Benjamin Bouvier 7de74e2c04 feat(event cache): reload the linked chunk from the store, if storage's enabled 2024-12-02 16:27:05 +01:00
Benjamin Bouvier 019de4ffa0 test(event cache): add a test that the event cache correctly stores updates 2024-12-02 16:27:05 +01:00
Benjamin Bouvier 9f1e3c179b feat(event cache): propagate linked chunk in-memory updates to storage (conditionally) 2024-12-02 16:27:05 +01:00
Damir Jelić 17e17f0b9c ci: Build the Mac framework using the reldbg profile 2024-12-02 15:54:47 +01:00
Benjamin Bouvier 5da36d13c8 fix(event cache): consider empty items chunks in the memory store 2024-12-02 14:09:42 +01:00
Benjamin Bouvier cce322f9c8 test(event cache): add integration test for handling updates and reloading a linked chunk 2024-12-02 14:09:42 +01:00
Benjamin Bouvier ed3b03f454 feat(event cache): implement reloading a linked chunk from the memory store too 2024-12-02 14:09:42 +01:00
Benjamin Bouvier 27e1cded2e feat(event cache): reload a linked chunk from a sqlite store 2024-12-02 14:09:42 +01:00
Ivan Enderlin ad3d1fb6b3 refactor(ui): Use an iterator instead of Vec to represent events.
This patch changes `TimelineStateTransaction::add_remote_events_at`
to take an `IntoIterator<Item = Into<SyncTimelineEvent>>`
for `events`. In the current code, it saves one
`iter().map(Into::into).collect::<Vec<_>>()`, but it will save another
one when we will support `VectorDiff`s coming from the `EventCache`.

It also avoids to allocate a vector to pass new events (this mostly
happens in the test, but it can happen in real life).
2024-12-02 13:09:07 +01:00
Jorge Martín d2fecb6701 fix(room_preview): When requesting a room summary, use fallback server names
If no server names are provided for the room summary request and the
room's server name doesn't match the current user's server name, add the
room alias/id server name as a fallback value. This seems to fix room
preview through federation.

Also, when getting a summary for a room list item, if it's an invite
one, add the server name of the inviter's user id as another possible
fallback.

Changelog: Use the inviter's server name and the server name from the
room alias as fallback values for the via parameter when requesting the
room summary from the homeserver.
2024-12-02 12:11:47 +01:00
Kévin Commaille 685386df13 chore(xtask): Fix scope of push_dir
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-11-30 18:03:54 +01:00
Kévin Commaille f94b202341 chore(xtask): Upgrade xshell
Gets rid of an unexpected_cfgs warning.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-11-30 18:03:54 +01:00
Kévin Commaille d1a6956e77 chore(sdk): Disable unused_async clippy lint for unimplemented method
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-11-30 09:21:23 +01:00
Damir Jelić 2d2215edbe chore: Make use of the member event builder in the EventFactory 2024-11-30 09:20:49 +01:00
Damir Jelić bcd0d20e2f test: Add a method to build m.room.member events in the EventFactory 2024-11-30 09:20:49 +01:00
Kévin Commaille ba5881355d chore(test): Upgrade ctor
Fixes the `unexpected_cfgs` warning so it doesn't need to be disabled anymore.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-11-29 19:59:24 +01:00
Andy Balaam 1072d0a019 chore(js_tracing): Elide explicit lifetime to satisfy clippy 2024-11-29 18:45:45 +01:00
Damir Jelić 783c86aa78 ci: Build the Mac framework in release mode
The dev profile fails with a linker issue about not finding the
__chkstk_darwin symbol.
2024-11-29 18:45:45 +01:00
Damir Jelić 5564fe8852 ci: Bump the mac OS runner to 15 2024-11-29 18:45:45 +01:00
Damir Jelić e1f0037fd5 chore: Define the lifetime of some const strings explicitly 2024-11-28 11:53:35 +01:00
Benjamin Bouvier daa984f7de feat(event cache store): enable foreign keys pragma \o/ 2024-11-28 11:48:46 +01:00
Benjamin Bouvier aa0eb760de test(event cache): add a test for reading events from multiple rooms
This was to make sure that we can search by blob.
2024-11-28 11:48:46 +01:00
Benjamin Bouvier 9ed65bc321 task(event cache): address review points 2024-11-28 11:48:46 +01:00
Benjamin Bouvier ce95b6089f doc(event cache): add the copyright notice and basic module doc comment 2024-11-28 11:48:46 +01:00
Benjamin Bouvier c6ba71ae33 feat(event cache): allow reloading from the store, and test functionalities
This required adding support for *reading* out of the event cache, for
the sqlite backend. This paves the way for the next PR (reload from the
cache), and it should also help with testing at the `EventCacheStore`
trait layer some day.
2024-11-28 11:48:46 +01:00
Benjamin Bouvier e57d38cf57 doc(common): add a note that a decrypted raw event always has a room id 2024-11-28 11:48:46 +01:00
Benjamin Bouvier 9bea0cff24 feat(event cache): implement the sqlite backend for events 2024-11-28 11:48:46 +01:00
Benjamin Bouvier 197da2c585 doc(timeline): tweak comments when inserting a new item
A comment was duplicating (the first trace! that's removed here), and
the second block comment only applied to new items, and was not as
concise as it could be.
2024-11-28 10:09:50 +01:00
Ivan Enderlin d2ecd745f6 chore(ui): Unify the logic for timeline item insertions
This patch unifies the logic for inserting timeline items at `Start`
and `End` positions. Both `TimelineItemPositions` can share the same
implementation, making separate logic unnecessary. Previously, `End`
included a duplicated events check as well, while `Start` did not, leading
to inconsistency.

The changes strictly involve moving and refactoring, with no functional
modifications.
2024-11-28 08:25:16 +01:00
Damir Jelić e99939db85 refactor(crypto): Rename the IncomingResponse enum to AnyIncomingResponse 2024-11-27 19:55:27 +01:00
Damir Jelić 600a708e7b refactor!(crypto): Rename the OutgoingRequests enum to AnyOutgoingRequest 2024-11-27 19:55:27 +01:00
Damir Jelić a94a5f1716 chore(crypto): Split out the requests module 2024-11-27 19:55:27 +01:00
Damir Jelić 46064680ce refactor!(crypto): Don't re-export the request types from the request module 2024-11-27 19:55:27 +01:00
Damir Jelić 6fe5acfc97 refactor(crypto): Move the requests module under the types module 2024-11-27 19:55:27 +01:00
Valere 3369903766 Merge pull request #4275 from matrix-org/valere/utd_hook_historical_message
feat(utd_hook): Report device-historical expected UTD with new reason
2024-11-27 18:23:53 +01:00
Valere a0c86d9645 feat(utd_hook): Report historical expected UTD with new reason
This PR introduces a new variant to `UtdCause` specifically for device-historical messages (`HistoricalMessage`). These messages cannot be decrypted if key storage is inaccessible. Applications can leverage this new variant to provide more informative error messages to users.
2024-11-27 18:09:06 +01:00
Damir Jelić 7a454888a3 chore: Bump the deps and move some of them to the workspace 2024-11-27 17:03:50 +01:00
Ivan Enderlin 37f52e1c6c fix(common): LinkedChunk emits an Update::NewItemsChunk when constructed.
This patch updates `LinkedChunk::new_with_update_history` to emit an
`Update::NewItemsChunk` because the first chunk is created and it must
emit an update accordingly.
2024-11-27 14:40:26 +01:00
Ivan Enderlin 185423539e test(ui): Fix the test_echo test.
This patch fixes the `test_echo` test. It was doing the following:

* client sends an event to the server,
* servers acknowledges with the ID `$wWgymRfo7ri1uQx0NXO40vLJ`,
* client syncs and the server returns one event with ID `$7at8sd:localhost`,
* the test expects those 2 events to be the same!, which is incorrect.

The test was working because the transaction IDs are the same, but
that's an abuse of the existing code (the code will change soon, another
patch is coming). Whatever the code does: the connection must be based
on the event ID, not the transaction ID.
2024-11-27 14:29:24 +01:00
Stefan Ceriu 9e20659d5d chore: bring back MediaSource JSON serialization methods 2024-11-27 15:13:24 +02:00
Damir Jelić 7783188769 chore: Box the OidcSession so the AuthSession enum isn't unnecessarily big 2024-11-27 13:23:34 +01:00
Damir Jelić 514af54c4c chore: Fix some clippy warnings about our docs 2024-11-27 13:23:34 +01:00
Damir Jelić ad615b7612 chore: Fix some clippy lint warnings around the usage of map_or 2024-11-27 13:23:34 +01:00
Damir Jelić a1b7906a7d chore: Fix some clippy lints around lifetimes 2024-11-27 13:23:34 +01:00
Damir Jelić 79c8d2c345 chore: Don't build the docs for xtask
Building the docs for xtask spews a bunch of unexpected cfg warnings. As
these warnings come from a macro in a dependency and the docs for xtask
don't exist nor will, let's just not build them with the rest of the
docs.
2024-11-27 13:23:34 +01:00
Damir Jelić dcf6af405d chore: Silence unexpected cfg warnings
These are all coming from macro invocations of macros that are defined
in other crates. It's likely a clippy issue. We should try to revert
this the next time we bump the nightly version we're using.
2024-11-27 13:23:34 +01:00
Damir Jelić bb598b61a5 chore: Bump the nightly version we use for the CI 2024-11-27 13:23:34 +01:00
Ivan Enderlin 1c554c4912 chore(ui): Clarifies what TimelineItemPosition::UpdateDecrypted holds.
This patch tries to clear confusion around
`TimelineItemPosition::UpdateDecrypted(usize)`: it does contains
a timeline item index. This patch changes to
`TimelineItemPosition::UpdateDecrypted { timeline_item_index: usize }`
2024-11-27 12:04:59 +01:00
Benjamin Bouvier 21f8b7ed31 refactor(linked chunk): simplify further impl of the LinkedChunkRebuilder 2024-11-27 11:01:44 +01:00
Benjamin Bouvier 23ee8e25dd feat(linked chunk): add a way to reconstruct a linked chunk from its raw representation 2024-11-27 11:01:44 +01:00
Benjamin Bouvier 1098095846 refactor(linked chunk): replace LinkedChunk::len() with a simpler implementation
It's unused so it's mostly cosmetic, and it's trivial to reimplement
using `linked_chunk.items().count()`; let's do that instead of keeping
the perfect exact count synchronized with the chunks, which pollutes the
code in a few places.
2024-11-27 10:16:12 +01:00
Ivan Enderlin 3e7d7e8a31 chore(ui): Rename TimelineEnd to TimelineNewItemPosition.
This patch renames `TimelineEnd` into `TimelineNewItemPosition` for
2 reasons:

1. In the following patches, we will introduce a new variant to insert
   at a specific index, so the suffix `End` would no longer make sense.

2. It's exactly like `TimelineItemPosition` except that it's used
   only and strictly only to add **new** items, which is why we can't use
   `TimelineItemPosition` because it contains the `UpdateDecrypted`
   variant. This renaming reflects it's only about **new** items.

This patch takes the opportunity to move the `RemoteEventOrigin` inside
`TimelineNewItemPosition` to simplify method signatures. They always
go together.
2024-11-26 20:29:31 +01:00
Benjamin Bouvier 2c45316bcb fixup! fix(room): make Room::history_visibility() return an Option 2024-11-26 19:02:46 +01:00
Benjamin Bouvier 8dc7c1f876 fix(ui): have the room list service require the create and history visibility events
These two are required to properly compute the room preview of a joined
room:

- m.room.create ends up filling the `room_type` (space or not)
- m.room.history_visibility ends up filling the `is_world_readable`
  field.
2024-11-26 19:02:46 +01:00
Benjamin Bouvier db84936dcd fix(room): make Room::history_visibility() return an Option
And introduce `Room::history_visibility_or_default()` to return a better
sensible default, according to the spec.
2024-11-26 19:02:46 +01:00
Kévin Commaille 75d7d07013 chore(ffi): Fix thumbnail size info
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-11-26 15:45:54 +01:00
Kévin Commaille d4d5f45edc feat(media)!: Make all fields of Thumbnail required
It seems sensible to assume that if a client is able to generate a thumbnail,
it should be able to get all this information for it too.
A thumbnail with no information is not really useful, as we don't know when it could be used instead of the original image.

Removes `BaseThumbnailInfo`.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-11-26 15:20:07 +01:00
Kévin Commaille d0257d1cb2 refactor(media): Add method to split Thumbnail into parts
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-11-26 15:20:07 +01:00
Kévin Commaille ecf44348cf fix(client): Do not use the encrypted original file's content type as the encrypted thumbnail's content type
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
2024-11-26 15:20:07 +01:00
Ivan Enderlin cc8bc05537 refactor: RoomEvents::reset really clear the linked chunk.
This patch updates `RoomEvents::reset` to not drop the `LinkedChunk` to
clear it.
2024-11-26 15:19:24 +01:00
Ivan Enderlin 728d646ce2 fix(common): AsVector clears its internal state on Update::Clear.
This patch fixes a bug in `AsVector`: when an `Update::Clear` value
is received, `AsVector`'s internal state must be cleared too, i.e. the
`UpdateToVectorDiff::chunks` field should be reset to an initial value!

This patch adds a test to ensure this works as expected.
2024-11-26 15:19:24 +01:00
Stefan Ceriu ca397dca0f feat(ffi): wrap Ruma MediaSources and run validations before passing them over FFI
Ruma doesn't currently validate mxuri's and as such `MediaSource`s passed over FFI can contain invalid/empty URLs. This change introduces a wrapper type around Ruma's and failable transformations so that appropiate actions can be taken beforehand e.g. returning a `TimelineItemContent::FailedToParseMessageLike` or nil-ing out the thumbnail info.
2024-11-26 15:40:24 +02:00
Benjamin Bouvier 1fbe6815c3 task(event cache): log whenever we receive an ignore user list change 2024-11-26 12:29:34 +01:00
Ivan Enderlin c61f70727f fix: RelationalLinkedChunk handles Update::Clear.
What the title says.
2024-11-25 17:45:01 +01:00
Ivan Enderlin 2abbf58825 feat(common): Implement LinkedChunk::clear.
This patch implements `LinkedChunk::clear`. The code from `impl Drop
for LinkedChunk` has been moved inside `Ends::clear`, and replaced by
a simple `self.links.clear()`. In addition, `LinkedChunk::clear` indeed
calls `self.links.clear()` but also resets all fields.

This patch adds the `Clear` variant to `Update`.

This patch updates `AsVector` to emit a `VectorDiff::Clear` on
`Update::Clear`.

Finally, this patch adds the necessary tests.
2024-11-25 17:08:43 +01:00
Ivan Enderlin b979b2ea1e doc(common): Fix typos. 2024-11-25 17:08:27 +01:00
Ivan Enderlin 24b968ad39 refactor: EventCacheStore::handle_linked_chunk_updates takes a Vec<Update>.
This patch updates `EventCacheStore::handle_linked_chunk_updates` to
take a `Vec<Update<Item, Gap>>` instead of `&[Update<Item, Gap>]`.
In fact, `linked_chunk::ObservableUpdates::take()` already returns a
`Vec<Update<Item, Gap>>`; we can simply forward this `Vec` up to here
without any further clones.
2024-11-25 17:08:27 +01:00
Ivan Enderlin faa8aa2b9c fix(base): Move all fields of MemoryStore inside a StdRwLock<_>.
This patch creates a new `MemoryStoreInner` and moves all fields from
`MemoryStore` into this new type. All locks are removed, but a new lock
is added around `MemoryStoreInner`. That way we have a single lock.
2024-11-25 17:08:27 +01:00
Ivan Enderlin db9ee9d87b refactor: Add constructors for Position and ChunkIdentifier.
This patch adds constructors for `Position` and `ChunkIdentifier` so
that we keep their inner values private.
2024-11-25 17:08:27 +01:00
Ivan Enderlin 1dbb494b94 feat(common): RelationalLinkedChunk stores the RoomId. 2024-11-25 17:08:27 +01:00
Ivan Enderlin fe52b4cb78 feat(common): EventCacheStore::handle_linked_chunk_updates takes a &RoomId. 2024-11-25 17:08:27 +01:00
Ivan Enderlin 5519442ad8 doc(common): Fix a typo. 2024-11-25 17:08:27 +01:00
Ivan Enderlin 88363d8033 feat(base): MemoryStore uses RelationalLinkedChunk to store events.
That's it.
2024-11-25 17:08:27 +01:00
Ivan Enderlin fb5d8f29ac feat(common): Implement RelationalLinkedChunk.
A `RelationalLinkedChunk` is like a `LinkedChunk` but with a relational
layout, similar to what we would have in a database.

This is used by memory stores. The idea is to have a data layout that
is similar for memory stores and for relational database stores, to
represent a `LinkedChunk`.

This type is also designed to receive `Update`. Applying `Update`s
directly on a `LinkedChunk` is not ideal and particularly not trivial
as the `Update`s do _not_ match the internal data layout of the
`LinkedChunk`, they have been designed for storages, like a relational
database for example.

This type is not as performant as `LinkedChunk` (in terms of memory
layout, CPU caches etc.). It is only designed to be used in memory
stores, which are mostly used for test purposes or light usages of the
SDK.
2024-11-25 17:08:27 +01:00
Benjamin Bouvier 912b121d27 feat(timeline): make more errors transparent 2024-11-25 15:11:02 +01:00
Benjamin Bouvier 2e975d9b19 fix(base): all EventCacheStoreLock must refer to the same underlying cross-process lock
And not duplicate it once per `EventCacheStoreLock`.
2024-11-25 15:11:02 +01:00
Benjamin Bouvier edc93e62b4 task(sdk): expose the SqliteEventCacheStore from the SDK crate
And use it in multiverse.
2024-11-25 15:11:02 +01:00
Ivan Enderlin 9d6ffa951f doc(sdk): Specify how the Client::observe_events works. 2024-11-25 11:49:36 +01:00
Benjamin Bouvier 079ec023b7 task(oidc): add logs when refreshing an OIDC token 2024-11-25 10:58:50 +01:00
Damir Jelić e55a1c7e00 chore: Rework the crypto crate README 2024-11-22 18:20:38 +01:00
Damir Jelić ddd737e4d8 docs: Add a tutorial to the crypto crate
Changelog: Add a tutorial describing how to add end-to-end encryption
support to an existing library.
2024-11-22 18:20:38 +01:00
Mauro Romito 38a15afc9c build (apple): add dynamic type to debug package 2024-11-22 18:44:48 +02:00
Jorge Martín fa93daabd2 feat(ffi): Add RoomInfo::join_rule field to bindings
Breaking-Change: Add `RoomInfo::join_rule` field, remove `RoomInfo::is_public` in the FFI crate, as they contain the same info.
2024-11-22 16:09:55 +01:00
Jorge Martín 6b0987385e refactor(room_preview): make RoomPreview use the local known data only for joined rooms
When instantiating a room preview, previously it would try to just check if the room exists locally either as joined, invited, knocked, left, etc., and then retrieve the info we cached about it.

While this seems fine for most cases, it turns out for non-joined rooms, the info we have locally will **always** be the one we received when the invite/knock/leave action took place and it'll never be updated,
so we may have the case where we knock into a room, never receive a response, someone changes the join rule of the room to something else and we'll think about this room as a 'request to join' room until we clear the local cache.

To prevent that, we can only use the local data for joined rooms, which are constantly updated, and try to use the room summary API and other fallbacks for the rest, even if they're rooms known to us.
2024-11-22 14:20:50 +01:00
Benjamin Bouvier 48fbda844f fix(oidc): make sure we keep track of an ongoing OIDC refresh up to the end
There's a lock making sure we're not doing multiple refreshes of an OIDC
token at the same time. Unfortunately, this lock could be dropped, if
the task spawned by the inner function was detached.

The lock must be held throughout the entire detached task's lifetime,
which this refactoring ensures, by setting the lock's result after
calling the inner function.
2024-11-21 18:36:11 +01:00
Damir Jelić bc70f3c051 refactor: Clean up the Room::compute_display_name() method 2024-11-21 14:34:38 +01:00
Benjamin Bouvier d2f255d613 feat(ffi): add a new function helper to create a caption edit
It has the same semantics used when creating a caption (if no formatted
caption is provided, assume a provided caption is markdown and use that
as the formatted caption).
2024-11-21 10:40:39 +01:00
Doug bf86b168d7 feat(timeline): mark media events as editable in the timeline (#4303)
This PR makes audio, file, image and video messages be editable so that
the timeline signals when it is possible to use #4277/#4300 for editing
captions.
2024-11-21 10:25:32 +01:00
Ivan Enderlin e5ca44bb04 feat(base): Add EventCacheStore::handle_linked_chunk_updates.
This patch adds the `handle_linked_chunk_updates` method on the
`EventCacheStore` trait. Part of
https://github.com/matrix-org/matrix-rust-sdk/issues/3280.
2024-11-20 16:39:49 +01:00
Benjamin Bouvier 1f563c964c task: add manual Sync impl for VerificationCache to avoid overflowing evaluation requirements 2024-11-20 16:33:39 +01:00
Benjamin Bouvier 9a9730d59e task: move the EventFactory to the matrix-sdk-test crate
This makes it available to the crypto crate, by lowering it into the
local dependency tree.
2024-11-20 16:33:39 +01:00
Benjamin Bouvier af3ce4b32b task: remove the dependency from common to test
The (matrix-sdk-)common crate used the (matrix-sdk-)test crate only to
benefit from the `async_test` proc macro, which is conveniently defined
in another crate.

My goal is to make `EventFactory`, at this point in the commit history,
defined in the main SDK crate, available in the test crate.
`EventFactory` makes use of some types defined in common, so there's a
circular dependency at the moment.

To split this circular dependency, I've changed the common crate to
depend on the test-macro crate directly; now the test crate can depend
on the common crate, and everybody's happy.
2024-11-20 16:33:39 +01:00
Benjamin Bouvier 03f0c3a001 task: move the MockClientBuilder to its own mock file 2024-11-20 16:33:39 +01:00
Benjamin Bouvier 639833acf1 task: move test_utils.rs to test_utils/mod.rs
This is more in line with what we're doing in the SDK in general.
2024-11-20 16:33:39 +01:00
Benjamin Bouvier 60893d2797 test(send_queue): add tests for editing a caption while media not sent yet
test(timeline): add an integration test for sending an attachment

test(timeline): add tests for multiple caption edits and local reaction to a media upload
2024-11-20 10:11:56 +01:00
Benjamin Bouvier 9e45111d8b feat(send queue): allow updating caption while the media is being sent 2024-11-20 10:11:56 +01:00
Benjamin Bouvier 0080f17c1f feat(base): add a way to update a dependent send queue request 2024-11-20 10:11:56 +01:00
Benjamin Bouvier fa47af3dd6 refactor!(base): rename StateStore::update_dependent_queued_request to mark_dependent_queued_requests_as_ready 2024-11-20 10:11:56 +01:00
Benjamin Bouvier c4ff07124b feat(ffi): allow editing a media caption from the FFI layer 2024-11-20 10:11:56 +01:00
Benjamin Bouvier 900cf5d071 room: create edits to add a caption to a media event 2024-11-20 10:11:56 +01:00
Benjamin Bouvier 8a6ced0e8f fix(send queue): when adding a local reaction, look for media events in dependent requests too 2024-11-19 17:22:06 +01:00
Benjamin Bouvier f20401c657 test(timeline): add an integration test for sending an attachment in the timeline
Also includes a caption for a file media event, which acts as a
regression test for the previous commit.
2024-11-19 16:59:31 +01:00
Benjamin Bouvier b987fc1de2 fix(media): include the formatted caption and filename for audio and file attachments too 2024-11-19 16:59:31 +01:00
Benjamin Bouvier efeac2ef39 fix(base): clear a room's send queue and dependent event queue after removing it from the state store 2024-11-19 16:50:35 +01:00
Valere 6b80055bd2 fix(utd_hook): Fix regression causing retry to report false late decrypt (#4252)
There has been a recent change on `Decryptor::decrypt_event_impl` causing
the function to return an TimelineEvent of kind unable to decrypt
instead of failing with an error.

The `late_decrypt` detection code was not changed, causing any retry to
mark UTDs as late decrypt.
2024-11-19 16:40:18 +01:00
Jorge Martín 0af53e99ee feat(room_preview): Compute display name for RoomPreview when possible 2024-11-19 16:11:09 +01:00
Jorge Martín bc0c2a6be2 feat(room_preview): Add RoomPreview::heroes field for known rooms 2024-11-19 16:11:09 +01:00
430 changed files with 39137 additions and 12387 deletions
+6
View File
@@ -24,6 +24,7 @@ allow = [
"ISC",
"MIT",
"MPL-2.0",
"Unicode-3.0",
"Zlib",
]
exceptions = [
@@ -54,8 +55,13 @@ allow-git = [
"https://github.com/element-hq/tracing.git",
# Sam as for the tracing dependency.
"https://github.com/element-hq/paranoid-android.git",
# Well, it's Ruma.
"https://github.com/ruma/ruma",
# A patch override for the bindings: https://github.com/rodrimati1992/const_panic/pull/10
"https://github.com/jplatte/const_panic",
# A patch override for the bindings: https://github.com/smol-rs/async-compat/pull/22
"https://github.com/jplatte/async-compat",
# We can release vodozemac whenever we need but let's not block development
# on releases.
"https://github.com/matrix-org/vodozemac",
]
+1 -1
View File
@@ -17,7 +17,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2024-06-25
toolchain: nightly-2024-11-26
components: rustfmt
- name: Run Benchmarks
+4 -4
View File
@@ -85,7 +85,7 @@ jobs:
java-version: '17'
- name: Install android sdk
uses: malinskiy/action-android/install-sdk@release/0.1.4
uses: malinskiy/action-android/install-sdk@release/0.1.7
- name: Install android ndk
uses: nttld/setup-ndk@v1
@@ -131,7 +131,7 @@ jobs:
test-apple:
name: matrix-rust-components-swift
needs: xtask
runs-on: macos-14
runs-on: macos-15
if: github.event_name == 'push' || !github.event.pull_request.draft
steps:
@@ -175,7 +175,7 @@ jobs:
run: swift test
- name: Build Framework
run: target/debug/xtask swift build-framework --target=aarch64-apple-ios --profile=dev
run: target/debug/xtask swift build-framework --target=aarch64-apple-ios --profile=reldbg
complement-crypto:
name: "Run Complement Crypto tests"
@@ -186,7 +186,7 @@ jobs:
test-crypto-apple-framework-generation:
name: Generate Crypto FFI Apple XCFramework
runs-on: macos-14
runs-on: macos-15
if: github.event_name == 'push' || !github.event.pull_request.draft
steps:
+21 -26
View File
@@ -18,6 +18,9 @@ concurrency:
env:
CARGO_TERM_COLOR: always
# Insta.rs is run directly via cargo test. We don't want insta.rs to create new snapshots files.
# Just want it to run the tests (option `no` instead of `auto`).
INSTA_UPDATE: no
jobs:
xtask:
@@ -221,12 +224,16 @@ jobs:
- name: '[m]-common'
cmd: matrix-sdk-common
- name: '[m], no-default'
cmd: matrix-sdk-no-default
- name: '[m]-ui'
cmd: matrix-sdk-ui
check_only: true
- name: '[m]-indexeddb'
cmd: indexeddb
- name: '[m], no-default, wasm-flags'
cmd: matrix-sdk-no-default
- name: '[m], indexeddb stores'
cmd: matrix-sdk-indexeddb-stores
@@ -245,6 +252,7 @@ jobs:
- name: Install wasm-pack
uses: qmaru/wasm-pack-action@v0.5.0
if: '!matrix.check_only'
with:
version: v0.10.3
@@ -274,27 +282,10 @@ jobs:
target/debug/xtask ci wasm ${{ matrix.cmd }}
- name: Wasm-Pack test
if: '!matrix.check_only'
run: |
target/debug/xtask ci wasm-pack ${{ matrix.cmd }}
formatting:
name: Check Formatting
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2024-06-25
components: rustfmt
- name: Cargo fmt
run: |
cargo fmt -- --check
typos:
name: Spell Check with Typos
runs-on: ubuntu-latest
@@ -304,10 +295,10 @@ jobs:
uses: actions/checkout@v4
- name: Check the spelling of the files in our repo
uses: crate-ci/typos@v1.27.3
uses: crate-ci/typos@v1.29.5
clippy:
name: Run clippy
lint:
name: Lint
needs: xtask
runs-on: ubuntu-latest
@@ -323,8 +314,8 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2024-06-25
components: clippy
toolchain: nightly-2024-11-26
components: clippy, rustfmt
- name: Load cache
uses: Swatinem/rust-cache@v2
@@ -338,6 +329,10 @@ jobs:
key: "${{ needs.xtask.outputs.cachekey-linux }}"
fail-on-cache-miss: true
- name: Check Formatting
run: |
target/debug/xtask ci style
- name: Clippy
run: |
target/debug/xtask ci clippy
+2 -2
View File
@@ -36,7 +36,7 @@ jobs:
- name: Install Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly-2024-06-25
toolchain: nightly-2024-11-26
- name: Install Node.js
uses: actions/setup-node@v4
@@ -53,7 +53,7 @@ jobs:
env:
RUSTDOCFLAGS: "--enable-index-page -Zunstable-options --cfg docsrs -Dwarnings"
run:
cargo doc --no-deps --workspace --features docsrs
cargo doc --no-deps --workspace --features docsrs --exclude=xtask
- name: Upload artifact
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
+1 -1
View File
@@ -35,7 +35,7 @@ jobs:
os-name: 🐧
cachekey-id: linux
- os: macos-14
- os: macos-15
os-name: 🍏
cachekey-id: macos
+72 -36
View File
@@ -30,6 +30,33 @@ integration tests that need a running synapse instance. These tests reside in
synapse for testing purposes.
### Snapshot Testing
You can add/review snapshot tests using [insta.rs](https://insta.rs)
Every new struct/enum that derives `Serialize` `Deserialise` should have a snapshot test for it.
Any code change that breaks serialisation will then break a test, the author will then have to decide
how to handle migration and test it if needed.
And for an improved review experience it's recommended (but not necessary) to install the cargo-insta tool:
Unix:
```
curl -LsSf https://insta.rs/install.sh | sh
```
Windows:
```
powershell -c "irm https://insta.rs/install.ps1 | iex"
```
Usual flow is to first run the test, then review them.
```
cargo insta test
cargo insta review
```
## Pull requests
Ideally, a PR should have a *proper title*, with *atomic logical commits*, and
@@ -45,9 +72,46 @@ that is, just the branch name.)
# Writing changelog entries
We aim to maintain clear and informative changelogs that accurately reflect the
changes in our project. This guide will help you write useful changelog entries
using git-cliff, which fetches changelog entries from commit messages.
Our goal is to maintain clear, concise, and informative changelogs that
accurately document changes in the project. Changelog entries should be written
manually for each crate in the `/crates/$CRATE_NAME/Changelog.md` file.
Be sure to include a link to the pull request for additional context. A
well-written changelog entry should be understandable even to those who may not
be deeply familiar with the project. Provide enough context to ensure clarity
and ease of understanding.
A couple of examples of bad changelog entry would look like:
```markdown
- Fixed a panic.
```
```markdown
- Added the Bar function to Foo.
```
A good example of a changelog entry could look like the following:
```markdown
- Use the inviter's server name and the server name from the room alias as
fallback values for the via parameter when requesting the room summary from
the homeserver. This ensures requests succeed even when the room being
previewed is hosted on a federated server.
([#4357](https://github.com/matrix-org/matrix-rust-sdk/pull/4357))
```
For security-related changelog entries, please include the following additional
details alongside the pull request number:
* Impact: Clearly describe the issue's potential impact on users or systems.
* CVE Number: If available, include the CVE (Common Vulnerabilities and Exposures) identifier.
* GitHub Advisory Link: Provide a link to the corresponding GitHub security advisory for further context.
```markdown
- Use a constant-time Base64 encoder for secret key material to mitigate
side-channel attacks leaking secret key material ([#156](https://github.com/matrix-org/vodozemac/pull/156)) (Low, [CVE-2024-40640](https://www.cve.org/CVERecord?id=CVE-2024-40640), [GHSA-j8cm-g7r6-hfpq](https://github.com/matrix-org/vodozemac/security/advisories/GHSA-j8cm-g7r6-hfpq)).
```
## Commit message format
@@ -74,45 +138,20 @@ The type of changes which will be included in changelogs is one of the following
The scope is optional and can specify the area of the codebase affected (e.g.,
olm, cipher).
### Changelog trailer
In addition to the Conventional Commit format, you can use the `Changelog` git
trailer to specify the changelog message explicitly. When that trailer is
present, its value will be used as the changelog entry instead of the commit's
leading line. The `Breaking-Change` git trailer can be used in a similar manner
if the changelog entry should be marked as a breaking change.
#### Example commit message
```
feat: Add a method to encode Ed25519 public keys to Base64
This patch adds the `Ed25519PublicKey::to_base64()` method, which allows us to
stringify Ed25519 and thus present them to users. It's also commonly used when
Ed25519 keys need to be inserted into JSON.
Changelog: Add the `Ed25519PublicKey::to_base64()` method which can be used to
stringify the Ed25519 public key.
```
In this commit message, the content specified in the `Changelog` trailer will be
used for the changelog entry.
Be careful to add at least one whitespace after new lines to create a paragraph.
### Security fixes
Commits addressing security vulnerabilities must include specific trailers for
vulnerability metadata. These commits are required to include at least the
`Security-Impact` trailer to indicate that the commit is a security fix.
vulnerability metadata, which should also be reflected in the corresponding
changelog entry.
Security issues have some additional git-trailers:
The metadata must be included in the following git-trailers:
* `Security-Impact`: The magnitude of harm that can be expected, i.e. low/moderate/high/critical.
* `CVE`: The CVE that was assigned to this issue.
* `GitHub-Advisory`: The GitHub advisory identifier.
Please include all of the fields that are available.
Example:
```
@@ -131,9 +170,6 @@ material.
Security-Impact: Low
CVE: CVE-2024-40640
GitHub-Advisory: GHSA-j8cm-g7r6-hfpq
Changelog: Use a constant-time Base64 encoder for secret key material
to mitigate side-channel attacks leaking secret key material.
```
## Review process
Generated
+840 -362
View File
File diff suppressed because it is too large Load Diff
+72 -44
View File
@@ -18,34 +18,49 @@ default-members = ["benchmarks", "crates/*", "labs/*"]
resolver = "2"
[workspace.package]
rust-version = "1.76"
rust-version = "1.83"
[workspace.dependencies]
anyhow = "1.0.68"
assert-json-diff = "2"
anyhow = "1.0.95"
aquamarine = "0.6.0"
assert-json-diff = "2.0.2"
assert_matches = "1.5.0"
assert_matches2 = "0.1.1"
assert_matches2 = "0.1.2"
async-rx = "0.1.3"
async-stream = "0.3.3"
async-trait = "0.1.60"
async-stream = "0.3.5"
async-trait = "0.1.85"
as_variant = "1.2.0"
base64 = "0.22.0"
byteorder = "1.4.3"
base64 = "0.22.1"
byteorder = "1.5.0"
chrono = "0.4.39"
eyeball = { version = "0.8.8", features = ["tracing"] }
eyeball-im = { version = "0.5.1", features = ["tracing"] }
eyeball-im-util = "0.7.0"
futures-core = "0.3.28"
futures-executor = "0.3.21"
futures-util = "0.3.26"
growable-bloom-filter = "2.1.0"
http = "1.1.0"
imbl = "3.0.0"
itertools = "0.12.0"
once_cell = "1.16.0"
pin-project-lite = "0.2.9"
eyeball-im = { version = "0.6.0", features = ["tracing"] }
eyeball-im-util = "0.8.0"
futures-core = "0.3.31"
futures-executor = "0.3.31"
futures-util = "0.3.31"
getrandom = { version = "0.2.15", default-features = false }
gloo-timers = "0.3.0"
growable-bloom-filter = "2.1.1"
hkdf = "0.12.4"
hmac = "0.12.1"
http = "1.2.0"
imbl = "4.0.1"
indexmap = "2.7.1"
insta = { version = "1.42.1", features = ["json"] }
itertools = "0.14.0"
js-sys = "0.3.69"
mime = "0.3.17"
once_cell = "1.20.2"
pbkdf2 = { version = "0.12.2" }
pin-project-lite = "0.2.16"
proptest = { version = "1.6.0", default-features = false, features = ["std"] }
rand = "0.8.5"
reqwest = { version = "0.12.4", default-features = false }
ruma = { version = "0.11.1", features = [
reqwest = { version = "0.12.12", default-features = false }
rmp-serde = "1.3.0"
# Be careful to use commits from the https://github.com/ruma/ruma/tree/ruma-0.12
# branch until a proper release with breaking changes happens.
ruma = { version = "0.12.1", features = [
"client-api-c",
"compat-upload-signatures",
"compat-user-id",
@@ -58,38 +73,45 @@ ruma = { version = "0.11.1", features = [
"unstable-msc3489",
"unstable-msc4075",
"unstable-msc4140",
"unstable-msc4171",
] }
ruma-common = "0.14.1"
serde = "1.0.151"
serde_html_form = "0.2.0"
serde_json = "1.0.91"
ruma-common = { version = "0.15.1" }
serde = "1.0.217"
serde_html_form = "0.2.7"
serde_json = "1.0.138"
sha2 = "0.10.8"
similar-asserts = "1.5.0"
similar-asserts = "1.6.1"
stream_assert = "0.1.1"
thiserror = "1.0.38"
tokio = { version = "1.39.1", default-features = false, features = ["sync"] }
tokio-stream = "0.1.14"
tempfile = "3.16.0"
thiserror = "2.0.11"
tokio = { version = "1.43.0", default-features = false, features = ["sync"] }
tokio-stream = "0.1.17"
tracing = { version = "0.1.40", default-features = false, features = ["std"] }
tracing-core = "0.1.32"
tracing-subscriber = "0.3.18"
unicode-normalization = "0.1.24"
uniffi = { version = "0.28.0" }
uniffi_bindgen = { version = "0.28.0" }
url = "2.5.0"
vodozemac = { version = "0.8.0", features = ["insecure-pk-encryption"] }
wiremock = "0.6.0"
zeroize = "1.6.0"
url = "2.5.4"
uuid = "1.12.1"
vodozemac = { version = "0.9.0", features = ["insecure-pk-encryption"] }
wasm-bindgen = "0.2.84"
wasm-bindgen-test = "0.3.33"
web-sys = "0.3.69"
wiremock = "0.6.2"
zeroize = "1.8.1"
matrix-sdk = { path = "crates/matrix-sdk", version = "0.8.0", default-features = false }
matrix-sdk-base = { path = "crates/matrix-sdk-base", version = "0.8.0" }
matrix-sdk-common = { path = "crates/matrix-sdk-common", version = "0.8.0" }
matrix-sdk-crypto = { path = "crates/matrix-sdk-crypto", version = "0.8.0" }
matrix-sdk = { path = "crates/matrix-sdk", version = "0.10.0", default-features = false }
matrix-sdk-base = { path = "crates/matrix-sdk-base", version = "0.10.0" }
matrix-sdk-common = { path = "crates/matrix-sdk-common", version = "0.10.0" }
matrix-sdk-crypto = { path = "crates/matrix-sdk-crypto", version = "0.10.0" }
matrix-sdk-ffi-macros = { path = "bindings/matrix-sdk-ffi-macros", version = "0.7.0" }
matrix-sdk-indexeddb = { path = "crates/matrix-sdk-indexeddb", version = "0.8.0", default-features = false }
matrix-sdk-qrcode = { path = "crates/matrix-sdk-qrcode", version = "0.8.0" }
matrix-sdk-sqlite = { path = "crates/matrix-sdk-sqlite", version = "0.8.0", default-features = false }
matrix-sdk-store-encryption = { path = "crates/matrix-sdk-store-encryption", version = "0.8.0" }
matrix-sdk-test = { path = "testing/matrix-sdk-test", version = "0.7.0" }
matrix-sdk-ui = { path = "crates/matrix-sdk-ui", version = "0.8.0", default-features = false }
matrix-sdk-indexeddb = { path = "crates/matrix-sdk-indexeddb", version = "0.10.0", default-features = false }
matrix-sdk-qrcode = { path = "crates/matrix-sdk-qrcode", version = "0.10.0" }
matrix-sdk-sqlite = { path = "crates/matrix-sdk-sqlite", version = "0.10.0", default-features = false }
matrix-sdk-store-encryption = { path = "crates/matrix-sdk-store-encryption", version = "0.10.0" }
matrix-sdk-test = { path = "testing/matrix-sdk-test", version = "0.10.0" }
matrix-sdk-ui = { path = "crates/matrix-sdk-ui", version = "0.10.0", default-features = false }
# Default release profile, select with `--release`
[profile.release]
@@ -106,6 +128,9 @@ debug = 0
# for the extra time of optimizing it for a clean build of matrix-sdk-ffi.
quote = { opt-level = 2 }
sha2 = { opt-level = 2 }
# faster runs for insta.rs snapshot testing
insta.opt-level = 3
similar.opt-level = 3
# Custom profile with full debugging info, use `--profile dbg` to select
[profile.dbg]
@@ -131,7 +156,10 @@ paranoid-android = { git = "https://github.com/element-hq/paranoid-android.git",
[workspace.lints.rust]
rust_2018_idioms = "warn"
semicolon_in_expressions_from_macros = "warn"
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] }
unexpected_cfgs = { level = "warn", check-cfg = [
'cfg(tarpaulin_include)', # Used by tarpaulin (code coverage)
'cfg(ruma_unstable_exhaustive_types)', # Used by Ruma's EventContent derive macro
] }
unused_extern_crates = "warn"
unused_import_braces = "warn"
unused_qualifications = "warn"
+2 -8
View File
@@ -24,17 +24,11 @@ The rust-sdk consists of multiple crates that can be picked at your convenience:
- **matrix-sdk-crypto** - No (network) IO encryption state machine that can be
used to add Matrix E2EE support to your client or client library.
## Minimum Supported Rust Version (MSRV)
These crates are built with the Rust language version 2021 and require a minimum compiler version of `1.70`.
## Status
The library is in an alpha state, things that are implemented generally work but
the API will change in breaking ways.
The library is considered production ready and backs multiple client implementations such as Element X [[1]](https://github.com/element-hq/element-x-ios) [[2]](https://github.com/element-hq/element-x-android) and [Fractal](https://gitlab.gnome.org/World/fractal). Client developers should feel confident to build upon it.
If you are interested in using the matrix-sdk now is the time to try it out and
provide feedback.
Development of the SDK has been primarily sponsored by Element though accepts contributions from all.
## Bindings
+2 -2
View File
@@ -17,8 +17,8 @@ The procedure is as follows:
git switch -c release-x.y.z
  ```
2. Prepare the release. This will update the `README.md`, prepend the `CHANGELOG.md`
file using `git cliff`, and bump the version in the `Cargo.toml` file.
2. Prepare the release. This will update the `README.md`, set the versions in
the `CHANGELOG.md` file, and bump the version in the `Cargo.toml` file.
```bash
cargo xtask release prepare --execute minor|patch|rc
-121
View File
@@ -1,121 +0,0 @@
# Upgrades 0.5 ➜ 0.6
This is a rough migration guide to help you upgrade your code using matrix-sdk 0.5 to the newly released matrix-sdk 0.6 . While it won't cover all edge cases and problems, we are trying to get the most common issues covered. If you experience any other difficulties in upgrade or need support with using the matrix-sdk in general, please approach us in our [matrix-sdk channel on matrix.org][matrix-channel].
## Minimum Supported Rust Version Update: `1.60`
We have updated the minimal rust version you need in order to build `matrix-sdk`, as we require some new dependency resolving features from it:
> These crates are built with the Rust language version 2021 and require a minimum compiler version of 1.60
## Dependencies
Many dependencies have been upgraded. Most notably, we are using `ruma` at version `0.7.0` now. It has seen some renamings and restructurings since our last release, so you might find that some Types have new names now.
## Repo Structure Updates
If you are looking at the repository itself, you will find we've rearranged the code quite a bit: we have split out any bindings-specific and testing related crates (and other things) into respective folders, and we've moved all `examples` into its own top-level-folder with each example as their own crate (rendering them easier to find and copy as starting points), all in all slimming down the `crates` folder to the core aspects.
## Architecture Changes / API overall
### Builder Pattern
We are moving to the [builder pattern][] (familiar from e.g. `std::io:process:Command`) as the main configurable path for many aspects of the API, including to construct Matrix-Requests and workflows. This has been and is an on-going effort, and this release sees a lot of APIs transitioning to this pattern, you should already be familiar with from the `matrix_sdk::Client::builder()` in `0.5`. This pattern been extended onto:
- the [login configuration][login builder] and [login with sso][ssologin builder],
- [`SledStore` configuratiion][sled-store builder]
- [`Indexeddb` configuration][indexeddb builder]
Most have fallback (though maybe with deprecation warning) support for an existing code path, but these are likely to be removed in upcoming releases.
### Splitting of concerns: Media
In an effort to declutter the `Client` API dedicated types have been created dealing with specific concerns in one place. In `0.5` we introduced `client.account()`, and `client.encryption()`, we are doing the same with `client.media()` to manage media and attachments in one place with the [`media::Media` type][media typ] now.
The signatures of media uploads, have also changed slightly: rather than expecting a reader `R: Read + Seek`, it now is a simple `&[u8]`. Which also means no more unnecessary `seek(0)` to reset the cursor, as we are just taking an immutable reference now.
### Event Handling & sync updaes
If you are using the `client.register_event_handler` function to receive updates on incoming sync events, you'll find yourself with a deprecation warning now. That is because we've refactored and redesigned the event handler logic to allowing `removing` of event handlers on the fly, too. For that the new `add_event_handler()` (and `add_room_event_handler`) will hand you an `EventHandlerHandle` (pardon the pun), which you can pass to `remove_event_handler`, or by using the convenient `client.event_handler_drop_guard` to create a `DropGuard` that will remove the handler when the guard is dropped. While the code still works, we recommend you switch to the new one, as we will be removing the `register_event_handler` and `register_event_handler_context` in a coming release.
Secondly, you will find a new [`sync_with_result_callback` sync function][sync with result]. Other than the previous sync functions, this will pass the entire `Result` to your callback, allowing you to handle errors or even raise some yourself to stop the loop. Further more, it will propagate any unhandled errors (it still handles retries as before) to the outer caller, allowing the higher level to decide how to handle that (e.g. in case of a network failure). This result-returning-behavior also punshes through the existing `sync` and `sync_with_callback`-API, allowing you to handle them on a higher level now (rather than the futures just resolving). If you find that warning, just adding a `?` to the `.await` of the call is probably the quickest way to move forward.
### Refresh Tokens
This release now [supports `refresh_token`s][refresh tokens PR] as part of the [`Session`][session]. It is implemented with a default-flag in serde so deserializing a previously serialized Session (e.g. in a store) will work as before. As part of `refresh_token` support, you can now configure the client via `ClientBuilder.request_refresh_token()` to refresh the access token automagically on certain failures or do it manually by calling `client.refresh_access_token()` yourself. Auto-refresh is _off_ by default.
You can stay informed about updates on the access token by listening to `client.session_tokens_signal()`.
### Further changes
- [`MessageOptions`][message options] has been updated to Matrix 1.3 by making the `from` parameter optional (and function signatures have been updated, too). You can now request the server sends you messages from the first one you are allowed to have received.
- `client.user_id()` is not a `future` anymore. Remove any `.await` you had behind it.
- `verified()`, `blacklisted()` and `deleted()` on `matrix_sdk::encryption::identities::Device` have been renamed with a `is_` prefix.
- `verified()` on `matrix_sdk::encryption::identities::UserIdentity`, too has been prefixed with `is_` and thus is now called `is_verified()`.
- The top-level crypto and state-store types of Indexeddb and Sled have been renamed to unique types>
- `state_store` and `crypto_store` do not need to be boxed anymore when passed to the [`StoreConfig`][store config]
- Indexeddb's `SerializationError` is now `IndexedDBStoreError`
- Javascript specific features are now behind the `js` feature-gate
- The new experimental next generation of sync ("sliding sync"), with a totally revamped api, can be found behind the optional `sliding-sync`-feature-gate
## Quick Troubleshooting
You find yourself focused with any of these, here are the steps to follow to upgrade your code accordingly:
### warning: use of deprecated associated function `matrix_sdk::Client::register_event_handler`: Use [`Client::add_event_handler`](#method.add_event_handler) instead
As it says on the tin: we have deprecated this function in favor of the newer removable handler approach (see above). You can still continue to use this `fn` for now, but it will be removed in a future release.
### warning: use of deprecated associated function `matrix_sdk::Client::login`: Replaced by [`Client::login_username`](#method.login_username)
We have replaced the login facilities with a `LoginBuilder` and recommend you use that from now on. This isn't an error yet, but the function will be removed in a future release.
### expected slice `[u8]`, found struct ...
We've updated the `send_attachment` and `Media` signatures to use `&[u8]` rather than `reader: Read + Seek` as it is more convenient and common place for most architectures anyways. If you are using `File::open(path)?` to get that handler, you can just replace that with `std::fs::read(path)?`
### no method named `verified` found for struct `matrix_sdk::encryption::identities::Device` in the current scope
Boolean flags like `verified`, `deleted`, `blacklisted`, etc have been renamed with a `is_` prefix. So, just follow the cargo suggestion:
```
|
69 | device.verified()
| ^^^^^^^^ help: there is an associated function with a similar name: `is_verified`
```
### unresolved import `matrix_sdk::ruma::events::AnySyncRoomEvent`
Ruma has been updated to `0.7.0`, you will find some ruma Events names have changed, most notably, the `AnySyncRoomEvent` is now named `AnySyncTimelineEvent` (and not `AnySyncStateEvent`, which cargo wrongly suggests). Just rename the import and usage of it.
### `std::option::Option<&matrix_sdk::ruma::UserId>` is not a future
You are seeing something along the lines of:
```
19 | if room_member.state_key != client.user_id().await.unwrap() {
| ^^^^^^ `std::option::Option<&matrix_sdk::ruma::UserId>` is not a future
|
= help: the trait `Future` is not implemented for `std::option::Option<&matrix_sdk::ruma::UserId>`
= note: std::option::Option<&matrix_sdk::ruma::UserId> must be a future or must implement `IntoFuture` to be awaited
= note: required because of the requirements on the impl of `IntoFuture` for `std::option::Option<&matrix_sdk::ruma::UserId>`
help: remove the `.await`
|
19 - if room_member.state_key != client.user_id().await.unwrap() {
19 + if room_member.state_key != client.user_id().unwrap() {
```
You are using `client.user_id().await` but `user_id()` is no longer `async`. Just follow the cargo suggestion and remove the `.await`, it is not necessary any longer.
[matrix-channel]: https://matrix.to/#/#matrix-rust-sdk:matrix.org
[builder pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
[login builder]: https://docs.rs/matrix-sdk/latest/matrix_sdk/struct.LoginBuilder.html
[ssologin builder]: https://docs.rs/matrix-sdk/latest/matrix_sdk/struct.SsoLoginBuilder.html
[sled-store builder]: https://docs.rs/matrix-sdk-sled/latest/matrix_sdk_sled/struct.SledStateStoreBuilder.html
[indexeddb builder]: https://docs.rs/matrix-sdk-indexeddb/latest/matrix_sdk_indexeddb/struct.IndexeddbStateStoreBuilder.html
[media type]: https://docs.rs/matrix-sdk/latest/matrix_sdk//media/struct.Media.html
[sync with result]: https://docs.rs/matrix-sdk/latest/matrix_sdk/struct.Client.html#method.sync_with_result_callback
[session]: https://docs.rs/matrix-sdk/latest/matrix_sdk/struct.Session.html
[refresh tokens PR]: https://github.com/matrix-org/matrix-rust-sdk/pull/892
[store config]: https://docs.rs/matrix-sdk-base/latest/matrix_sdk_base/store/struct.StoreConfig.html
[message options]: https://docs.rs/matrix-sdk/latest/matrix_sdk/room/struct.MessagesOptions.html
+1 -1
View File
@@ -23,7 +23,7 @@ tokio = { workspace = true, default-features = false, features = ["rt-multi-thre
wiremock = { workspace = true }
[target.'cfg(target_os = "linux")'.dependencies]
pprof = { version = "0.13.0", features = ["flamegraph", "criterion"] }
pprof = { version = "0.14.0", features = ["flamegraph", "criterion"] }
[[bench]]
name = "crypto_bench"
+14 -27
View File
@@ -1,22 +1,20 @@
use std::{sync::Arc, time::Duration};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use matrix_sdk::{
config::SyncSettings,
test_utils::{events::EventFactory, logged_in_client_with_server},
utils::IntoRawStateEventContent,
};
use matrix_sdk::{config::SyncSettings, test_utils::logged_in_client_with_server};
use matrix_sdk_base::{
store::StoreConfig, BaseClient, RoomInfo, RoomState, SessionMeta, StateChanges, StateStore,
};
use matrix_sdk_sqlite::SqliteStateStore;
use matrix_sdk_test::{EventBuilder, JoinedRoomBuilder, StateTestEvent, SyncResponseBuilder};
use matrix_sdk_test::{
event_factory::EventFactory, JoinedRoomBuilder, StateTestEvent, SyncResponseBuilder,
};
use matrix_sdk_ui::{timeline::TimelineFocus, Timeline};
use ruma::{
api::client::membership::get_member_events,
device_id,
events::room::member::{RoomMemberEvent, RoomMemberEventContent},
owned_room_id, owned_user_id,
events::room::member::{MembershipState, RoomMemberEvent},
mxc_uri, owned_room_id, owned_user_id,
serde::Raw,
user_id, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId,
};
@@ -34,28 +32,17 @@ pub fn receive_all_members_benchmark(c: &mut Criterion) {
let runtime = Builder::new_multi_thread().build().expect("Can't create runtime");
let room_id = owned_room_id!("!room:example.com");
let ev_builder = EventBuilder::new();
let f = EventFactory::new().room(&room_id);
let mut member_events: Vec<Raw<RoomMemberEvent>> = Vec::with_capacity(MEMBERS_IN_ROOM);
let member_content_json = json!({
"avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF",
"displayname": "Alice Margatroid",
"membership": "join",
"reason": "Looking for support",
});
let member_content: Raw<RoomMemberEventContent> =
member_content_json.into_raw_state_event_content().cast();
for i in 0..MEMBERS_IN_ROOM {
let user_id = OwnedUserId::try_from(format!("@user_{}:matrix.org", i)).unwrap();
let state_key = user_id.to_string();
let event: Raw<RoomMemberEvent> = ev_builder
.make_state_event(
&user_id,
&room_id,
&state_key,
member_content.deserialize().unwrap(),
None,
)
.cast();
let event = f
.member(&user_id)
.membership(MembershipState::Join)
.avatar_url(mxc_uri!("mxc://example.org/SEsfnsuifSDFSSEF"))
.display_name("Alice Margatroid")
.reason("Looking for support")
.into_raw();
member_events.push(event);
}
+1 -1
View File
@@ -2,8 +2,8 @@ use std::sync::Arc;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use matrix_sdk::{
authentication::matrix::{MatrixSession, MatrixSessionTokens},
config::StoreConfig,
matrix_auth::{MatrixSession, MatrixSessionTokens},
Client, RoomInfo, RoomState, StateChanges,
};
use matrix_sdk_base::{store::MemoryStore, SessionMeta, StateStore as _};
+1
View File
@@ -13,6 +13,7 @@ let package = Package(
],
products: [
.library(name: "MatrixRustSDK",
type: .dynamic,
targets: ["MatrixRustSDK"]),
],
targets: [
-4
View File
@@ -71,10 +71,6 @@ $ cp ../../target/aarch64-linux-android/debug/libmatrix_crypto.so \
/home/example/matrix-sdk-android/src/main/jniLibs/aarch64/libuniffi_olm.so
```
## Minimum Supported Rust Version (MSRV)
These crates are built with the Rust language version 2021 and require a minimum compiler version of `1.62`.
## License
[Apache-2.0](https://www.apache.org/licenses/LICENSE-2.0)
+41 -17
View File
@@ -1,10 +1,15 @@
use std::{env, error::Error};
use std::{
env,
error::Error,
path::{Path, PathBuf},
process::Command,
};
use vergen::EmitBuilder;
/// Adds a temporary workaround for an issue with the Rust compiler and Android
/// in x86_64 devices: https://github.com/rust-lang/rust/issues/109717.
/// The workaround comes from: https://github.com/mozilla/application-services/pull/5442
/// The workaround is based on: https://github.com/mozilla/application-services/pull/5442
///
/// IMPORTANT: if you modify this, make sure to modify
/// [../matrix-sdk-ffi/build.rs] too!
@@ -12,26 +17,45 @@ fn setup_x86_64_android_workaround() {
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set");
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set");
if target_arch == "x86_64" && target_os == "android" {
let android_ndk_home = env::var("ANDROID_NDK_HOME").expect("ANDROID_NDK_HOME not set");
let build_os = match env::consts::OS {
"linux" => "linux",
"macos" => "darwin",
"windows" => "windows",
_ => panic!(
"Unsupported OS. You must use either Linux, MacOS or Windows to build the crate."
),
};
const DEFAULT_CLANG_VERSION: &str = "18";
let clang_version =
env::var("NDK_CLANG_VERSION").unwrap_or_else(|_| DEFAULT_CLANG_VERSION.to_owned());
let linux_x86_64_lib_dir = format!(
"toolchains/llvm/prebuilt/{build_os}-x86_64/lib/clang/{clang_version}/lib/linux/"
// Configure rust to statically link against the `libclang_rt.builtins` supplied
// with clang.
// cargo-ndk sets CC_x86_64-linux-android to the path to `clang`, within the
// Android NDK.
let clang_path = PathBuf::from(
env::var("CC_x86_64-linux-android").expect("CC_x86_64-linux-android not set"),
);
println!("cargo:rustc-link-search={android_ndk_home}/{linux_x86_64_lib_dir}");
// clang_path should now look something like
// `.../sdk/ndk/28.0.12674087/toolchains/llvm/prebuilt/linux-x86_64/bin/clang`.
// We strip `/bin/clang` from the end to get the toolchain path.
let toolchain_path = clang_path
.ancestors()
.nth(2)
.expect("could not find NDK toolchain path")
.to_str()
.expect("NDK toolchain path is not valid UTF-8");
let clang_version = get_clang_major_version(&clang_path);
println!("cargo:rustc-link-search={toolchain_path}/lib/clang/{clang_version}/lib/linux/");
println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android");
}
}
/// Run the clang binary at `clang_path`, and return its major version number
fn get_clang_major_version(clang_path: &Path) -> String {
let clang_output =
Command::new(clang_path).arg("-dumpversion").output().expect("failed to start clang");
if !clang_output.status.success() {
panic!("failed to run clang: {}", String::from_utf8_lossy(&clang_output.stderr));
}
let clang_version = String::from_utf8(clang_output.stdout).expect("clang output is not utf8");
clang_version.split('.').next().expect("could not parse clang output").to_owned()
}
fn main() -> Result<(), Box<dyn Error>> {
setup_x86_64_android_workaround();
@@ -1,19 +1,25 @@
use std::{mem::ManuallyDrop, sync::Arc};
use matrix_sdk_crypto::dehydrated_devices::{
DehydratedDevice as InnerDehydratedDevice, DehydratedDevices as InnerDehydratedDevices,
RehydratedDevice as InnerRehydratedDevice,
use matrix_sdk_crypto::{
dehydrated_devices::{
DehydratedDevice as InnerDehydratedDevice, DehydratedDevices as InnerDehydratedDevices,
RehydratedDevice as InnerRehydratedDevice,
},
store::DehydratedDeviceKey as InnerDehydratedDeviceKey,
};
use ruma::{api::client::dehydrated_device, events::AnyToDeviceEvent, serde::Raw, OwnedDeviceId};
use serde_json::json;
use tokio::runtime::Handle;
use zeroize::Zeroize;
use crate::{CryptoStoreError, DehydratedDeviceKey};
#[derive(Debug, thiserror::Error, uniffi::Error)]
#[uniffi(flat_error)]
pub enum DehydrationError {
#[error(transparent)]
Pickle(#[from] matrix_sdk_crypto::vodozemac::LibolmPickleError),
Pickle(#[from] matrix_sdk_crypto::vodozemac::DehydratedDeviceError),
#[error(transparent)]
LegacyPickle(#[from] matrix_sdk_crypto::vodozemac::LibolmPickleError),
#[error(transparent)]
MissingSigningKey(#[from] matrix_sdk_crypto::SignatureError),
#[error(transparent)]
@@ -22,6 +28,8 @@ pub enum DehydrationError {
Store(#[from] matrix_sdk_crypto::CryptoStoreError),
#[error("The pickle key has an invalid length, expected 32 bytes, got {0}")]
PickleKeyLength(usize),
#[error(transparent)]
Rand(#[from] rand::Error),
}
impl From<matrix_sdk_crypto::dehydrated_devices::DehydrationError> for DehydrationError {
@@ -29,10 +37,16 @@ impl From<matrix_sdk_crypto::dehydrated_devices::DehydrationError> for Dehydrati
match value {
matrix_sdk_crypto::dehydrated_devices::DehydrationError::Json(e) => Self::Json(e),
matrix_sdk_crypto::dehydrated_devices::DehydrationError::Pickle(e) => Self::Pickle(e),
matrix_sdk_crypto::dehydrated_devices::DehydrationError::LegacyPickle(e) => {
Self::LegacyPickle(e)
}
matrix_sdk_crypto::dehydrated_devices::DehydrationError::MissingSigningKey(e) => {
Self::MissingSigningKey(e)
}
matrix_sdk_crypto::dehydrated_devices::DehydrationError::Store(e) => Self::Store(e),
matrix_sdk_crypto::dehydrated_devices::DehydrationError::PickleKeyLength(l) => {
Self::PickleKeyLength(l)
}
}
}
}
@@ -66,14 +80,14 @@ impl DehydratedDevices {
pub fn rehydrate(
&self,
pickle_key: Vec<u8>,
pickle_key: &DehydratedDeviceKey,
device_id: String,
device_data: String,
) -> Result<Arc<RehydratedDevice>, DehydrationError> {
let device_data: Raw<_> = serde_json::from_str(&device_data)?;
let device_id: OwnedDeviceId = device_id.into();
let mut key = get_pickle_key(&pickle_key)?;
let key = InnerDehydratedDeviceKey::from_slice(&pickle_key.inner)?;
let ret = RehydratedDevice {
runtime: self.runtime.to_owned(),
@@ -85,10 +99,41 @@ impl DehydratedDevices {
}
.into();
key.zeroize();
Ok(ret)
}
/// Get the cached dehydrated device pickle key if any.
///
/// None if the key was not previously cached (via
/// [`Self::save_dehydrated_device_pickle_key`]).
///
/// Should be used to periodically rotate the dehydrated device to avoid
/// OTK exhaustion and accumulation of to_device messages.
pub fn get_dehydrated_device_key(
&self,
) -> Result<Option<crate::DehydratedDeviceKey>, CryptoStoreError> {
Ok(self
.runtime
.block_on(self.inner.get_dehydrated_device_pickle_key())?
.map(crate::DehydratedDeviceKey::from))
}
/// Store the dehydrated device pickle key in the crypto store.
///
/// This is useful if the client wants to periodically rotate dehydrated
/// devices to avoid OTK exhaustion and accumulated to_device problems.
pub fn save_dehydrated_device_key(
&self,
pickle_key: &crate::DehydratedDeviceKey,
) -> Result<(), CryptoStoreError> {
let pickle_key = InnerDehydratedDeviceKey::from_slice(&pickle_key.inner)?;
Ok(self.runtime.block_on(self.inner.save_dehydrated_device_pickle_key(&pickle_key))?)
}
/// Deletes the previously stored dehydrated device pickle key.
pub fn delete_dehydrated_device_key(&self) -> Result<(), CryptoStoreError> {
Ok(self.runtime.block_on(self.inner.delete_dehydrated_device_pickle_key())?)
}
}
#[derive(uniffi::Object)]
@@ -138,15 +183,13 @@ impl DehydratedDevice {
pub fn keys_for_upload(
&self,
device_display_name: String,
pickle_key: Vec<u8>,
pickle_key: &DehydratedDeviceKey,
) -> Result<UploadDehydratedDeviceRequest, DehydrationError> {
let mut key = get_pickle_key(&pickle_key)?;
let key = InnerDehydratedDeviceKey::from_slice(&pickle_key.inner)?;
let request =
self.runtime.block_on(self.inner.keys_for_upload(device_display_name, &key))?;
key.zeroize();
Ok(request.into())
}
}
@@ -177,15 +220,36 @@ impl From<dehydrated_device::put_dehydrated_device::unstable::Request>
}
}
fn get_pickle_key(pickle_key: &[u8]) -> Result<Box<[u8; 32]>, DehydrationError> {
let pickle_key_length = pickle_key.len();
#[cfg(test)]
mod tests {
use crate::{dehydrated_devices::DehydrationError, DehydratedDeviceKey};
if pickle_key_length == 32 {
let mut key = Box::new([0u8; 32]);
key.copy_from_slice(pickle_key);
#[test]
fn test_creating_dehydrated_key() {
let result = DehydratedDeviceKey::new();
assert!(result.is_ok());
let dehydrated_device_key = result.unwrap();
let base_64 = dehydrated_device_key.to_base64();
let inner_bytes = dehydrated_device_key.inner;
Ok(key)
} else {
Err(DehydrationError::PickleKeyLength(pickle_key_length))
let copy = DehydratedDeviceKey::from_slice(&inner_bytes).unwrap();
assert_eq!(base_64, copy.to_base64());
}
#[test]
fn test_creating_dehydrated_key_failure() {
let bytes = [0u8; 24];
let pickle_key = DehydratedDeviceKey::from_slice(&bytes);
assert!(pickle_key.is_err());
match pickle_key {
Err(DehydrationError::PickleKeyLength(pickle_key_length)) => {
assert_eq!(bytes.len(), pickle_key_length);
}
_ => panic!("Should have failed!"),
}
}
}
+6 -3
View File
@@ -1,8 +1,9 @@
#![allow(missing_docs)]
use matrix_sdk_crypto::{
store::CryptoStoreError as InnerStoreError, KeyExportError, MegolmError, OlmError,
SecretImportError as RustSecretImportError, SignatureError as InnerSignatureError,
store::{CryptoStoreError as InnerStoreError, DehydrationError as InnerDehydrationError},
KeyExportError, MegolmError, OlmError, SecretImportError as RustSecretImportError,
SignatureError as InnerSignatureError,
};
use matrix_sdk_sqlite::OpenStoreError;
use ruma::{IdParseError, OwnedUserId};
@@ -57,6 +58,8 @@ pub enum CryptoStoreError {
InvalidUserId(String, IdParseError),
#[error(transparent)]
Identifier(#[from] IdParseError),
#[error(transparent)]
DehydrationError(#[from] InnerDehydrationError),
}
#[derive(Debug, thiserror::Error, uniffi::Error)]
@@ -112,7 +115,7 @@ mod tests {
#[test]
fn test_withheld_error_mapping() {
use matrix_sdk_crypto::types::events::room_key_withheld::WithheldCode;
use matrix_sdk_common::deserialized_responses::WithheldCode;
let inner_error = MegolmError::MissingRoomKey(Some(WithheldCode::Unverified));
+48 -5
View File
@@ -36,7 +36,10 @@ pub use machine::{KeyRequestPair, OlmMachine, SignatureVerification};
use matrix_sdk_common::deserialized_responses::{ShieldState as RustShieldState, ShieldStateCode};
use matrix_sdk_crypto::{
olm::{IdentityKeys, InboundGroupSession, SenderData, Session},
store::{Changes, CryptoStore, PendingChanges, RoomSettings as RustRoomSettings},
store::{
Changes, CryptoStore, DehydratedDeviceKey as InnerDehydratedDeviceKey, PendingChanges,
RoomSettings as RustRoomSettings,
},
types::{
DeviceKey, DeviceKeys, EventEncryptionAlgorithm as RustEventEncryptionAlgorithm, SigningKey,
},
@@ -62,6 +65,8 @@ pub use verification::{
};
use vodozemac::{Curve25519PublicKey, Ed25519PublicKey};
use crate::dehydrated_devices::DehydrationError;
/// Struct collecting data that is important to migrate to the rust-sdk
#[derive(Deserialize, Serialize, uniffi::Record)]
pub struct MigrationData {
@@ -675,15 +680,20 @@ pub struct EncryptionSettings {
impl From<EncryptionSettings> for RustEncryptionSettings {
fn from(v: EncryptionSettings) -> Self {
let sharing_strategy = if v.only_allow_trusted_devices {
CollectStrategy::OnlyTrustedDevices
} else if v.error_on_verified_user_problem {
CollectStrategy::ErrorOnVerifiedUserProblem
} else {
CollectStrategy::AllDevices
};
RustEncryptionSettings {
algorithm: v.algorithm.into(),
rotation_period: Duration::from_secs(v.rotation_period),
rotation_period_msgs: v.rotation_period_msgs,
history_visibility: v.history_visibility.into(),
sharing_strategy: CollectStrategy::DeviceBasedStrategy {
only_allow_trusted_devices: v.only_allow_trusted_devices,
error_on_verified_user_problem: v.error_on_verified_user_problem,
},
sharing_strategy,
}
}
}
@@ -822,6 +832,39 @@ impl TryFrom<matrix_sdk_crypto::store::BackupKeys> for BackupKeys {
}
}
/// Dehydrated device key
#[derive(uniffi::Record, Clone)]
pub struct DehydratedDeviceKey {
pub(crate) inner: Vec<u8>,
}
impl DehydratedDeviceKey {
/// Generates a new random pickle key.
pub fn new() -> Result<Self, DehydrationError> {
let inner = InnerDehydratedDeviceKey::new()?;
Ok(inner.into())
}
/// Creates a new dehydration pickle key from the given slice.
///
/// Fail if the slice length is not 32.
pub fn from_slice(slice: &[u8]) -> Result<Self, DehydrationError> {
let inner = InnerDehydratedDeviceKey::from_slice(slice)?;
Ok(inner.into())
}
/// Export the [`DehydratedDeviceKey`] as a base64 encoded string.
pub fn to_base64(&self) -> String {
let inner = InnerDehydratedDeviceKey::from_slice(&self.inner).unwrap();
inner.to_base64()
}
}
impl From<InnerDehydratedDeviceKey> for DehydratedDeviceKey {
fn from(pickle_key: InnerDehydratedDeviceKey) -> Self {
DehydratedDeviceKey { inner: pickle_key.into() }
}
}
impl From<matrix_sdk_crypto::store::RoomKeyCounts> for RoomKeyCounts {
fn from(count: matrix_sdk_crypto::store::RoomKeyCounts) -> Self {
Self { total: count.total as i64, backed_up: count.backed_up as i64 }
@@ -17,8 +17,8 @@ use matrix_sdk_crypto::{
decrypt_room_key_export, encrypt_room_key_export,
olm::ExportedRoomKey,
store::{BackupDecryptionKey, Changes},
DecryptionSettings, LocalTrust, OlmMachine as InnerMachine, ToDeviceRequest,
UserIdentity as SdkUserIdentity,
types::requests::ToDeviceRequest,
DecryptionSettings, LocalTrust, OlmMachine as InnerMachine, UserIdentity as SdkUserIdentity,
};
use ruma::{
api::{
+15 -12
View File
@@ -4,9 +4,12 @@ use std::collections::HashMap;
use http::Response;
use matrix_sdk_crypto::{
CrossSigningBootstrapRequests, IncomingResponse, KeysBackupRequest, OutgoingRequest,
OutgoingVerificationRequest as SdkVerificationRequest, RoomMessageRequest, ToDeviceRequest,
UploadSigningKeysRequest as RustUploadSigningKeysRequest,
types::requests::{
AnyIncomingResponse, KeysBackupRequest, OutgoingRequest,
OutgoingVerificationRequest as SdkVerificationRequest, RoomMessageRequest, ToDeviceRequest,
UploadSigningKeysRequest as RustUploadSigningKeysRequest,
},
CrossSigningBootstrapRequests,
};
use ruma::{
api::client::{
@@ -136,7 +139,7 @@ pub enum Request {
impl From<OutgoingRequest> for Request {
fn from(r: OutgoingRequest) -> Self {
use matrix_sdk_crypto::OutgoingRequests::*;
use matrix_sdk_crypto::types::requests::AnyOutgoingRequest::*;
match r.request() {
KeysUpload(u) => {
@@ -338,16 +341,16 @@ impl From<RoomMessageResponse> for OwnedResponse {
}
}
impl<'a> From<&'a OwnedResponse> for IncomingResponse<'a> {
impl<'a> From<&'a OwnedResponse> for AnyIncomingResponse<'a> {
fn from(r: &'a OwnedResponse) -> Self {
match r {
OwnedResponse::KeysClaim(r) => IncomingResponse::KeysClaim(r),
OwnedResponse::KeysQuery(r) => IncomingResponse::KeysQuery(r),
OwnedResponse::KeysUpload(r) => IncomingResponse::KeysUpload(r),
OwnedResponse::ToDevice(r) => IncomingResponse::ToDevice(r),
OwnedResponse::SignatureUpload(r) => IncomingResponse::SignatureUpload(r),
OwnedResponse::KeysBackup(r) => IncomingResponse::KeysBackup(r),
OwnedResponse::RoomMessage(r) => IncomingResponse::RoomMessage(r),
OwnedResponse::KeysClaim(r) => AnyIncomingResponse::KeysClaim(r),
OwnedResponse::KeysQuery(r) => AnyIncomingResponse::KeysQuery(r),
OwnedResponse::KeysUpload(r) => AnyIncomingResponse::KeysUpload(r),
OwnedResponse::ToDevice(r) => AnyIncomingResponse::ToDevice(r),
OwnedResponse::SignatureUpload(r) => AnyIncomingResponse::SignatureUpload(r),
OwnedResponse::KeysBackup(r) => AnyIncomingResponse::KeysBackup(r),
OwnedResponse::RoomMessage(r) => AnyIncomingResponse::RoomMessage(r),
}
}
}
@@ -791,8 +791,7 @@ impl VerificationRequest {
// task.
let should_break = matches!(
state,
RustVerificationRequestState::Done { .. }
| RustVerificationRequestState::Cancelled { .. }
RustVerificationRequestState::Done | RustVerificationRequestState::Cancelled { .. }
);
let state = Self::convert_verification_request(&request, state);
@@ -9,6 +9,7 @@ readme = "README.md"
repository = "https://github.com/matrix-org/matrix-rust-sdk"
rust-version = { workspace = true }
version = "0.7.0"
publish = false
[lib]
proc-macro = true
@@ -22,3 +23,6 @@ syn = { version = "2.0.43", features = ["full", "extra-traits"] }
[lints]
workspace = true
[package.metadata.release]
release = false
+5
View File
@@ -2,6 +2,9 @@
Breaking changes:
- Matrix client API errors coming from API responses will now be mapped to `ClientError::MatrixApi`, containing both the
original message and the associated error code and kind.
- `EventSendState` now has two additional variants: `CrossSigningNotSetup` and
`SendingFromUnverifiedDevice`. These indicate that your own device is not
properly cross-signed, which is a requirement when using the identity-based
@@ -33,3 +36,5 @@ Additions:
- Add `Encryption::get_user_identity` which returns `UserIdentity`
- Add `ClientBuilder::room_key_recipient_strategy`
- Add `Room::send_raw`
- Expose `withdraw_verification` to `UserIdentity`
-2
View File
@@ -56,7 +56,6 @@ features = [
"anyhow",
"e2e-encryption",
"experimental-oidc",
"experimental-sliding-sync",
"experimental-widgets",
"markdown",
"rustls-tls", # note: differ from block below
@@ -71,7 +70,6 @@ features = [
"anyhow",
"e2e-encryption",
"experimental-oidc",
"experimental-sliding-sync",
"experimental-widgets",
"markdown",
"native-tls", # note: differ from block above
+41 -17
View File
@@ -1,10 +1,15 @@
use std::{env, error::Error};
use std::{
env,
error::Error,
path::{Path, PathBuf},
process::Command,
};
use vergen::EmitBuilder;
/// Adds a temporary workaround for an issue with the Rust compiler and Android
/// in x86_64 devices: https://github.com/rust-lang/rust/issues/109717.
/// The workaround comes from: https://github.com/mozilla/application-services/pull/5442
/// The workaround is based on: https://github.com/mozilla/application-services/pull/5442
///
/// IMPORTANT: if you modify this, make sure to modify
/// [../matrix-sdk-crypto-ffi/build.rs] too!
@@ -12,26 +17,45 @@ fn setup_x86_64_android_workaround() {
let target_os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS not set");
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH not set");
if target_arch == "x86_64" && target_os == "android" {
let android_ndk_home = env::var("ANDROID_NDK_HOME").expect("ANDROID_NDK_HOME not set");
let build_os = match env::consts::OS {
"linux" => "linux",
"macos" => "darwin",
"windows" => "windows",
_ => panic!(
"Unsupported OS. You must use either Linux, MacOS or Windows to build the crate."
),
};
const DEFAULT_CLANG_VERSION: &str = "18";
let clang_version =
env::var("NDK_CLANG_VERSION").unwrap_or_else(|_| DEFAULT_CLANG_VERSION.to_owned());
let linux_x86_64_lib_dir = format!(
"toolchains/llvm/prebuilt/{build_os}-x86_64/lib/clang/{clang_version}/lib/linux/"
// Configure rust to statically link against the `libclang_rt.builtins` supplied
// with clang.
// cargo-ndk sets CC_x86_64-linux-android to the path to `clang`, within the
// Android NDK.
let clang_path = PathBuf::from(
env::var("CC_x86_64-linux-android").expect("CC_x86_64-linux-android not set"),
);
println!("cargo:rustc-link-search={android_ndk_home}/{linux_x86_64_lib_dir}");
// clang_path should now look something like
// `.../sdk/ndk/28.0.12674087/toolchains/llvm/prebuilt/linux-x86_64/bin/clang`.
// We strip `/bin/clang` from the end to get the toolchain path.
let toolchain_path = clang_path
.ancestors()
.nth(2)
.expect("could not find NDK toolchain path")
.to_str()
.expect("NDK toolchain path is not valid UTF-8");
let clang_version = get_clang_major_version(&clang_path);
println!("cargo:rustc-link-search={toolchain_path}/lib/clang/{clang_version}/lib/linux/");
println!("cargo:rustc-link-lib=static=clang_rt.builtins-x86_64-android");
}
}
/// Run the clang binary at `clang_path`, and return its major version number
fn get_clang_major_version(clang_path: &Path) -> String {
let clang_output =
Command::new(clang_path).arg("-dumpversion").output().expect("failed to start clang");
if !clang_output.status.success() {
panic!("failed to run clang: {}", String::from_utf8_lossy(&clang_output.stderr));
}
let clang_version = String::from_utf8(clang_output.stdout).expect("clang output is not utf8");
clang_version.split('.').next().expect("could not parse clang output").to_owned()
}
fn main() -> Result<(), Box<dyn Error>> {
setup_x86_64_android_workaround();
uniffi::generate_scaffolding("./src/api.udl").expect("Building the UDL file failed");
-12
View File
@@ -8,15 +8,3 @@ dictionary Mentions {
interface RoomMessageEventContentWithoutRelation {
RoomMessageEventContentWithoutRelation with_mentions(Mentions mentions);
};
[Error]
interface ClientError {
Generic(string msg);
};
interface MediaSource {
[Name=from_json, Throws=ClientError]
constructor(string json);
string to_json();
string url();
};
@@ -5,7 +5,7 @@ use std::{
};
use matrix_sdk::{
oidc::{
authentication::oidc::{
registrations::OidcRegistrationsError,
types::{
iana::oauth::OAuthClientAuthenticationMethod,
+102 -43
View File
@@ -7,11 +7,7 @@ use std::{
use anyhow::{anyhow, Context as _};
use matrix_sdk::{
media::{
MediaFileHandle as SdkMediaFileHandle, MediaFormat, MediaRequestParameters,
MediaThumbnailSettings,
},
oidc::{
authentication::oidc::{
registrations::{ClientId, OidcRegistrations},
requests::account_management::AccountManagementActionFull,
types::{
@@ -23,6 +19,10 @@ use matrix_sdk::{
},
OidcAuthorizationData, OidcSession,
},
media::{
MediaFileHandle as SdkMediaFileHandle, MediaFormat, MediaRequestParameters,
MediaThumbnailSettings,
},
reqwest::StatusCode,
ruma::{
api::client::{
@@ -32,9 +32,7 @@ use matrix_sdk::{
user_directory::search_users,
},
events::{
room::{
avatar::RoomAvatarEventContent, encryption::RoomEncryptionEventContent, MediaSource,
},
room::{avatar::RoomAvatarEventContent, encryption::RoomEncryptionEventContent},
AnyInitialStateEvent, AnyToDeviceEvent, InitialStateEvent,
},
serde::Raw,
@@ -55,7 +53,12 @@ use ruma::{
},
events::{
ignored_user_list::IgnoredUserListEventContent,
room::{join_rules::RoomJoinRulesEventContent, power_levels::RoomPowerLevelsEventContent},
room::{
join_rules::{
AllowRule as RumaAllowRule, JoinRule as RumaJoinRule, RoomJoinRulesEventContent,
},
power_levels::RoomPowerLevelsEventContent,
},
GlobalAccountDataEventType,
},
push::{HttpPusherData as RumaHttpPusherData, PushFormat as RumaPushFormat},
@@ -76,7 +79,7 @@ use crate::{
notification_settings::NotificationSettings,
room_directory_search::RoomDirectorySearch,
room_preview::RoomPreview,
ruma::AuthData,
ruma::{AuthData, MediaSource},
sync_service::{SyncService, SyncServiceBuilder},
task_handle::TaskHandle,
utils::AsyncRuntimeDropped,
@@ -117,7 +120,7 @@ impl TryFrom<PusherKind> for RumaPusherKind {
let mut ruma_data = RumaHttpPusherData::new(data.url);
if let Some(payload) = data.default_payload {
let json: Value = serde_json::from_str(&payload)?;
ruma_data.default_payload = json;
ruma_data.data.insert("default_payload".to_owned(), json);
}
ruma_data.format = data.format.map(Into::into);
Ok(Self::Http(ruma_data))
@@ -450,7 +453,7 @@ impl Client {
.inner
.media()
.get_media_file(
&MediaRequestParameters { source, format: MediaFormat::File },
&MediaRequestParameters { source: source.media_source, format: MediaFormat::File },
filename,
&mime_type,
use_cache,
@@ -723,7 +726,7 @@ impl Client {
&self,
media_source: Arc<MediaSource>,
) -> Result<Vec<u8>, ClientError> {
let source = (*media_source).clone();
let source = (*media_source).clone().media_source;
debug!(?source, "requesting media file");
Ok(self
@@ -739,9 +742,9 @@ impl Client {
width: u64,
height: u64,
) -> Result<Vec<u8>, ClientError> {
let source = (*media_source).clone();
let source = (*media_source).clone().media_source;
debug!(source = ?media_source, width, height, "requesting media thumbnail");
debug!(?source, width, height, "requesting media thumbnail");
Ok(self
.inner
.media()
@@ -1149,17 +1152,6 @@ impl Client {
let alias = RoomAliasId::parse(alias)?;
self.inner.is_room_alias_available(&alias).await.map_err(Into::into)
}
/// Creates a new room alias associated with the provided room id.
pub async fn create_room_alias(
&self,
room_alias: String,
room_id: String,
) -> Result<(), ClientError> {
let room_alias = RoomAliasId::parse(room_alias)?;
let room_id = RoomId::parse(room_id)?;
self.inner.create_room_alias(&room_alias, &room_id).await.map_err(Into::into)
}
}
#[matrix_sdk_ffi_macros::export(callback_interface)]
@@ -1459,6 +1451,9 @@ pub enum RoomVisibility {
/// Indicates that the room will not be shown in the published room list.
Private,
/// A custom value that's not present in the spec.
Custom { value: String },
}
impl From<RoomVisibility> for Visibility {
@@ -1466,6 +1461,17 @@ impl From<RoomVisibility> for Visibility {
match value {
RoomVisibility::Public => Self::Public,
RoomVisibility::Private => Self::Private,
RoomVisibility::Custom { value } => value.as_str().into(),
}
}
}
impl From<Visibility> for RoomVisibility {
fn from(value: Visibility) -> Self {
match value {
Visibility::Public => Self::Public,
Visibility::Private => Self::Private,
_ => Self::Custom { value: value.as_str().to_owned() },
}
}
}
@@ -1529,10 +1535,13 @@ impl Session {
match auth_api {
// Build the session from the regular Matrix Auth Session.
AuthApi::Matrix(a) => {
let matrix_sdk::matrix_auth::MatrixSession {
let matrix_sdk::authentication::matrix::MatrixSession {
meta: matrix_sdk::SessionMeta { user_id, device_id },
tokens:
matrix_sdk::matrix_auth::MatrixSessionTokens { access_token, refresh_token },
matrix_sdk::authentication::matrix::MatrixSessionTokens {
access_token,
refresh_token,
},
} = a.session().context("Missing session")?;
Ok(Session {
@@ -1547,10 +1556,10 @@ impl Session {
}
// Build the session from the OIDC UserSession.
AuthApi::Oidc(api) => {
let matrix_sdk::oidc::UserSession {
let matrix_sdk::authentication::oidc::UserSession {
meta: matrix_sdk::SessionMeta { user_id, device_id },
tokens:
matrix_sdk::oidc::OidcSessionTokens {
matrix_sdk::authentication::oidc::OidcSessionTokens {
access_token,
refresh_token,
latest_id_token,
@@ -1611,12 +1620,12 @@ impl TryFrom<Session> for AuthSession {
.transpose()
.context("OIDC latest_id_token is invalid.")?;
let user_session = matrix_sdk::oidc::UserSession {
let user_session = matrix_sdk::authentication::oidc::UserSession {
meta: matrix_sdk::SessionMeta {
user_id: user_id.try_into()?,
device_id: device_id.into(),
},
tokens: matrix_sdk::oidc::OidcSessionTokens {
tokens: matrix_sdk::authentication::oidc::OidcSessionTokens {
access_token,
refresh_token,
latest_id_token,
@@ -1630,15 +1639,15 @@ impl TryFrom<Session> for AuthSession {
user: user_session,
};
Ok(AuthSession::Oidc(session))
Ok(AuthSession::Oidc(session.into()))
} else {
// Create a regular Matrix Session.
let session = matrix_sdk::matrix_auth::MatrixSession {
let session = matrix_sdk::authentication::matrix::MatrixSession {
meta: matrix_sdk::SessionMeta {
user_id: user_id.try_into()?,
device_id: device_id.into(),
},
tokens: matrix_sdk::matrix_auth::MatrixSessionTokens {
tokens: matrix_sdk::authentication::matrix::MatrixSessionTokens {
access_token,
refresh_token,
},
@@ -1917,9 +1926,13 @@ pub enum AllowRule {
/// Only a member of the `room_id` Room can join the one this rule is used
/// in.
RoomMembership { room_id: String },
/// A custom allow rule implementation, containing its JSON representation
/// as a `String`.
Custom { json: String },
}
impl TryFrom<JoinRule> for ruma::events::room::join_rules::JoinRule {
impl TryFrom<JoinRule> for RumaJoinRule {
type Error = ClientError;
fn try_from(value: JoinRule) -> Result<Self, Self::Error> {
@@ -1929,11 +1942,11 @@ impl TryFrom<JoinRule> for ruma::events::room::join_rules::JoinRule {
JoinRule::Knock => Ok(Self::Knock),
JoinRule::Private => Ok(Self::Private),
JoinRule::Restricted { rules } => {
let rules = allow_rules_from(rules)?;
let rules = ruma_allow_rules_from_ffi(rules)?;
Ok(Self::Restricted(ruma::events::room::join_rules::Restricted::new(rules)))
}
JoinRule::KnockRestricted { rules } => {
let rules = allow_rules_from(rules)?;
let rules = ruma_allow_rules_from_ffi(rules)?;
Ok(Self::KnockRestricted(ruma::events::room::join_rules::Restricted::new(rules)))
}
JoinRule::Custom { repr } => Ok(serde_json::from_str(&repr)?),
@@ -1941,12 +1954,10 @@ impl TryFrom<JoinRule> for ruma::events::room::join_rules::JoinRule {
}
}
fn allow_rules_from(
value: Vec<AllowRule>,
) -> Result<Vec<ruma::events::room::join_rules::AllowRule>, ClientError> {
fn ruma_allow_rules_from_ffi(value: Vec<AllowRule>) -> Result<Vec<RumaAllowRule>, ClientError> {
let mut ret = Vec::with_capacity(value.len());
for rule in value {
let rule: Result<ruma::events::room::join_rules::AllowRule, ClientError> = rule.try_into();
let rule: Result<RumaAllowRule, ClientError> = rule.try_into();
match rule {
Ok(rule) => ret.push(rule),
Err(error) => return Err(error),
@@ -1955,7 +1966,7 @@ fn allow_rules_from(
Ok(ret)
}
impl TryFrom<AllowRule> for ruma::events::room::join_rules::AllowRule {
impl TryFrom<AllowRule> for RumaAllowRule {
type Error = ClientError;
fn try_from(value: AllowRule) -> Result<Self, Self::Error> {
@@ -1966,6 +1977,54 @@ impl TryFrom<AllowRule> for ruma::events::room::join_rules::AllowRule {
room_id,
)))
}
AllowRule::Custom { json } => Ok(Self::_Custom(Box::new(serde_json::from_str(&json)?))),
}
}
}
impl TryFrom<RumaJoinRule> for JoinRule {
type Error = String;
fn try_from(value: RumaJoinRule) -> Result<Self, Self::Error> {
match value {
RumaJoinRule::Knock => Ok(JoinRule::Knock),
RumaJoinRule::Public => Ok(JoinRule::Public),
RumaJoinRule::Private => Ok(JoinRule::Private),
RumaJoinRule::KnockRestricted(restricted) => {
let rules = restricted.allow.into_iter().map(TryInto::try_into).collect::<Result<
Vec<_>,
Self::Error,
>>(
)?;
Ok(JoinRule::KnockRestricted { rules })
}
RumaJoinRule::Restricted(restricted) => {
let rules = restricted.allow.into_iter().map(TryInto::try_into).collect::<Result<
Vec<_>,
Self::Error,
>>(
)?;
Ok(JoinRule::Restricted { rules })
}
RumaJoinRule::Invite => Ok(JoinRule::Invite),
RumaJoinRule::_Custom(_) => Ok(JoinRule::Custom { repr: value.as_str().to_owned() }),
_ => Err(format!("Unknown JoinRule: {:?}", value)),
}
}
}
impl TryFrom<RumaAllowRule> for AllowRule {
type Error = String;
fn try_from(value: RumaAllowRule) -> Result<Self, Self::Error> {
match value {
RumaAllowRule::RoomMembership(membership) => {
Ok(AllowRule::RoomMembership { room_id: membership.room_id.to_string() })
}
RumaAllowRule::_Custom(repr) => {
let json = serde_json::to_string(&repr)
.map_err(|e| format!("Couldn't serialize custom AllowRule: {e:?}"))?;
Ok(Self::Custom { json })
}
_ => Err(format!("Invalid AllowRule: {:?}", value)),
}
}
}
+45 -7
View File
@@ -1,4 +1,4 @@
use std::{fs, num::NonZeroUsize, path::PathBuf, sync::Arc, time::Duration};
use std::{fs, num::NonZeroUsize, path::Path, sync::Arc, time::Duration};
use futures_util::StreamExt;
use matrix_sdk::{
@@ -8,6 +8,7 @@ use matrix_sdk::{
CollectStrategy, TrustRequirement,
},
encryption::{BackupDownloadStrategy, EncryptionSettings},
event_cache::EventCacheError,
reqwest::Certificate,
ruma::{ServerName, UserId},
sliding_sync::{
@@ -202,6 +203,8 @@ pub enum ClientBuildError {
SlidingSyncVersion(VersionBuilderError),
#[error(transparent)]
Sdk(MatrixClientBuildError),
#[error(transparent)]
EventCache(#[from] EventCacheError),
#[error("Failed to build the client: {message}")]
Generic { message: String },
}
@@ -269,6 +272,10 @@ pub struct ClientBuilder {
room_key_recipient_strategy: CollectStrategy,
decryption_trust_requirement: TrustRequirement,
request_config: Option<RequestConfig>,
/// Whether to enable use of the event cache store, for reloading events
/// when building timelines et al.
use_event_cache_persistent_storage: bool,
}
#[matrix_sdk_ffi_macros::export]
@@ -299,9 +306,27 @@ impl ClientBuilder {
room_key_recipient_strategy: Default::default(),
decryption_trust_requirement: TrustRequirement::Untrusted,
request_config: Default::default(),
use_event_cache_persistent_storage: false,
})
}
/// Whether to use the event cache persistent storage or not.
///
/// This is a temporary feature flag, for testing the event cache's
/// persistent storage. Follow new developments in https://github.com/matrix-org/matrix-rust-sdk/issues/3280.
///
/// This is disabled by default. When disabled, a one-time cleanup is
/// performed when creating the client, and it will clear all the events
/// previously stored in the event cache.
///
/// When enabled, it will attempt to store events in the event cache as
/// they're received, and reuse them when reconstructing timelines.
pub fn use_event_cache_persistent_storage(self: Arc<Self>, value: bool) -> Arc<Self> {
let mut builder = unwrap_or_clone_arc(self);
builder.use_event_cache_persistent_storage = value;
Arc::new(builder)
}
pub fn cross_process_store_locks_holder_name(
self: Arc<Self>,
holder_name: String,
@@ -484,8 +509,8 @@ impl ClientBuilder {
}
if let Some(session_paths) = &builder.session_paths {
let data_path = PathBuf::from(&session_paths.data_path);
let cache_path = PathBuf::from(&session_paths.cache_path);
let data_path = Path::new(&session_paths.data_path);
let cache_path = Path::new(&session_paths.cache_path);
debug!(
data_path = %data_path.to_string_lossy(),
@@ -493,12 +518,12 @@ impl ClientBuilder {
"Creating directories for data and cache stores.",
);
fs::create_dir_all(&data_path)?;
fs::create_dir_all(&cache_path)?;
fs::create_dir_all(data_path)?;
fs::create_dir_all(cache_path)?;
inner_builder = inner_builder.sqlite_store_with_cache_path(
&data_path,
&cache_path,
data_path,
cache_path,
builder.passphrase.as_deref(),
);
} else {
@@ -624,6 +649,19 @@ impl ClientBuilder {
let sdk_client = inner_builder.build().await?;
if builder.use_event_cache_persistent_storage {
// Enable the persistent storage \o/
sdk_client.event_cache().enable_storage()?;
} else {
// Get rid of all the previous events, if any.
let store = sdk_client
.event_cache_store()
.lock()
.await
.map_err(EventCacheError::LockingStorage)?;
store.clear_all_rooms_chunks().await.map_err(EventCacheError::Storage)?;
}
Ok(Arc::new(
Client::new(sdk_client, builder.enable_oidc_refresh_lock, builder.session_delegate)
.await?,
+11 -2
View File
@@ -254,7 +254,7 @@ impl Encryption {
/// Therefore it is necessary to poll the server for an answer every time
/// you want to differentiate between those two states.
pub async fn backup_exists_on_server(&self) -> Result<bool, ClientError> {
Ok(self.inner.backups().exists_on_server().await?)
Ok(self.inner.backups().fetch_exists_on_server().await?)
}
pub fn recovery_state(&self) -> RecoveryState {
@@ -281,7 +281,7 @@ impl Encryption {
}
pub async fn is_last_device(&self) -> Result<bool> {
Ok(self.inner.recovery().are_we_the_last_man_standing().await?)
Ok(self.inner.recovery().is_last_device().await?)
}
pub async fn wait_for_backup_upload_steady_state(
@@ -478,6 +478,15 @@ impl UserIdentity {
Ok(self.inner.pin().await?)
}
/// Remove the requirement for this identity to be verified.
///
/// If an identity was previously verified and is not anymore it will be
/// reported to the user. In order to remove this notice users have to
/// verify again or to withdraw the verification requirement.
pub(crate) async fn withdraw_verification(&self) -> Result<(), ClientError> {
Ok(self.inner.withdraw_verification().await?)
}
/// Get the public part of the Master key of this user identity.
///
/// The public part of the Master key is usually used to uniquely identify
+472 -6
View File
@@ -1,20 +1,23 @@
use std::{collections::HashMap, fmt, fmt::Display};
use std::{collections::HashMap, fmt, fmt::Display, time::SystemTime};
use matrix_sdk::{
encryption::CryptoStoreError, event_cache::EventCacheError, oidc::OidcError, reqwest,
room::edit::EditError, send_queue::RoomSendQueueError, HttpError, IdParseError,
authentication::oidc::OidcError, encryption::CryptoStoreError, event_cache::EventCacheError,
reqwest, room::edit::EditError, send_queue::RoomSendQueueError, HttpError, IdParseError,
NotificationSettingsError as SdkNotificationSettingsError,
QueueWedgeError as SdkQueueWedgeError, StoreError,
};
use matrix_sdk_ui::{encryption_sync_service, notification_client, sync_service, timeline};
use ruma::api::client::error::{ErrorBody, ErrorKind as RumaApiErrorKind, RetryAfter};
use uniffi::UnexpectedUniFFICallbackError;
use crate::room_list::RoomListError;
use crate::{room_list::RoomListError, timeline::FocusEventError};
#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum ClientError {
#[error("client error: {msg}")]
Generic { msg: String },
#[error("api error {code}: {msg}")]
MatrixApi { kind: ErrorKind, code: String, msg: String },
}
impl ClientError {
@@ -43,7 +46,22 @@ impl From<UnexpectedUniFFICallbackError> for ClientError {
impl From<matrix_sdk::Error> for ClientError {
fn from(e: matrix_sdk::Error) -> Self {
Self::new(e)
match e {
matrix_sdk::Error::Http(http_error) => {
if let Some(api_error) = http_error.as_client_api_error() {
if let ErrorBody::Standard { kind, message } = &api_error.body {
let code = kind.errcode().to_string();
let Ok(kind) = kind.to_owned().try_into() else {
// We couldn't parse the API error, so we return a generic one instead
return Self::Generic { msg: message.to_string() };
};
return Self::MatrixApi { kind, code, msg: message.to_owned() };
}
}
Self::Generic { msg: http_error.to_string() }
}
_ => Self::Generic { msg: e.to_string() },
}
}
}
@@ -155,6 +173,18 @@ impl From<RoomSendQueueError> for ClientError {
}
}
impl From<NotYetImplemented> for ClientError {
fn from(_: NotYetImplemented) -> Self {
Self::new("This functionality is not implemented yet.")
}
}
impl From<FocusEventError> for ClientError {
fn from(e: FocusEventError) -> Self {
Self::new(e)
}
}
/// Bindings version of the sdk type replacing OwnedUserId/DeviceIds with simple
/// String.
///
@@ -321,3 +351,439 @@ impl From<matrix_sdk::Error> for NotificationSettingsError {
#[derive(thiserror::Error, Debug)]
#[error("not implemented yet")]
pub struct NotYetImplemented;
#[derive(Clone, Debug, PartialEq, Eq, uniffi::Enum)]
// Please keep the variants sorted alphabetically.
pub enum ErrorKind {
/// `M_BAD_ALIAS`
///
/// One or more [room aliases] within the `m.room.canonical_alias` event do
/// not point to the room ID for which the state event is to be sent to.
///
/// [room aliases]: https://spec.matrix.org/latest/client-server-api/#room-aliases
BadAlias,
/// `M_BAD_JSON`
///
/// The request contained valid JSON, but it was malformed in some way, e.g.
/// missing required keys, invalid values for keys.
BadJson,
/// `M_BAD_STATE`
///
/// The state change requested cannot be performed, such as attempting to
/// unban a user who is not banned.
BadState,
/// `M_BAD_STATUS`
///
/// The application service returned a bad status.
BadStatus {
/// The HTTP status code of the response.
status: Option<u16>,
/// The body of the response.
body: Option<String>,
},
/// `M_CANNOT_LEAVE_SERVER_NOTICE_ROOM`
///
/// The user is unable to reject an invite to join the [server notices]
/// room.
///
/// [server notices]: https://spec.matrix.org/latest/client-server-api/#server-notices
CannotLeaveServerNoticeRoom,
/// `M_CANNOT_OVERWRITE_MEDIA`
///
/// The [`create_content_async`] endpoint was called with a media ID that
/// already has content.
///
/// [`create_content_async`]: crate::media::create_content_async
CannotOverwriteMedia,
/// `M_CAPTCHA_INVALID`
///
/// The Captcha provided did not match what was expected.
CaptchaInvalid,
/// `M_CAPTCHA_NEEDED`
///
/// A Captcha is required to complete the request.
CaptchaNeeded,
/// `M_CONNECTION_FAILED`
///
/// The connection to the application service failed.
ConnectionFailed,
/// `M_CONNECTION_TIMEOUT`
///
/// The connection to the application service timed out.
ConnectionTimeout,
/// `M_DUPLICATE_ANNOTATION`
///
/// The request is an attempt to send a [duplicate annotation].
///
/// [duplicate annotation]: https://spec.matrix.org/latest/client-server-api/#avoiding-duplicate-annotations
DuplicateAnnotation,
/// `M_EXCLUSIVE`
///
/// The resource being requested is reserved by an application service, or
/// the application service making the request has not created the
/// resource.
Exclusive,
/// `M_FORBIDDEN`
///
/// Forbidden access, e.g. joining a room without permission, failed login.
Forbidden,
/// `M_GUEST_ACCESS_FORBIDDEN`
///
/// The room or resource does not permit [guests] to access it.
///
/// [guests]: https://spec.matrix.org/latest/client-server-api/#guest-access
GuestAccessForbidden,
/// `M_INCOMPATIBLE_ROOM_VERSION`
///
/// The client attempted to join a room that has a version the server does
/// not support.
IncompatibleRoomVersion {
/// The room's version.
room_version: String,
},
/// `M_INVALID_PARAM`
///
/// A parameter that was specified has the wrong value. For example, the
/// server expected an integer and instead received a string.
InvalidParam,
/// `M_INVALID_ROOM_STATE`
///
/// The initial state implied by the parameters to the [`create_room`]
/// request is invalid, e.g. the user's `power_level` is set below that
/// necessary to set the room name.
///
/// [`create_room`]: crate::room::create_room
InvalidRoomState,
/// `M_INVALID_USERNAME`
///
/// The desired user name is not valid.
InvalidUsername,
/// `M_LIMIT_EXCEEDED`
///
/// The request has been refused due to [rate limiting]: too many requests
/// have been sent in a short period of time.
///
/// [rate limiting]: https://spec.matrix.org/latest/client-server-api/#rate-limiting
LimitExceeded {
/// How long a client should wait before they can try again.
retry_after_ms: Option<u64>,
},
/// `M_MISSING_PARAM`
///
/// A required parameter was missing from the request.
MissingParam,
/// `M_MISSING_TOKEN`
///
/// No [access token] was specified for the request, but one is required.
///
/// [access token]: https://spec.matrix.org/latest/client-server-api/#client-authentication
MissingToken,
/// `M_NOT_FOUND`
///
/// No resource was found for this request.
NotFound,
/// `M_NOT_JSON`
///
/// The request did not contain valid JSON.
NotJson,
/// `M_NOT_YET_UPLOADED`
///
/// An `mxc:` URI generated with the [`create_mxc_uri`] endpoint was used
/// and the content is not yet available.
///
/// [`create_mxc_uri`]: crate::media::create_mxc_uri
NotYetUploaded,
/// `M_RESOURCE_LIMIT_EXCEEDED`
///
/// The request cannot be completed because the homeserver has reached a
/// resource limit imposed on it. For example, a homeserver held in a
/// shared hosting environment may reach a resource limit if it starts
/// using too much memory or disk space.
ResourceLimitExceeded {
/// A URI giving a contact method for the server administrator.
admin_contact: String,
},
/// `M_ROOM_IN_USE`
///
/// The [room alias] specified in the [`create_room`] request is already
/// taken.
///
/// [`create_room`]: crate::room::create_room
/// [room alias]: https://spec.matrix.org/latest/client-server-api/#room-aliases
RoomInUse,
/// `M_SERVER_NOT_TRUSTED`
///
/// The client's request used a third-party server, e.g. identity server,
/// that this server does not trust.
ServerNotTrusted,
/// `M_THREEPID_AUTH_FAILED`
///
/// Authentication could not be performed on the [third-party identifier].
///
/// [third-party identifier]: https://spec.matrix.org/latest/client-server-api/#adding-account-administrative-contact-information
ThreepidAuthFailed,
/// `M_THREEPID_DENIED`
///
/// The server does not permit this [third-party identifier]. This may
/// happen if the server only permits, for example, email addresses from
/// a particular domain.
///
/// [third-party identifier]: https://spec.matrix.org/latest/client-server-api/#adding-account-administrative-contact-information
ThreepidDenied,
/// `M_THREEPID_IN_USE`
///
/// The [third-party identifier] is already in use by another user.
///
/// [third-party identifier]: https://spec.matrix.org/latest/client-server-api/#adding-account-administrative-contact-information
ThreepidInUse,
/// `M_THREEPID_MEDIUM_NOT_SUPPORTED`
///
/// The homeserver does not support adding a [third-party identifier] of the
/// given medium.
///
/// [third-party identifier]: https://spec.matrix.org/latest/client-server-api/#adding-account-administrative-contact-information
ThreepidMediumNotSupported,
/// `M_THREEPID_NOT_FOUND`
///
/// No account matching the given [third-party identifier] could be found.
///
/// [third-party identifier]: https://spec.matrix.org/latest/client-server-api/#adding-account-administrative-contact-information
ThreepidNotFound,
/// `M_TOO_LARGE`
///
/// The request or entity was too large.
TooLarge,
/// `M_UNABLE_TO_AUTHORISE_JOIN`
///
/// The room is [restricted] and none of the conditions can be validated by
/// the homeserver. This can happen if the homeserver does not know
/// about any of the rooms listed as conditions, for example.
///
/// [restricted]: https://spec.matrix.org/latest/client-server-api/#restricted-rooms
UnableToAuthorizeJoin,
/// `M_UNABLE_TO_GRANT_JOIN`
///
/// A different server should be attempted for the join. This is typically
/// because the resident server can see that the joining user satisfies
/// one or more conditions, such as in the case of [restricted rooms],
/// but the resident server would be unable to meet the authorization
/// rules.
///
/// [restricted rooms]: https://spec.matrix.org/latest/client-server-api/#restricted-rooms
UnableToGrantJoin,
/// `M_UNAUTHORIZED`
///
/// The request was not correctly authorized. Usually due to login failures.
Unauthorized,
/// `M_UNKNOWN`
///
/// An unknown error has occurred.
Unknown,
/// `M_UNKNOWN_TOKEN`
///
/// The [access or refresh token] specified was not recognized.
///
/// [access or refresh token]: https://spec.matrix.org/latest/client-server-api/#client-authentication
UnknownToken {
/// If this is `true`, the client is in a "[soft logout]" state, i.e.
/// the server requires re-authentication but the session is not
/// invalidated. The client can acquire a new access token by
/// specifying the device ID it is already using to the login API.
///
/// [soft logout]: https://spec.matrix.org/latest/client-server-api/#soft-logout
soft_logout: bool,
},
/// `M_UNRECOGNIZED`
///
/// The server did not understand the request.
///
/// This is expected to be returned with a 404 HTTP status code if the
/// endpoint is not implemented or a 405 HTTP status code if the
/// endpoint is implemented, but the incorrect HTTP method is used.
Unrecognized,
/// `M_UNSUPPORTED_ROOM_VERSION`
///
/// The request to [`create_room`] used a room version that the server does
/// not support.
///
/// [`create_room`]: crate::room::create_room
UnsupportedRoomVersion,
/// `M_URL_NOT_SET`
///
/// The application service doesn't have a URL configured.
UrlNotSet,
/// `M_USER_DEACTIVATED`
///
/// The user ID associated with the request has been deactivated.
UserDeactivated,
/// `M_USER_IN_USE`
///
/// The desired user ID is already taken.
UserInUse,
/// `M_USER_LOCKED`
///
/// The account has been [locked] and cannot be used at this time.
///
/// [locked]: https://spec.matrix.org/latest/client-server-api/#account-locking
UserLocked,
/// `M_USER_SUSPENDED`
///
/// The account has been [suspended] and can only be used for limited
/// actions at this time.
///
/// [suspended]: https://spec.matrix.org/latest/client-server-api/#account-suspension
UserSuspended,
/// `M_WEAK_PASSWORD`
///
/// The password was [rejected] by the server for being too weak.
///
/// [rejected]: https://spec.matrix.org/latest/client-server-api/#notes-on-password-management
WeakPassword,
/// `M_WRONG_ROOM_KEYS_VERSION`
///
/// The version of the [room keys backup] provided in the request does not
/// match the current backup version.
///
/// [room keys backup]: https://spec.matrix.org/latest/client-server-api/#server-side-key-backups
WrongRoomKeysVersion {
/// The currently active backup version.
current_version: Option<String>,
},
/// A custom API error.
Custom { errcode: String },
}
impl TryFrom<RumaApiErrorKind> for ErrorKind {
type Error = NotYetImplemented;
fn try_from(value: RumaApiErrorKind) -> Result<Self, Self::Error> {
match &value {
RumaApiErrorKind::BadAlias => Ok(ErrorKind::BadAlias),
RumaApiErrorKind::BadJson => Ok(ErrorKind::BadJson),
RumaApiErrorKind::BadState => Ok(ErrorKind::BadState),
RumaApiErrorKind::BadStatus { status, body } => Ok(ErrorKind::BadStatus {
status: status.map(|code| code.clone().as_u16()),
body: body.clone(),
}),
RumaApiErrorKind::CannotLeaveServerNoticeRoom => {
Ok(ErrorKind::CannotLeaveServerNoticeRoom)
}
RumaApiErrorKind::CannotOverwriteMedia => Ok(ErrorKind::CannotOverwriteMedia),
RumaApiErrorKind::CaptchaInvalid => Ok(ErrorKind::CaptchaInvalid),
RumaApiErrorKind::CaptchaNeeded => Ok(ErrorKind::CaptchaNeeded),
RumaApiErrorKind::ConnectionFailed => Ok(ErrorKind::ConnectionFailed),
RumaApiErrorKind::ConnectionTimeout => Ok(ErrorKind::ConnectionTimeout),
RumaApiErrorKind::DuplicateAnnotation => Ok(ErrorKind::DuplicateAnnotation),
RumaApiErrorKind::Exclusive => Ok(ErrorKind::Exclusive),
RumaApiErrorKind::Forbidden { .. } => Ok(ErrorKind::Forbidden),
RumaApiErrorKind::GuestAccessForbidden => Ok(ErrorKind::GuestAccessForbidden),
RumaApiErrorKind::IncompatibleRoomVersion { room_version } => {
Ok(ErrorKind::IncompatibleRoomVersion { room_version: room_version.to_string() })
}
RumaApiErrorKind::InvalidParam => Ok(ErrorKind::InvalidParam),
RumaApiErrorKind::InvalidRoomState => Ok(ErrorKind::InvalidRoomState),
RumaApiErrorKind::InvalidUsername => Ok(ErrorKind::InvalidUsername),
RumaApiErrorKind::LimitExceeded { retry_after } => {
let retry_after_ms = match retry_after {
Some(RetryAfter::Delay(duration)) => Some(duration.as_millis() as u64),
Some(RetryAfter::DateTime(system_time)) => {
let duration = system_time.duration_since(SystemTime::now()).ok();
duration.map(|duration| duration.as_millis() as u64)
}
None => None,
};
Ok(ErrorKind::LimitExceeded { retry_after_ms })
}
RumaApiErrorKind::MissingParam => Ok(ErrorKind::MissingParam),
RumaApiErrorKind::MissingToken => Ok(ErrorKind::MissingToken),
RumaApiErrorKind::NotFound => Ok(ErrorKind::NotFound),
RumaApiErrorKind::NotJson => Ok(ErrorKind::NotJson),
RumaApiErrorKind::NotYetUploaded => Ok(ErrorKind::NotYetUploaded),
RumaApiErrorKind::ResourceLimitExceeded { admin_contact } => {
Ok(ErrorKind::ResourceLimitExceeded { admin_contact: admin_contact.to_owned() })
}
RumaApiErrorKind::RoomInUse => Ok(ErrorKind::RoomInUse),
RumaApiErrorKind::ServerNotTrusted => Ok(ErrorKind::ServerNotTrusted),
RumaApiErrorKind::ThreepidAuthFailed => Ok(ErrorKind::ThreepidAuthFailed),
RumaApiErrorKind::ThreepidDenied => Ok(ErrorKind::ThreepidDenied),
RumaApiErrorKind::ThreepidInUse => Ok(ErrorKind::ThreepidInUse),
RumaApiErrorKind::ThreepidMediumNotSupported => {
Ok(ErrorKind::ThreepidMediumNotSupported)
}
RumaApiErrorKind::ThreepidNotFound => Ok(ErrorKind::ThreepidNotFound),
RumaApiErrorKind::TooLarge => Ok(ErrorKind::TooLarge),
RumaApiErrorKind::UnableToAuthorizeJoin => Ok(ErrorKind::UnableToAuthorizeJoin),
RumaApiErrorKind::UnableToGrantJoin => Ok(ErrorKind::UnableToGrantJoin),
RumaApiErrorKind::Unauthorized => Ok(ErrorKind::Unauthorized),
RumaApiErrorKind::Unknown => Ok(ErrorKind::Unknown),
RumaApiErrorKind::UnknownToken { soft_logout } => {
Ok(ErrorKind::UnknownToken { soft_logout: soft_logout.to_owned() })
}
RumaApiErrorKind::Unrecognized => Ok(ErrorKind::Unrecognized),
RumaApiErrorKind::UnsupportedRoomVersion => Ok(ErrorKind::UnsupportedRoomVersion),
RumaApiErrorKind::UrlNotSet => Ok(ErrorKind::UrlNotSet),
RumaApiErrorKind::UserDeactivated => Ok(ErrorKind::UserDeactivated),
RumaApiErrorKind::UserInUse => Ok(ErrorKind::UserInUse),
RumaApiErrorKind::UserLocked => Ok(ErrorKind::UserLocked),
RumaApiErrorKind::UserSuspended => Ok(ErrorKind::UserSuspended),
RumaApiErrorKind::WeakPassword => Ok(ErrorKind::WeakPassword),
RumaApiErrorKind::WrongRoomKeysVersion { current_version } => {
Ok(ErrorKind::WrongRoomKeysVersion { current_version: current_version.to_owned() })
}
RumaApiErrorKind::_Custom { .. } => {
// There is no way to map the extra values since they're private, so we omit
// them
Ok(ErrorKind::Custom { errcode: value.errcode().to_string() })
}
// In any other case, return it as the mapping not being yet implemented
_ => Err(NotYetImplemented),
}
}
}
+41 -4
View File
@@ -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, Relation},
redaction::SyncRoomRedactionEvent,
},
AnySyncMessageLikeEvent, AnySyncStateEvent, AnySyncTimelineEvent, AnyTimelineEvent,
MessageLikeEventContent as RumaMessageLikeEventContent, RedactContent,
RedactedStateEventContent, StaticStateEventContent, SyncMessageLikeEvent, SyncStateEvent,
@@ -14,6 +17,7 @@ use ruma::{
use crate::{
room_member::MembershipState,
ruma::{MessageType, NotifyType},
utils::Timestamp,
ClientError,
};
@@ -30,8 +34,8 @@ impl TimelineEvent {
self.0.sender().to_string()
}
pub fn timestamp(&self) -> u64 {
self.0.origin_server_ts().0.into()
pub fn timestamp(&self) -> Timestamp {
self.0.origin_server_ts().into()
}
pub fn event_type(&self) -> Result<TimelineEventType, ClientError> {
@@ -202,7 +206,7 @@ impl TryFrom<AnySyncMessageLikeEvent> for MessageLikeEventContent {
_ => None,
});
MessageLikeEventContent::RoomMessage {
message_type: original_content.msgtype.into(),
message_type: original_content.msgtype.try_into()?,
in_reply_to_event_id,
}
}
@@ -356,6 +360,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)]
+5 -4
View File
@@ -1,6 +1,8 @@
// TODO: target-os conditional would be good.
#![allow(unused_qualifications, clippy::new_without_default)]
#![allow(clippy::empty_line_after_doc_comments)] // Needed because uniffi macros contain empty
// lines after docs.
mod authentication;
mod chunk_iterator;
@@ -12,6 +14,7 @@ mod error;
mod event;
mod helpers;
mod identity_status_change;
mod live_location_share;
mod notification;
mod notification_settings;
mod platform;
@@ -33,13 +36,11 @@ mod utils;
mod widget;
use async_compat::TOKIO1 as RUNTIME;
use matrix_sdk::ruma::events::room::{
message::RoomMessageEventContentWithoutRelation, MediaSource,
};
use matrix_sdk::ruma::events::room::message::RoomMessageEventContentWithoutRelation;
use self::{
error::ClientError,
ruma::{MediaSourceExt, Mentions, RoomMessageEventContentWithoutRelationExt},
ruma::{Mentions, RoomMessageEventContentWithoutRelationExt},
task_handle::TaskHandle,
};
@@ -0,0 +1,32 @@
// Copyright 2024 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 crate::ruma::LocationContent;
#[derive(uniffi::Record)]
pub struct LastLocation {
/// The most recent location content of the user.
pub location: LocationContent,
/// A timestamp in milliseconds since Unix Epoch on that day in local
/// time.
pub ts: u64,
}
/// Details of a users live location share.
#[derive(uniffi::Record)]
pub struct LiveLocationShare {
/// The user's last known location.
pub last_location: LastLocation,
/// The live status of the live location share.
pub(crate) is_live: bool,
/// The user ID of the person sharing their live location.
pub user_id: String,
}
+171 -5
View File
@@ -14,8 +14,11 @@ use tracing_subscriber::{
EnvFilter, Layer,
};
use crate::tracing::LogLevel;
pub fn log_panics() {
std::env::set_var("RUST_BACKTRACE", "1");
log_panics::init();
}
@@ -228,12 +231,76 @@ pub struct TracingFileConfiguration {
max_files: Option<u64>,
}
#[derive(PartialEq, PartialOrd)]
enum LogTarget {
Hyper,
MatrixSdkFfi,
MatrixSdk,
MatrixSdkClient,
MatrixSdkCrypto,
MatrixSdkCryptoAccount,
MatrixSdkOidc,
MatrixSdkHttpClient,
MatrixSdkSlidingSync,
MatrixSdkBaseSlidingSync,
MatrixSdkUiTimeline,
MatrixSdkEventCache,
MatrixSdkBaseEventCache,
MatrixSdkEventCacheStore,
}
impl LogTarget {
fn as_str(&self) -> &'static str {
match self {
LogTarget::Hyper => "hyper",
LogTarget::MatrixSdkFfi => "matrix_sdk_ffi",
LogTarget::MatrixSdk => "matrix_sdk",
LogTarget::MatrixSdkClient => "matrix_sdk::client",
LogTarget::MatrixSdkCrypto => "matrix_sdk_crypto",
LogTarget::MatrixSdkCryptoAccount => "matrix_sdk_crypto::olm::account",
LogTarget::MatrixSdkOidc => "matrix_sdk::oidc",
LogTarget::MatrixSdkHttpClient => "matrix_sdk::http_client",
LogTarget::MatrixSdkSlidingSync => "matrix_sdk::sliding_sync",
LogTarget::MatrixSdkBaseSlidingSync => "matrix_sdk_base::sliding_sync",
LogTarget::MatrixSdkUiTimeline => "matrix_sdk_ui::timeline",
LogTarget::MatrixSdkEventCache => "matrix_sdk::event_cache",
LogTarget::MatrixSdkBaseEventCache => "matrix_sdk_base::event_cache",
LogTarget::MatrixSdkEventCacheStore => "matrix_sdk_sqlite::event_cache_store",
}
}
}
const DEFAULT_TARGET_LOG_LEVELS: &[(LogTarget, LogLevel)] = &[
(LogTarget::Hyper, LogLevel::Warn),
(LogTarget::MatrixSdkFfi, LogLevel::Info),
(LogTarget::MatrixSdk, LogLevel::Info),
(LogTarget::MatrixSdkClient, LogLevel::Trace),
(LogTarget::MatrixSdkCrypto, LogLevel::Debug),
(LogTarget::MatrixSdkCryptoAccount, LogLevel::Trace),
(LogTarget::MatrixSdkOidc, LogLevel::Trace),
(LogTarget::MatrixSdkHttpClient, LogLevel::Debug),
(LogTarget::MatrixSdkSlidingSync, LogLevel::Info),
(LogTarget::MatrixSdkBaseSlidingSync, LogLevel::Info),
(LogTarget::MatrixSdkUiTimeline, LogLevel::Info),
(LogTarget::MatrixSdkEventCache, LogLevel::Info),
(LogTarget::MatrixSdkBaseEventCache, LogLevel::Info),
(LogTarget::MatrixSdkEventCacheStore, LogLevel::Info),
];
const IMMUTABLE_TARGET_LOG_LEVELS: &[LogTarget] = &[
LogTarget::Hyper, // Too verbose
LogTarget::MatrixSdk, // Too generic
LogTarget::MatrixSdkFfi, // Too verbose
];
#[derive(uniffi::Record)]
pub struct TracingConfiguration {
/// A filter line following the [RUST_LOG format].
///
/// [RUST_LOG format]: https://rust-lang-nursery.github.io/rust-cookbook/development_tools/debugging/config_log.html
filter: String,
/// The desired log level
log_level: LogLevel,
/// Additional targets that the FFI client would like to use e.g.
/// the target names for created [`crate::tracing::Span`]
extra_targets: Option<Vec<String>>,
/// Whether to log to stdout, or in the logcat on Android.
write_to_stdout_or_system: bool,
@@ -242,12 +309,111 @@ pub struct TracingConfiguration {
write_to_files: Option<TracingFileConfiguration>,
}
fn build_tracing_filter(config: &TracingConfiguration) -> String {
// We are intentionally not setting a global log level because we don't want to
// risk third party crates logging sensitive information.
// As such we need to make sure that panics will be properly logged.
// On 2025-01-08, `log_panics` uses the `panic` target, at the error log level.
let mut filters = vec!["panic=error".to_owned()];
DEFAULT_TARGET_LOG_LEVELS.iter().for_each(|(target, level)| {
// Use the default if the log level shouldn't be changed for this target or
// if it's already logging more than requested
let level = if IMMUTABLE_TARGET_LOG_LEVELS.contains(target) || level > &config.log_level {
level.as_str()
} else {
config.log_level.as_str()
};
filters.push(format!("{}={}", target.as_str(), level));
});
// Finally append the extra targets requested by the client
if let Some(extra_targets) = &config.extra_targets {
for target in extra_targets {
filters.push(format!("{}={}", target, config.log_level.as_str()));
}
}
filters.join(",")
}
#[matrix_sdk_ffi_macros::export]
pub fn setup_tracing(config: TracingConfiguration) {
log_panics();
tracing_subscriber::registry()
.with(EnvFilter::new(&config.filter))
.with(EnvFilter::new(build_tracing_filter(&config)))
.with(text_layers(config))
.init();
}
#[cfg(test)]
mod tests {
use super::build_tracing_filter;
#[test]
fn test_default_tracing_filter() {
let config = super::TracingConfiguration {
log_level: super::LogLevel::Error,
extra_targets: Some(vec!["super_duper_app".to_owned()]),
write_to_stdout_or_system: true,
write_to_files: None,
};
let filter = build_tracing_filter(&config);
assert_eq!(
filter,
"panic=error,\
hyper=warn,\
matrix_sdk_ffi=info,\
matrix_sdk=info,\
matrix_sdk::client=trace,\
matrix_sdk_crypto=debug,\
matrix_sdk_crypto::olm::account=trace,\
matrix_sdk::oidc=trace,\
matrix_sdk::http_client=debug,\
matrix_sdk::sliding_sync=info,\
matrix_sdk_base::sliding_sync=info,\
matrix_sdk_ui::timeline=info,\
matrix_sdk::event_cache=info,\
matrix_sdk_base::event_cache=info,\
matrix_sdk_sqlite::event_cache_store=info,\
super_duper_app=error"
);
}
#[test]
fn test_trace_tracing_filter() {
let config = super::TracingConfiguration {
log_level: super::LogLevel::Trace,
extra_targets: Some(vec!["super_duper_app".to_owned(), "some_other_span".to_owned()]),
write_to_stdout_or_system: true,
write_to_files: None,
};
let filter = build_tracing_filter(&config);
assert_eq!(
filter,
"panic=error,\
hyper=warn,\
matrix_sdk_ffi=info,\
matrix_sdk=info,\
matrix_sdk::client=trace,\
matrix_sdk_crypto=trace,\
matrix_sdk_crypto::olm::account=trace,\
matrix_sdk::oidc=trace,\
matrix_sdk::http_client=trace,\
matrix_sdk::sliding_sync=trace,\
matrix_sdk_base::sliding_sync=trace,\
matrix_sdk_ui::timeline=trace,\
matrix_sdk::event_cache=trace,\
matrix_sdk_base::event_cache=trace,\
matrix_sdk_sqlite::event_cache_store=trace,\
super_duper_app=trace,\
some_other_span=trace"
);
}
}
+423 -75
View File
@@ -1,17 +1,16 @@
use std::{collections::HashMap, pin::pin, sync::Arc};
use anyhow::{Context, Result};
use futures_util::StreamExt;
use futures_util::{pin_mut, StreamExt};
use matrix_sdk::{
crypto::LocalTrust,
event_cache::paginator::PaginatorError,
room::{
edit::EditedContent, power_levels::RoomPowerLevelChanges, Room as SdkRoom, RoomMemberRole,
},
ComposerDraft as SdkComposerDraft, ComposerDraftType as SdkComposerDraftType,
RoomHero as SdkRoomHero, RoomMemberships, RoomState,
};
use matrix_sdk_ui::timeline::{PaginationError, RoomExt, TimelineFocus};
use matrix_sdk_ui::timeline::{default_event_filter, RoomExt};
use mime::Mime;
use ruma::{
api::client::room::report_content,
@@ -20,26 +19,32 @@ use ruma::{
call::notify,
room::{
avatar::ImageInfo as RumaAvatarImageInfo,
message::RoomMessageEventContentWithoutRelation,
history_visibility::HistoryVisibility as RumaHistoryVisibility,
join_rules::JoinRule as RumaJoinRule, message::RoomMessageEventContentWithoutRelation,
power_levels::RoomPowerLevels as RumaPowerLevels, MediaSource,
},
TimelineEventType,
AnyMessageLikeEventContent, AnySyncTimelineEvent, TimelineEventType,
},
EventId, Int, OwnedDeviceId, OwnedUserId, RoomAliasId, UserId,
};
use tokio::sync::RwLock;
use tracing::error;
use tracing::{error, warn};
use super::RUNTIME;
use crate::{
chunk_iterator::ChunkIterator,
error::{ClientError, MediaInfoError, RoomError},
client::{JoinRule, RoomVisibility},
error::{ClientError, MediaInfoError, NotYetImplemented, RoomError},
event::{MessageLikeEventType, StateEventType},
identity_status_change::IdentityStatusChange,
live_location_share::{LastLocation, LiveLocationShare},
room_info::RoomInfo,
room_member::RoomMember,
ruma::{ImageInfo, Mentions, NotifyType},
timeline::{FocusEventError, ReceiptType, SendHandle, Timeline},
ruma::{ImageInfo, LocationContent, Mentions, NotifyType},
timeline::{
configuration::{AllowedMessageTypes, TimelineConfiguration},
ReceiptType, SendHandle, Timeline,
},
utils::u64_to_uint,
TaskHandle,
};
@@ -50,6 +55,7 @@ pub enum Membership {
Joined,
Left,
Knocked,
Banned,
}
impl From<RoomState> for Membership {
@@ -59,6 +65,7 @@ impl From<RoomState> for Membership {
RoomState::Joined => Membership::Joined,
RoomState::Left => Membership::Left,
RoomState::Knocked => Membership::Knocked,
RoomState::Banned => Membership::Banned,
}
}
}
@@ -83,10 +90,6 @@ impl Room {
#[matrix_sdk_ffi_macros::export]
impl Room {
pub fn id(&self) -> String {
self.inner.room_id().to_string()
}
/// Returns the room's name from the state event if available, otherwise
/// compute a room name based on the room's nature (DM or not) and number of
/// members.
@@ -196,68 +199,42 @@ impl Room {
}
}
/// Returns a timeline focused on the given event.
///
/// Note: this timeline is independent from that returned with
/// [`Self::timeline`], and as such it is not cached.
pub async fn timeline_focused_on_event(
/// Build a new timeline instance with the given configuration.
pub async fn timeline_with_configuration(
&self,
event_id: String,
num_context_events: u16,
internal_id_prefix: Option<String>,
) -> Result<Arc<Timeline>, FocusEventError> {
let parsed_event_id = EventId::parse(&event_id).map_err(|err| {
FocusEventError::InvalidEventId { event_id: event_id.clone(), err: err.to_string() }
})?;
configuration: TimelineConfiguration,
) -> Result<Arc<Timeline>, ClientError> {
let mut builder = matrix_sdk_ui::timeline::Timeline::builder(&self.inner);
let room = &self.inner;
builder = builder.with_focus(configuration.focus.try_into()?);
let mut builder = matrix_sdk_ui::timeline::Timeline::builder(room);
if let AllowedMessageTypes::Only { types } = configuration.allowed_message_types {
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)) => {
types.contains(&content.msgtype.into())
}
_ => false,
},
_ => false,
}
});
}
if let Some(internal_id_prefix) = internal_id_prefix {
if let Some(internal_id_prefix) = configuration.internal_id_prefix {
builder = builder.with_internal_id_prefix(internal_id_prefix);
}
let timeline = match builder
.with_focus(TimelineFocus::Event { target: parsed_event_id, num_context_events })
.build()
.await
{
Ok(t) => t,
Err(err) => {
if let matrix_sdk_ui::timeline::Error::PaginationError(
PaginationError::Paginator(PaginatorError::EventNotFound(..)),
) = err
{
return Err(FocusEventError::EventNotFound { event_id: event_id.to_string() });
}
return Err(FocusEventError::Other { msg: err.to_string() });
}
};
builder = builder.with_date_divider_mode(configuration.date_divider_mode.into());
let timeline = builder.build().await?;
Ok(Timeline::new(timeline))
}
pub async fn pinned_events_timeline(
&self,
internal_id_prefix: Option<String>,
max_events_to_load: u16,
max_concurrent_requests: u16,
) -> Result<Arc<Timeline>, ClientError> {
let room = &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);
}
let timeline = builder
.with_focus(TimelineFocus::PinnedEvents { max_events_to_load, max_concurrent_requests })
.build()
.await?;
Ok(Timeline::new(timeline))
pub fn id(&self) -> String {
self.inner.room_id().to_string()
}
pub fn is_encrypted(&self) -> Result<bool, ClientError> {
@@ -298,7 +275,7 @@ impl Room {
}
pub async fn room_info(&self) -> Result<RoomInfo, ClientError> {
Ok(RoomInfo::new(&self.inner).await?)
RoomInfo::new(&self.inner).await
}
pub fn subscribe_to_room_info_updates(
@@ -336,6 +313,22 @@ impl Room {
Ok(())
}
/// Send a raw event to the room.
///
/// # Arguments
///
/// * `event_type` - The type of the event to send.
///
/// * `content` - The content of the event to send encoded as JSON string.
pub async fn send_raw(&self, event_type: String, content: String) -> Result<(), ClientError> {
let content_json: serde_json::Value = serde_json::from_str(&content)
.map_err(|e| ClientError::Generic { msg: format!("Failed to parse JSON: {e}") })?;
self.inner.send_raw(&event_type, content_json).await?;
Ok(())
}
/// Redacts an event from the room.
///
/// # Arguments
@@ -386,15 +379,12 @@ impl Room {
let int_score = score.map(|value| value.into());
self.inner
.client()
.send(
report_content::v3::Request::new(
self.inner.room_id().into(),
event_id,
int_score,
reason,
),
None,
)
.send(report_content::v3::Request::new(
self.inner.room_id().into(),
event_id,
int_score,
reason,
))
.await?;
Ok(())
}
@@ -840,6 +830,305 @@ impl Room {
Ok(())
}
/// Clear the event cache storage for the current room.
///
/// This will remove all the information related to the event cache, in
/// memory and in the persisted storage, if enabled.
pub async fn clear_event_cache_storage(&self) -> Result<(), ClientError> {
let (room_event_cache, _drop_handles) = self.inner.event_cache().await?;
room_event_cache.clear().await?;
Ok(())
}
/// Subscribes to requests to join this room (knock member events), using a
/// `listener` to be notified of the changes.
///
/// The current requests to join the room will be emitted immediately
/// when subscribing, along with a [`TaskHandle`] to cancel the
/// subscription.
pub async fn subscribe_to_knock_requests(
self: Arc<Self>,
listener: Box<dyn KnockRequestsListener>,
) -> Result<Arc<TaskHandle>, ClientError> {
let (stream, seen_ids_cleanup_handle) = self.inner.subscribe_to_knock_requests().await?;
let handle = Arc::new(TaskHandle::new(RUNTIME.spawn(async move {
pin_mut!(stream);
while let Some(requests) = stream.next().await {
listener.call(requests.into_iter().map(Into::into).collect());
}
// Cancel the seen ids cleanup task
seen_ids_cleanup_handle.abort();
})));
Ok(handle)
}
/// Return a debug representation for the internal room events data
/// structure, one line per entry in the resulting vector.
pub async fn room_events_debug_string(&self) -> Result<Vec<String>, ClientError> {
let (cache, _drop_guards) = self.inner.event_cache().await?;
Ok(cache.debug_string().await)
}
/// Update the canonical alias of the room.
///
/// Note that publishing the alias in the room directory is done separately.
pub async fn update_canonical_alias(
&self,
alias: Option<String>,
alt_aliases: Vec<String>,
) -> Result<(), ClientError> {
let new_alias = alias.map(TryInto::try_into).transpose()?;
let new_alt_aliases =
alt_aliases.into_iter().map(RoomAliasId::parse).collect::<Result<_, _>>()?;
self.inner
.privacy_settings()
.update_canonical_alias(new_alias, new_alt_aliases)
.await
.map_err(Into::into)
}
/// Publish a new room alias for this room in the room directory.
///
/// Returns:
/// - `true` if the room alias didn't exist and it's now published.
/// - `false` if the room alias was already present so it couldn't be
/// published.
pub async fn publish_room_alias_in_room_directory(
&self,
alias: String,
) -> Result<bool, ClientError> {
let new_alias = RoomAliasId::parse(alias)?;
self.inner
.privacy_settings()
.publish_room_alias_in_room_directory(&new_alias)
.await
.map_err(Into::into)
}
/// Remove an existing room alias for this room in the room directory.
///
/// Returns:
/// - `true` if the room alias was present and it's now removed from the
/// room directory.
/// - `false` if the room alias didn't exist so it couldn't be removed.
pub async fn remove_room_alias_from_room_directory(
&self,
alias: String,
) -> Result<bool, ClientError> {
let alias = RoomAliasId::parse(alias)?;
self.inner
.privacy_settings()
.remove_room_alias_from_room_directory(&alias)
.await
.map_err(Into::into)
}
/// Enable End-to-end encryption in this room.
pub async fn enable_encryption(&self) -> Result<(), ClientError> {
self.inner.enable_encryption().await.map_err(Into::into)
}
/// Update room history visibility for this room.
pub async fn update_history_visibility(
&self,
visibility: RoomHistoryVisibility,
) -> Result<(), ClientError> {
let visibility: RumaHistoryVisibility = visibility.try_into()?;
self.inner
.privacy_settings()
.update_room_history_visibility(visibility)
.await
.map_err(Into::into)
}
/// Update the join rule for this room.
pub async fn update_join_rules(&self, new_rule: JoinRule) -> Result<(), ClientError> {
let new_rule: RumaJoinRule = new_rule.try_into()?;
self.inner.privacy_settings().update_join_rule(new_rule).await.map_err(Into::into)
}
/// Update the room's visibility in the room directory.
pub async fn update_room_visibility(
&self,
visibility: RoomVisibility,
) -> Result<(), ClientError> {
self.inner
.privacy_settings()
.update_room_visibility(visibility.into())
.await
.map_err(Into::into)
}
/// Returns the visibility for this room in the room directory.
///
/// [Public](`RoomVisibility::Public`) rooms are listed in the room
/// directory and can be found using it.
pub async fn get_room_visibility(&self) -> Result<RoomVisibility, ClientError> {
let visibility = self.inner.privacy_settings().get_room_visibility().await?;
Ok(visibility.into())
}
/// Start the current users live location share in the room.
pub async fn start_live_location_share(&self, duration_millis: u64) -> Result<(), ClientError> {
self.inner.start_live_location_share(duration_millis, None).await?;
Ok(())
}
/// Stop the current users live location share in the room.
pub async fn stop_live_location_share(&self) -> Result<(), ClientError> {
self.inner.stop_live_location_share().await.expect("Unable to stop live location share");
Ok(())
}
/// Send the current users live location beacon in the room.
pub async fn send_live_location(&self, geo_uri: String) -> Result<(), ClientError> {
self.inner
.send_location_beacon(geo_uri)
.await
.expect("Unable to send live location beacon");
Ok(())
}
/// Subscribes to live location shares in this room, using a `listener` to
/// be notified of the changes.
///
/// The current live location shares will be emitted immediately when
/// subscribing, along with a [`TaskHandle`] to cancel the subscription.
pub fn subscribe_to_live_location_shares(
self: Arc<Self>,
listener: Box<dyn LiveLocationShareListener>,
) -> Arc<TaskHandle> {
let room = self.inner.clone();
Arc::new(TaskHandle::new(RUNTIME.spawn(async move {
let subscription = room.observe_live_location_shares();
let mut stream = subscription.subscribe();
let mut pinned_stream = pin!(stream);
while let Some(event) = pinned_stream.next().await {
let last_location = LocationContent {
body: "".to_owned(),
geo_uri: event.last_location.location.uri.clone().to_string(),
description: None,
zoom_level: None,
asset: None,
};
let Some(beacon_info) = event.beacon_info else {
warn!("Live location share is missing the associated beacon_info state, skipping event.");
continue;
};
listener.call(vec![LiveLocationShare {
last_location: LastLocation {
location: last_location,
ts: event.last_location.ts.0.into(),
},
is_live: beacon_info.is_live(),
user_id: event.user_id.to_string(),
}])
}
})))
}
/// Forget this room.
///
/// This communicates to the homeserver that it should forget the room.
///
/// Only left or banned-from rooms can be forgotten.
pub async fn forget(&self) -> Result<(), ClientError> {
self.inner.forget().await?;
Ok(())
}
}
/// A listener for receiving new live location shares in a room.
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait LiveLocationShareListener: Sync + Send {
fn call(&self, live_location_shares: Vec<LiveLocationShare>);
}
impl From<matrix_sdk::room::knock_requests::KnockRequest> for KnockRequest {
fn from(request: matrix_sdk::room::knock_requests::KnockRequest) -> Self {
Self {
event_id: request.event_id.to_string(),
user_id: request.member_info.user_id.to_string(),
room_id: request.room_id().to_string(),
display_name: request.member_info.display_name.clone(),
avatar_url: request.member_info.avatar_url.as_ref().map(|url| url.to_string()),
reason: request.member_info.reason.clone(),
timestamp: request.timestamp.map(|ts| ts.into()),
is_seen: request.is_seen,
actions: Arc::new(KnockRequestActions { inner: request }),
}
}
}
/// A listener for receiving new requests to a join a room.
#[matrix_sdk_ffi_macros::export(callback_interface)]
pub trait KnockRequestsListener: Send + Sync {
fn call(&self, join_requests: Vec<KnockRequest>);
}
/// An FFI representation of a request to join a room.
#[derive(Debug, Clone, uniffi::Record)]
pub struct KnockRequest {
/// The event id of the event that contains the `knock` membership change.
pub event_id: String,
/// The user id of the user who's requesting to join the room.
pub user_id: String,
/// The room id of the room whose access was requested.
pub room_id: String,
/// The optional display name of the user who's requesting to join the room.
pub display_name: Option<String>,
/// The optional avatar url of the user who's requesting to join the room.
pub avatar_url: Option<String>,
/// An optional reason why the user wants join the room.
pub reason: Option<String>,
/// The timestamp when this request was created.
pub timestamp: Option<u64>,
/// Whether the knock request has been marked as `seen` so it can be
/// filtered by the client.
pub is_seen: bool,
/// A set of actions to perform for this knock request.
pub actions: Arc<KnockRequestActions>,
}
/// A set of actions to perform for a knock request.
#[derive(Debug, Clone, uniffi::Object)]
pub struct KnockRequestActions {
inner: matrix_sdk::room::knock_requests::KnockRequest,
}
#[matrix_sdk_ffi_macros::export]
impl KnockRequestActions {
/// Accepts the knock request by inviting the user to the room.
pub async fn accept(&self) -> Result<(), ClientError> {
self.inner.accept().await.map_err(Into::into)
}
/// Declines the knock request by kicking the user from the room with an
/// optional reason.
pub async fn decline(&self, reason: Option<String>) -> Result<(), ClientError> {
self.inner.decline(reason.as_deref()).await.map_err(Into::into)
}
/// Declines the knock request by banning the user from the room with an
/// optional reason.
pub async fn decline_and_ban(&self, reason: Option<String>) -> Result<(), ClientError> {
self.inner.decline_and_ban(reason.as_deref()).await.map_err(Into::into)
}
/// Marks the knock request as 'seen'.
///
/// **IMPORTANT**: this won't update the current reference to this request,
/// a new one with the updated value should be emitted instead.
pub async fn mark_as_seen(&self) -> Result<(), ClientError> {
self.inner.mark_as_seen().await.map_err(Into::into)
}
}
/// Generates a `matrix.to` permalink to the given room alias.
@@ -973,7 +1262,7 @@ impl TryFrom<ImageInfo> for RumaAvatarImageInfo {
fn try_from(value: ImageInfo) -> Result<Self, MediaInfoError> {
let thumbnail_url = if let Some(media_source) = value.thumbnail_source {
match media_source.as_ref() {
match &media_source.as_ref().media_source {
MediaSource::Plain(mxc_uri) => Some(mxc_uri.clone()),
MediaSource::Encrypted(_) => return Err(MediaInfoError::InvalidField),
}
@@ -1073,3 +1362,62 @@ impl TryFrom<ComposerDraftType> for SdkComposerDraftType {
Ok(draft_type)
}
}
#[derive(Debug, Clone, uniffi::Enum)]
pub enum RoomHistoryVisibility {
/// Previous events are accessible to newly joined members from the point
/// they were invited onwards.
///
/// Events stop being accessible when the member's state changes to
/// something other than *invite* or *join*.
Invited,
/// Previous events are accessible to newly joined members from the point
/// they joined the room onwards.
/// Events stop being accessible when the member's state changes to
/// something other than *join*.
Joined,
/// Previous events are always accessible to newly joined members.
///
/// All events in the room are accessible, even those sent when the member
/// was not a part of the room.
Shared,
/// All events while this is the `HistoryVisibility` value may be shared by
/// any participating homeserver with anyone, regardless of whether they
/// have ever joined the room.
WorldReadable,
/// A custom visibility value.
Custom { value: String },
}
impl TryFrom<RumaHistoryVisibility> for RoomHistoryVisibility {
type Error = NotYetImplemented;
fn try_from(value: RumaHistoryVisibility) -> Result<Self, Self::Error> {
match value {
RumaHistoryVisibility::Invited => Ok(RoomHistoryVisibility::Invited),
RumaHistoryVisibility::Shared => Ok(RoomHistoryVisibility::Shared),
RumaHistoryVisibility::WorldReadable => Ok(RoomHistoryVisibility::WorldReadable),
RumaHistoryVisibility::Joined => Ok(RoomHistoryVisibility::Joined),
RumaHistoryVisibility::_Custom(_) => {
Ok(RoomHistoryVisibility::Custom { value: value.to_string() })
}
_ => Err(NotYetImplemented),
}
}
}
impl TryFrom<RoomHistoryVisibility> for RumaHistoryVisibility {
type Error = NotYetImplemented;
fn try_from(value: RoomHistoryVisibility) -> Result<Self, Self::Error> {
match value {
RoomHistoryVisibility::Invited => Ok(RumaHistoryVisibility::Invited),
RoomHistoryVisibility::Shared => Ok(RumaHistoryVisibility::Shared),
RoomHistoryVisibility::Joined => Ok(RumaHistoryVisibility::Joined),
RoomHistoryVisibility::WorldReadable => Ok(RumaHistoryVisibility::WorldReadable),
RoomHistoryVisibility::Custom { .. } => Err(NotYetImplemented),
}
}
}
+17 -3
View File
@@ -1,10 +1,13 @@
use std::collections::HashMap;
use matrix_sdk::RoomState;
use tracing::warn;
use crate::{
client::JoinRule,
error::ClientError,
notification_settings::RoomNotificationMode,
room::{Membership, RoomHero},
room::{Membership, RoomHero, RoomHistoryVisibility},
room_member::RoomMember,
};
@@ -54,12 +57,16 @@ pub struct RoomInfo {
/// Events causing mentions/highlights for the user, according to their
/// notification settings.
num_unread_mentions: u64,
/// The currently pinned event ids
/// The currently pinned event ids.
pinned_event_ids: Vec<String>,
/// The join rule for this room, if known.
join_rule: Option<JoinRule>,
/// The history visibility for this room, if known.
history_visibility: RoomHistoryVisibility,
}
impl RoomInfo {
pub(crate) async fn new(room: &matrix_sdk::Room) -> matrix_sdk::Result<Self> {
pub(crate) async fn new(room: &matrix_sdk::Room) -> Result<Self, ClientError> {
let unread_notification_counts = room.unread_notification_counts();
let power_levels_map = room.users_with_power_levels().await;
@@ -70,6 +77,11 @@ impl RoomInfo {
let pinned_event_ids =
room.pinned_event_ids().unwrap_or_default().iter().map(|id| id.to_string()).collect();
let join_rule = room.join_rule().try_into();
if let Err(e) = &join_rule {
warn!("Failed to parse join rule: {:?}", e);
}
Ok(Self {
id: room.room_id().to_string(),
creator: room.creator().as_ref().map(ToString::to_string),
@@ -118,6 +130,8 @@ impl RoomInfo {
num_unread_notifications: room.num_unread_notifications(),
num_unread_mentions: room.num_unread_mentions(),
pinned_event_ids,
join_rule: join_rule.ok(),
history_visibility: room.history_visibility_or_default().try_into()?,
})
}
}
+13 -2
View File
@@ -566,7 +566,7 @@ impl RoomListItem {
}
async fn room_info(&self) -> Result<RoomInfo, ClientError> {
Ok(RoomInfo::new(self.inner.inner_room()).await?)
RoomInfo::new(self.inner.inner_room()).await
}
/// The room's current membership state.
@@ -616,7 +616,8 @@ impl RoomListItem {
// Do the thing.
let client = self.inner.client();
let (room_or_alias_id, server_names) = if let Some(alias) = self.inner.canonical_alias() {
let (room_or_alias_id, mut server_names) = if let Some(alias) = self.inner.canonical_alias()
{
let room_or_alias_id: OwnedRoomOrAliasId = alias.into();
(room_or_alias_id, Vec::new())
} else {
@@ -624,6 +625,16 @@ impl RoomListItem {
(room_or_alias_id, server_names)
};
// If no server names are provided and the room's membership is invited,
// add the server name from the sender's user id as a fallback value
if server_names.is_empty() {
if let Ok(invite_details) = self.inner.invite_details().await {
if let Some(inviter) = invite_details.inviter {
server_names.push(inviter.user_id().server_name().to_owned());
}
}
}
let room_preview = client.get_room_preview(&room_or_alias_id, server_names).await?;
Ok(Arc::new(RoomPreview::new(AsyncRuntimeDropped::new(client), room_preview)))
+3 -1
View File
@@ -76,7 +76,7 @@ pub fn matrix_to_user_permalink(user_id: String) -> Result<String, ClientError>
Ok(user_id.matrix_to_uri().to_string())
}
#[derive(uniffi::Record)]
#[derive(Clone, uniffi::Record)]
pub struct RoomMember {
pub user_id: String,
pub display_name: Option<String>,
@@ -87,6 +87,7 @@ pub struct RoomMember {
pub normalized_power_level: i64,
pub is_ignored: bool,
pub suggested_role_for_power_level: RoomMemberRole,
pub membership_change_reason: Option<String>,
}
impl TryFrom<SdkRoomMember> for RoomMember {
@@ -103,6 +104,7 @@ impl TryFrom<SdkRoomMember> for RoomMember {
normalized_power_level: m.normalized_power_level(),
is_ignored: m.is_ignored(),
suggested_role_for_power_level: m.suggested_role_for_power_level(),
membership_change_reason: m.event().reason().map(|s| s.to_owned()),
})
}
}
+45 -2
View File
@@ -4,7 +4,10 @@ use ruma::{room::RoomType as RumaRoomType, space::SpaceRoomJoinRule};
use tracing::warn;
use crate::{
client::JoinRule, error::ClientError, room::Membership, room_member::RoomMember,
client::JoinRule,
error::ClientError,
room::{Membership, RoomHero},
room_member::RoomMember,
utils::AsyncRuntimeDropped,
};
@@ -38,6 +41,10 @@ impl RoomPreview {
.try_into()
.map_err(|_| anyhow::anyhow!("unhandled SpaceRoomJoinRule kind"))?,
is_direct: info.is_direct,
heroes: info
.heroes
.as_ref()
.map(|heroes| heroes.iter().map(|h| h.to_owned().into()).collect()),
})
}
@@ -57,6 +64,40 @@ impl RoomPreview {
let invite_details = room.invite_details().await.ok()?;
invite_details.inviter.and_then(|m| m.try_into().ok())
}
/// Forget the room if we had access to it, and it was left or banned.
pub async fn forget(&self) -> Result<(), ClientError> {
let room =
self.client.get_room(&self.inner.room_id).context("missing room for a room preview")?;
room.forget().await?;
Ok(())
}
/// Get the membership details for the current user.
pub async fn own_membership_details(&self) -> Option<RoomMembershipDetails> {
let room = self.client.get_room(&self.inner.room_id)?;
let (own_member, sender_member) = match room.own_membership_details().await {
Ok(memberships) => memberships,
Err(error) => {
warn!("Couldn't get membership info: {error}");
return None;
}
};
Some(RoomMembershipDetails {
own_room_member: own_member.try_into().ok()?,
sender_room_member: sender_member.and_then(|member| member.try_into().ok()),
})
}
}
/// Contains the current user's room member info and the optional room member
/// info of the sender of the `m.room.member` event that this info represents.
#[derive(uniffi::Record)]
pub struct RoomMembershipDetails {
pub own_room_member: RoomMember,
pub sender_room_member: Option<RoomMember>,
}
impl RoomPreview {
@@ -85,13 +126,15 @@ pub struct RoomPreviewInfo {
/// The room type (space, custom) or nothing, if it's a regular room.
pub room_type: RoomType,
/// Is the history world-readable for this room?
pub is_history_world_readable: bool,
pub is_history_world_readable: Option<bool>,
/// The membership state for the current user, if known.
pub membership: Option<Membership>,
/// The join rule for this room (private, public, knock, etc.).
pub join_rule: JoinRule,
/// Whether the room is direct or not, if known.
pub is_direct: Option<bool>,
/// Room heroes.
pub heroes: Option<Vec<RoomHero>>,
}
impl TryFrom<SpaceRoomJoinRule> for JoinRule {
+137 -67
View File
@@ -15,9 +15,7 @@
use std::{collections::BTreeSet, sync::Arc, time::Duration};
use extension_trait::extension_trait;
use matrix_sdk::attachment::{
BaseAudioInfo, BaseFileInfo, BaseImageInfo, BaseThumbnailInfo, BaseVideoInfo,
};
use matrix_sdk::attachment::{BaseAudioInfo, BaseFileInfo, BaseImageInfo, BaseVideoInfo};
use ruma::{
assign,
events::{
@@ -42,7 +40,8 @@ use ruma::{
VideoInfo as RumaVideoInfo,
VideoMessageEventContent as RumaVideoMessageEventContent,
},
ImageInfo as RumaImageInfo, MediaSource, ThumbnailInfo as RumaThumbnailInfo,
ImageInfo as RumaImageInfo, MediaSource as RumaMediaSource,
ThumbnailInfo as RumaThumbnailInfo,
},
},
matrix_uri::MatrixId as RumaMatrixId,
@@ -154,11 +153,6 @@ impl From<&RumaMatrixId> for MatrixId {
}
}
#[matrix_sdk_ffi_macros::export]
pub fn media_source_from_url(url: String) -> Arc<MediaSource> {
Arc::new(MediaSource::Plain(url.into()))
}
#[matrix_sdk_ffi_macros::export]
pub fn message_event_content_new(
msgtype: MessageType,
@@ -200,21 +194,84 @@ pub fn message_event_content_from_html_as_emote(
)))
}
#[extension_trait]
pub impl MediaSourceExt for MediaSource {
fn from_json(json: String) -> Result<MediaSource, ClientError> {
let res = serde_json::from_str(&json)?;
Ok(res)
#[derive(Clone, uniffi::Object)]
pub struct MediaSource {
pub(crate) media_source: RumaMediaSource,
}
#[matrix_sdk_ffi_macros::export]
impl MediaSource {
#[uniffi::constructor]
pub fn from_url(url: String) -> Result<Arc<MediaSource>, ClientError> {
let media_source = RumaMediaSource::Plain(url.into());
media_source.verify()?;
Ok(Arc::new(MediaSource { media_source }))
}
fn to_json(&self) -> String {
serde_json::to_string(self).expect("Media source should always be serializable ")
pub fn url(&self) -> String {
self.media_source.url()
}
// Used on Element X Android
#[uniffi::constructor]
pub fn from_json(json: String) -> Result<Arc<Self>, ClientError> {
let media_source: RumaMediaSource = serde_json::from_str(&json)?;
media_source.verify()?;
Ok(Arc::new(MediaSource { media_source }))
}
// Used on Element X Android
pub fn to_json(&self) -> String {
serde_json::to_string(&self.media_source)
.expect("Media source should always be serializable ")
}
}
impl TryFrom<RumaMediaSource> for MediaSource {
type Error = ClientError;
fn try_from(value: RumaMediaSource) -> Result<Self, Self::Error> {
value.verify()?;
Ok(Self { media_source: value })
}
}
impl TryFrom<&RumaMediaSource> for MediaSource {
type Error = ClientError;
fn try_from(value: &RumaMediaSource) -> Result<Self, Self::Error> {
value.verify()?;
Ok(Self { media_source: value.clone() })
}
}
impl From<MediaSource> for RumaMediaSource {
fn from(value: MediaSource) -> Self {
value.media_source
}
}
#[extension_trait]
pub(crate) impl MediaSourceExt for RumaMediaSource {
fn verify(&self) -> Result<(), ClientError> {
match self {
RumaMediaSource::Plain(url) => {
url.validate().map_err(|e| ClientError::Generic { msg: e.to_string() })?;
}
RumaMediaSource::Encrypted(file) => {
file.url.validate().map_err(|e| ClientError::Generic { msg: e.to_string() })?;
}
}
Ok(())
}
fn url(&self) -> String {
match self {
MediaSource::Plain(url) => url.to_string(),
MediaSource::Encrypted(file) => file.url.to_string(),
RumaMediaSource::Plain(url) => url.to_string(),
RumaMediaSource::Encrypted(file) => file.url.to_string(),
}
}
}
@@ -280,7 +337,7 @@ fn get_body_and_filename(filename: String, caption: Option<String>) -> (String,
}
impl TryFrom<MessageType> for RumaMessageType {
type Error = serde_json::Error;
type Error = ClientError;
fn try_from(value: MessageType) -> Result<Self, Self::Error> {
Ok(match value {
@@ -292,7 +349,7 @@ impl TryFrom<MessageType> for RumaMessageType {
MessageType::Image { content } => {
let (body, filename) = get_body_and_filename(content.filename, content.caption);
let mut event_content =
RumaImageMessageEventContent::new(body, (*content.source).clone())
RumaImageMessageEventContent::new(body, (*content.source).clone().into())
.info(content.info.map(Into::into).map(Box::new));
event_content.formatted = content.formatted_caption.map(Into::into);
event_content.filename = filename;
@@ -301,7 +358,7 @@ impl TryFrom<MessageType> for RumaMessageType {
MessageType::Audio { content } => {
let (body, filename) = get_body_and_filename(content.filename, content.caption);
let mut event_content =
RumaAudioMessageEventContent::new(body, (*content.source).clone())
RumaAudioMessageEventContent::new(body, (*content.source).clone().into())
.info(content.info.map(Into::into).map(Box::new));
event_content.formatted = content.formatted_caption.map(Into::into);
event_content.filename = filename;
@@ -310,7 +367,7 @@ impl TryFrom<MessageType> for RumaMessageType {
MessageType::Video { content } => {
let (body, filename) = get_body_and_filename(content.filename, content.caption);
let mut event_content =
RumaVideoMessageEventContent::new(body, (*content.source).clone())
RumaVideoMessageEventContent::new(body, (*content.source).clone().into())
.info(content.info.map(Into::into).map(Box::new));
event_content.formatted = content.formatted_caption.map(Into::into);
event_content.filename = filename;
@@ -319,7 +376,7 @@ impl TryFrom<MessageType> for RumaMessageType {
MessageType::File { content } => {
let (body, filename) = get_body_and_filename(content.filename, content.caption);
let mut event_content =
RumaFileMessageEventContent::new(body, (*content.source).clone())
RumaFileMessageEventContent::new(body, (*content.source).clone().into())
.info(content.info.map(Into::into).map(Box::new));
event_content.formatted = content.formatted_caption.map(Into::into);
event_content.filename = filename;
@@ -345,9 +402,11 @@ impl TryFrom<MessageType> for RumaMessageType {
}
}
impl From<RumaMessageType> for MessageType {
fn from(value: RumaMessageType) -> Self {
match value {
impl TryFrom<RumaMessageType> for MessageType {
type Error = ClientError;
fn try_from(value: RumaMessageType) -> Result<Self, Self::Error> {
Ok(match value {
RumaMessageType::Emote(c) => MessageType::Emote {
content: EmoteMessageContent {
body: c.body.clone(),
@@ -359,16 +418,17 @@ impl From<RumaMessageType> for MessageType {
filename: c.filename().to_owned(),
caption: c.caption().map(ToString::to_string),
formatted_caption: c.formatted_caption().map(Into::into),
source: Arc::new(c.source.clone()),
info: c.info.as_deref().map(Into::into),
source: Arc::new(c.source.try_into()?),
info: c.info.as_deref().map(TryInto::try_into).transpose()?,
},
},
RumaMessageType::Audio(c) => MessageType::Audio {
content: AudioMessageContent {
filename: c.filename().to_owned(),
caption: c.caption().map(ToString::to_string),
formatted_caption: c.formatted_caption().map(Into::into),
source: Arc::new(c.source.clone()),
source: Arc::new(c.source.try_into()?),
info: c.info.as_deref().map(Into::into),
audio: c.audio.map(Into::into),
voice: c.voice.map(Into::into),
@@ -379,8 +439,8 @@ impl From<RumaMessageType> for MessageType {
filename: c.filename().to_owned(),
caption: c.caption().map(ToString::to_string),
formatted_caption: c.formatted_caption().map(Into::into),
source: Arc::new(c.source.clone()),
info: c.info.as_deref().map(Into::into),
source: Arc::new(c.source.try_into()?),
info: c.info.as_deref().map(TryInto::try_into).transpose()?,
},
},
RumaMessageType::File(c) => MessageType::File {
@@ -388,8 +448,8 @@ impl From<RumaMessageType> for MessageType {
filename: c.filename().to_owned(),
caption: c.caption().map(ToString::to_string),
formatted_caption: c.formatted_caption().map(Into::into),
source: Arc::new(c.source.clone()),
info: c.info.as_deref().map(Into::into),
source: Arc::new(c.source.try_into()?),
info: c.info.as_deref().map(TryInto::try_into).transpose()?,
},
},
RumaMessageType::Notice(c) => MessageType::Notice {
@@ -425,7 +485,7 @@ impl From<RumaMessageType> for MessageType {
msgtype: value.msgtype().to_owned(),
body: value.body().to_owned(),
},
}
})
}
}
@@ -510,6 +570,7 @@ pub struct ImageInfo {
pub thumbnail_info: Option<ThumbnailInfo>,
pub thumbnail_source: Option<Arc<MediaSource>>,
pub blurhash: Option<String>,
pub is_animated: Option<bool>,
}
impl From<ImageInfo> for RumaImageInfo {
@@ -520,8 +581,9 @@ impl From<ImageInfo> for RumaImageInfo {
mimetype: value.mimetype,
size: value.size.map(u64_to_uint),
thumbnail_info: value.thumbnail_info.map(Into::into).map(Box::new),
thumbnail_source: value.thumbnail_source.map(|source| (*source).clone()),
thumbnail_source: value.thumbnail_source.map(|source| (*source).clone().into()),
blurhash: value.blurhash,
is_animated: value.is_animated,
})
}
}
@@ -543,6 +605,7 @@ impl TryFrom<&ImageInfo> for BaseImageInfo {
width: Some(width),
size: Some(size),
blurhash: Some(blurhash),
is_animated: value.is_animated,
})
}
}
@@ -625,7 +688,7 @@ impl From<VideoInfo> for RumaVideoInfo {
mimetype: value.mimetype,
size: value.size.map(u64_to_uint),
thumbnail_info: value.thumbnail_info.map(Into::into).map(Box::new),
thumbnail_source: value.thumbnail_source.map(|source| (*source).clone()),
thumbnail_source: value.thumbnail_source.map(|source| (*source).clone().into()),
blurhash: value.blurhash,
})
}
@@ -668,7 +731,7 @@ impl From<FileInfo> for RumaFileInfo {
mimetype: value.mimetype,
size: value.size.map(u64_to_uint),
thumbnail_info: value.thumbnail_info.map(Into::into).map(Box::new),
thumbnail_source: value.thumbnail_source.map(|source| (*source).clone()),
thumbnail_source: value.thumbnail_source.map(|source| (*source).clone().into()),
})
}
}
@@ -703,21 +766,6 @@ impl From<ThumbnailInfo> for RumaThumbnailInfo {
}
}
impl TryFrom<&ThumbnailInfo> for BaseThumbnailInfo {
type Error = MediaInfoError;
fn try_from(value: &ThumbnailInfo) -> Result<Self, MediaInfoError> {
let height = UInt::try_from(value.height.ok_or(MediaInfoError::MissingField)?)
.map_err(|_| MediaInfoError::InvalidField)?;
let width = UInt::try_from(value.width.ok_or(MediaInfoError::MissingField)?)
.map_err(|_| MediaInfoError::InvalidField)?;
let size = UInt::try_from(value.size.ok_or(MediaInfoError::MissingField)?)
.map_err(|_| MediaInfoError::InvalidField)?;
Ok(BaseThumbnailInfo { height: Some(height), width: Some(width), size: Some(size) })
}
}
#[derive(Clone, uniffi::Record)]
pub struct NoticeMessageContent {
pub body: String,
@@ -790,8 +838,10 @@ pub enum MessageFormat {
Unknown { format: String },
}
impl From<&matrix_sdk::ruma::events::room::ImageInfo> for ImageInfo {
fn from(info: &matrix_sdk::ruma::events::room::ImageInfo) -> Self {
impl TryFrom<&matrix_sdk::ruma::events::room::ImageInfo> for ImageInfo {
type Error = ClientError;
fn try_from(info: &matrix_sdk::ruma::events::room::ImageInfo) -> Result<Self, Self::Error> {
let thumbnail_info = info.thumbnail_info.as_ref().map(|info| ThumbnailInfo {
height: info.height.map(Into::into),
width: info.width.map(Into::into),
@@ -799,15 +849,21 @@ impl From<&matrix_sdk::ruma::events::room::ImageInfo> for ImageInfo {
size: info.size.map(Into::into),
});
Self {
Ok(Self {
height: info.height.map(Into::into),
width: info.width.map(Into::into),
mimetype: info.mimetype.clone(),
size: info.size.map(Into::into),
thumbnail_info,
thumbnail_source: info.thumbnail_source.clone().map(Arc::new),
thumbnail_source: info
.thumbnail_source
.as_ref()
.map(TryInto::try_into)
.transpose()?
.map(Arc::new),
blurhash: info.blurhash.clone(),
}
is_animated: info.is_animated,
})
}
}
@@ -821,8 +877,10 @@ impl From<&RumaAudioInfo> for AudioInfo {
}
}
impl From<&RumaVideoInfo> for VideoInfo {
fn from(info: &RumaVideoInfo) -> Self {
impl TryFrom<&RumaVideoInfo> for VideoInfo {
type Error = ClientError;
fn try_from(info: &RumaVideoInfo) -> Result<Self, Self::Error> {
let thumbnail_info = info.thumbnail_info.as_ref().map(|info| ThumbnailInfo {
height: info.height.map(Into::into),
width: info.width.map(Into::into),
@@ -830,21 +888,28 @@ impl From<&RumaVideoInfo> for VideoInfo {
size: info.size.map(Into::into),
});
Self {
Ok(Self {
duration: info.duration,
height: info.height.map(Into::into),
width: info.width.map(Into::into),
mimetype: info.mimetype.clone(),
size: info.size.map(Into::into),
thumbnail_info,
thumbnail_source: info.thumbnail_source.clone().map(Arc::new),
thumbnail_source: info
.thumbnail_source
.as_ref()
.map(TryInto::try_into)
.transpose()?
.map(Arc::new),
blurhash: info.blurhash.clone(),
}
})
}
}
impl From<&RumaFileInfo> for FileInfo {
fn from(info: &RumaFileInfo) -> Self {
impl TryFrom<&RumaFileInfo> for FileInfo {
type Error = ClientError;
fn try_from(info: &RumaFileInfo) -> Result<Self, Self::Error> {
let thumbnail_info = info.thumbnail_info.as_ref().map(|info| ThumbnailInfo {
height: info.height.map(Into::into),
width: info.width.map(Into::into),
@@ -852,12 +917,17 @@ impl From<&RumaFileInfo> for FileInfo {
size: info.size.map(Into::into),
});
Self {
Ok(Self {
mimetype: info.mimetype.clone(),
size: info.size.map(Into::into),
thumbnail_info,
thumbnail_source: info.thumbnail_source.clone().map(Arc::new),
}
thumbnail_source: info
.thumbnail_source
.as_ref()
.map(TryInto::try_into)
.transpose()?
.map(Arc::new),
})
}
}
@@ -13,7 +13,7 @@ use ruma::UserId;
use tracing::{error, info};
use super::RUNTIME;
use crate::error::ClientError;
use crate::{error::ClientError, utils::Timestamp};
#[derive(uniffi::Object)]
pub struct SessionVerificationEmoji {
@@ -46,7 +46,7 @@ pub struct SessionVerificationRequestDetails {
device_id: String,
display_name: Option<String>,
/// First time this device was seen in milliseconds since epoch.
first_seen_timestamp: u64,
first_seen_timestamp: Timestamp,
}
#[matrix_sdk_ffi_macros::export(callback_interface)]
@@ -242,7 +242,7 @@ impl SessionVerificationController {
flow_id: request.flow_id().into(),
device_id: other_device_data.device_id().into(),
display_name: other_device_data.display_name().map(str::to_string),
first_seen_timestamp: other_device_data.first_time_seen_ts().get().into(),
first_seen_timestamp: other_device_data.first_time_seen_ts().into(),
});
}
}
+32 -3
View File
@@ -38,6 +38,7 @@ pub enum SyncServiceState {
Running,
Terminated,
Error,
Offline,
}
impl From<MatrixSyncServiceState> for SyncServiceState {
@@ -47,6 +48,7 @@ impl From<MatrixSyncServiceState> for SyncServiceState {
MatrixSyncServiceState::Running => Self::Running,
MatrixSyncServiceState::Terminated => Self::Terminated,
MatrixSyncServiceState::Error => Self::Error,
MatrixSyncServiceState::Offline => Self::Offline,
}
}
}
@@ -72,11 +74,11 @@ impl SyncService {
}
pub async fn start(&self) {
self.inner.start().await;
self.inner.start().await
}
pub async fn stop(&self) -> Result<(), ClientError> {
Ok(self.inner.stop().await?)
pub async fn stop(&self) {
self.inner.stop().await
}
pub fn state(&self, listener: Box<dyn SyncServiceStateObserver>) -> Arc<TaskHandle> {
@@ -118,6 +120,13 @@ impl SyncServiceBuilder {
Arc::new(Self { client: this.client, builder, utd_hook: this.utd_hook })
}
/// Enable the "offline" mode for the [`SyncService`].
pub fn with_offline_mode(self: Arc<Self>) -> Arc<Self> {
let this = unwrap_or_clone_arc(self);
let builder = this.builder.with_offline_mode();
Arc::new(Self { client: this.client, builder, utd_hook: this.utd_hook })
}
pub async fn with_utd_hook(
self: Arc<Self>,
delegate: Box<dyn UnableToDecryptDelegate>,
@@ -201,6 +210,22 @@ pub struct UnableToDecryptInfo {
/// What we know about what caused this UTD. E.g. was this event sent when
/// we were not a member of this room?
pub cause: UtdCause,
/// The difference between the event creation time (`origin_server_ts`) and
/// the time our device was created. If negative, this event was sent
/// *before* our device was created.
pub event_local_age_millis: i64,
/// Whether the user had verified their own identity at the point they
/// received the UTD event.
pub user_trusts_own_identity: bool,
/// The homeserver of the user that sent the undecryptable event.
pub sender_homeserver: String,
/// Our local user's own homeserver, or `None` if the client is not logged
/// in.
pub own_homeserver: Option<String>,
}
impl From<SdkUnableToDecryptInfo> for UnableToDecryptInfo {
@@ -209,6 +234,10 @@ impl From<SdkUnableToDecryptInfo> for UnableToDecryptInfo {
event_id: value.event_id.to_string(),
time_to_decrypt_ms: value.time_to_decrypt.map(|ttd| ttd.as_millis() as u64),
cause: value.cause,
event_local_age_millis: value.event_local_age_millis,
user_trusts_own_identity: value.user_trusts_own_identity,
sender_homeserver: value.sender_homeserver.to_string(),
own_homeserver: value.own_homeserver.map(String::from),
}
}
}
@@ -0,0 +1,85 @@
use ruma::EventId;
use super::FocusEventError;
use crate::{error::ClientError, event::RoomMessageEventMessageType};
#[derive(uniffi::Enum)]
pub enum TimelineFocus {
Live,
Event { event_id: String, num_context_events: u16 },
PinnedEvents { max_events_to_load: u16, max_concurrent_requests: u16 },
}
impl TryFrom<TimelineFocus> for matrix_sdk_ui::timeline::TimelineFocus {
type Error = ClientError;
fn try_from(
value: TimelineFocus,
) -> Result<matrix_sdk_ui::timeline::TimelineFocus, Self::Error> {
match value {
TimelineFocus::Live => Ok(Self::Live),
TimelineFocus::Event { event_id, num_context_events } => {
let parsed_event_id =
EventId::parse(&event_id).map_err(|err| FocusEventError::InvalidEventId {
event_id: event_id.clone(),
err: err.to_string(),
})?;
Ok(Self::Event { target: parsed_event_id, num_context_events })
}
TimelineFocus::PinnedEvents { max_events_to_load, max_concurrent_requests } => {
Ok(Self::PinnedEvents { max_events_to_load, max_concurrent_requests })
}
}
}
}
/// Changes how date dividers get inserted, either in between each day or in
/// between each month
#[derive(uniffi::Enum)]
pub enum DateDividerMode {
Daily,
Monthly,
}
impl From<DateDividerMode> for matrix_sdk_ui::timeline::DateDividerMode {
fn from(value: DateDividerMode) -> Self {
match value {
DateDividerMode::Daily => Self::Daily,
DateDividerMode::Monthly => Self::Monthly,
}
}
}
#[derive(uniffi::Enum)]
pub enum AllowedMessageTypes {
All,
Only { types: Vec<RoomMessageEventMessageType> },
}
/// Various options used to configure the timeline's behavior.
///
/// # Arguments
///
/// * `internal_id_prefix` -
///
/// * `allowed_message_types` -
///
/// * `date_divider_mode` -
#[derive(uniffi::Record)]
pub struct TimelineConfiguration {
/// What should the timeline focus on?
pub focus: TimelineFocus,
/// A list of [`RoomMessageEventMessageType`] that will be allowed to appear
/// in the timeline
pub allowed_message_types: AllowedMessageTypes,
/// 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.
pub internal_id_prefix: Option<String>,
/// How often to insert date dividers
pub date_divider_mode: DateDividerMode,
}
+47 -15
View File
@@ -16,26 +16,56 @@ use std::{collections::HashMap, sync::Arc};
use matrix_sdk::{crypto::types::events::UtdCause, room::power_levels::power_level_user_changes};
use matrix_sdk_ui::timeline::{PollResult, RoomPinnedEventsChange, TimelineDetails};
use ruma::events::{room::MediaSource, FullStateEventContent};
use ruma::events::{room::MediaSource as RumaMediaSource, EventContent, FullStateEventContent};
use super::ProfileDetails;
use crate::ruma::{ImageInfo, Mentions, MessageType, PollKind};
use crate::{
error::ClientError,
ruma::{ImageInfo, MediaSource, MediaSourceExt, Mentions, MessageType, PollKind},
utils::Timestamp,
};
impl From<matrix_sdk_ui::timeline::TimelineItemContent> for TimelineItemContent {
fn from(value: matrix_sdk_ui::timeline::TimelineItemContent) -> Self {
use matrix_sdk_ui::timeline::TimelineItemContent as Content;
match value {
Content::Message(message) => TimelineItemContent::Message { content: message.into() },
Content::Message(message) => {
let msgtype = message.msgtype().msgtype().to_owned();
match TryInto::<MessageContent>::try_into(message) {
Ok(message) => TimelineItemContent::Message { content: message },
Err(error) => TimelineItemContent::FailedToParseMessageLike {
event_type: msgtype,
error: error.to_string(),
},
}
}
Content::RedactedMessage => TimelineItemContent::RedactedMessage,
Content::Sticker(sticker) => {
let content = sticker.content();
TimelineItemContent::Sticker {
body: content.body.clone(),
info: (&content.info).into(),
source: Arc::new(MediaSource::from(content.source.clone())),
let media_source = RumaMediaSource::from(content.source.clone());
if let Err(error) = media_source.verify() {
return TimelineItemContent::FailedToParseMessageLike {
event_type: sticker.content().event_type().to_string(),
error: error.to_string(),
};
}
match TryInto::<ImageInfo>::try_into(&content.info) {
Ok(info) => TimelineItemContent::Sticker {
body: content.body.clone(),
info,
source: Arc::new(MediaSource { media_source }),
},
Err(error) => TimelineItemContent::FailedToParseMessageLike {
event_type: sticker.content().event_type().to_string(),
error: error.to_string(),
},
}
}
@@ -117,16 +147,18 @@ pub struct MessageContent {
pub mentions: Option<Mentions>,
}
impl From<matrix_sdk_ui::timeline::Message> for MessageContent {
fn from(value: matrix_sdk_ui::timeline::Message) -> Self {
Self {
msg_type: value.msgtype().clone().into(),
impl TryFrom<matrix_sdk_ui::timeline::Message> for MessageContent {
type Error = ClientError;
fn try_from(value: matrix_sdk_ui::timeline::Message) -> Result<Self, Self::Error> {
Ok(Self {
msg_type: value.msgtype().clone().try_into()?,
body: value.body().to_owned(),
in_reply_to: value.in_reply_to().map(|r| Arc::new(r.clone().into())),
is_edited: value.is_edited(),
thread_root: value.thread_root().map(|id| id.to_string()),
mentions: value.mentions().cloned().map(|m| m.into()),
}
})
}
}
@@ -156,7 +188,7 @@ pub enum TimelineItemContent {
max_selections: u64,
answers: Vec<PollAnswer>,
votes: HashMap<String, Vec<String>>,
end_time: Option<u64>,
end_time: Option<Timestamp>,
has_been_edited: bool,
},
CallInvite,
@@ -288,7 +320,7 @@ pub struct Reaction {
#[derive(Clone, uniffi::Record)]
pub struct ReactionSenderData {
pub sender_id: String,
pub timestamp: u64,
pub timestamp: Timestamp,
}
#[derive(Clone, uniffi::Enum)]
@@ -450,7 +482,7 @@ impl From<PollResult> for TimelineItemContent {
.map(|i| PollAnswer { id: i.id, text: i.text })
.collect(),
votes: value.votes,
end_time: value.end_time,
end_time: value.end_time.map(|t| t.into()),
has_been_edited: value.has_been_edited,
}
}
+194 -196
View File
@@ -19,12 +19,10 @@ use as_variant::as_variant;
use content::{InReplyToDetails, RepliedToEventDetails};
use eyeball_im::VectorDiff;
use futures_util::{pin_mut, StreamExt as _};
#[cfg(doc)]
use matrix_sdk::crypto::CollectStrategy;
use matrix_sdk::{
attachment::{
AttachmentConfig, AttachmentInfo, BaseAudioInfo, BaseFileInfo, BaseImageInfo,
BaseThumbnailInfo, BaseVideoInfo, Thumbnail,
BaseVideoInfo, Thumbnail,
},
deserialized_responses::{ShieldState as SdkShieldState, ShieldStateCode},
room::edit::EditedContent as SdkEditedContent,
@@ -53,7 +51,7 @@ use ruma::{
},
AnyMessageLikeEventContent,
},
EventId,
EventId, UInt,
};
use tokio::{
sync::Mutex,
@@ -63,21 +61,21 @@ use tracing::{error, warn};
use uuid::Uuid;
use self::content::{Reaction, ReactionSenderData, TimelineItemContent};
#[cfg(doc)]
use crate::client_builder::ClientBuilder;
use crate::{
client::ProgressWatcher,
error::{ClientError, RoomError},
event::EventOrTransactionId,
helpers::unwrap_or_clone_arc,
ruma::{
AssetType, AudioInfo, FileInfo, FormattedBody, ImageInfo, PollKind, ThumbnailInfo,
VideoInfo,
AssetType, AudioInfo, FileInfo, FormattedBody, ImageInfo, Mentions, PollKind,
ThumbnailInfo, VideoInfo,
},
task_handle::TaskHandle,
utils::Timestamp,
RUNTIME,
};
pub mod configuration;
mod content;
pub use content::MessageContent;
@@ -101,77 +99,120 @@ impl Timeline {
unsafe { Arc::from_raw(Arc::into_raw(inner) as _) }
}
async fn send_attachment(
&self,
filename: String,
fn send_attachment(
self: Arc<Self>,
params: UploadParameters,
attachment_info: AttachmentInfo,
mime_type: Option<String>,
attachment_config: AttachmentConfig,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Result<(), RoomError> {
thumbnail: Option<Thumbnail>,
) -> Result<Arc<SendAttachmentJoinHandle>, RoomError> {
let mime_str = mime_type.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
let mime_type =
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;
let mut request = self.inner.send_attachment(filename, mime_type, attachment_config);
let formatted_caption = formatted_body_from(
params.caption.as_deref(),
params.formatted_caption.map(Into::into),
);
if use_send_queue {
request = request.use_send_queue();
}
let attachment_config = AttachmentConfig::new()
.thumbnail(thumbnail)
.info(attachment_info)
.caption(params.caption)
.formatted_caption(formatted_caption)
.mentions(params.mentions.map(Into::into));
if let Some(progress_watcher) = progress_watcher {
let mut subscriber = request.subscribe_to_send_progress();
RUNTIME.spawn(async move {
while let Some(progress) = subscriber.next().await {
progress_watcher.transmission_progress(progress.into());
}
});
}
let handle = SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let mut request =
self.inner.send_attachment(params.filename, mime_type, attachment_config);
request.await.map_err(|_| RoomError::FailedSendingAttachment)?;
Ok(())
if params.use_send_queue {
request = request.use_send_queue();
}
if let Some(progress_watcher) = progress_watcher {
let mut subscriber = request.subscribe_to_send_progress();
RUNTIME.spawn(async move {
while let Some(progress) = subscriber.next().await {
progress_watcher.transmission_progress(progress.into());
}
});
}
request.await.map_err(|_| RoomError::FailedSendingAttachment)?;
Ok(())
}));
Ok(handle)
}
}
fn build_thumbnail_info(
thumbnail_url: Option<String>,
thumbnail_path: Option<String>,
thumbnail_info: Option<ThumbnailInfo>,
) -> Result<AttachmentConfig, RoomError> {
match (thumbnail_url, thumbnail_info) {
(None, None) => Ok(AttachmentConfig::new()),
) -> Result<Option<Thumbnail>, RoomError> {
match (thumbnail_path, thumbnail_info) {
(None, None) => Ok(None),
(Some(thumbnail_url), Some(thumbnail_info)) => {
(Some(thumbnail_path), Some(thumbnail_info)) => {
let thumbnail_data =
fs::read(thumbnail_url).map_err(|_| RoomError::InvalidThumbnailData)?;
fs::read(thumbnail_path).map_err(|_| RoomError::InvalidThumbnailData)?;
let base_thumbnail_info = BaseThumbnailInfo::try_from(&thumbnail_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
let height = thumbnail_info
.height
.and_then(|u| UInt::try_from(u).ok())
.ok_or(RoomError::InvalidAttachmentData)?;
let width = thumbnail_info
.width
.and_then(|u| UInt::try_from(u).ok())
.ok_or(RoomError::InvalidAttachmentData)?;
let size = thumbnail_info
.size
.and_then(|u| UInt::try_from(u).ok())
.ok_or(RoomError::InvalidAttachmentData)?;
let mime_str =
thumbnail_info.mimetype.as_ref().ok_or(RoomError::InvalidAttachmentMimeType)?;
let mime_type =
mime_str.parse::<Mime>().map_err(|_| RoomError::InvalidAttachmentMimeType)?;
let thumbnail = Thumbnail {
Ok(Some(Thumbnail {
data: thumbnail_data,
content_type: mime_type,
info: Some(base_thumbnail_info),
};
Ok(AttachmentConfig::with_thumbnail(thumbnail))
height,
width,
size,
}))
}
_ => {
warn!("Ignoring thumbnail because either the thumbnail URL or info isn't defined");
Ok(AttachmentConfig::new())
warn!("Ignoring thumbnail because either the thumbnail path or info isn't defined");
Ok(None)
}
}
}
#[derive(uniffi::Record)]
pub struct UploadParameters {
/// Filename (previously called "url") for the media to be sent.
filename: String,
/// Optional non-formatted caption, for clients that support it.
caption: Option<String>,
/// Optional HTML-formatted caption, for clients that support it.
formatted_caption: Option<FormattedBody>,
// Optional intentional mentions to be sent with the media.
mentions: Option<Mentions>,
/// Should the media be sent with the send queue, or synchronously?
///
/// Watching progress only works with the synchronous method, at the moment.
use_send_queue: bool,
}
#[matrix_sdk_ffi_macros::export]
impl Timeline {
pub async fn add_listener(&self, listener: Box<dyn TimelineListener>) -> Arc<TaskHandle> {
let (timeline_items, timeline_stream) = self.inner.subscribe_batched().await;
let (timeline_items, timeline_stream) = self.inner.subscribe().await;
Arc::new(TaskHandle::new(RUNTIME.spawn(async move {
pin_mut!(timeline_stream);
@@ -226,16 +267,16 @@ impl Timeline {
/// Paginate backwards, whether we are in focused mode or in live mode.
///
/// Returns whether we hit the end of the timeline or not.
/// Returns whether we hit the start of the timeline or not.
pub async fn paginate_backwards(&self, num_events: u16) -> Result<bool, ClientError> {
Ok(self.inner.paginate_backwards(num_events).await?)
}
/// Paginate forwards, when in focused mode.
/// Paginate forwards, whether we are in focused mode or in live mode.
///
/// Returns whether we hit the end of the timeline or not.
pub async fn focused_paginate_forwards(&self, num_events: u16) -> Result<bool, ClientError> {
Ok(self.inner.focused_paginate_forwards(num_events).await?)
pub async fn paginate_forwards(&self, num_events: u16) -> Result<bool, ClientError> {
Ok(self.inner.paginate_forwards(num_events).await?)
}
pub async fn send_read_receipt(
@@ -279,171 +320,83 @@ impl Timeline {
}
}
#[allow(clippy::too_many_arguments)]
pub fn send_image(
self: Arc<Self>,
url: String,
thumbnail_url: Option<String>,
params: UploadParameters,
thumbnail_path: Option<String>,
image_info: ImageInfo,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_image_info = BaseImageInfo::try_from(&image_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
let attachment_info = AttachmentInfo::Image(base_image_info);
let attachment_config = build_thumbnail_info(thumbnail_url, image_info.thumbnail_info)?
.info(attachment_info)
.caption(caption)
.formatted_caption(formatted_caption);
self.send_attachment(
url,
image_info.mimetype,
attachment_config,
progress_watcher,
use_send_queue,
)
.await
}))
) -> Result<Arc<SendAttachmentJoinHandle>, RoomError> {
let attachment_info = AttachmentInfo::Image(
BaseImageInfo::try_from(&image_info).map_err(|_| RoomError::InvalidAttachmentData)?,
);
let thumbnail = build_thumbnail_info(thumbnail_path, image_info.thumbnail_info)?;
self.send_attachment(
params,
attachment_info,
image_info.mimetype,
progress_watcher,
thumbnail,
)
}
#[allow(clippy::too_many_arguments)]
pub fn send_video(
self: Arc<Self>,
url: String,
thumbnail_url: Option<String>,
params: UploadParameters,
thumbnail_path: Option<String>,
video_info: VideoInfo,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_video_info: BaseVideoInfo = BaseVideoInfo::try_from(&video_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
let attachment_info = AttachmentInfo::Video(base_video_info);
let attachment_config = build_thumbnail_info(thumbnail_url, video_info.thumbnail_info)?
.info(attachment_info)
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));
self.send_attachment(
url,
video_info.mimetype,
attachment_config,
progress_watcher,
use_send_queue,
)
.await
}))
) -> Result<Arc<SendAttachmentJoinHandle>, RoomError> {
let attachment_info = AttachmentInfo::Video(
BaseVideoInfo::try_from(&video_info).map_err(|_| RoomError::InvalidAttachmentData)?,
);
let thumbnail = build_thumbnail_info(thumbnail_path, video_info.thumbnail_info)?;
self.send_attachment(
params,
attachment_info,
video_info.mimetype,
progress_watcher,
thumbnail,
)
}
pub fn send_audio(
self: Arc<Self>,
url: String,
params: UploadParameters,
audio_info: AudioInfo,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
let attachment_info = AttachmentInfo::Audio(base_audio_info);
let attachment_config = AttachmentConfig::new()
.info(attachment_info)
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));
self.send_attachment(
url,
audio_info.mimetype,
attachment_config,
progress_watcher,
use_send_queue,
)
.await
}))
) -> Result<Arc<SendAttachmentJoinHandle>, RoomError> {
let attachment_info = AttachmentInfo::Audio(
BaseAudioInfo::try_from(&audio_info).map_err(|_| RoomError::InvalidAttachmentData)?,
);
self.send_attachment(params, attachment_info, audio_info.mimetype, progress_watcher, None)
}
#[allow(clippy::too_many_arguments)]
pub fn send_voice_message(
self: Arc<Self>,
url: String,
params: UploadParameters,
audio_info: AudioInfo,
waveform: Vec<u16>,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_audio_info: BaseAudioInfo = BaseAudioInfo::try_from(&audio_info)
.map_err(|_| RoomError::InvalidAttachmentData)?;
let attachment_info =
AttachmentInfo::Voice { audio_info: base_audio_info, waveform: Some(waveform) };
let attachment_config = AttachmentConfig::new()
.info(attachment_info)
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));
self.send_attachment(
url,
audio_info.mimetype,
attachment_config,
progress_watcher,
use_send_queue,
)
.await
}))
) -> Result<Arc<SendAttachmentJoinHandle>, RoomError> {
let attachment_info = AttachmentInfo::Voice {
audio_info: BaseAudioInfo::try_from(&audio_info)
.map_err(|_| RoomError::InvalidAttachmentData)?,
waveform: Some(waveform),
};
self.send_attachment(params, attachment_info, audio_info.mimetype, progress_watcher, None)
}
pub fn send_file(
self: Arc<Self>,
url: String,
params: UploadParameters,
file_info: FileInfo,
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
progress_watcher: Option<Box<dyn ProgressWatcher>>,
use_send_queue: bool,
) -> Arc<SendAttachmentJoinHandle> {
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
SendAttachmentJoinHandle::new(RUNTIME.spawn(async move {
let base_file_info: BaseFileInfo =
BaseFileInfo::try_from(&file_info).map_err(|_| RoomError::InvalidAttachmentData)?;
let attachment_info = AttachmentInfo::File(base_file_info);
let attachment_config = AttachmentConfig::new()
.info(attachment_info)
.caption(caption)
.formatted_caption(formatted_caption.map(Into::into));
self.send_attachment(
url,
file_info.mimetype,
attachment_config,
progress_watcher,
use_send_queue,
)
.await
}))
) -> Result<Arc<SendAttachmentJoinHandle>, RoomError> {
let attachment_info = AttachmentInfo::File(
BaseFileInfo::try_from(&file_info).map_err(|_| RoomError::InvalidAttachmentData)?,
);
self.send_attachment(params, attachment_info, file_info.mimetype, progress_watcher, None)
}
pub async fn create_poll(
@@ -545,6 +498,7 @@ impl Timeline {
.await
{
Ok(()) => Ok(()),
Err(timeline::Error::EventNotInTimeline(_)) => {
// If we couldn't edit, assume it was an (remote) event that wasn't in the
// timeline, and try to edit it via the room itself.
@@ -560,7 +514,8 @@ impl Timeline {
room.send_queue().send(edit_event).await?;
Ok(())
}
Err(err) => Err(err)?,
Err(err) => Err(err.into()),
}
}
@@ -977,7 +932,7 @@ impl TimelineItem {
pub fn as_virtual(self: Arc<Self>) -> Option<VirtualTimelineItem> {
use matrix_sdk_ui::timeline::VirtualTimelineItem as VItem;
match self.0.as_virtual()? {
VItem::DayDivider(ts) => Some(VirtualTimelineItem::DayDivider { ts: ts.0.into() }),
VItem::DateDivider(ts) => Some(VirtualTimelineItem::DateDivider { ts: (*ts).into() }),
VItem::ReadMarker => Some(VirtualTimelineItem::ReadMarker),
}
}
@@ -1072,9 +1027,10 @@ pub struct EventTimelineItem {
is_own: bool,
is_editable: bool,
content: TimelineItemContent,
timestamp: u64,
timestamp: Timestamp,
reactions: Vec<Reaction>,
local_send_state: Option<EventSendState>,
local_created_at: Option<u64>,
read_receipts: HashMap<String, Receipt>,
origin: Option<EventItemOrigin>,
can_be_replied_to: bool,
@@ -1092,7 +1048,7 @@ impl From<matrix_sdk_ui::timeline::EventTimelineItem> for EventTimelineItem {
.into_iter()
.map(|(sender_id, info)| ReactionSenderData {
sender_id: sender_id.to_string(),
timestamp: info.timestamp.0.into(),
timestamp: info.timestamp.into(),
})
.collect(),
})
@@ -1109,9 +1065,10 @@ impl From<matrix_sdk_ui::timeline::EventTimelineItem> for EventTimelineItem {
is_own: item.is_own(),
is_editable: item.is_editable(),
content: item.content().clone().into(),
timestamp: item.timestamp().0.into(),
timestamp: item.timestamp().into(),
reactions,
local_send_state: item.send_state().map(|s| s.into()),
local_created_at: item.local_created_at().map(|t| t.0.into()),
read_receipts,
origin: item.origin(),
can_be_replied_to: item.can_be_replied_to(),
@@ -1122,12 +1079,12 @@ impl From<matrix_sdk_ui::timeline::EventTimelineItem> for EventTimelineItem {
#[derive(Clone, uniffi::Record)]
pub struct Receipt {
pub timestamp: Option<u64>,
pub timestamp: Option<Timestamp>,
}
impl From<ruma::events::receipt::Receipt> for Receipt {
fn from(value: ruma::events::receipt::Receipt) -> Self {
Receipt { timestamp: value.ts.map(|ts| ts.0.into()) }
Receipt { timestamp: value.ts.map(|ts| ts.into()) }
}
}
@@ -1246,11 +1203,12 @@ impl SendAttachmentJoinHandle {
/// A [`TimelineItem`](super::TimelineItem) that doesn't correspond to an event.
#[derive(uniffi::Enum)]
pub enum VirtualTimelineItem {
/// A divider between messages of two days.
DayDivider {
/// A divider between messages of different day or month depending on
/// timeline settings.
DateDivider {
/// A timestamp in milliseconds since Unix Epoch on that day in local
/// time.
ts: u64,
ts: Timestamp,
},
/// The user's own read marker.
@@ -1277,17 +1235,34 @@ impl From<ReceiptType> for ruma::api::client::receipt::create_receipt::v3::Recei
#[derive(Clone, uniffi::Enum)]
pub enum EditedContent {
RoomMessage { content: Arc<RoomMessageEventContentWithoutRelation> },
PollStart { poll_data: PollData },
RoomMessage {
content: Arc<RoomMessageEventContentWithoutRelation>,
},
MediaCaption {
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
mentions: Option<Mentions>,
},
PollStart {
poll_data: PollData,
},
}
impl TryFrom<EditedContent> for SdkEditedContent {
type Error = ClientError;
fn try_from(value: EditedContent) -> Result<Self, Self::Error> {
match value {
EditedContent::RoomMessage { content } => {
Ok(SdkEditedContent::RoomMessage((*content).clone()))
}
EditedContent::MediaCaption { caption, formatted_caption, mentions } => {
Ok(SdkEditedContent::MediaCaption {
caption,
formatted_caption: formatted_caption.map(Into::into),
mentions: mentions.map(Into::into),
})
}
EditedContent::PollStart { poll_data } => {
let block: UnstablePollStartContentBlock = poll_data.clone().try_into()?;
Ok(SdkEditedContent::PollStart {
@@ -1299,6 +1274,25 @@ impl TryFrom<EditedContent> for SdkEditedContent {
}
}
/// Create a caption edit.
///
/// If no `formatted_caption` is provided, then it's assumed the `caption`
/// represents valid Markdown that can be used as the formatted caption.
#[matrix_sdk_ffi_macros::export]
fn create_caption_edit(
caption: Option<String>,
formatted_caption: Option<FormattedBody>,
mentions: Option<Mentions>,
) -> EditedContent {
let formatted_caption =
formatted_body_from(caption.as_deref(), formatted_caption.map(Into::into));
EditedContent::MediaCaption {
caption,
formatted_caption: formatted_caption.as_ref().map(Into::into),
mentions,
}
}
/// Wrapper to retrieve some timeline item info lazily.
#[derive(Clone, uniffi::Object)]
pub struct LazyTimelineItemProvider(Arc<matrix_sdk_ui::timeline::EventTimelineItem>);
@@ -1324,4 +1318,8 @@ impl LazyTimelineItemProvider {
fn get_send_handle(&self) -> Option<Arc<SendHandle>> {
self.0.local_echo_send_handle().map(|handle| Arc::new(SendHandle::new(handle)))
}
fn contains_only_emojis(&self) -> bool {
self.0.contains_only_emojis()
}
}
+10
View File
@@ -185,6 +185,16 @@ impl LogLevel {
LogLevel::Trace => tracing::Level::TRACE,
}
}
pub(crate) fn as_str(&self) -> &'static str {
match self {
LogLevel::Error => "error",
LogLevel::Warn => "warn",
LogLevel::Info => "info",
LogLevel::Debug => "debug",
LogLevel::Trace => "trace",
}
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord)]
+12 -1
View File
@@ -15,9 +15,20 @@
use std::{mem::ManuallyDrop, ops::Deref};
use async_compat::TOKIO1 as RUNTIME;
use ruma::UInt;
use ruma::{MilliSecondsSinceUnixEpoch, UInt};
use tracing::warn;
#[derive(Debug, Clone)]
pub struct Timestamp(u64);
impl From<MilliSecondsSinceUnixEpoch> for Timestamp {
fn from(date: MilliSecondsSinceUnixEpoch) -> Self {
Self(date.0.into())
}
}
uniffi::custom_newtype!(Timestamp, u64);
pub(crate) fn u64_to_uint(u: u64) -> UInt {
UInt::new(u).unwrap_or_else(|| {
warn!("u64 -> UInt conversion overflowed, falling back to UInt::MAX");
-47
View File
@@ -1,47 +0,0 @@
# This git-cliff configuration file is used to generate weekly reports for This
# Week in Matrix amongst others.
[changelog]
header = """
# This Week in the Matrix Rust SDK ({{ now() | date(format="%Y-%m-%d") }})
"""
body = """
{% for commit in commits %}
{% set_global commit_message = commit.message -%}
{% for footer in commit.footers -%}
{% if footer.token | lower == "changelog" -%}
{% set_global commit_message = footer.value -%}
{% elif footer.token | lower == "breaking-change" -%}
{% set_global commit_message = footer.value -%}
{% endif -%}
{% endfor -%}
- {{ commit_message | upper_first }}
{% endfor %}
"""
trim = true
footer = ""
[git]
conventional_commits = true
filter_unconventional = true
commit_preprocessors = [
{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](https://github.com/matrix-org/matrix-rust-sdk/pull/${2}))"},
]
commit_parsers = [
{ message = "^feat", group = "Features" },
{ message = "^fix", group = "Bug Fixes" },
{ message = "^doc", group = "Documentation" },
{ message = "^perf", group = "Performance" },
{ message = "^refactor", group = "Refactor", skip = true },
{ message = "^chore\\(release\\): prepare for", skip = true },
{ message = "^chore", skip = true },
{ message = "^style", group = "Styling", skip = true },
{ message = "^test", skip = true },
{ message = "^ci", skip = true },
]
filter_commits = true
tag_pattern = "[0-9]*"
skip_tags = ""
ignore_tags = ""
date_order = false
sort_commits = "newest"
-91
View File
@@ -1,91 +0,0 @@
# This git-cliff configuration file is used to generate release reports.
[changelog]
# changelog header
header = """
# Changelog\n
All notable changes to this project will be documented in this file.\n
"""
# template for the changelog body
# https://keats.github.io/tera/docs/
body = """
{% if version %}\
## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }}
{% else %}\
## [unreleased]
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
### {{ group | upper_first }}
{% for commit in commits %}
{% set_global commit_message = commit.message -%}
{% set_global breaking = commit.breaking -%}
{% for footer in commit.footers -%}
{% if footer.token | lower == "changelog" -%}
{% set_global commit_message = footer.value -%}
{% elif footer.token | lower == "breaking-change" -%}
{% set_global commit_message = footer.value -%}
{% elif footer.token | lower == "security-impact" -%}
{% set_global security_impact = footer.value -%}
{% elif footer.token | lower == "cve" -%}
{% set_global cve = footer.value -%}
{% elif footer.token | lower == "github-advisory" -%}
{% set_global github_advisory = footer.value -%}
{% endif -%}
{% endfor -%}
- {% if breaking %}[**breaking**] {% endif %}{{ commit_message | upper_first }}
{% if security_impact -%}
(\
*{{ security_impact | upper_first }}*\
{% if cve -%}, [{{ cve | upper }}](https://www.cve.org/CVERecord?id={{ cve }}){% endif -%}\
{% if github_advisory -%}, [{{ github_advisory | upper }}](https://github.com/matrix-org/matrix-rust-sdk/security/advisories/{{ github_advisory }}){% endif -%}
)
{% endif -%}
{% endfor %}
{% endfor %}\n
"""
# remove the leading and trailing whitespace from the template
trim = true
# changelog footer
footer = """
<!-- generated by git-cliff -->
"""
[git]
# parse the commits based on https://www.conventionalcommits.org
conventional_commits = true
# filter out the commits that are not conventional
filter_unconventional = true
# regex for preprocessing the commit messages
commit_preprocessors = [
{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](https://github.com/matrix-org/matrix-rust-sdk/pull/${2}))"},
]
# regex for parsing and grouping commits
commit_parsers = [
{ footer = "Security-Impact:", group = "Security" },
{ footer = "CVE:", group = "Security" },
{ footer = "GitHub-Advisory:", group = "Security" },
{ message = "^feat", group = "Features" },
{ message = "^fix", group = "Bug Fixes" },
{ message = "^doc", group = "Documentation" },
{ message = "^perf", group = "Performance" },
{ message = "^refactor", group = "Refactor" },
{ message = "^chore\\(release\\): prepare for", skip = true },
{ message = "^chore", skip = true },
{ message = "^style", group = "Styling", skip = true },
{ message = "^test", skip = true },
{ message = "^ci", skip = true },
]
# forbid parsers from skipping breaking changes
protect_breaking_commits = true
# filter out the commits that are not matched by commit parsers
filter_commits = true
# glob pattern for matching git tags
tag_pattern = "[0-9]*"
# regex for skipping tags
skip_tags = ""
# regex for ignoring tags
ignore_tags = ""
# sort the tags chronologically
date_order = false
# sort the commits inside sections by oldest/newest order
sort_commits = "oldest"
+53
View File
@@ -2,6 +2,59 @@
All notable changes to this project will be documented in this file.
<!-- next-header -->
## [Unreleased] - ReleaseDate
## [0.10.0] - 2025-02-04
### Features
- [**breaking**] `EventCacheStore` allows to control which media content is
allowed in the media cache, and how long it should be kept, with a
`MediaRetentionPolicy`:
- `EventCacheStore::add_media_content()` has an extra argument,
`ignore_policy`, which decides whether a media content should ignore the
`MediaRetentionPolicy`. It should be stored alongside the media content.
- `EventCacheStore` has four new methods: `media_retention_policy()`,
`set_media_retention_policy()`, `set_ignore_media_retention_policy()` and
`clean_up_media_cache()`.
- `EventCacheStore` implementations should delegate media cache methods to the
methods of the same name of `MediaService` to use the `MediaRetentionPolicy`.
They need to implement the `EventCacheStoreMedia` trait that can be tested
with the `event_cache_store_media_integration_tests!` macro.
([#4571](https://github.com/matrix-org/matrix-rust-sdk/pull/4571))
### Refactor
- [**breaking**] Replaced `Room::compute_display_name` with the reintroduced
`Room::display_name()`. The new method computes a display name, or return a
cached value from the previous successful computation. If you need a sync
variant, consider using `Room::cached_display_name()`.
([#4470](https://github.com/matrix-org/matrix-rust-sdk/pull/4470))
- [**breaking**]: The reexported types `SyncTimelineEvent` and `TimelineEvent`
have been fused into a single type `TimelineEvent`, and its field
`push_actions` has been made `Option`al (it is set to `None` when we couldn't
compute the push actions, because we lacked some information).
([#4568](https://github.com/matrix-org/matrix-rust-sdk/pull/4568))
## [0.9.0] - 2024-12-18
### Features
- Introduced support for
[MSC4171](https://github.com/matrix-org/matrix-rust-sdk/pull/4335), enabling
the designation of certain users as service members. These flagged users are
excluded from the room display name calculation.
([#4335](https://github.com/matrix-org/matrix-rust-sdk/pull/4335))
### Bug Fixes
- Fix an off-by-one error in the `ObservableMap` when the `remove()` method is
called. Previously, items following the removed item were not shifted left by
one position, leaving them at incorrect indices.
([#4346](https://github.com/matrix-org/matrix-rust-sdk/pull/4346))
## [0.8.0] - 2024-11-19
### Bug Fixes
+19 -13
View File
@@ -9,7 +9,7 @@ name = "matrix-sdk-base"
readme = "README.md"
repository = "https://github.com/matrix-org/matrix-rust-sdk"
rust-version = { workspace = true }
version = "0.8.0"
version = "0.10.0"
[package.metadata.docs.rs]
all-features = true
@@ -21,16 +21,15 @@ e2e-encryption = ["dep:matrix-sdk-crypto"]
js = ["matrix-sdk-common/js", "matrix-sdk-crypto?/js", "ruma/js", "matrix-sdk-store-encryption/js"]
qrcode = ["matrix-sdk-crypto?/qrcode"]
automatic-room-key-forwarding = ["matrix-sdk-crypto?/automatic-room-key-forwarding"]
experimental-sliding-sync = [
"ruma/unstable-msc3575",
"ruma/unstable-msc4186",
]
uniffi = ["dep:uniffi", "matrix-sdk-crypto?/uniffi", "matrix-sdk-common/uniffi"]
# Private feature, see
# https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823 for the gory
# details.
test-send-sync = []
test-send-sync = [
"matrix-sdk-common/test-send-sync",
"matrix-sdk-crypto?/test-send-sync",
]
# "message-ids" feature doesn't do anything and is deprecated.
message-ids = []
@@ -49,9 +48,9 @@ as_variant = { workspace = true }
assert_matches = { workspace = true, optional = true }
assert_matches2 = { workspace = true, optional = true }
async-trait = { workspace = true }
bitflags = { version = "2.4.0", features = ["serde"] }
decancer = "3.2.4"
eyeball = { workspace = true }
bitflags = { version = "2.8.0", features = ["serde"] }
decancer = "3.2.8"
eyeball = { workspace = true, features = ["async-lock"] }
eyeball-im = { workspace = true }
futures-util = { workspace = true }
growable-bloom-filter = { workspace = true }
@@ -61,9 +60,16 @@ matrix-sdk-crypto = { workspace = true, optional = true }
matrix-sdk-store-encryption = { workspace = true }
matrix-sdk-test = { workspace = true, optional = true }
once_cell = { workspace = true }
regex = "1.11.0"
ruma = { workspace = true, features = ["canonical-json", "unstable-msc3381", "unstable-msc2867", "rand"] }
unicode-normalization = "0.1.24"
regex = "1.11.1"
ruma = { workspace = true, features = [
"canonical-json",
"unstable-msc2867",
"unstable-msc3381",
"unstable-msc3575",
"unstable-msc4186",
"rand",
] }
unicode-normalization = { workspace = true }
serde = { workspace = true, features = ["rc"] }
serde_json = { workspace = true }
tokio = { workspace = true }
@@ -85,7 +91,7 @@ similar-asserts = { workspace = true }
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.33"
wasm-bindgen-test = { workspace = true }
[lints]
workspace = true
+81 -76
View File
@@ -26,8 +26,8 @@ use eyeball_im::{Vector, VectorDiff};
use futures_util::Stream;
#[cfg(feature = "e2e-encryption")]
use matrix_sdk_crypto::{
store::DynCryptoStore, CollectStrategy, DecryptionSettings, EncryptionSettings,
EncryptionSyncChanges, OlmError, OlmMachine, RoomEventDecryptionResult, ToDeviceRequest,
store::DynCryptoStore, types::requests::ToDeviceRequest, CollectStrategy, DecryptionSettings,
EncryptionSettings, EncryptionSyncChanges, OlmError, OlmMachine, RoomEventDecryptionResult,
TrustRequirement,
};
#[cfg(feature = "e2e-encryption")]
@@ -63,17 +63,17 @@ use tokio::sync::{broadcast, Mutex};
use tokio::sync::{RwLock, RwLockReadGuard};
use tracing::{debug, error, info, instrument, trace, warn};
#[cfg(all(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
#[cfg(feature = "e2e-encryption")]
use crate::latest_event::{is_suitable_for_latest_event, LatestEvent, PossibleLatestEvent};
#[cfg(feature = "e2e-encryption")]
use crate::RoomMemberships;
use crate::{
deserialized_responses::{DisplayName, RawAnySyncOrStrippedTimelineEvent, SyncTimelineEvent},
deserialized_responses::{DisplayName, RawAnySyncOrStrippedTimelineEvent, TimelineEvent},
error::{Error, Result},
event_cache::store::EventCacheStoreLock,
response_processors::AccountDataProcessor,
rooms::{
normal::{RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons},
normal::{RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons, RoomMembersUpdate},
Room, RoomInfo, RoomState,
},
store::{
@@ -129,7 +129,7 @@ pub struct BaseClient {
#[cfg(not(tarpaulin_include))]
impl fmt::Debug for BaseClient {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Client")
f.debug_struct("BaseClient")
.field("session_meta", &self.store.session_meta())
.field("sync_token", &self.store.sync_token)
.finish_non_exhaustive()
@@ -347,9 +347,9 @@ impl BaseClient {
Ok(())
}
/// Attempt to decrypt the given raw event into a `SyncTimelineEvent`.
/// Attempt to decrypt the given raw event into a [`TimelineEvent`].
///
/// In the case of a decryption error, returns a `SyncTimelineEvent`
/// In the case of a decryption error, returns a [`TimelineEvent`]
/// representing the decryption error; in the case of problems with our
/// application, returns `Err`.
///
@@ -359,7 +359,7 @@ impl BaseClient {
&self,
event: &Raw<AnySyncTimelineEvent>,
room_id: &RoomId,
) -> Result<Option<SyncTimelineEvent>> {
) -> Result<Option<TimelineEvent>> {
let olm = self.olm_machine().await;
let Some(olm) = olm.as_ref() else { return Ok(None) };
@@ -372,7 +372,7 @@ impl BaseClient {
.await?
{
RoomEventDecryptionResult::Decrypted(decrypted) => {
let event: SyncTimelineEvent = decrypted.into();
let event: TimelineEvent = decrypted.into();
if let Ok(AnySyncTimelineEvent::MessageLike(e)) = event.raw().deserialize() {
match &e {
@@ -394,7 +394,7 @@ impl BaseClient {
event
}
RoomEventDecryptionResult::UnableToDecrypt(utd_info) => {
SyncTimelineEvent::new_utd_event(event.clone(), utd_info)
TimelineEvent::new_utd_event(event.clone(), utd_info)
}
};
@@ -423,7 +423,7 @@ impl BaseClient {
for raw_event in events {
// Start by assuming we have a plaintext event. We'll replace it with a
// decrypted or UTD event below if necessary.
let mut event = SyncTimelineEvent::new(raw_event);
let mut event = TimelineEvent::new(raw_event);
match event.raw().deserialize() {
Ok(e) => {
@@ -535,7 +535,7 @@ impl BaseClient {
},
);
}
event.push_actions = actions.to_owned();
event.push_actions = Some(actions.to_owned());
}
}
Err(e) => {
@@ -766,16 +766,12 @@ impl BaseClient {
let (events, room_key_updates) =
o.receive_sync_changes(encryption_sync_changes).await?;
#[cfg(feature = "experimental-sliding-sync")]
for room_key_update in room_key_updates {
if let Some(room) = self.get_room(&room_key_update.room_id) {
self.decrypt_latest_events(&room, changes, room_info_notable_updates).await;
}
}
#[cfg(not(feature = "experimental-sliding-sync"))] // Silence unused variable warnings.
let _ = (room_key_updates, changes, room_info_notable_updates);
Ok(events)
} else {
// If we have no OlmMachine, just return the events that were passed in.
@@ -789,7 +785,7 @@ impl BaseClient {
/// that we can and if we can, change latest_event to reflect what we
/// found, and remove any older encrypted events from
/// latest_encrypted_events.
#[cfg(all(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
#[cfg(feature = "e2e-encryption")]
async fn decrypt_latest_events(
&self,
room: &Room,
@@ -810,7 +806,7 @@ impl BaseClient {
/// (i.e. we can usefully display it as a message preview). Returns the
/// decrypted event if we found one, along with its index in the
/// latest_encrypted_events list, or None if we didn't find one.
#[cfg(all(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
#[cfg(feature = "e2e-encryption")]
async fn decrypt_latest_suitable_event(
&self,
room: &Room,
@@ -983,6 +979,9 @@ impl BaseClient {
let mut new_rooms = RoomUpdates::default();
let mut notifications = Default::default();
let mut updated_members_in_room: BTreeMap<OwnedRoomId, BTreeSet<OwnedUserId>> =
BTreeMap::new();
for (room_id, new_info) in response.rooms.join {
let room = self.store.get_or_create_room(
&room_id,
@@ -1011,6 +1010,8 @@ impl BaseClient {
)
.await?;
updated_members_in_room.insert(room_id.to_owned(), user_ids.clone());
for raw in &new_info.ephemeral.events {
match raw.deserialize() {
Ok(AnySyncEphemeralRoomEvent::Receipt(event)) => {
@@ -1252,6 +1253,13 @@ impl BaseClient {
// above. Oh well.
new_rooms.update_in_memory_caches(&self.store).await;
for (room_id, member_ids) in updated_members_in_room {
if let Some(room) = self.get_room(&room_id) {
let _ =
room.room_member_updates_sender.send(RoomMembersUpdate::Partial(member_ids));
}
}
info!("Processed a sync response in {:?}", now.elapsed());
let response = SyncResponse {
@@ -1401,6 +1409,8 @@ impl BaseClient {
self.store.save_changes(&changes).await?;
self.apply_changes(&changes, Default::default());
let _ = room.room_member_updates_sender.send(RoomMembersUpdate::FullReload);
Ok(())
}
@@ -1459,10 +1469,14 @@ impl BaseClient {
pub async fn share_room_key(&self, room_id: &RoomId) -> Result<Vec<Arc<ToDeviceRequest>>> {
match self.olm_machine().await.as_ref() {
Some(o) => {
let (history_visibility, settings) = self
.get_room(room_id)
.map(|r| (r.history_visibility(), r.encryption_settings()))
.unwrap_or((HistoryVisibility::Joined, None));
let Some(room) = self.get_room(room_id) else {
return Err(Error::InsufficientData);
};
let history_visibility = room.history_visibility_or_default();
let Some(room_encryption_event) = room.encryption_settings() else {
return Err(Error::EncryptionNotEnabled);
};
// Don't share the group session with members that are invited
// if the history visibility is set to `Joined`
@@ -1474,9 +1488,8 @@ impl BaseClient {
let members = self.store.get_user_ids(room_id, filter).await?;
let settings = settings.ok_or(Error::EncryptionNotEnabled)?;
let settings = EncryptionSettings::new(
settings,
room_encryption_event,
history_visibility,
self.room_key_recipient_strategy.clone(),
);
@@ -1503,8 +1516,14 @@ impl BaseClient {
/// # Arguments
///
/// * `room_id` - The id of the room that should be forgotten.
pub async fn forget_room(&self, room_id: &RoomId) -> StoreResult<()> {
self.store.forget_room(room_id).await
pub async fn forget_room(&self, room_id: &RoomId) -> Result<()> {
// Forget the room in the state store.
self.store.forget_room(room_id).await?;
// Remove the room in the event cache store too.
self.event_cache_store().lock().await?.remove_room(room_id).await?;
Ok(())
}
/// Get the olm machine.
@@ -1727,10 +1746,13 @@ fn handle_room_member_event_for_profiles(
#[cfg(test)]
mod tests {
use matrix_sdk_test::{
async_test, ruma_response_from_json, sync_timeline_event, InvitedRoomBuilder,
async_test, event_factory::EventFactory, ruma_response_from_json, InvitedRoomBuilder,
LeftRoomBuilder, StateTestEvent, StrippedStateTestEvent, SyncResponseBuilder,
};
use ruma::{api::client as api, room_id, serde::Raw, user_id, UserId};
use ruma::{
api::client as api, event_id, events::room::member::MembershipState, room_id, serde::Raw,
user_id,
};
use serde_json::{json, value::to_raw_value};
use super::BaseClient;
@@ -1750,17 +1772,15 @@ mod tests {
let mut sync_builder = SyncResponseBuilder::new();
let response = sync_builder
.add_left_room(LeftRoomBuilder::new(room_id).add_timeline_event(sync_timeline_event!({
"content": {
"displayname": "Alice",
"membership": "left",
},
"event_id": "$994173582443PhrSn:example.org",
"origin_server_ts": 1432135524678u64,
"sender": user_id,
"state_key": user_id,
"type": "m.room.member",
})))
.add_left_room(
LeftRoomBuilder::new(room_id).add_timeline_event(
EventFactory::new()
.member(user_id)
.membership(MembershipState::Leave)
.display_name("Alice")
.event_id(event_id!("$994173582443PhrSn:example.org")),
),
)
.build_sync_response();
client.receive_sync_response(response).await.unwrap();
assert_eq!(client.get_room(room_id).unwrap().state(), RoomState::Left);
@@ -1872,18 +1892,37 @@ mod tests {
);
}
#[cfg(all(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_when_there_are_no_latest_encrypted_events_decrypting_them_does_nothing() {
use std::collections::BTreeMap;
use matrix_sdk_test::event_factory::EventFactory;
use ruma::{event_id, events::room::member::MembershipState};
use crate::{rooms::normal::RoomInfoNotableUpdateReasons, StateChanges};
// Given a room
let user_id = user_id!("@u:u.to");
let room_id = room_id!("!r:u.to");
let client = logged_in_base_client(Some(user_id)).await;
let room = process_room_join_test_helper(&client, room_id, "$1", user_id).await;
let mut sync_builder = SyncResponseBuilder::new();
let response = sync_builder
.add_joined_room(
matrix_sdk_test::JoinedRoomBuilder::new(room_id).add_timeline_event(
EventFactory::new()
.member(user_id)
.display_name("Alice")
.membership(MembershipState::Join)
.event_id(event_id!("$1")),
),
)
.build_sync_response();
client.receive_sync_response(response).await.unwrap();
let room = client.get_room(room_id).expect("Just-created room not found!");
// Sanity: it has no latest_encrypted_events or latest_event
assert!(room.latest_encrypted_events().is_empty());
@@ -1905,40 +1944,6 @@ mod tests {
.contains(RoomInfoNotableUpdateReasons::LATEST_EVENT));
}
// TODO: I wanted to write more tests here for decrypt_latest_events but I got
// lost trying to set up my OlmMachine to be able to encrypt and decrypt
// events. In the meantime, there are tests for the most difficult logic
// inside Room. --andyb
#[cfg(feature = "e2e-encryption")]
async fn process_room_join_test_helper(
client: &BaseClient,
room_id: &ruma::RoomId,
event_id: &str,
user_id: &UserId,
) -> crate::Room {
let mut sync_builder = SyncResponseBuilder::new();
let response = sync_builder
.add_joined_room(matrix_sdk_test::JoinedRoomBuilder::new(room_id).add_timeline_event(
sync_timeline_event!({
"content": {
"displayname": "Alice",
"membership": "join",
},
"event_id": event_id,
"origin_server_ts": 1432135524678u64,
"sender": user_id,
"state_key": user_id,
"type": "m.room.member",
}),
))
.build_sync_response();
client.receive_sync_response(response).await.unwrap();
client.get_room(room_id).expect("Just-created room not found!")
}
#[async_test]
async fn test_deserialization_failure() {
let user_id = user_id!("@alice:example.org");
+4 -4
View File
@@ -27,7 +27,7 @@ use ruma::{
pub struct DebugListOfRawEventsNoId<'a, T>(pub &'a [Raw<T>]);
#[cfg(not(tarpaulin_include))]
impl<'a, T> fmt::Debug for DebugListOfRawEventsNoId<'a, T> {
impl<T> fmt::Debug for DebugListOfRawEventsNoId<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut list = f.debug_list();
list.entries(self.0.iter().map(DebugRawEventNoId));
@@ -41,7 +41,7 @@ impl<'a, T> fmt::Debug for DebugListOfRawEventsNoId<'a, T> {
pub struct DebugInvitedRoom<'a>(pub &'a InvitedRoom);
#[cfg(not(tarpaulin_include))]
impl<'a> fmt::Debug for DebugInvitedRoom<'a> {
impl fmt::Debug for DebugInvitedRoom<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("InvitedRoom")
.field("invite_state", &DebugListOfRawEvents(&self.0.invite_state.events))
@@ -55,7 +55,7 @@ impl<'a> fmt::Debug for DebugInvitedRoom<'a> {
pub struct DebugKnockedRoom<'a>(pub &'a KnockedRoom);
#[cfg(not(tarpaulin_include))]
impl<'a> fmt::Debug for DebugKnockedRoom<'a> {
impl fmt::Debug for DebugKnockedRoom<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("KnockedRoom")
.field("knock_state", &DebugListOfRawEvents(&self.0.knock_state.events))
@@ -66,7 +66,7 @@ impl<'a> fmt::Debug for DebugKnockedRoom<'a> {
pub(crate) struct DebugListOfRawEvents<'a, T>(pub &'a [Raw<T>]);
#[cfg(not(tarpaulin_include))]
impl<'a, T> fmt::Debug for DebugListOfRawEvents<'a, T> {
impl<T> fmt::Debug for DebugListOfRawEvents<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut list = f.debug_list();
list.entries(self.0.iter().map(DebugRawEvent));
@@ -30,7 +30,7 @@ use ruma::{
StateEventContent, StaticStateEventContent, StrippedStateEvent, SyncStateEvent,
},
serde::Raw,
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, UserId,
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, UInt, UserId,
};
use serde::Serialize;
use unicode_normalization::UnicodeNormalization;
@@ -160,12 +160,12 @@ impl PartialEq for DisplayName {
impl DisplayName {
/// Regex pattern matching an MXID.
const MXID_PATTERN: &str = "@.+[:.].+";
const MXID_PATTERN: &'static str = "@.+[:.].+";
/// Regex pattern matching some left-to-right formatting marks:
/// * LTR and RTL marks U+200E and U+200F
/// * LTR/RTL and other directional formatting marks U+202A - U+202F
const LEFT_TO_RIGHT_PATTERN: &str = "[\u{202a}-\u{202f}\u{200e}\u{200f}]";
const LEFT_TO_RIGHT_PATTERN: &'static str = "[\u{202a}-\u{202f}\u{200e}\u{200f}]";
/// Regex pattern matching bunch of unicode control characters and otherwise
/// misleading/invisible characters.
@@ -176,7 +176,7 @@ impl DisplayName {
/// * Blank/invisible characters (U2800, U2062-U2063)
/// * Arabic Letter RTL mark U+061C
/// * Zero width no-break space (BOM) U+FEFF
const HIDDEN_CHARACTERS_PATTERN: &str =
const HIDDEN_CHARACTERS_PATTERN: &'static str =
"[\u{2000}-\u{200D}\u{300}-\u{036f}\u{2062}-\u{2063}\u{2800}\u{061c}\u{feff}]";
/// Creates a new [`DisplayName`] from the given raw string.
@@ -476,6 +476,23 @@ impl MemberEvent {
.unwrap_or_else(|| self.user_id().localpart()),
)
}
/// The optional reason why the membership changed.
pub fn reason(&self) -> Option<&str> {
match self {
MemberEvent::Sync(SyncStateEvent::Original(c)) => c.content.reason.as_deref(),
MemberEvent::Stripped(e) => e.content.reason.as_deref(),
_ => None,
}
}
/// The optional timestamp for this member event.
pub fn timestamp(&self) -> Option<UInt> {
match self {
MemberEvent::Sync(SyncStateEvent::Original(c)) => Some(c.origin_server_ts.0),
_ => None,
}
}
}
impl SyncOrStrippedState<RoomPowerLevelsEventContent> {
@@ -585,7 +602,7 @@ mod test {
}
#[test]
fn test_display_name_equality_cyrilic() {
fn test_display_name_equality_cyrillic() {
// Display name with scritpure symbols
assert_display_name_eq!("alice", "аlice");
}
+11
View File
@@ -15,10 +15,13 @@
//! Error conditions.
use matrix_sdk_common::store_locks::LockStoreError;
#[cfg(feature = "e2e-encryption")]
use matrix_sdk_crypto::{CryptoStoreError, MegolmError, OlmError};
use thiserror::Error;
use crate::event_cache::store::EventCacheStoreError;
/// Result type of the rust-sdk.
pub type Result<T, E = Error> = std::result::Result<T, E>;
@@ -42,6 +45,14 @@ pub enum Error {
#[error(transparent)]
StateStore(#[from] crate::store::StoreError),
/// An error happened while manipulating the event cache store.
#[error(transparent)]
EventCacheStore(#[from] EventCacheStoreError),
/// An error happened while attempting to lock the event cache store.
#[error(transparent)]
EventCacheLock(#[from] LockStoreError),
/// An error occurred in the crypto store.
#[cfg(feature = "e2e-encryption")]
#[error(transparent)]
@@ -14,12 +14,12 @@
//! Event cache store and common types shared with `matrix_sdk::event_cache`.
use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
use matrix_sdk_common::deserialized_responses::TimelineEvent;
pub mod store;
/// The kind of event the event storage holds.
pub type Event = SyncTimelineEvent;
pub type Event = TimelineEvent;
/// The kind of gap the event storage holds.
#[derive(Clone, Debug)]
@@ -14,13 +14,88 @@
//! Trait and macro of integration tests for `EventCacheStore` implementations.
use assert_matches::assert_matches;
use async_trait::async_trait;
use matrix_sdk_common::{
deserialized_responses::{
AlgorithmInfo, DecryptedRoomEvent, EncryptionInfo, TimelineEvent, TimelineEventKind,
VerificationState,
},
linked_chunk::{
ChunkContent, ChunkIdentifier as CId, LinkedChunk, LinkedChunkBuilder, Position, RawChunk,
Update,
},
};
use matrix_sdk_test::{event_factory::EventFactory, ALICE, DEFAULT_TEST_ROOM_ID};
use ruma::{
api::client::media::get_content_thumbnail::v3::Method, events::room::MediaSource, mxc_uri, uint,
api::client::media::get_content_thumbnail::v3::Method, events::room::MediaSource, mxc_uri,
push::Action, room_id, uint, RoomId,
};
use super::DynEventCacheStore;
use crate::media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings};
use super::{media::IgnoreMediaRetentionPolicy, DynEventCacheStore};
use crate::{
event_cache::{Event, Gap},
media::{MediaFormat, MediaRequestParameters, MediaThumbnailSettings},
};
/// Create a test event with all data filled, for testing that linked chunk
/// correctly stores event data.
///
/// Keep in sync with [`check_test_event`].
pub fn make_test_event(room_id: &RoomId, content: &str) -> TimelineEvent {
let encryption_info = EncryptionInfo {
sender: (*ALICE).into(),
sender_device: None,
algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
curve25519_key: "1337".to_owned(),
sender_claimed_keys: Default::default(),
},
verification_state: VerificationState::Verified,
};
let event = EventFactory::new()
.text_msg(content)
.room(room_id)
.sender(*ALICE)
.into_raw_timeline()
.cast();
TimelineEvent {
kind: TimelineEventKind::Decrypted(DecryptedRoomEvent {
event,
encryption_info,
unsigned_encryption_info: None,
}),
push_actions: Some(vec![Action::Notify]),
}
}
/// Check that an event created with [`make_test_event`] contains the expected
/// data.
///
/// Keep in sync with [`make_test_event`].
#[track_caller]
pub fn check_test_event(event: &TimelineEvent, text: &str) {
// Check push actions.
let actions = event.push_actions.as_ref().unwrap();
assert_eq!(actions.len(), 1);
assert_matches!(&actions[0], Action::Notify);
// Check content.
assert_matches!(&event.kind, TimelineEventKind::Decrypted(d) => {
// Check encryption fields.
assert_eq!(d.encryption_info.sender, *ALICE);
assert_matches!(&d.encryption_info.algorithm_info, AlgorithmInfo::MegolmV1AesSha2 { curve25519_key, .. } => {
assert_eq!(curve25519_key, "1337");
});
// Check event.
let deserialized = d.event.deserialize().unwrap();
assert_matches!(deserialized, ruma::events::AnyMessageLikeEvent::RoomMessage(msg) => {
assert_eq!(msg.as_original().unwrap().content.body(), text);
});
});
}
/// `EventCacheStore` integration tests.
///
@@ -34,6 +109,24 @@ pub trait EventCacheStoreIntegrationTests {
/// Test replacing a MXID.
async fn test_replace_media_key(&self);
/// Test handling updates to a linked chunk and reloading these updates from
/// the store.
async fn test_handle_updates_and_rebuild_linked_chunk(&self);
/// Test that rebuilding a linked chunk from an empty store doesn't return
/// anything.
async fn test_rebuild_empty_linked_chunk(&self);
/// Test that clear all the rooms' linked chunks works.
async fn test_clear_all_rooms_chunks(&self);
/// Test that removing a room from storage empties all associated data.
async fn test_remove_room(&self);
}
fn rebuild_linked_chunk(raws: Vec<RawChunk<Event, Gap>>) -> Option<LinkedChunk<3, Event, Gap>> {
LinkedChunkBuilder::from_raw_parts(raws).build().unwrap()
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
@@ -75,7 +168,9 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {
);
// Let's add the media.
self.add_media_content(&request_file, content.clone()).await.expect("adding media failed");
self.add_media_content(&request_file, content.clone(), IgnoreMediaRetentionPolicy::No)
.await
.expect("adding media failed");
// Media is present in the cache.
assert_eq!(
@@ -83,6 +178,11 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {
Some(&content),
"media not found though added"
);
assert_eq!(
self.get_media_content_for_uri(uri).await.unwrap().as_ref(),
Some(&content),
"media not found by URI though added"
);
// Let's remove the media.
self.remove_media_content(&request_file).await.expect("removing media failed");
@@ -92,9 +192,13 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {
self.get_media_content(&request_file).await.unwrap().is_none(),
"media still there after removing"
);
assert!(
self.get_media_content_for_uri(uri).await.unwrap().is_none(),
"media still found by URI after removing"
);
// Let's add the media again.
self.add_media_content(&request_file, content.clone())
self.add_media_content(&request_file, content.clone(), IgnoreMediaRetentionPolicy::No)
.await
.expect("adding media again failed");
@@ -105,9 +209,13 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {
);
// Let's add the thumbnail media.
self.add_media_content(&request_thumbnail, thumbnail_content.clone())
.await
.expect("adding thumbnail failed");
self.add_media_content(
&request_thumbnail,
thumbnail_content.clone(),
IgnoreMediaRetentionPolicy::No,
)
.await
.expect("adding thumbnail failed");
// Media's thumbnail is present.
assert_eq!(
@@ -116,10 +224,20 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {
"thumbnail not found"
);
// We get a file with the URI, we don't know which one.
assert!(
self.get_media_content_for_uri(uri).await.unwrap().is_some(),
"media not found by URI though two where added"
);
// Let's add another media with a different URI.
self.add_media_content(&request_other_file, other_content.clone())
.await
.expect("adding other media failed");
self.add_media_content(
&request_other_file,
other_content.clone(),
IgnoreMediaRetentionPolicy::No,
)
.await
.expect("adding other media failed");
// Other file is present.
assert_eq!(
@@ -127,6 +245,11 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {
Some(&other_content),
"other file not found"
);
assert_eq!(
self.get_media_content_for_uri(other_uri).await.unwrap().as_ref(),
Some(&other_content),
"other file not found by URI"
);
// Let's remove media based on URI.
self.remove_media_content_for_uri(uri).await.expect("removing all media for uri failed");
@@ -143,6 +266,14 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {
self.get_media_content(&request_other_file).await.unwrap().is_some(),
"other media was removed"
);
assert!(
self.get_media_content_for_uri(uri).await.unwrap().is_none(),
"media found by URI wasn't removed"
);
assert!(
self.get_media_content_for_uri(other_uri).await.unwrap().is_some(),
"other media found by URI was removed"
);
}
async fn test_replace_media_key(&self) {
@@ -158,7 +289,9 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {
assert!(self.get_media_content(&req).await.unwrap().is_none(), "unexpected media found");
// Add the media.
self.add_media_content(&req, content.clone()).await.expect("adding media failed");
self.add_media_content(&req, content.clone(), IgnoreMediaRetentionPolicy::No)
.await
.expect("adding media failed");
// Sanity-check: media is found after adding it.
assert_eq!(self.get_media_content(&req).await.unwrap().unwrap(), b"hello");
@@ -182,6 +315,193 @@ impl EventCacheStoreIntegrationTests for DynEventCacheStore {
// Finding with the new request does work.
assert_eq!(self.get_media_content(&new_req).await.unwrap().unwrap(), b"hello");
}
async fn test_handle_updates_and_rebuild_linked_chunk(&self) {
let room_id = room_id!("!r0:matrix.org");
self.handle_linked_chunk_updates(
room_id,
vec![
// new chunk
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
// new items on 0
Update::PushItems {
at: Position::new(CId::new(0), 0),
items: vec![
make_test_event(room_id, "hello"),
make_test_event(room_id, "world"),
],
},
// a gap chunk
Update::NewGapChunk {
previous: Some(CId::new(0)),
new: CId::new(1),
next: None,
gap: Gap { prev_token: "parmesan".to_owned() },
},
// another items chunk
Update::NewItemsChunk { previous: Some(CId::new(1)), new: CId::new(2), next: None },
// new items on 0
Update::PushItems {
at: Position::new(CId::new(2), 0),
items: vec![make_test_event(room_id, "sup")],
},
],
)
.await
.unwrap();
// The linked chunk is correctly reloaded.
let raws = self.reload_linked_chunk(room_id).await.unwrap();
let lc = rebuild_linked_chunk(raws).expect("linked chunk not empty");
let mut chunks = lc.chunks();
{
let first = chunks.next().unwrap();
// Note: we can't assert the previous/next chunks, as these fields and their
// getters are private.
assert_eq!(first.identifier(), CId::new(0));
assert_matches!(first.content(), ChunkContent::Items(events) => {
assert_eq!(events.len(), 2);
check_test_event(&events[0], "hello");
check_test_event(&events[1], "world");
});
}
{
let second = chunks.next().unwrap();
assert_eq!(second.identifier(), CId::new(1));
assert_matches!(second.content(), ChunkContent::Gap(gap) => {
assert_eq!(gap.prev_token, "parmesan");
});
}
{
let third = chunks.next().unwrap();
assert_eq!(third.identifier(), CId::new(2));
assert_matches!(third.content(), ChunkContent::Items(events) => {
assert_eq!(events.len(), 1);
check_test_event(&events[0], "sup");
});
}
assert!(chunks.next().is_none());
}
async fn test_rebuild_empty_linked_chunk(&self) {
// When I rebuild a linked chunk from an empty store, it's empty.
let raw_parts = self.reload_linked_chunk(&DEFAULT_TEST_ROOM_ID).await.unwrap();
assert!(rebuild_linked_chunk(raw_parts).is_none());
}
async fn test_clear_all_rooms_chunks(&self) {
let r0 = room_id!("!r0:matrix.org");
let r1 = room_id!("!r1:matrix.org");
// Add updates for the first room.
self.handle_linked_chunk_updates(
r0,
vec![
// new chunk
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
// new items on 0
Update::PushItems {
at: Position::new(CId::new(0), 0),
items: vec![make_test_event(r0, "hello"), make_test_event(r0, "world")],
},
],
)
.await
.unwrap();
// Add updates for the second room.
self.handle_linked_chunk_updates(
r1,
vec![
// Empty items chunk.
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
// a gap chunk
Update::NewGapChunk {
previous: Some(CId::new(0)),
new: CId::new(1),
next: None,
gap: Gap { prev_token: "bleu d'auvergne".to_owned() },
},
// another items chunk
Update::NewItemsChunk { previous: Some(CId::new(1)), new: CId::new(2), next: None },
// new items on 0
Update::PushItems {
at: Position::new(CId::new(2), 0),
items: vec![make_test_event(r0, "yummy")],
},
],
)
.await
.unwrap();
// Sanity check: both linked chunks can be reloaded.
assert!(rebuild_linked_chunk(self.reload_linked_chunk(r0).await.unwrap()).is_some());
assert!(rebuild_linked_chunk(self.reload_linked_chunk(r1).await.unwrap()).is_some());
// Clear the chunks.
self.clear_all_rooms_chunks().await.unwrap();
// Both rooms now have no linked chunk.
assert!(rebuild_linked_chunk(self.reload_linked_chunk(r0).await.unwrap()).is_none());
assert!(rebuild_linked_chunk(self.reload_linked_chunk(r1).await.unwrap()).is_none());
}
async fn test_remove_room(&self) {
let r0 = room_id!("!r0:matrix.org");
let r1 = room_id!("!r1:matrix.org");
// Add updates to the first room.
self.handle_linked_chunk_updates(
r0,
vec![
// new chunk
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
// new items on 0
Update::PushItems {
at: Position::new(CId::new(0), 0),
items: vec![make_test_event(r0, "hello"), make_test_event(r0, "world")],
},
],
)
.await
.unwrap();
// Add updates to the second room.
self.handle_linked_chunk_updates(
r1,
vec![
// new chunk
Update::NewItemsChunk { previous: None, new: CId::new(0), next: None },
// new items on 0
Update::PushItems {
at: Position::new(CId::new(0), 0),
items: vec![make_test_event(r0, "yummy")],
},
],
)
.await
.unwrap();
// Try to remove content from r0.
self.remove_room(r0).await.unwrap();
// Check that r0 doesn't have a linked chunk anymore.
let r0_linked_chunk = self.reload_linked_chunk(r0).await.unwrap();
assert!(r0_linked_chunk.is_empty());
// Check that r1 is unaffected.
let r1_linked_chunk = self.reload_linked_chunk(r1).await.unwrap();
assert!(!r1_linked_chunk.is_empty());
}
}
/// Macro building to allow your `EventCacheStore` implementation to run the
@@ -236,6 +556,34 @@ macro_rules! event_cache_store_integration_tests {
get_event_cache_store().await.unwrap().into_event_cache_store();
event_cache_store.test_replace_media_key().await;
}
#[async_test]
async fn test_handle_updates_and_rebuild_linked_chunk() {
let event_cache_store =
get_event_cache_store().await.unwrap().into_event_cache_store();
event_cache_store.test_handle_updates_and_rebuild_linked_chunk().await;
}
#[async_test]
async fn test_rebuild_empty_linked_chunk() {
let event_cache_store =
get_event_cache_store().await.unwrap().into_event_cache_store();
event_cache_store.test_rebuild_empty_linked_chunk().await;
}
#[async_test]
async fn test_clear_all_rooms_chunks() {
let event_cache_store =
get_event_cache_store().await.unwrap().into_event_cache_store();
event_cache_store.test_clear_all_rooms_chunks().await;
}
#[async_test]
async fn test_remove_room() {
let event_cache_store =
get_event_cache_store().await.unwrap().into_event_cache_store();
event_cache_store.test_remove_room().await;
}
}
};
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,330 @@
// Copyright 2025 Kévin Commaille
//
// 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.
//! Configuration to decide whether or not to keep media in the cache, allowing
//! to do periodic cleanups to avoid to have the size of the media cache grow
//! indefinitely.
//!
//! To proceed to a cleanup, first set the [`MediaRetentionPolicy`] to use with
//! [`EventCacheStore::set_media_retention_policy()`]. Then call
//! [`EventCacheStore::clean_up_media_cache()`].
//!
//! In the future, other settings will allow to run automatic periodic cleanup
//! jobs.
//!
//! [`EventCacheStore::set_media_retention_policy()`]: crate::event_cache::store::EventCacheStore::set_media_retention_policy
//! [`EventCacheStore::clean_up_media_cache()`]: crate::event_cache::store::EventCacheStore::clean_up_media_cache
use ruma::time::{Duration, SystemTime};
use serde::{Deserialize, Serialize};
/// The retention policy for media content used by the [`EventCacheStore`].
///
/// [`EventCacheStore`]: crate::event_cache::store::EventCacheStore
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MediaRetentionPolicy {
/// The maximum authorized size of the overall media cache, in bytes.
///
/// The cache size is defined as the sum of the sizes of all the (possibly
/// encrypted) media contents in the cache, excluding any metadata
/// associated with them.
///
/// If this is set and the cache size is bigger than this value, the oldest
/// media contents in the cache will be removed during a cleanup until the
/// cache size is below this threshold.
///
/// Note that it is possible for the cache size to temporarily exceed this
/// value between two cleanups.
///
/// Defaults to 400 MiB.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_cache_size: Option<usize>,
/// The maximum authorized size of a single media content, in bytes.
///
/// The size of a media content is the size taken by the content in the
/// database, after it was possibly encrypted, so it might differ from the
/// initial size of the content.
///
/// The maximum authorized size of a single media content is actually the
/// lowest value between `max_cache_size` and `max_file_size`.
///
/// If it is set, media content bigger than the maximum size will not be
/// cached. If the maximum size changed after media content that exceeds the
/// new value was cached, the corresponding content will be removed
/// during a cleanup.
///
/// Defaults to 20 MiB.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_file_size: Option<usize>,
/// The duration after which unaccessed media content is considered
/// expired.
///
/// If this is set, media content whose last access is older than this
/// duration will be removed from the media cache during a cleanup.
///
/// Defaults to 60 days.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_access_expiry: Option<Duration>,
}
impl MediaRetentionPolicy {
/// Create a [`MediaRetentionPolicy`] with the default values.
pub fn new() -> Self {
Self::default()
}
/// Create an empty [`MediaRetentionPolicy`].
///
/// This means that all media will be cached and cleanups have no effect.
pub fn empty() -> Self {
Self { max_cache_size: None, max_file_size: None, last_access_expiry: None }
}
/// Set the maximum authorized size of the overall media cache, in bytes.
pub fn with_max_cache_size(mut self, size: Option<usize>) -> Self {
self.max_cache_size = size;
self
}
/// Set the maximum authorized size of a single media content, in bytes.
pub fn with_max_file_size(mut self, size: Option<usize>) -> Self {
self.max_file_size = size;
self
}
/// Set the duration before which unaccessed media content is considered
/// expired.
pub fn with_last_access_expiry(mut self, duration: Option<Duration>) -> Self {
self.last_access_expiry = duration;
self
}
/// Whether this policy has limitations.
///
/// If this policy has no limitations, a cleanup job would have no effect.
///
/// Returns `true` if at least one limitation is set.
pub fn has_limitations(&self) -> bool {
self.max_cache_size.is_some()
|| self.max_file_size.is_some()
|| self.last_access_expiry.is_some()
}
/// Whether the given size exceeds the maximum authorized size of the media
/// cache.
///
/// # Arguments
///
/// * `size` - The overall size of the media cache to check, in bytes.
pub fn exceeds_max_cache_size(&self, size: usize) -> bool {
self.max_cache_size.is_some_and(|max_size| size > max_size)
}
/// The computed maximum authorized size of a single media content, in
/// bytes.
///
/// This is the lowest value between `max_cache_size` and `max_file_size`.
pub fn computed_max_file_size(&self) -> Option<usize> {
match (self.max_cache_size, self.max_file_size) {
(None, None) => None,
(None, Some(size)) => Some(size),
(Some(size), None) => Some(size),
(Some(max_cache_size), Some(max_file_size)) => Some(max_cache_size.min(max_file_size)),
}
}
/// Whether the given size, in bytes, exceeds the computed maximum
/// authorized size of a single media content.
///
/// # Arguments
///
/// * `size` - The size of the media content to check, in bytes.
pub fn exceeds_max_file_size(&self, size: usize) -> bool {
self.computed_max_file_size().is_some_and(|max_size| size > max_size)
}
/// Whether a content whose last access was at the given time has expired.
///
/// # Arguments
///
/// * `current_time` - The current time.
///
/// * `last_access_time` - The time when the media content to check was last
/// accessed.
pub fn has_content_expired(
&self,
current_time: SystemTime,
last_access_time: SystemTime,
) -> bool {
self.last_access_expiry.is_some_and(|max_duration| {
current_time
.duration_since(last_access_time)
// If this returns an error, the last access time is newer than the current time.
// This shouldn't happen but in this case the content cannot be expired.
.is_ok_and(|elapsed| elapsed >= max_duration)
})
}
}
impl Default for MediaRetentionPolicy {
fn default() -> Self {
Self {
// 400 MiB.
max_cache_size: Some(400 * 1024 * 1024),
// 20 MiB.
max_file_size: Some(20 * 1024 * 1024),
// 60 days.
last_access_expiry: Some(Duration::from_secs(60 * 24 * 60 * 60)),
}
}
}
#[cfg(test)]
mod tests {
use ruma::time::{Duration, SystemTime};
use super::MediaRetentionPolicy;
#[test]
fn test_media_retention_policy_has_limitations() {
let mut policy = MediaRetentionPolicy::empty();
assert!(!policy.has_limitations());
policy = policy.with_last_access_expiry(Some(Duration::from_secs(60)));
assert!(policy.has_limitations());
policy = policy.with_last_access_expiry(None);
assert!(!policy.has_limitations());
policy = policy.with_max_cache_size(Some(1_024));
assert!(policy.has_limitations());
policy = policy.with_max_cache_size(None);
assert!(!policy.has_limitations());
policy = policy.with_max_file_size(Some(1_024));
assert!(policy.has_limitations());
policy = policy.with_max_file_size(None);
assert!(!policy.has_limitations());
// With default values.
assert!(MediaRetentionPolicy::new().has_limitations());
}
#[test]
fn test_media_retention_policy_max_cache_size() {
let file_size = 2_048;
let mut policy = MediaRetentionPolicy::empty();
assert!(!policy.exceeds_max_cache_size(file_size));
assert_eq!(policy.computed_max_file_size(), None);
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_cache_size(Some(4_096));
assert!(!policy.exceeds_max_cache_size(file_size));
assert_eq!(policy.computed_max_file_size(), Some(4_096));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_cache_size(Some(2_048));
assert!(!policy.exceeds_max_cache_size(file_size));
assert_eq!(policy.computed_max_file_size(), Some(2_048));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_cache_size(Some(1_024));
assert!(policy.exceeds_max_cache_size(file_size));
assert_eq!(policy.computed_max_file_size(), Some(1_024));
assert!(policy.exceeds_max_file_size(file_size));
}
#[test]
fn test_media_retention_policy_max_file_size() {
let file_size = 2_048;
let mut policy = MediaRetentionPolicy::empty();
assert_eq!(policy.computed_max_file_size(), None);
assert!(!policy.exceeds_max_file_size(file_size));
// With max_file_size only.
policy = policy.with_max_file_size(Some(4_096));
assert_eq!(policy.computed_max_file_size(), Some(4_096));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_file_size(Some(2_048));
assert_eq!(policy.computed_max_file_size(), Some(2_048));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_file_size(Some(1_024));
assert_eq!(policy.computed_max_file_size(), Some(1_024));
assert!(policy.exceeds_max_file_size(file_size));
// With max_cache_size as well.
policy = policy.with_max_cache_size(Some(2_048));
assert_eq!(policy.computed_max_file_size(), Some(1_024));
assert!(policy.exceeds_max_file_size(file_size));
policy = policy.with_max_file_size(Some(2_048));
assert_eq!(policy.computed_max_file_size(), Some(2_048));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_file_size(Some(4_096));
assert_eq!(policy.computed_max_file_size(), Some(2_048));
assert!(!policy.exceeds_max_file_size(file_size));
policy = policy.with_max_cache_size(Some(1_024));
assert_eq!(policy.computed_max_file_size(), Some(1_024));
assert!(policy.exceeds_max_file_size(file_size));
}
#[test]
fn test_media_retention_policy_has_content_expired() {
let epoch = SystemTime::UNIX_EPOCH;
let last_access_time = epoch + Duration::from_secs(30);
let epoch_plus_60 = epoch + Duration::from_secs(60);
let epoch_plus_120 = epoch + Duration::from_secs(120);
let mut policy = MediaRetentionPolicy::empty();
assert!(!policy.has_content_expired(epoch, last_access_time));
assert!(!policy.has_content_expired(last_access_time, last_access_time));
assert!(!policy.has_content_expired(epoch_plus_60, last_access_time));
assert!(!policy.has_content_expired(epoch_plus_120, last_access_time));
policy = policy.with_last_access_expiry(Some(Duration::from_secs(120)));
assert!(!policy.has_content_expired(epoch, last_access_time));
assert!(!policy.has_content_expired(last_access_time, last_access_time));
assert!(!policy.has_content_expired(epoch_plus_60, last_access_time));
assert!(!policy.has_content_expired(epoch_plus_120, last_access_time));
policy = policy.with_last_access_expiry(Some(Duration::from_secs(60)));
assert!(!policy.has_content_expired(epoch, last_access_time));
assert!(!policy.has_content_expired(last_access_time, last_access_time));
assert!(!policy.has_content_expired(epoch_plus_60, last_access_time));
assert!(policy.has_content_expired(epoch_plus_120, last_access_time));
policy = policy.with_last_access_expiry(Some(Duration::from_secs(30)));
assert!(!policy.has_content_expired(epoch, last_access_time));
assert!(!policy.has_content_expired(last_access_time, last_access_time));
assert!(policy.has_content_expired(epoch_plus_60, last_access_time));
assert!(policy.has_content_expired(epoch_plus_120, last_access_time));
policy = policy.with_last_access_expiry(Some(Duration::from_secs(0)));
assert!(!policy.has_content_expired(epoch, last_access_time));
assert!(policy.has_content_expired(last_access_time, last_access_time));
assert!(policy.has_content_expired(epoch_plus_60, last_access_time));
assert!(policy.has_content_expired(epoch_plus_120, last_access_time));
}
}
@@ -0,0 +1,884 @@
// Copyright 2025 Kévin Commaille
//
// 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 async_trait::async_trait;
use matrix_sdk_common::{locks::Mutex, AsyncTraitDeps};
use ruma::{time::SystemTime, MxcUri};
use tokio::sync::Mutex as AsyncMutex;
use super::MediaRetentionPolicy;
use crate::{event_cache::store::EventCacheStoreError, media::MediaRequestParameters};
/// API for implementors of [`EventCacheStore`] to manage their media through
/// their implementation of [`EventCacheStoreMedia`].
///
/// [`EventCacheStore`]: crate::event_cache::store::EventCacheStore
#[derive(Debug)]
pub struct MediaService<Time: TimeProvider = DefaultTimeProvider> {
/// The time provider.
time_provider: Time,
/// The current [`MediaRetentionPolicy`].
policy: Mutex<MediaRetentionPolicy>,
/// A mutex to ensure a single cleanup is running at a time.
cleanup_guard: AsyncMutex<()>,
}
impl MediaService {
/// Construct a new default `MediaService`.
///
/// [`MediaService::restore()`] should be called after constructing the
/// `MediaService` to restore its previous state.
pub fn new() -> Self {
Self::default()
}
}
impl Default for MediaService {
fn default() -> Self {
Self::with_time_provider(DefaultTimeProvider)
}
}
impl<Time> MediaService<Time>
where
Time: TimeProvider,
{
/// Construct a new `MediaService` with the given `TimeProvider` and an
/// empty `MediaRetentionPolicy`.
fn with_time_provider(time_provider: Time) -> Self {
Self {
time_provider,
policy: Mutex::new(MediaRetentionPolicy::empty()),
cleanup_guard: AsyncMutex::new(()),
}
}
/// Restore the previous state of the [`MediaRetentionPolicy`] from data
/// that was persisted in the store.
///
/// This should be called immediately after constructing the `MediaService`.
///
/// # Arguments
///
/// * `policy` - The `MediaRetentionPolicy` that was persisted in the store.
pub fn restore(&self, policy: Option<MediaRetentionPolicy>) {
if let Some(policy) = policy {
*self.policy.lock() = policy;
}
}
/// Set the `MediaRetentionPolicy` of this service.
///
/// # Arguments
///
/// * `store` - The `EventCacheStoreMedia`.
///
/// * `policy` - The `MediaRetentionPolicy` to use.
pub async fn set_media_retention_policy<Store: EventCacheStoreMedia>(
&self,
store: &Store,
policy: MediaRetentionPolicy,
) -> Result<(), Store::Error> {
store.set_media_retention_policy_inner(policy).await?;
*self.policy.lock() = policy;
Ok(())
}
/// Get the `MediaRetentionPolicy` of this service.
pub fn media_retention_policy(&self) -> MediaRetentionPolicy {
*self.policy.lock()
}
/// Add a media file's content in the media store.
///
/// # Arguments
///
/// * `store` - The `EventCacheStoreMedia`.
///
/// * `request` - The `MediaRequestParameters` of the file.
///
/// * `content` - The content of the file.
///
/// * `ignore_policy` - Whether the current `MediaRetentionPolicy` should be
/// ignored.
pub async fn add_media_content<Store: EventCacheStoreMedia>(
&self,
store: &Store,
request: &MediaRequestParameters,
content: Vec<u8>,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Store::Error> {
let policy = self.media_retention_policy();
if ignore_policy == IgnoreMediaRetentionPolicy::No
&& policy.exceeds_max_file_size(content.len())
{
// We do not cache the content.
return Ok(());
}
store
.add_media_content_inner(
request,
content,
self.time_provider.now(),
policy,
ignore_policy,
)
.await
}
/// Set whether the current [`MediaRetentionPolicy`] should be ignored for
/// the media.
///
/// The change will be taken into account in the next cleanup.
///
/// # Arguments
///
/// * `store` - The `EventCacheStoreMedia`.
///
/// * `request` - The `MediaRequestParameters` of the file.
///
/// * `ignore_policy` - Whether the current `MediaRetentionPolicy` should be
/// ignored.
pub async fn set_ignore_media_retention_policy<Store: EventCacheStoreMedia>(
&self,
store: &Store,
request: &MediaRequestParameters,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Store::Error> {
store.set_ignore_media_retention_policy_inner(request, ignore_policy).await
}
/// Get a media file's content out of the media store.
///
/// # Arguments
///
/// * `store` - The `EventCacheStoreMedia`.
///
/// * `request` - The `MediaRequestParameters` of the file.
pub async fn get_media_content<Store: EventCacheStoreMedia>(
&self,
store: &Store,
request: &MediaRequestParameters,
) -> Result<Option<Vec<u8>>, Store::Error> {
store.get_media_content_inner(request, self.time_provider.now()).await
}
/// Get a media file's content associated to an `MxcUri` from the
/// media store.
///
/// # Arguments
///
/// * `store` - The `EventCacheStoreMedia`.
///
/// * `uri` - The `MxcUri` of the media file.
pub async fn get_media_content_for_uri<Store: EventCacheStoreMedia>(
&self,
store: &Store,
uri: &MxcUri,
) -> Result<Option<Vec<u8>>, Store::Error> {
store.get_media_content_for_uri_inner(uri, self.time_provider.now()).await
}
/// Clean up the media cache with the current `MediaRetentionPolicy`.
///
/// If there is already an ongoing cleanup, this is a noop.
///
/// # Arguments
///
/// * `store` - The `EventCacheStoreMedia`.
pub async fn clean_up_media_cache<Store: EventCacheStoreMedia>(
&self,
store: &Store,
) -> Result<(), Store::Error> {
let Ok(_guard) = self.cleanup_guard.try_lock() else {
// There is another ongoing cleanup.
return Ok(());
};
let policy = self.media_retention_policy();
if !policy.has_limitations() {
// No need to call the backend.
return Ok(());
}
store.clean_up_media_cache_inner(policy, self.time_provider.now()).await
}
}
/// An abstract trait that can be used to implement different store backends
/// for the media cache of the SDK.
///
/// The main purposes of this trait are to be able to centralize where we handle
/// [`MediaRetentionPolicy`] by wrapping this in a [`MediaService`], and to
/// simplify the implementation of tests by being able to have complete control
/// over the `SystemTime`s provided to the store.
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait EventCacheStoreMedia: AsyncTraitDeps {
/// The error type used by this media cache store.
type Error: fmt::Debug + Into<EventCacheStoreError>;
/// The persisted media retention policy in the media cache.
async fn media_retention_policy_inner(
&self,
) -> Result<Option<MediaRetentionPolicy>, Self::Error>;
/// Persist the media retention policy in the media cache.
///
/// # Arguments
///
/// * `policy` - The `MediaRetentionPolicy` to persist.
async fn set_media_retention_policy_inner(
&self,
policy: MediaRetentionPolicy,
) -> Result<(), Self::Error>;
/// Add a media file's content in the media cache.
///
/// # Arguments
///
/// * `request` - The `MediaRequestParameters` of the file.
///
/// * `content` - The content of the file.
///
/// * `current_time` - The current time, to set the last access time of the
/// media.
///
/// * `policy` - The media retention policy, to check whether the media is
/// too big to be cached.
///
/// * `ignore_policy` - Whether the `MediaRetentionPolicy` should be ignored
/// for this media. This setting should be persisted alongside the media
/// and taken into account whenever the policy is used.
async fn add_media_content_inner(
&self,
request: &MediaRequestParameters,
content: Vec<u8>,
current_time: SystemTime,
policy: MediaRetentionPolicy,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error>;
/// Set whether the current [`MediaRetentionPolicy`] should be ignored for
/// the media.
///
/// If the media of the given request is not found, this should be a noop.
///
/// The change will be taken into account in the next cleanup.
///
/// # Arguments
///
/// * `request` - The `MediaRequestParameters` of the file.
///
/// * `ignore_policy` - Whether the current `MediaRetentionPolicy` should be
/// ignored.
async fn set_ignore_media_retention_policy_inner(
&self,
request: &MediaRequestParameters,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error>;
/// Get a media file's content out of the media cache.
///
/// # Arguments
///
/// * `request` - The `MediaRequestParameters` of the file.
///
/// * `current_time` - The current time, to update the last access time of
/// the media.
async fn get_media_content_inner(
&self,
request: &MediaRequestParameters,
current_time: SystemTime,
) -> Result<Option<Vec<u8>>, Self::Error>;
/// Get a media file's content associated to an `MxcUri` from the
/// media store.
///
/// # Arguments
///
/// * `uri` - The `MxcUri` of the media file.
///
/// * `current_time` - The current time, to update the last access time of
/// the media.
async fn get_media_content_for_uri_inner(
&self,
uri: &MxcUri,
current_time: SystemTime,
) -> Result<Option<Vec<u8>>, Self::Error>;
/// Clean up the media cache with the given policy.
///
/// For the integration tests, it is expected that content that does not
/// pass the last access expiry and max file size criteria will be
/// removed first. After that, the remaining cache size should be
/// computed to compare against the max cache size criteria.
///
/// # Arguments
///
/// * `policy` - The media retention policy to use for the cleanup. The
/// `cleanup_frequency` will be ignored.
///
/// * `current_time` - The current time, to be used to check for expired
/// content.
async fn clean_up_media_cache_inner(
&self,
policy: MediaRetentionPolicy,
current_time: SystemTime,
) -> Result<(), Self::Error>;
}
/// Whether the [`MediaRetentionPolicy`] should be ignored for the current
/// content.
///
/// Some media cache actions are noops when the media content that is processed
/// is filtered out by the policy. This can break some features of the SDK, like
/// the send queue, that expects to be able to persist all media files in the
/// store to restore them when the client is restored.
///
/// This can be converted to a boolean with
/// [`IgnoreMediaRetentionPolicy::is_yes()`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IgnoreMediaRetentionPolicy {
/// The media retention policy will be ignored and the current action will
/// not be a noop.
///
/// Any media content in this state must NOT be used when applying a
/// `MediaRetentionPolicy`. This applies to ANY criteria, like the maximum
/// file size, the maximum cache size or the last access expiry.
///
/// This state is supposed to be transient, and to only be used internally
/// by the SDK.
Yes,
/// The media retention policy will be respected and the current action
/// might be a noop.
No,
}
impl IgnoreMediaRetentionPolicy {
/// Whether this is an [`IgnoreMediaRetentionPolicy::Yes`] variant.
pub fn is_yes(self) -> bool {
matches!(self, Self::Yes)
}
}
/// An abstract trait to provide the current `SystemTime` for the
/// [`MediaService`].
pub trait TimeProvider {
/// The current time.
fn now(&self) -> SystemTime;
}
/// The default time provider, that calls `ruma::time::SystemTime::now()`.
#[derive(Debug)]
pub struct DefaultTimeProvider;
impl TimeProvider for DefaultTimeProvider {
fn now(&self) -> SystemTime {
SystemTime::now()
}
}
#[cfg(test)]
mod tests {
use std::{fmt, sync::MutexGuard};
use async_trait::async_trait;
use matrix_sdk_common::locks::Mutex;
use matrix_sdk_test::async_test;
use ruma::{
events::room::MediaSource,
mxc_uri,
time::{Duration, SystemTime},
MxcUri, OwnedMxcUri,
};
use super::{EventCacheStoreMedia, IgnoreMediaRetentionPolicy, MediaService, TimeProvider};
use crate::{
event_cache::store::{media::MediaRetentionPolicy, EventCacheStoreError},
media::{MediaFormat, MediaRequestParameters, UniqueKey},
};
#[derive(Debug, Default)]
struct MockEventCacheStoreMedia {
inner: Mutex<MockEventCacheStoreMediaInner>,
}
impl MockEventCacheStoreMedia {
/// Whether the store was accessed.
fn accessed(&self) -> bool {
self.inner.lock().accessed
}
/// Reset the `accessed` boolean.
fn reset_accessed(&self) {
self.inner.lock().accessed = false;
}
/// Access the inner store.
///
/// Should be called for every access to the inner store as it also sets
/// the `accessed` boolean.
fn inner(&self) -> MutexGuard<'_, MockEventCacheStoreMediaInner> {
let mut inner = self.inner.lock();
inner.accessed = true;
inner
}
}
#[derive(Debug, Default)]
struct MockEventCacheStoreMediaInner {
/// Whether this store was accessed.
///
/// Must be set to `true` for any operation that unlocks the store.
accessed: bool,
/// The persisted media retention policy.
media_retention_policy: Option<MediaRetentionPolicy>,
/// The list of media content.
media_list: Vec<MediaContent>,
/// The time of the last cleanup.
cleanup_time: Option<SystemTime>,
}
#[derive(Debug, Clone)]
struct MediaContent {
/// The unique key for the media content.
key: String,
/// The original URI of the media content.
uri: OwnedMxcUri,
/// The media content.
content: Vec<u8>,
/// Whether the `MediaRetentionPolicy` should be ignored for this media
/// content;
ignore_policy: bool,
/// The time of the last access of the media content.
last_access: SystemTime,
}
#[derive(Debug)]
struct MockEventCacheStoreMediaError;
impl fmt::Display for MockEventCacheStoreMediaError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MockEventCacheStoreMediaError")
}
}
impl std::error::Error for MockEventCacheStoreMediaError {}
impl From<MockEventCacheStoreMediaError> for EventCacheStoreError {
fn from(value: MockEventCacheStoreMediaError) -> Self {
Self::backend(value)
}
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl EventCacheStoreMedia for MockEventCacheStoreMedia {
type Error = MockEventCacheStoreMediaError;
async fn media_retention_policy_inner(
&self,
) -> Result<Option<MediaRetentionPolicy>, Self::Error> {
Ok(self.inner().media_retention_policy)
}
async fn set_media_retention_policy_inner(
&self,
policy: MediaRetentionPolicy,
) -> Result<(), Self::Error> {
self.inner().media_retention_policy = Some(policy);
Ok(())
}
async fn add_media_content_inner(
&self,
request: &MediaRequestParameters,
content: Vec<u8>,
current_time: SystemTime,
policy: MediaRetentionPolicy,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error> {
let ignore_policy = ignore_policy.is_yes();
if !ignore_policy && policy.exceeds_max_file_size(content.len()) {
return Ok(());
}
let mut inner = self.inner();
let key = request.unique_key();
if let Some(pos) = inner.media_list.iter().position(|content| content.key == key) {
let media_content = &mut inner.media_list[pos];
media_content.content = content;
media_content.last_access = current_time;
media_content.ignore_policy = ignore_policy;
} else {
inner.media_list.push(MediaContent {
key,
uri: request.uri().to_owned(),
content,
ignore_policy,
last_access: current_time,
});
}
Ok(())
}
async fn set_ignore_media_retention_policy_inner(
&self,
request: &MediaRequestParameters,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error> {
let key = request.unique_key();
let mut inner = self.inner();
if let Some(pos) = inner.media_list.iter().position(|content| content.key == key) {
inner.media_list[pos].ignore_policy = ignore_policy.is_yes();
}
Ok(())
}
async fn get_media_content_inner(
&self,
request: &MediaRequestParameters,
current_time: SystemTime,
) -> Result<Option<Vec<u8>>, Self::Error> {
let key = request.unique_key();
let mut inner = self.inner();
let Some(media_content) =
inner.media_list.iter_mut().find(|content| content.key == key)
else {
return Ok(None);
};
media_content.last_access = current_time;
Ok(Some(media_content.content.clone()))
}
async fn get_media_content_for_uri_inner(
&self,
uri: &MxcUri,
current_time: SystemTime,
) -> Result<Option<Vec<u8>>, Self::Error> {
let mut inner = self.inner();
let Some(media_content) =
inner.media_list.iter_mut().find(|content| content.uri == uri)
else {
return Ok(None);
};
media_content.last_access = current_time;
Ok(Some(media_content.content.clone()))
}
async fn clean_up_media_cache_inner(
&self,
_policy: MediaRetentionPolicy,
current_time: SystemTime,
) -> Result<(), Self::Error> {
// This is mostly a noop. We don't care about this test implementation, only
// whether this method was called with the right time.
self.inner().cleanup_time = Some(current_time);
Ok(())
}
}
#[derive(Debug)]
struct MockTimeProvider {
now: Mutex<SystemTime>,
}
impl MockTimeProvider {
/// Construct a `MockTimeProvider` with the given current time.
fn new(now: SystemTime) -> Self {
Self { now: Mutex::new(now) }
}
/// Set the current time.
fn set_now(&self, now: SystemTime) {
*self.now.lock() = now;
}
}
impl TimeProvider for MockTimeProvider {
fn now(&self) -> SystemTime {
*self.now.lock()
}
}
#[async_test]
async fn test_media_service_empty_policy() {
let content = b"some text content";
let uri = mxc_uri!("mxc://server.local/AbcDe1234");
let request = MediaRequestParameters {
source: MediaSource::Plain(uri.to_owned()),
format: MediaFormat::File,
};
let now = SystemTime::UNIX_EPOCH;
let store = MockEventCacheStoreMedia::default();
let service = MediaService::with_time_provider(MockTimeProvider::new(now));
// By default an empty policy is used.
assert!(!service.media_retention_policy().has_limitations());
service.restore(None);
assert!(!service.media_retention_policy().has_limitations());
assert!(!store.accessed());
// Add media.
service
.add_media_content(&store, &request, content.to_vec(), IgnoreMediaRetentionPolicy::No)
.await
.unwrap();
assert!(store.accessed());
let media_content = store.inner().media_list[0].clone();
assert_eq!(media_content.uri, uri);
assert_eq!(media_content.content, content);
assert!(!media_content.ignore_policy);
assert_eq!(media_content.last_access, now);
let now = now + Duration::from_secs(60);
service.time_provider.set_now(now);
store.reset_accessed();
// Get media from request.
let loaded_content = service.get_media_content(&store, &request).await.unwrap();
assert!(store.accessed());
assert_eq!(loaded_content.as_deref(), Some(content.as_slice()));
// The last access time was updated.
let media = store.inner().media_list[0].clone();
assert_eq!(media.last_access, now);
let now = now + Duration::from_secs(60);
service.time_provider.set_now(now);
store.reset_accessed();
// Get media from URI.
let loaded_content = service.get_media_content_for_uri(&store, uri).await.unwrap();
assert!(store.accessed());
assert_eq!(loaded_content.as_deref(), Some(content.as_slice()));
// The last access time was updated.
let media = store.inner().media_list[0].clone();
assert_eq!(media.last_access, now);
// Update ignore_policy.
service
.set_ignore_media_retention_policy(&store, &request, IgnoreMediaRetentionPolicy::Yes)
.await
.unwrap();
assert!(store.accessed());
let media_content = store.inner().media_list[0].clone();
assert!(media_content.ignore_policy);
// Try a cleanup. With the empty policy the store should not be accessed.
assert_eq!(store.inner().cleanup_time, None);
store.reset_accessed();
service.clean_up_media_cache(&store).await.unwrap();
assert!(!store.accessed());
assert_eq!(store.inner().cleanup_time, None);
}
#[async_test]
async fn test_media_service_non_empty_policy() {
// Content of less than 32 bytes.
let small_content = b"some text content";
let small_uri = mxc_uri!("mxc://server.local/small");
let small_request = MediaRequestParameters {
source: MediaSource::Plain(small_uri.to_owned()),
format: MediaFormat::File,
};
// Content of more than 32 bytes.
let big_content = b"some much much larger text content";
let big_uri = mxc_uri!("mxc://server.local/big");
let big_request = MediaRequestParameters {
source: MediaSource::Plain(big_uri.to_owned()),
format: MediaFormat::File,
};
// Limit the file size to 32 bytes in the retention policy.
let policy = MediaRetentionPolicy { max_file_size: Some(32), ..Default::default() };
let now = SystemTime::UNIX_EPOCH;
let store = MockEventCacheStoreMedia::default();
let service = MediaService::with_time_provider(MockTimeProvider::new(now));
// Check that restoring the policy works.
service.restore(Some(MediaRetentionPolicy::default()));
assert_eq!(service.media_retention_policy(), MediaRetentionPolicy::default());
assert!(!store.accessed());
// Set the media retention policy.
service.set_media_retention_policy(&store, policy).await.unwrap();
assert!(store.accessed());
assert_eq!(service.media_retention_policy(), policy);
assert_eq!(store.inner().media_retention_policy, Some(policy));
store.reset_accessed();
// Add small media, it should work because its size is lower than the max file
// size.
service
.add_media_content(
&store,
&small_request,
small_content.to_vec(),
IgnoreMediaRetentionPolicy::No,
)
.await
.unwrap();
assert!(store.accessed());
let media_content = store.inner().media_list[0].clone();
assert_eq!(media_content.uri, small_uri);
assert_eq!(media_content.content, small_content);
assert!(!media_content.ignore_policy);
assert_eq!(media_content.last_access, now);
let now = now + Duration::from_secs(60);
service.time_provider.set_now(now);
store.reset_accessed();
// Get media from request.
let loaded_content = service.get_media_content(&store, &small_request).await.unwrap();
assert!(store.accessed());
assert_eq!(loaded_content.as_deref(), Some(small_content.as_slice()));
// The last access time was updated.
let media = store.inner().media_list[0].clone();
assert_eq!(media.last_access, now);
let now = now + Duration::from_secs(60);
service.time_provider.set_now(now);
store.reset_accessed();
// Get media from URI.
let loaded_content = service.get_media_content_for_uri(&store, small_uri).await.unwrap();
assert!(store.accessed());
assert_eq!(loaded_content.as_deref(), Some(small_content.as_slice()));
// The last access time was updated.
let media = store.inner().media_list[0].clone();
assert_eq!(media.last_access, now);
let now = now + Duration::from_secs(60);
service.time_provider.set_now(now);
store.reset_accessed();
// Add big media, it will not work because it is bigger than the max file size.
service
.add_media_content(
&store,
&big_request,
big_content.to_vec(),
IgnoreMediaRetentionPolicy::No,
)
.await
.unwrap();
assert!(!store.accessed());
assert_eq!(store.inner().media_list.len(), 1);
store.reset_accessed();
let loaded_content = service.get_media_content(&store, &big_request).await.unwrap();
assert!(store.accessed());
assert_eq!(loaded_content, None);
store.reset_accessed();
let loaded_content = service.get_media_content_for_uri(&store, big_uri).await.unwrap();
assert!(store.accessed());
assert_eq!(loaded_content, None);
// Add big media, but this time ignore the policy.
service
.add_media_content(
&store,
&big_request,
big_content.to_vec(),
IgnoreMediaRetentionPolicy::Yes,
)
.await
.unwrap();
assert!(store.accessed());
assert_eq!(store.inner().media_list.len(), 2);
store.reset_accessed();
// Get media from request.
let loaded_content = service.get_media_content(&store, &big_request).await.unwrap();
assert!(store.accessed());
assert_eq!(loaded_content.as_deref(), Some(big_content.as_slice()));
// The last access time was updated.
let media = store.inner().media_list[1].clone();
assert_eq!(media.last_access, now);
let now = now + Duration::from_secs(60);
service.time_provider.set_now(now);
store.reset_accessed();
// Get media from URI.
let loaded_content = service.get_media_content_for_uri(&store, big_uri).await.unwrap();
assert!(store.accessed());
assert_eq!(loaded_content.as_deref(), Some(big_content.as_slice()));
// The last access time was updated.
let media = store.inner().media_list[1].clone();
assert_eq!(media.last_access, now);
// Try a cleanup, the store should be accessed.
assert_eq!(store.inner().cleanup_time, None);
let now = now + Duration::from_secs(60);
service.time_provider.set_now(now);
store.reset_accessed();
service.clean_up_media_cache(&store).await.unwrap();
assert!(store.accessed());
assert_eq!(store.inner().cleanup_time, Some(now));
}
}
@@ -0,0 +1,28 @@
// Copyright 2025 Kévin Commaille
//
// 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.
//! Types and traits regarding media caching of the event cache store.
mod media_retention_policy;
mod media_service;
#[cfg(any(test, feature = "testing"))]
#[macro_use]
pub mod integration_tests;
#[cfg(any(test, feature = "testing"))]
pub use self::integration_tests::EventCacheStoreMediaIntegrationTests;
pub use self::{
media_retention_policy::MediaRetentionPolicy,
media_service::{EventCacheStoreMedia, IgnoreMediaRetentionPolicy, MediaService},
};
@@ -12,16 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use std::{collections::HashMap, num::NonZeroUsize, sync::RwLock as StdRwLock, time::Instant};
use std::{collections::HashMap, num::NonZeroUsize, sync::RwLock as StdRwLock};
use async_trait::async_trait;
use matrix_sdk_common::{
ring_buffer::RingBuffer, store_locks::memory_store_helper::try_take_leased_lock,
linked_chunk::{relational::RelationalLinkedChunk, RawChunk, Update},
ring_buffer::RingBuffer,
store_locks::memory_store_helper::try_take_leased_lock,
};
use ruma::{
time::{Instant, SystemTime},
MxcUri, OwnedMxcUri, RoomId,
};
use ruma::{MxcUri, OwnedMxcUri};
use super::{EventCacheStore, EventCacheStoreError, Result};
use crate::media::{MediaRequestParameters, UniqueKey as _};
use super::{
media::{EventCacheStoreMedia, IgnoreMediaRetentionPolicy, MediaRetentionPolicy, MediaService},
EventCacheStore, EventCacheStoreError, Result,
};
use crate::{
event_cache::{Event, Gap},
media::{MediaRequestParameters, UniqueKey as _},
};
/// In-memory, non-persistent implementation of the `EventCacheStore`.
///
@@ -29,8 +40,35 @@ use crate::media::{MediaRequestParameters, UniqueKey as _};
#[allow(clippy::type_complexity)]
#[derive(Debug)]
pub struct MemoryStore {
media: StdRwLock<RingBuffer<(OwnedMxcUri, String /* unique key */, Vec<u8>)>>,
leases: StdRwLock<HashMap<String, (String, Instant)>>,
inner: StdRwLock<MemoryStoreInner>,
media_service: MediaService,
}
#[derive(Debug)]
struct MemoryStoreInner {
media: RingBuffer<MediaContent>,
leases: HashMap<String, (String, Instant)>,
events: RelationalLinkedChunk<Event, Gap>,
media_retention_policy: Option<MediaRetentionPolicy>,
}
/// A media content in the `MemoryStore`.
#[derive(Debug)]
struct MediaContent {
/// The URI of the content.
uri: OwnedMxcUri,
/// The unique key of the content.
key: String,
/// The bytes of the content.
data: Vec<u8>,
/// Whether we should ignore the [`MediaRetentionPolicy`] for this content.
ignore_policy: bool,
/// The time of the last access of the content.
last_access: SystemTime,
}
// SAFETY: `new_unchecked` is safe because 20 is not zero.
@@ -39,8 +77,14 @@ const NUMBER_OF_MEDIAS: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(20)
impl Default for MemoryStore {
fn default() -> Self {
Self {
media: StdRwLock::new(RingBuffer::new(NUMBER_OF_MEDIAS)),
leases: Default::default(),
inner: StdRwLock::new(MemoryStoreInner {
media: RingBuffer::new(NUMBER_OF_MEDIAS),
leases: Default::default(),
events: RelationalLinkedChunk::new(),
media_retention_policy: None,
}),
// No need to call `restore()` since nothing is persisted.
media_service: MediaService::new(),
}
}
}
@@ -63,20 +107,45 @@ impl EventCacheStore for MemoryStore {
key: &str,
holder: &str,
) -> Result<bool, Self::Error> {
Ok(try_take_leased_lock(&self.leases, lease_duration_ms, key, holder))
let mut inner = self.inner.write().unwrap();
Ok(try_take_leased_lock(&mut inner.leases, lease_duration_ms, key, holder))
}
async fn handle_linked_chunk_updates(
&self,
room_id: &RoomId,
updates: Vec<Update<Event, Gap>>,
) -> Result<(), Self::Error> {
let mut inner = self.inner.write().unwrap();
inner.events.apply_updates(room_id, updates);
Ok(())
}
async fn reload_linked_chunk(
&self,
room_id: &RoomId,
) -> Result<Vec<RawChunk<Event, Gap>>, Self::Error> {
let inner = self.inner.read().unwrap();
inner
.events
.reload_chunks(room_id)
.map_err(|err| EventCacheStoreError::InvalidData { details: err })
}
async fn clear_all_rooms_chunks(&self) -> Result<(), Self::Error> {
self.inner.write().unwrap().events.clear();
Ok(())
}
async fn add_media_content(
&self,
request: &MediaRequestParameters,
data: Vec<u8>,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<()> {
// Avoid duplication. Let's try to remove it first.
self.remove_media_content(request).await?;
// Now, let's add it.
self.media.write().unwrap().push((request.uri().to_owned(), request.unique_key(), data));
Ok(())
self.media_service.add_media_content(self, request, data, ignore_policy).await
}
async fn replace_media_key(
@@ -86,54 +155,280 @@ impl EventCacheStore for MemoryStore {
) -> Result<(), Self::Error> {
let expected_key = from.unique_key();
let mut medias = self.media.write().unwrap();
if let Some((mxc, key, _)) = medias.iter_mut().find(|(_, key, _)| *key == expected_key) {
*mxc = to.uri().to_owned();
*key = to.unique_key();
let mut inner = self.inner.write().unwrap();
if let Some(media_content) =
inner.media.iter_mut().find(|media_content| media_content.key == expected_key)
{
media_content.uri = to.uri().to_owned();
media_content.key = to.unique_key();
}
Ok(())
}
async fn get_media_content(&self, request: &MediaRequestParameters) -> Result<Option<Vec<u8>>> {
let expected_key = request.unique_key();
let media = self.media.read().unwrap();
Ok(media.iter().find_map(|(_media_uri, media_key, media_content)| {
(media_key == &expected_key).then(|| media_content.to_owned())
}))
self.media_service.get_media_content(self, request).await
}
async fn remove_media_content(&self, request: &MediaRequestParameters) -> Result<()> {
let expected_key = request.unique_key();
let mut media = self.media.write().unwrap();
let Some(index) = media
.iter()
.position(|(_media_uri, media_key, _media_content)| media_key == &expected_key)
let mut inner = self.inner.write().unwrap();
let Some(index) =
inner.media.iter().position(|media_content| media_content.key == expected_key)
else {
return Ok(());
};
media.remove(index);
inner.media.remove(index);
Ok(())
}
async fn get_media_content_for_uri(
&self,
uri: &MxcUri,
) -> Result<Option<Vec<u8>>, Self::Error> {
self.media_service.get_media_content_for_uri(self, uri).await
}
async fn remove_media_content_for_uri(&self, uri: &MxcUri) -> Result<()> {
let mut media = self.media.write().unwrap();
let expected_key = uri.to_owned();
let positions = media
let mut inner = self.inner.write().unwrap();
let positions = inner
.media
.iter()
.enumerate()
.filter_map(|(position, (media_uri, _media_key, _media_content))| {
(media_uri == &expected_key).then_some(position)
})
.filter_map(|(position, media_content)| (media_content.uri == uri).then_some(position))
.collect::<Vec<_>>();
// Iterate in reverse-order so that positions stay valid after first removals.
for position in positions.into_iter().rev() {
media.remove(position);
inner.media.remove(position);
}
Ok(())
}
async fn set_media_retention_policy(
&self,
policy: MediaRetentionPolicy,
) -> Result<(), Self::Error> {
self.media_service.set_media_retention_policy(self, policy).await
}
fn media_retention_policy(&self) -> MediaRetentionPolicy {
self.media_service.media_retention_policy()
}
async fn set_ignore_media_retention_policy(
&self,
request: &MediaRequestParameters,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error> {
self.media_service.set_ignore_media_retention_policy(self, request, ignore_policy).await
}
async fn clean_up_media_cache(&self) -> Result<(), Self::Error> {
self.media_service.clean_up_media_cache(self).await
}
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl EventCacheStoreMedia for MemoryStore {
type Error = EventCacheStoreError;
async fn media_retention_policy_inner(
&self,
) -> Result<Option<MediaRetentionPolicy>, Self::Error> {
Ok(self.inner.read().unwrap().media_retention_policy)
}
async fn set_media_retention_policy_inner(
&self,
policy: MediaRetentionPolicy,
) -> Result<(), Self::Error> {
self.inner.write().unwrap().media_retention_policy = Some(policy);
Ok(())
}
async fn add_media_content_inner(
&self,
request: &MediaRequestParameters,
data: Vec<u8>,
last_access: SystemTime,
policy: MediaRetentionPolicy,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error> {
// Avoid duplication. Let's try to remove it first.
self.remove_media_content(request).await?;
let ignore_policy = ignore_policy.is_yes();
if !ignore_policy && policy.exceeds_max_file_size(data.len()) {
// Do not store it.
return Ok(());
};
// Now, let's add it.
let mut inner = self.inner.write().unwrap();
inner.media.push(MediaContent {
uri: request.uri().to_owned(),
key: request.unique_key(),
data,
ignore_policy,
last_access,
});
Ok(())
}
async fn set_ignore_media_retention_policy_inner(
&self,
request: &MediaRequestParameters,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error> {
let mut inner = self.inner.write().unwrap();
let expected_key = request.unique_key();
if let Some(media_content) = inner.media.iter_mut().find(|media| media.key == expected_key)
{
media_content.ignore_policy = ignore_policy.is_yes();
}
Ok(())
}
async fn get_media_content_inner(
&self,
request: &MediaRequestParameters,
current_time: SystemTime,
) -> Result<Option<Vec<u8>>, Self::Error> {
let mut inner = self.inner.write().unwrap();
let expected_key = request.unique_key();
// First get the content out of the buffer, we are going to put it back at the
// end.
let Some(index) = inner.media.iter().position(|media| media.key == expected_key) else {
return Ok(None);
};
let Some(mut content) = inner.media.remove(index) else {
return Ok(None);
};
// Clone the data.
let data = content.data.clone();
// Update the last access time.
content.last_access = current_time;
// Put it back in the buffer.
inner.media.push(content);
Ok(Some(data))
}
async fn get_media_content_for_uri_inner(
&self,
expected_uri: &MxcUri,
current_time: SystemTime,
) -> Result<Option<Vec<u8>>, Self::Error> {
let mut inner = self.inner.write().unwrap();
// First get the content out of the buffer, we are going to put it back at the
// end.
let Some(index) = inner.media.iter().position(|media| media.uri == expected_uri) else {
return Ok(None);
};
let Some(mut content) = inner.media.remove(index) else {
return Ok(None);
};
// Clone the data.
let data = content.data.clone();
// Update the last access time.
content.last_access = current_time;
// Put it back in the buffer.
inner.media.push(content);
Ok(Some(data))
}
async fn clean_up_media_cache_inner(
&self,
policy: MediaRetentionPolicy,
current_time: SystemTime,
) -> Result<(), Self::Error> {
if !policy.has_limitations() {
// We can safely skip all the checks.
return Ok(());
}
let mut inner = self.inner.write().unwrap();
// First, check media content that exceed the max filesize.
if policy.computed_max_file_size().is_some() {
inner.media.retain(|content| {
content.ignore_policy || !policy.exceeds_max_file_size(content.data.len())
});
}
// Then, clean up expired media content.
if policy.last_access_expiry.is_some() {
inner.media.retain(|content| {
content.ignore_policy
|| !policy.has_content_expired(current_time, content.last_access)
});
}
// Finally, if the cache size is too big, remove old items until it fits.
if let Some(max_cache_size) = policy.max_cache_size {
// Reverse the iterator because in case the cache size is overflowing, we want
// to count the number of old items to remove. Items are sorted by last access
// and old items are at the start.
let (_, items_to_remove) = inner.media.iter().enumerate().rev().fold(
(0usize, Vec::with_capacity(NUMBER_OF_MEDIAS.into())),
|(mut cache_size, mut items_to_remove), (index, content)| {
if content.ignore_policy {
// Do not count it.
return (cache_size, items_to_remove);
}
let remove_item = if items_to_remove.is_empty() {
// We have not reached the max cache size yet.
if let Some(sum) = cache_size.checked_add(content.data.len()) {
cache_size = sum;
// Start removing items if we have exceeded the max cache size.
cache_size > max_cache_size
} else {
// The cache size is overflowing, remove the remaining items, since the
// max cache size cannot be bigger than
// usize::MAX.
true
}
} else {
// We have reached the max cache size already, just remove it.
true
};
if remove_item {
items_to_remove.push(index);
}
(cache_size, items_to_remove)
},
);
// The indexes are already in reverse order so we can just iterate in that order
// to remove them starting by the end.
for index in items_to_remove {
inner.media.remove(index);
}
}
Ok(())
@@ -142,12 +437,14 @@ impl EventCacheStore for MemoryStore {
#[cfg(test)]
mod tests {
use super::{EventCacheStore, MemoryStore, Result};
use super::{MemoryStore, Result};
use crate::event_cache_store_media_integration_tests;
async fn get_event_cache_store() -> Result<impl EventCacheStore> {
async fn get_event_cache_store() -> Result<MemoryStore> {
Ok(MemoryStore::new())
}
event_cache_store_integration_tests!();
event_cache_store_integration_tests_time!();
event_cache_store_media_integration_tests!(with_media_size_tests);
}
@@ -24,6 +24,7 @@ use std::{fmt, ops::Deref, str::Utf8Error, sync::Arc};
#[cfg(any(test, feature = "testing"))]
#[macro_use]
pub mod integration_tests;
pub mod media;
mod memory_store;
mod traits;
@@ -36,14 +37,14 @@ pub use matrix_sdk_store_encryption::Error as StoreEncryptionError;
pub use self::integration_tests::EventCacheStoreIntegrationTests;
pub use self::{
memory_store::MemoryStore,
traits::{DynEventCacheStore, EventCacheStore, IntoEventCacheStore},
traits::{DynEventCacheStore, EventCacheStore, IntoEventCacheStore, DEFAULT_CHUNK_CAPACITY},
};
/// The high-level public type to represent an `EventCacheStore` lock.
#[derive(Clone)]
pub struct EventCacheStoreLock {
/// The inner cross process lock that is used to lock the `EventCacheStore`.
cross_process_lock: CrossProcessStoreLock<LockableEventCacheStore>,
cross_process_lock: Arc<CrossProcessStoreLock<LockableEventCacheStore>>,
/// The store itself.
///
@@ -70,11 +71,11 @@ impl EventCacheStoreLock {
let store = store.into_event_cache_store();
Self {
cross_process_lock: CrossProcessStoreLock::new(
cross_process_lock: Arc::new(CrossProcessStoreLock::new(
LockableEventCacheStore(store.clone()),
"default".to_owned(),
holder,
),
)),
store,
}
}
@@ -100,13 +101,13 @@ pub struct EventCacheStoreLockGuard<'a> {
}
#[cfg(not(tarpaulin_include))]
impl<'a> fmt::Debug for EventCacheStoreLockGuard<'a> {
impl fmt::Debug for EventCacheStoreLockGuard<'_> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.debug_struct("EventCacheStoreLockGuard").finish_non_exhaustive()
}
}
impl<'a> Deref for EventCacheStoreLockGuard<'a> {
impl Deref for EventCacheStoreLockGuard<'_> {
type Target = DynEventCacheStore;
fn deref(&self) -> &Self::Target {
@@ -138,12 +139,23 @@ pub enum EventCacheStoreError {
#[error("Error encoding or decoding data from the event cache store: {0}")]
Codec(#[from] Utf8Error),
/// The store failed to serialize or deserialize some data.
#[error("Error serializing or deserializing data from the event cache store: {0}")]
Serialization(#[from] serde_json::Error),
/// The database format has changed in a backwards incompatible way.
#[error(
"The database format of the event cache store changed in an incompatible way, \
current version: {0}, latest version: {1}"
)]
UnsupportedDatabaseVersion(usize, usize),
/// The store contains invalid data.
#[error("The store contains invalid data: {details}")]
InvalidData {
/// Details why the data contained in the store was invalid.
details: String,
},
}
impl EventCacheStoreError {
@@ -15,11 +15,25 @@
use std::{fmt, sync::Arc};
use async_trait::async_trait;
use matrix_sdk_common::AsyncTraitDeps;
use ruma::MxcUri;
use matrix_sdk_common::{
linked_chunk::{RawChunk, Update},
AsyncTraitDeps,
};
use ruma::{MxcUri, RoomId};
use super::EventCacheStoreError;
use crate::media::MediaRequestParameters;
use super::{
media::{IgnoreMediaRetentionPolicy, MediaRetentionPolicy},
EventCacheStoreError,
};
use crate::{
event_cache::{Event, Gap},
media::MediaRequestParameters,
};
/// A default capacity for linked chunks, when manipulating in conjunction with
/// an `EventCacheStore` implementation.
// TODO: move back?
pub const DEFAULT_CHUNK_CAPACITY: usize = 128;
/// An abstract trait that can be used to implement different store backends
/// for the event cache of the SDK.
@@ -37,6 +51,35 @@ pub trait EventCacheStore: AsyncTraitDeps {
holder: &str,
) -> Result<bool, Self::Error>;
/// An [`Update`] reflects an operation that has happened inside a linked
/// chunk. The linked chunk is used by the event cache to store the events
/// in-memory. This method aims at forwarding this update inside this store.
async fn handle_linked_chunk_updates(
&self,
room_id: &RoomId,
updates: Vec<Update<Event, Gap>>,
) -> Result<(), Self::Error>;
/// Remove all data tied to a given room from the cache.
async fn remove_room(&self, room_id: &RoomId) -> Result<(), Self::Error> {
// Right now, this means removing all the linked chunk. If implementations
// override this behavior, they should *also* include this code.
self.handle_linked_chunk_updates(room_id, vec![Update::Clear]).await
}
/// Return all the raw components of a linked chunk, so the caller may
/// reconstruct the linked chunk later.
async fn reload_linked_chunk(
&self,
room_id: &RoomId,
) -> Result<Vec<RawChunk<Event, Gap>>, Self::Error>;
/// Clear persisted events for all the rooms.
///
/// This will empty and remove all the linked chunks stored previously,
/// using the above [`Self::handle_linked_chunk_updates`] methods.
async fn clear_all_rooms_chunks(&self) -> Result<(), Self::Error>;
/// Add a media file's content in the media store.
///
/// # Arguments
@@ -48,6 +91,7 @@ pub trait EventCacheStore: AsyncTraitDeps {
&self,
request: &MediaRequestParameters,
content: Vec<u8>,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error>;
/// Replaces the given media's content key with another one.
@@ -95,6 +139,23 @@ pub trait EventCacheStore: AsyncTraitDeps {
request: &MediaRequestParameters,
) -> Result<(), Self::Error>;
/// Get a media file's content associated to an `MxcUri` from the
/// media store.
///
/// In theory, there could be several files stored using the same URI and a
/// different `MediaFormat`. This API is meant to be used with a media file
/// that has only been stored with a single format.
///
/// If there are several media files for a given URI in different formats,
/// this API will only return one of them. Which one is left as an
/// implementation detail.
///
/// # Arguments
///
/// * `uri` - The `MxcUri` of the media file.
async fn get_media_content_for_uri(&self, uri: &MxcUri)
-> Result<Option<Vec<u8>>, Self::Error>;
/// Remove all the media files' content associated to an `MxcUri` from the
/// media store.
///
@@ -105,6 +166,42 @@ pub trait EventCacheStore: AsyncTraitDeps {
///
/// * `uri` - The `MxcUri` of the media files.
async fn remove_media_content_for_uri(&self, uri: &MxcUri) -> Result<(), Self::Error>;
/// Set the `MediaRetentionPolicy` to use for deciding whether to store or
/// keep media content.
///
/// # Arguments
///
/// * `policy` - The `MediaRetentionPolicy` to use.
async fn set_media_retention_policy(
&self,
policy: MediaRetentionPolicy,
) -> Result<(), Self::Error>;
/// Get the current `MediaRetentionPolicy`.
fn media_retention_policy(&self) -> MediaRetentionPolicy;
/// Set whether the current [`MediaRetentionPolicy`] should be ignored for
/// the media.
///
/// The change will be taken into account in the next cleanup.
///
/// # Arguments
///
/// * `request` - The `MediaRequestParameters` of the file.
///
/// * `ignore_policy` - Whether the current `MediaRetentionPolicy` should be
/// ignored.
async fn set_ignore_media_retention_policy(
&self,
request: &MediaRequestParameters,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error>;
/// Clean up the media cache with the current `MediaRetentionPolicy`.
///
/// If there is already an ongoing cleanup, this is a noop.
async fn clean_up_media_cache(&self) -> Result<(), Self::Error>;
}
#[repr(transparent)]
@@ -131,12 +228,32 @@ impl<T: EventCacheStore> EventCacheStore for EraseEventCacheStoreError<T> {
self.0.try_take_leased_lock(lease_duration_ms, key, holder).await.map_err(Into::into)
}
async fn handle_linked_chunk_updates(
&self,
room_id: &RoomId,
updates: Vec<Update<Event, Gap>>,
) -> Result<(), Self::Error> {
self.0.handle_linked_chunk_updates(room_id, updates).await.map_err(Into::into)
}
async fn reload_linked_chunk(
&self,
room_id: &RoomId,
) -> Result<Vec<RawChunk<Event, Gap>>, Self::Error> {
self.0.reload_linked_chunk(room_id).await.map_err(Into::into)
}
async fn clear_all_rooms_chunks(&self) -> Result<(), Self::Error> {
self.0.clear_all_rooms_chunks().await.map_err(Into::into)
}
async fn add_media_content(
&self,
request: &MediaRequestParameters,
content: Vec<u8>,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error> {
self.0.add_media_content(request, content).await.map_err(Into::into)
self.0.add_media_content(request, content, ignore_policy).await.map_err(Into::into)
}
async fn replace_media_key(
@@ -161,9 +278,39 @@ impl<T: EventCacheStore> EventCacheStore for EraseEventCacheStoreError<T> {
self.0.remove_media_content(request).await.map_err(Into::into)
}
async fn get_media_content_for_uri(
&self,
uri: &MxcUri,
) -> Result<Option<Vec<u8>>, Self::Error> {
self.0.get_media_content_for_uri(uri).await.map_err(Into::into)
}
async fn remove_media_content_for_uri(&self, uri: &MxcUri) -> Result<(), Self::Error> {
self.0.remove_media_content_for_uri(uri).await.map_err(Into::into)
}
async fn set_media_retention_policy(
&self,
policy: MediaRetentionPolicy,
) -> Result<(), Self::Error> {
self.0.set_media_retention_policy(policy).await.map_err(Into::into)
}
fn media_retention_policy(&self) -> MediaRetentionPolicy {
self.0.media_retention_policy()
}
async fn set_ignore_media_retention_policy(
&self,
request: &MediaRequestParameters,
ignore_policy: IgnoreMediaRetentionPolicy,
) -> Result<(), Self::Error> {
self.0.set_ignore_media_retention_policy(request, ignore_policy).await.map_err(Into::into)
}
async fn clean_up_media_cache(&self) -> Result<(), Self::Error> {
self.0.clean_up_media_cache().await.map_err(Into::into)
}
}
/// A type-erased [`EventCacheStore`].
+44 -31
View File
@@ -1,28 +1,24 @@
//! Utilities for working with events to decide whether they are suitable for
//! use as a [crate::Room::latest_event].
#![cfg(any(feature = "e2e-encryption", feature = "experimental-sliding-sync"))]
use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
use matrix_sdk_common::deserialized_responses::TimelineEvent;
#[cfg(feature = "e2e-encryption")]
use ruma::events::{
call::{invite::SyncCallInviteEvent, notify::SyncCallNotifyEvent},
poll::unstable_start::SyncUnstablePollStartEvent,
relation::RelationType,
room::message::SyncRoomMessageEvent,
AnySyncMessageLikeEvent, AnySyncTimelineEvent,
};
use ruma::{
events::{
call::{invite::SyncCallInviteEvent, notify::SyncCallNotifyEvent},
poll::unstable_start::SyncUnstablePollStartEvent,
relation::RelationType,
room::{
member::{MembershipState, SyncRoomMemberEvent},
message::SyncRoomMessageEvent,
power_levels::RoomPowerLevels,
},
sticker::SyncStickerEvent,
AnySyncStateEvent,
AnySyncMessageLikeEvent, AnySyncStateEvent, AnySyncTimelineEvent,
},
MxcUri, OwnedEventId, UserId,
UserId,
};
use ruma::{MxcUri, OwnedEventId};
use serde::{Deserialize, Serialize};
use crate::MinimalRoomMemberEvent;
@@ -74,7 +70,7 @@ pub fn is_suitable_for_latest_event<'a>(
// Check if this is a replacement for another message. If it is, ignore it
if let Some(original_message) = message.as_original() {
let is_replacement =
original_message.content.relates_to.as_ref().map_or(false, |relates_to| {
original_message.content.relates_to.as_ref().is_some_and(|relates_to| {
if let Some(relation_type) = relates_to.rel_type() {
relation_type == RelationType::Replacement
} else {
@@ -83,12 +79,13 @@ pub fn is_suitable_for_latest_event<'a>(
});
if is_replacement {
return PossibleLatestEvent::NoUnsupportedMessageLikeType;
PossibleLatestEvent::NoUnsupportedMessageLikeType
} else {
PossibleLatestEvent::YesRoomMessage(message)
}
return PossibleLatestEvent::YesRoomMessage(message);
} else {
PossibleLatestEvent::YesRoomMessage(message)
}
return PossibleLatestEvent::YesRoomMessage(message);
}
AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::UnstablePollStart(poll)) => {
@@ -167,7 +164,7 @@ pub fn is_suitable_for_latest_event<'a>(
#[derive(Clone, Debug, Serialize)]
pub struct LatestEvent {
/// The actual event.
event: SyncTimelineEvent,
event: TimelineEvent,
/// The member profile of the event' sender.
#[serde(skip_serializing_if = "Option::is_none")]
@@ -181,7 +178,7 @@ pub struct LatestEvent {
#[derive(Deserialize)]
struct SerializedLatestEvent {
/// The actual event.
event: SyncTimelineEvent,
event: TimelineEvent,
/// The member profile of the event' sender.
#[serde(skip_serializing_if = "Option::is_none")]
@@ -214,7 +211,7 @@ impl<'de> Deserialize<'de> for LatestEvent {
Err(err) => variant_errors.push(err),
}
match serde_json::from_str::<SyncTimelineEvent>(raw.get()) {
match serde_json::from_str::<TimelineEvent>(raw.get()) {
Ok(value) => {
return Ok(LatestEvent {
event: value,
@@ -233,13 +230,13 @@ impl<'de> Deserialize<'de> for LatestEvent {
impl LatestEvent {
/// Create a new [`LatestEvent`] without the sender's profile.
pub fn new(event: SyncTimelineEvent) -> Self {
pub fn new(event: TimelineEvent) -> Self {
Self { event, sender_profile: None, sender_name_is_ambiguous: None }
}
/// Create a new [`LatestEvent`] with maybe the sender's profile.
pub fn new_with_sender_details(
event: SyncTimelineEvent,
event: TimelineEvent,
sender_profile: Option<MinimalRoomMemberEvent>,
sender_name_is_ambiguous: Option<bool>,
) -> Self {
@@ -247,17 +244,17 @@ impl LatestEvent {
}
/// Transform [`Self`] into an event.
pub fn into_event(self) -> SyncTimelineEvent {
pub fn into_event(self) -> TimelineEvent {
self.event
}
/// Get a reference to the event.
pub fn event(&self) -> &SyncTimelineEvent {
pub fn event(&self) -> &TimelineEvent {
&self.event
}
/// Get a mutable reference to the event.
pub fn event_mut(&mut self) -> &mut SyncTimelineEvent {
pub fn event_mut(&mut self) -> &mut TimelineEvent {
&mut self.event
}
@@ -297,11 +294,16 @@ impl LatestEvent {
#[cfg(test)]
mod tests {
#[cfg(feature = "e2e-encryption")]
use std::collections::BTreeMap;
#[cfg(feature = "e2e-encryption")]
use assert_matches::assert_matches;
#[cfg(feature = "e2e-encryption")]
use assert_matches2::assert_let;
use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
use matrix_sdk_common::deserialized_responses::TimelineEvent;
use ruma::serde::Raw;
#[cfg(feature = "e2e-encryption")]
use ruma::{
events::{
call::{
@@ -339,14 +341,16 @@ mod tests {
RedactedSyncMessageLikeEvent, RedactedUnsigned, StateUnsigned, SyncMessageLikeEvent,
UnsignedRoomRedactionEvent,
},
owned_event_id, owned_mxc_uri, owned_user_id,
serde::Raw,
MilliSecondsSinceUnixEpoch, UInt, VoipVersionId,
owned_event_id, owned_mxc_uri, owned_user_id, MilliSecondsSinceUnixEpoch, UInt,
VoipVersionId,
};
use serde_json::json;
use crate::latest_event::{is_suitable_for_latest_event, LatestEvent, PossibleLatestEvent};
use super::LatestEvent;
#[cfg(feature = "e2e-encryption")]
use super::{is_suitable_for_latest_event, PossibleLatestEvent};
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_room_messages_are_suitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage(
@@ -371,6 +375,7 @@ mod tests {
assert_eq!(m.content.msgtype.msgtype(), "m.image");
}
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_polls_are_suitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::UnstablePollStart(
@@ -394,6 +399,7 @@ mod tests {
assert_eq!(m.content.poll_start().question.text, "do you like rust?");
}
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_call_invites_are_suitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::CallInvite(
@@ -416,6 +422,7 @@ mod tests {
);
}
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_call_notifications_are_suitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::CallNotify(
@@ -438,6 +445,7 @@ mod tests {
);
}
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_stickers_are_suitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::Sticker(
@@ -460,6 +468,7 @@ mod tests {
);
}
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_different_types_of_messagelike_are_unsuitable() {
let event =
@@ -482,6 +491,7 @@ mod tests {
);
}
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_redacted_messages_are_suitable() {
// Ruma does not allow constructing UnsignedRoomRedactionEvent instances.
@@ -510,6 +520,7 @@ mod tests {
);
}
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_encrypted_messages_are_unsuitable() {
let event = AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomEncrypted(
@@ -533,6 +544,7 @@ mod tests {
);
}
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_state_events_are_unsuitable() {
let event = AnySyncTimelineEvent::State(AnySyncStateEvent::RoomTopic(
@@ -552,6 +564,7 @@ mod tests {
);
}
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_replacement_events_are_unsuitable() {
let mut event_content = RoomMessageEventContent::text_plain("Bye bye, world!");
@@ -583,7 +596,7 @@ mod tests {
latest_event: LatestEvent,
}
let event = SyncTimelineEvent::new(
let event = TimelineEvent::new(
Raw::from_json_string(json!({ "event_id": "$1" }).to_string()).unwrap(),
);
+3 -4
View File
@@ -37,7 +37,6 @@ mod rooms;
pub mod read_receipts;
pub use read_receipts::PreviousEventsProvider;
#[cfg(feature = "experimental-sliding-sync")]
pub mod sliding_sync;
pub mod store;
@@ -56,9 +55,9 @@ pub use http;
pub use matrix_sdk_crypto as crypto;
pub use once_cell;
pub use rooms::{
Room, RoomCreateWithCreatorEventContent, RoomDisplayName, RoomHero, RoomInfo,
RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons, RoomMember, RoomMemberships, RoomState,
RoomStateFilter,
apply_redaction, Room, RoomCreateWithCreatorEventContent, RoomDisplayName, RoomHero, RoomInfo,
RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons, RoomMember, RoomMembersUpdate,
RoomMemberships, RoomState, RoomStateFilter,
};
pub use store::{
ComposerDraft, ComposerDraftType, QueueWedgeError, StateChanges, StateStore, StateStoreDataKey,
+190 -243
View File
@@ -123,7 +123,7 @@ use std::{
};
use eyeball_im::Vector;
use matrix_sdk_common::{deserialized_responses::SyncTimelineEvent, ring_buffer::RingBuffer};
use matrix_sdk_common::{deserialized_responses::TimelineEvent, ring_buffer::RingBuffer};
use ruma::{
events::{
poll::{start::PollStartEventContent, unstable_start::UnstablePollStartEventContent},
@@ -202,7 +202,7 @@ impl RoomReadReceipts {
///
/// Returns whether a new event triggered a new unread/notification/mention.
#[inline(always)]
fn process_event(&mut self, event: &SyncTimelineEvent, user_id: &UserId) {
fn process_event(&mut self, event: &TimelineEvent, user_id: &UserId) {
if marks_as_unread(event.raw(), user_id) {
self.num_unread += 1;
}
@@ -210,7 +210,11 @@ impl RoomReadReceipts {
let mut has_notify = false;
let mut has_mention = false;
for action in &event.push_actions {
let Some(actions) = event.push_actions.as_ref() else {
return;
};
for action in actions.iter() {
if !has_notify && action.should_notify() {
self.num_notifications += 1;
has_notify = true;
@@ -236,7 +240,7 @@ impl RoomReadReceipts {
&mut self,
receipt_event_id: &EventId,
user_id: &UserId,
events: impl IntoIterator<Item = &'a SyncTimelineEvent>,
events: impl IntoIterator<Item = &'a TimelineEvent>,
) -> bool {
let mut counting_receipts = false;
@@ -269,11 +273,11 @@ impl RoomReadReceipts {
pub trait PreviousEventsProvider: Send + Sync {
/// Returns the list of known timeline events, in sync order, for the given
/// room.
fn for_room(&self, room_id: &RoomId) -> Vector<SyncTimelineEvent>;
fn for_room(&self, room_id: &RoomId) -> Vector<TimelineEvent>;
}
impl PreviousEventsProvider for () {
fn for_room(&self, _: &RoomId) -> Vector<SyncTimelineEvent> {
fn for_room(&self, _: &RoomId) -> Vector<TimelineEvent> {
Vector::new()
}
}
@@ -292,7 +296,7 @@ struct ReceiptSelector {
impl ReceiptSelector {
fn new(
all_events: &Vector<SyncTimelineEvent>,
all_events: &Vector<TimelineEvent>,
latest_active_receipt_event: Option<&EventId>,
) -> Self {
let event_id_to_pos = Self::create_sync_index(all_events.iter());
@@ -310,7 +314,7 @@ impl ReceiptSelector {
/// Create a mapping of `event_id` -> sync order for all events that have an
/// `event_id`.
fn create_sync_index<'a>(
events: impl Iterator<Item = &'a SyncTimelineEvent> + 'a,
events: impl Iterator<Item = &'a TimelineEvent> + 'a,
) -> BTreeMap<OwnedEventId, usize> {
// TODO: this should be cached and incrementally updated.
BTreeMap::from_iter(
@@ -405,7 +409,7 @@ impl ReceiptSelector {
/// Try to match an implicit receipt, that is, the one we get for events we
/// sent ourselves.
#[instrument(skip_all)]
fn try_match_implicit(&mut self, user_id: &UserId, new_events: &[SyncTimelineEvent]) {
fn try_match_implicit(&mut self, user_id: &UserId, new_events: &[TimelineEvent]) {
for ev in new_events {
// Get the `sender` field, if any, or skip this event.
let Ok(Some(sender)) = ev.raw().get_field::<OwnedUserId>("sender") else { continue };
@@ -432,13 +436,13 @@ impl ReceiptSelector {
/// Returns true if there's an event common to both groups of events, based on
/// their event id.
fn events_intersects<'a>(
previous_events: impl Iterator<Item = &'a SyncTimelineEvent>,
new_events: &[SyncTimelineEvent],
previous_events: impl Iterator<Item = &'a TimelineEvent>,
new_events: &[TimelineEvent],
) -> bool {
let previous_events_ids = BTreeSet::from_iter(previous_events.filter_map(|ev| ev.event_id()));
new_events
.iter()
.any(|ev| ev.event_id().map_or(false, |event_id| previous_events_ids.contains(&event_id)))
.any(|ev| ev.event_id().is_some_and(|event_id| previous_events_ids.contains(&event_id)))
}
/// Given a set of events coming from sync, for a room, update the
@@ -454,8 +458,8 @@ pub(crate) fn compute_unread_counts(
user_id: &UserId,
room_id: &RoomId,
receipt_event: Option<&ReceiptEventContent>,
previous_events: Vector<SyncTimelineEvent>,
new_events: &[SyncTimelineEvent],
previous_events: Vector<TimelineEvent>,
new_events: &[TimelineEvent],
read_receipts: &mut RoomReadReceipts,
) {
debug!(?read_receipts, "Starting.");
@@ -620,11 +624,14 @@ mod tests {
use std::{num::NonZeroUsize, ops::Not as _};
use eyeball_im::Vector;
use matrix_sdk_common::{deserialized_responses::SyncTimelineEvent, ring_buffer::RingBuffer};
use matrix_sdk_test::{sync_timeline_event, EventBuilder};
use matrix_sdk_common::{deserialized_responses::TimelineEvent, ring_buffer::RingBuffer};
use matrix_sdk_test::event_factory::EventFactory;
use ruma::{
event_id,
events::receipt::{ReceiptThread, ReceiptType},
events::{
receipt::{ReceiptThread, ReceiptType},
room::{member::MembershipState, message::MessageType},
},
owned_event_id, owned_user_id,
push::Action,
room_id, user_id, EventId, UserId,
@@ -638,24 +645,14 @@ mod tests {
let user_id = user_id!("@alice:example.org");
let other_user_id = user_id!("@bob:example.org");
let f = EventFactory::new();
// A message from somebody else marks the room as unread...
let ev = sync_timeline_event!({
"sender": other_user_id,
"type": "m.room.message",
"event_id": "$ida",
"origin_server_ts": 12344446,
"content": { "body":"A", "msgtype": "m.text" },
});
let ev = f.text_msg("A").event_id(event_id!("$ida")).sender(other_user_id).into_raw_sync();
assert!(marks_as_unread(&ev, user_id));
// ... but a message from ourselves doesn't.
let ev = sync_timeline_event!({
"sender": user_id,
"type": "m.room.message",
"event_id": "$ida",
"origin_server_ts": 12344446,
"content": { "body":"A", "msgtype": "m.text" },
});
let ev = f.text_msg("A").event_id(event_id!("$ida")).sender(user_id).into_raw_sync();
assert!(marks_as_unread(&ev, user_id).not());
}
@@ -665,24 +662,16 @@ mod tests {
let other_user_id = user_id!("@bob:example.org");
// An edit to a message from somebody else doesn't mark the room as unread.
let ev = sync_timeline_event!({
"sender": other_user_id,
"type": "m.room.message",
"event_id": "$ida",
"origin_server_ts": 12344446,
"content": {
"body": " * edited message",
"m.new_content": {
"body": "edited message",
"msgtype": "m.text"
},
"m.relates_to": {
"event_id": "$someeventid:localhost",
"rel_type": "m.replace"
},
"msgtype": "m.text"
},
});
let ev = EventFactory::new()
.text_msg("* edited message")
.edit(
event_id!("$someeventid:localhost"),
MessageType::text_plain("edited message").into(),
)
.event_id(event_id!("$ida"))
.sender(other_user_id)
.into_raw_sync();
assert!(marks_as_unread(&ev, user_id).not());
}
@@ -692,19 +681,11 @@ mod tests {
let other_user_id = user_id!("@bob:example.org");
// A redact of a message from somebody else doesn't mark the room as unread.
let ev = sync_timeline_event!({
"content": {
"reason": "🛑"
},
"event_id": "$151957878228ssqrJ:localhost",
"origin_server_ts": 151957878000000_u64,
"sender": other_user_id,
"type": "m.room.redaction",
"redacts": "$151957878228ssqrj:localhost",
"unsigned": {
"age": 85
}
});
let ev = EventFactory::new()
.redaction(event_id!("$151957878228ssqrj:localhost"))
.sender(other_user_id)
.event_id(event_id!("$151957878228ssqrJ:localhost"))
.into_raw_sync();
assert!(marks_as_unread(&ev, user_id).not());
}
@@ -715,22 +696,11 @@ mod tests {
let other_user_id = user_id!("@bob:example.org");
// A reaction from somebody else to a message doesn't mark the room as unread.
let ev = sync_timeline_event!({
"content": {
"m.relates_to": {
"event_id": "$15275047031IXQRi:localhost",
"key": "👍",
"rel_type": "m.annotation"
}
},
"event_id": "$15275047031IXQRi:localhost",
"origin_server_ts": 159027581000000_u64,
"sender": other_user_id,
"type": "m.reaction",
"unsigned": {
"age": 85
}
});
let ev = EventFactory::new()
.reaction(event_id!("$15275047031IXQRj:localhost"), "👍")
.sender(other_user_id)
.event_id(event_id!("$15275047031IXQRi:localhost"))
.into_raw_sync();
assert!(marks_as_unread(&ev, user_id).not());
}
@@ -739,18 +709,13 @@ mod tests {
fn test_state_event_doesnt_mark_as_unread() {
let user_id = user_id!("@alice:example.org");
let event_id = event_id!("$1");
let ev = sync_timeline_event!({
"content": {
"displayname": "Alice",
"membership": "join",
},
"event_id": event_id,
"origin_server_ts": 1432135524678u64,
"sender": user_id,
"state_key": user_id,
"type": "m.room.member",
});
let ev = EventFactory::new()
.member(user_id)
.membership(MembershipState::Join)
.display_name("Alice")
.event_id(event_id)
.into_raw_sync();
assert!(marks_as_unread(&ev, user_id).not());
let other_user_id = user_id!("@bob:example.org");
@@ -759,17 +724,14 @@ mod tests {
#[test]
fn test_count_unread_and_mentions() {
fn make_event(user_id: &UserId, push_actions: Vec<Action>) -> SyncTimelineEvent {
SyncTimelineEvent::new_with_push_actions(
sync_timeline_event!({
"sender": user_id,
"type": "m.room.message",
"event_id": "$ida",
"origin_server_ts": 12344446,
"content": { "body":"A", "msgtype": "m.text" },
}),
push_actions,
)
fn make_event(user_id: &UserId, push_actions: Vec<Action>) -> TimelineEvent {
let mut ev = EventFactory::new()
.text_msg("A")
.sender(user_id)
.event_id(event_id!("$ida"))
.into_event();
ev.push_actions = Some(push_actions);
ev
}
let user_id = user_id!("@alice:example.org");
@@ -843,14 +805,12 @@ mod tests {
// When provided with one event, that's not the receipt event, we don't count
// it.
fn make_event(event_id: &EventId) -> SyncTimelineEvent {
SyncTimelineEvent::new(sync_timeline_event!({
"sender": "@bob:example.org",
"type": "m.room.message",
"event_id": event_id,
"origin_server_ts": 12344446,
"content": { "body":"A", "msgtype": "m.text" },
}))
fn make_event(event_id: &EventId) -> TimelineEvent {
EventFactory::new()
.text_msg("A")
.sender(user_id!("@bob:example.org"))
.event_id(event_id)
.into()
}
let mut receipts = RoomReadReceipts {
@@ -948,20 +908,6 @@ mod tests {
assert_eq!(receipts.num_mentions, 0);
}
fn sync_timeline_message(
sender: &UserId,
event_id: impl serde::Serialize,
body: impl serde::Serialize,
) -> SyncTimelineEvent {
SyncTimelineEvent::new(sync_timeline_event!({
"sender": sender,
"type": "m.room.message",
"event_id": event_id,
"origin_server_ts": 42,
"content": { "body": body, "msgtype": "m.text" },
}))
}
/// Smoke test for `compute_unread_counts`.
#[test]
fn test_basic_compute_unread_counts() {
@@ -972,15 +918,14 @@ mod tests {
let mut previous_events = Vector::new();
let ev1 = sync_timeline_message(other_user_id, receipt_event_id, "A");
let ev2 = sync_timeline_message(other_user_id, "$2", "A");
let f = EventFactory::new();
let ev1 = f.text_msg("A").sender(other_user_id).event_id(receipt_event_id).into_event();
let ev2 = f.text_msg("A").sender(other_user_id).event_id(event_id!("$2")).into_event();
let receipt_event = EventBuilder::new().make_receipt_event_content([(
receipt_event_id.to_owned(),
ReceiptType::Read,
user_id.to_owned(),
ReceiptThread::Unthreaded,
)]);
let receipt_event = f
.read_receipts()
.add(receipt_event_id, user_id, ReceiptType::Read, ReceiptThread::Unthreaded)
.build();
let mut read_receipts = Default::default();
compute_unread_counts(
@@ -999,7 +944,8 @@ mod tests {
previous_events.push_back(ev1);
previous_events.push_back(ev2);
let new_event = sync_timeline_message(other_user_id, "$3", "A");
let new_event =
f.text_msg("A").sender(other_user_id).event_id(event_id!("$3")).into_event();
compute_unread_counts(
user_id,
room_id,
@@ -1013,13 +959,14 @@ mod tests {
assert_eq!(read_receipts.num_unread, 2);
}
fn make_test_events(user_id: &UserId) -> Vector<SyncTimelineEvent> {
let ev1 = sync_timeline_message(user_id, "$1", "With the lights out, it's less dangerous");
let ev2 = sync_timeline_message(user_id, "$2", "Here we are now, entertain us");
let ev3 = sync_timeline_message(user_id, "$3", "I feel stupid and contagious");
let ev4 = sync_timeline_message(user_id, "$4", "Here we are now, entertain us");
let ev5 = sync_timeline_message(user_id, "$5", "Hello, hello, hello, how low?");
vec![ev1, ev2, ev3, ev4, ev5].into()
fn make_test_events(user_id: &UserId) -> Vector<TimelineEvent> {
let f = EventFactory::new().sender(user_id);
let ev1 = f.text_msg("With the lights out, it's less dangerous").event_id(event_id!("$1"));
let ev2 = f.text_msg("Here we are now, entertain us").event_id(event_id!("$2"));
let ev3 = f.text_msg("I feel stupid and contagious").event_id(event_id!("$3"));
let ev4 = f.text_msg("Here we are now, entertain us").event_id(event_id!("$4"));
let ev5 = f.text_msg("Hello, hello, hello, how low?").event_id(event_id!("$5"));
[ev1, ev2, ev3, ev4, ev5].into_iter().map(Into::into).collect()
}
/// Test that when multiple receipts come in a single event, we can still
@@ -1035,30 +982,32 @@ mod tests {
// Given a receipt event marking events 1-3 as read using a combination of
// different thread and privacy types,
let f = EventFactory::new();
for receipt_type_1 in &[ReceiptType::Read, ReceiptType::ReadPrivate] {
for receipt_thread_1 in &[ReceiptThread::Unthreaded, ReceiptThread::Main] {
for receipt_type_2 in &[ReceiptType::Read, ReceiptType::ReadPrivate] {
for receipt_thread_2 in &[ReceiptThread::Unthreaded, ReceiptThread::Main] {
let receipt_event = EventBuilder::new().make_receipt_event_content([
(
owned_event_id!("$2"),
let receipt_event = f
.read_receipts()
.add(
event_id!("$2"),
user_id,
receipt_type_1.clone(),
user_id.to_owned(),
receipt_thread_1.clone(),
),
(
owned_event_id!("$3"),
)
.add(
event_id!("$3"),
user_id,
receipt_type_2.clone(),
user_id.to_owned(),
receipt_thread_2.clone(),
),
(
owned_event_id!("$1"),
)
.add(
event_id!("$1"),
user_id,
receipt_type_1.clone(),
user_id.to_owned(),
receipt_thread_2.clone(),
),
]);
)
.build();
// When I compute the notifications for this room (with no new events),
let mut read_receipts = RoomReadReceipts::default();
@@ -1118,12 +1067,10 @@ mod tests {
let events = make_test_events(user_id!("@bob:example.org"));
let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$6"),
ReceiptType::Read,
user_id.clone(),
ReceiptThread::Unthreaded,
)]);
let receipt_event = EventFactory::new()
.read_receipts()
.add(event_id!("$6"), &user_id, ReceiptType::Read, ReceiptThread::Unthreaded)
.build();
let mut read_receipts = RoomReadReceipts::default();
assert!(read_receipts.pending.is_empty());
@@ -1154,12 +1101,10 @@ mod tests {
let events = make_test_events(user_id!("@bob:example.org"));
let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$1"),
ReceiptType::Read,
user_id.clone(),
ReceiptThread::Unthreaded,
)]);
let receipt_event = EventFactory::new()
.read_receipts()
.add(event_id!("$1"), &user_id, ReceiptType::Read, ReceiptThread::Unthreaded)
.build();
// Sync with a read receipt *and* a single event that was already known: in that
// case, only consider the new events in isolation, and compute the
@@ -1190,12 +1135,7 @@ mod tests {
let events = make_test_events(uid);
// An event with no id.
let ev6 = SyncTimelineEvent::new(sync_timeline_event!({
"sender": uid,
"type": "m.room.message",
"origin_server_ts": 42,
"content": { "body": "yolo", "msgtype": "m.text" },
}));
let ev6 = EventFactory::new().text_msg("yolo").sender(uid).no_event_id().into_event();
let index = ReceiptSelector::create_sync_index(events.iter().chain(&[ev6]));
@@ -1261,8 +1201,9 @@ mod tests {
#[test]
fn test_receipt_selector_handle_pending_receipts_noop() {
let sender = user_id!("@bob:example.org");
let ev1 = sync_timeline_message(sender, event_id!("$1"), "yo");
let ev2 = sync_timeline_message(sender, event_id!("$2"), "well?");
let f = EventFactory::new().sender(sender);
let ev1 = f.text_msg("yo").event_id(event_id!("$1")).into_event();
let ev2 = f.text_msg("well?").event_id(event_id!("$2")).into_event();
let events: Vector<_> = vec![ev1, ev2].into();
{
@@ -1296,8 +1237,9 @@ mod tests {
#[test]
fn test_receipt_selector_handle_pending_receipts_doesnt_match_known_events() {
let sender = user_id!("@bob:example.org");
let ev1 = sync_timeline_message(sender, event_id!("$1"), "yo");
let ev2 = sync_timeline_message(sender, event_id!("$2"), "well?");
let f = EventFactory::new().sender(sender);
let ev1 = f.text_msg("yo").event_id(event_id!("$1")).into_event();
let ev2 = f.text_msg("well?").event_id(event_id!("$2")).into_event();
let events: Vector<_> = vec![ev1, ev2].into();
{
@@ -1332,8 +1274,9 @@ mod tests {
#[test]
fn test_receipt_selector_handle_pending_receipts_matches_known_events_no_initial() {
let sender = user_id!("@bob:example.org");
let ev1 = sync_timeline_message(sender, event_id!("$1"), "yo");
let ev2 = sync_timeline_message(sender, event_id!("$2"), "well?");
let f = EventFactory::new().sender(sender);
let ev1 = f.text_msg("yo").event_id(event_id!("$1")).into_event();
let ev2 = f.text_msg("well?").event_id(event_id!("$2")).into_event();
let events: Vector<_> = vec![ev1, ev2].into();
{
@@ -1373,8 +1316,9 @@ mod tests {
#[test]
fn test_receipt_selector_handle_pending_receipts_matches_known_events_with_initial() {
let sender = user_id!("@bob:example.org");
let ev1 = sync_timeline_message(sender, event_id!("$1"), "yo");
let ev2 = sync_timeline_message(sender, event_id!("$2"), "well?");
let f = EventFactory::new().sender(sender);
let ev1 = f.text_msg("yo").event_id(event_id!("$1")).into_event();
let ev2 = f.text_msg("well?").event_id(event_id!("$2")).into_event();
let events: Vector<_> = vec![ev1, ev2].into();
{
@@ -1412,21 +1356,25 @@ mod tests {
#[test]
fn test_receipt_selector_handle_new_receipt() {
let myself = owned_user_id!("@alice:example.org");
let myself = user_id!("@alice:example.org");
let events = make_test_events(user_id!("@bob:example.org"));
let f = EventFactory::new();
{
// Thread receipts are ignored.
let mut selector = ReceiptSelector::new(&events, None);
let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$5"),
ReceiptType::Read,
myself.clone(),
ReceiptThread::Thread(owned_event_id!("$2")),
)]);
let receipt_event = f
.read_receipts()
.add(
event_id!("$5"),
myself,
ReceiptType::Read,
ReceiptThread::Thread(owned_event_id!("$2")),
)
.build();
let pending = selector.handle_new_receipt(&myself, &receipt_event);
let pending = selector.handle_new_receipt(myself, &receipt_event);
assert!(pending.is_empty());
let best_receipt = selector.select();
@@ -1440,14 +1388,12 @@ mod tests {
// receipt.
let mut selector = ReceiptSelector::new(&events, None);
let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$6"),
receipt_type.clone(),
myself.clone(),
receipt_thread.clone(),
)]);
let receipt_event = f
.read_receipts()
.add(event_id!("$6"), myself, receipt_type.clone(), receipt_thread.clone())
.build();
let pending = selector.handle_new_receipt(&myself, &receipt_event);
let pending = selector.handle_new_receipt(myself, &receipt_event);
assert_eq!(pending[0], event_id!("$6"));
assert_eq!(pending.len(), 1);
@@ -1460,14 +1406,12 @@ mod tests {
// receipt.
let mut selector = ReceiptSelector::new(&events, None);
let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$3"),
receipt_type.clone(),
myself.clone(),
receipt_thread.clone(),
)]);
let receipt_event = f
.read_receipts()
.add(event_id!("$3"), myself, receipt_type.clone(), receipt_thread.clone())
.build();
let pending = selector.handle_new_receipt(&myself, &receipt_event);
let pending = selector.handle_new_receipt(myself, &receipt_event);
assert!(pending.is_empty());
let best_receipt = selector.select();
@@ -1479,14 +1423,12 @@ mod tests {
// better receipt.
let mut selector = ReceiptSelector::new(&events, Some(event_id!("$4")));
let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$3"),
receipt_type.clone(),
myself.clone(),
receipt_thread.clone(),
)]);
let receipt_event = f
.read_receipts()
.add(event_id!("$3"), myself, receipt_type.clone(), receipt_thread.clone())
.build();
let pending = selector.handle_new_receipt(&myself, &receipt_event);
let pending = selector.handle_new_receipt(myself, &receipt_event);
assert!(pending.is_empty());
let best_receipt = selector.select();
@@ -1498,14 +1440,12 @@ mod tests {
// new better receipt.
let mut selector = ReceiptSelector::new(&events, Some(event_id!("$2")));
let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$3"),
receipt_type.clone(),
myself.clone(),
receipt_thread.clone(),
)]);
let receipt_event = f
.read_receipts()
.add(event_id!("$3"), myself, receipt_type.clone(), receipt_thread.clone())
.build();
let pending = selector.handle_new_receipt(&myself, &receipt_event);
let pending = selector.handle_new_receipt(myself, &receipt_event);
assert!(pending.is_empty());
let best_receipt = selector.select();
@@ -1519,23 +1459,14 @@ mod tests {
// new better receipt.
let mut selector = ReceiptSelector::new(&events, Some(event_id!("$2")));
let receipt_event = EventBuilder::new().make_receipt_event_content([
(
owned_event_id!("$4"),
ReceiptType::ReadPrivate,
myself.clone(),
ReceiptThread::Unthreaded,
),
(
owned_event_id!("$6"),
ReceiptType::ReadPrivate,
myself.clone(),
ReceiptThread::Main,
),
(owned_event_id!("$3"), ReceiptType::Read, myself.clone(), ReceiptThread::Main),
]);
let receipt_event = f
.read_receipts()
.add(event_id!("$4"), myself, ReceiptType::ReadPrivate, ReceiptThread::Unthreaded)
.add(event_id!("$6"), myself, ReceiptType::ReadPrivate, ReceiptThread::Main)
.add(event_id!("$3"), myself, ReceiptType::Read, ReceiptThread::Main)
.build();
let pending = selector.handle_new_receipt(&myself, &receipt_event);
let pending = selector.handle_new_receipt(myself, &receipt_event);
assert_eq!(pending.len(), 1);
assert_eq!(pending[0], event_id!("$6"));
@@ -1560,8 +1491,16 @@ mod tests {
assert!(best_receipt.is_none());
// Now, if there are events I've written too...
events.push_back(sync_timeline_message(&myself, "$6", "A mulatto, an albino"));
events.push_back(sync_timeline_message(bob, "$7", "A mosquito, my libido"));
let f = EventFactory::new();
events.push_back(
f.text_msg("A mulatto, an albino")
.sender(&myself)
.event_id(event_id!("$6"))
.into_event(),
);
events.push_back(
f.text_msg("A mosquito, my libido").sender(bob).event_id(event_id!("$7")).into_event(),
);
let mut selector = ReceiptSelector::new(&events, None);
// And I search for my implicit read receipt,
@@ -1573,7 +1512,7 @@ mod tests {
#[test]
fn test_compute_unread_counts_with_implicit_receipt() {
let user_id = owned_user_id!("@alice:example.org");
let user_id = user_id!("@alice:example.org");
let bob = user_id!("@bob:example.org");
let room_id = room_id!("!room:example.org");
@@ -1581,28 +1520,36 @@ mod tests {
let mut events = make_test_events(bob);
// One by me,
events.push_back(sync_timeline_message(&user_id, "$6", "A mulatto, an albino"));
let f = EventFactory::new();
events.push_back(
f.text_msg("A mulatto, an albino")
.sender(user_id)
.event_id(event_id!("$6"))
.into_event(),
);
// And others by Bob,
events.push_back(sync_timeline_message(bob, "$7", "A mosquito, my libido"));
events.push_back(sync_timeline_message(bob, "$8", "A denial, a denial"));
events.push_back(
f.text_msg("A mosquito, my libido").sender(bob).event_id(event_id!("$7")).into_event(),
);
events.push_back(
f.text_msg("A denial, a denial").sender(bob).event_id(event_id!("$8")).into_event(),
);
let events: Vec<_> = events.into_iter().collect();
// I have a read receipt attached to one of Bob's event sent before my message,
let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$3"),
ReceiptType::Read,
user_id.clone(),
ReceiptThread::Unthreaded,
)]);
let receipt_event = f
.read_receipts()
.add(event_id!("$3"), user_id, ReceiptType::Read, ReceiptThread::Unthreaded)
.build();
let mut read_receipts = RoomReadReceipts::default();
// And I compute the unread counts for all those new events (no previous events
// in that room),
compute_unread_counts(
&user_id,
user_id,
room_id,
Some(&receipt_event),
Vector::new(),
@@ -18,9 +18,11 @@ use std::{
};
use ruma::{
events::{AnyGlobalAccountDataEvent, GlobalAccountDataEventType},
events::{
direct::OwnedDirectUserIdentifier, AnyGlobalAccountDataEvent, GlobalAccountDataEventType,
},
serde::Raw,
OwnedUserId, RoomId,
RoomId,
};
use tracing::{debug, instrument, trace, warn};
@@ -94,10 +96,10 @@ impl AccountDataProcessor {
for event in events {
let AnyGlobalAccountDataEvent::Direct(direct_event) = event else { continue };
let mut new_dms = HashMap::<&RoomId, HashSet<OwnedUserId>>::new();
for (user_id, rooms) in direct_event.content.iter() {
let mut new_dms = HashMap::<&RoomId, HashSet<OwnedDirectUserIdentifier>>::new();
for (user_identifier, rooms) in direct_event.content.iter() {
for room_id in rooms {
new_dms.entry(room_id).or_default().insert(user_id.clone());
new_dms.entry(room_id).or_default().insert(user_identifier.clone());
}
}
+5 -4
View File
@@ -1,4 +1,4 @@
#![allow(clippy::assign_op_pattern)] // triggered by bitflags! usage
#![allow(clippy::assign_op_pattern)] // Triggered by bitflags! usage
mod members;
pub(crate) mod normal;
@@ -12,8 +12,8 @@ use std::{
use bitflags::bitflags;
pub use members::RoomMember;
pub use normal::{
Room, RoomHero, RoomInfo, RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons, RoomState,
RoomStateFilter,
apply_redaction, Room, RoomHero, RoomInfo, RoomInfoNotableUpdate, RoomInfoNotableUpdateReasons,
RoomMembersUpdate, RoomState, RoomStateFilter,
};
use regex::Regex;
use ruma::{
@@ -21,6 +21,7 @@ use ruma::{
events::{
beacon_info::BeaconInfoEventContent,
call::member::{CallMemberEventContent, CallMemberStateKey},
direct::OwnedDirectUserIdentifier,
macros::EventContent,
room::{
avatar::RoomAvatarEventContent,
@@ -127,7 +128,7 @@ pub struct BaseRoomInfo {
pub(crate) create: Option<MinimalStateEvent<RoomCreateWithCreatorEventContent>>,
/// A list of user ids this room is considered as direct message, if this
/// room is a DM.
pub(crate) dm_targets: HashSet<OwnedUserId>,
pub(crate) dm_targets: HashSet<OwnedDirectUserIdentifier>,
/// The `m.room.encryption` event content that enabled E2EE in this room.
pub(crate) encryption: Option<RoomEncryptionEventContent>,
/// The guest access policy of this room.
File diff suppressed because it is too large Load Diff
+79 -47
View File
@@ -21,27 +21,21 @@ use std::ops::Deref;
use std::{borrow::Cow, collections::BTreeMap};
#[cfg(feature = "e2e-encryption")]
use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
#[cfg(feature = "e2e-encryption")]
use ruma::api::client::sync::sync_events::v5;
#[cfg(feature = "e2e-encryption")]
use ruma::events::AnyToDeviceEvent;
use matrix_sdk_common::deserialized_responses::TimelineEvent;
use ruma::{
api::client::sync::sync_events::v3::{self, InvitedRoom, KnockedRoom},
events::{
room::member::MembershipState, AnyRoomAccountDataEvent, AnyStrippedStateEvent,
AnySyncStateEvent, StateEventType,
AnySyncStateEvent,
},
serde::Raw,
JsOption, OwnedRoomId, RoomId, UInt, UserId,
};
#[cfg(feature = "e2e-encryption")]
use ruma::{api::client::sync::sync_events::v5, events::AnyToDeviceEvent, events::StateEventType};
use tracing::{debug, error, instrument, trace, warn};
use super::BaseClient;
#[cfg(feature = "e2e-encryption")]
use crate::latest_event::{is_suitable_for_latest_event, LatestEvent, PossibleLatestEvent};
#[cfg(feature = "e2e-encryption")]
use crate::RoomMemberships;
use crate::{
error::Result,
read_receipts::{compute_unread_counts, PreviousEventsProvider},
@@ -55,6 +49,11 @@ use crate::{
sync::{JoinedRoomUpdate, LeftRoomUpdate, Notification, RoomUpdates, SyncResponse},
Room, RoomInfo,
};
#[cfg(feature = "e2e-encryption")]
use crate::{
latest_event::{is_suitable_for_latest_event, LatestEvent, PossibleLatestEvent},
RoomMemberships,
};
impl BaseClient {
#[cfg(feature = "e2e-encryption")]
@@ -269,7 +268,7 @@ impl BaseClient {
.or_insert_with(JoinedRoomUpdate::default)
.account_data
.append(&mut raw.to_vec()),
RoomState::Left => new_rooms
RoomState::Left | RoomState::Banned => new_rooms
.leave
.entry(room_id.to_owned())
.or_insert_with(LeftRoomUpdate::default)
@@ -546,7 +545,7 @@ impl BaseClient {
))
}
RoomState::Left => Ok((
RoomState::Left | RoomState::Banned => Ok((
room_info,
None,
Some(LeftRoomUpdate::new(
@@ -691,7 +690,7 @@ impl BaseClient {
async fn cache_latest_events(
room: &Room,
room_info: &mut RoomInfo,
events: &[SyncTimelineEvent],
events: &[TimelineEvent],
changes: Option<&StateChanges>,
store: Option<&Store>,
) {
@@ -894,16 +893,17 @@ fn process_room_properties(
}
}
#[cfg(test)]
#[cfg(all(test, not(target_family = "wasm")))]
mod tests {
use std::{
collections::{BTreeMap, HashSet},
sync::{Arc, RwLock as SyncRwLock},
};
use std::collections::{BTreeMap, HashSet};
#[cfg(feature = "e2e-encryption")]
use std::sync::{Arc, RwLock as SyncRwLock};
use assert_matches::assert_matches;
use matrix_sdk_common::deserialized_responses::TimelineEvent;
#[cfg(feature = "e2e-encryption")]
use matrix_sdk_common::{
deserialized_responses::{SyncTimelineEvent, UnableToDecryptInfo, UnableToDecryptReason},
deserialized_responses::{UnableToDecryptInfo, UnableToDecryptReason},
ring_buffer::RingBuffer,
};
use matrix_sdk_test::async_test;
@@ -911,7 +911,7 @@ mod tests {
api::client::sync::sync_events::UnreadNotificationsCount,
assign, event_id,
events::{
direct::DirectEventContent,
direct::{DirectEventContent, DirectUserIdentifier, OwnedDirectUserIdentifier},
room::{
avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent,
@@ -929,13 +929,16 @@ mod tests {
};
use serde_json::json;
use super::{cache_latest_events, http};
#[cfg(feature = "e2e-encryption")]
use super::cache_latest_events;
use super::http;
use crate::{
rooms::normal::{RoomHero, RoomInfoNotableUpdateReasons},
store::MemoryStore,
test_utils::logged_in_base_client,
BaseClient, Room, RoomInfoNotableUpdate, RoomState,
BaseClient, RoomInfoNotableUpdate, RoomState,
};
#[cfg(feature = "e2e-encryption")]
use crate::{store::MemoryStore, Room};
#[async_test]
async fn test_notification_count_set() {
@@ -1247,7 +1250,7 @@ mod tests {
room.required_state.push(make_state_event(
user_b_id,
user_a_id.as_str(),
RoomMemberEventContent::new(membership),
RoomMemberEventContent::new(membership.clone()),
None,
));
let response = response_with_room(room_id, room);
@@ -1256,8 +1259,17 @@ mod tests {
.await
.expect("Failed to process sync");
// The room is left.
assert_eq!(client.get_room(room_id).unwrap().state(), RoomState::Left);
match membership {
MembershipState::Leave => {
// The room is left.
assert_eq!(client.get_room(room_id).unwrap().state(), RoomState::Left);
}
MembershipState::Ban => {
// The room is banned.
assert_eq!(client.get_room(room_id).unwrap().state(), RoomState::Banned);
}
_ => panic!("Unexpected membership state found: {membership}"),
}
// And it is added to the list of left rooms only.
assert!(!sync_resp.rooms.join.contains_key(room_id));
@@ -1337,7 +1349,7 @@ mod tests {
create_dm(&client, room_id, user_a_id, user_b_id, MembershipState::Join).await;
// (Sanity: B is a direct target, and is in Join state)
assert!(direct_targets(&client, room_id).contains(user_b_id));
assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id)));
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Join);
// When B leaves
@@ -1346,7 +1358,7 @@ mod tests {
// Then B is still a direct target, and is in Leave state (B is a direct target
// because we want to return to our old DM in the UI even if the other
// user left, so we can reinvite them. See https://github.com/matrix-org/matrix-rust-sdk/issues/2017)
assert!(direct_targets(&client, room_id).contains(user_b_id));
assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id)));
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Leave);
}
@@ -1362,7 +1374,7 @@ mod tests {
create_dm(&client, room_id, user_a_id, user_b_id, MembershipState::Invite).await;
// (Sanity: B is a direct target, and is in Invite state)
assert!(direct_targets(&client, room_id).contains(user_b_id));
assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id)));
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Invite);
// When B declines the invitation (i.e. leaves)
@@ -1371,7 +1383,7 @@ mod tests {
// Then B is still a direct target, and is in Leave state (B is a direct target
// because we want to return to our old DM in the UI even if the other
// user left, so we can reinvite them. See https://github.com/matrix-org/matrix-rust-sdk/issues/2017)
assert!(direct_targets(&client, room_id).contains(user_b_id));
assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id)));
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Leave);
}
@@ -1389,7 +1401,7 @@ mod tests {
assert_eq!(membership(&client, room_id, user_a_id).await, MembershipState::Join);
// (Sanity: B is a direct target, and is in Join state)
assert!(direct_targets(&client, room_id).contains(user_b_id));
assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id)));
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Join);
let room = client.get_room(room_id).unwrap();
@@ -1413,7 +1425,7 @@ mod tests {
assert_eq!(membership(&client, room_id, user_a_id).await, MembershipState::Join);
// (Sanity: B is a direct target, and is in Join state)
assert!(direct_targets(&client, room_id).contains(user_b_id));
assert!(direct_targets(&client, room_id).contains(<&DirectUserIdentifier>::from(user_b_id)));
assert_eq!(membership(&client, room_id, user_b_id).await, MembershipState::Invite);
let room = client.get_room(room_id).unwrap();
@@ -1930,6 +1942,7 @@ mod tests {
);
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_when_no_events_we_dont_cache_any() {
let events = &[];
@@ -1937,6 +1950,7 @@ mod tests {
assert!(chosen.is_none());
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_when_only_one_event_we_cache_it() {
let event1 = make_event("m.room.message", "$1");
@@ -1945,6 +1959,7 @@ mod tests {
assert_eq!(ev_id(chosen), rawev_id(event1));
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_with_multiple_events_we_cache_the_last_one() {
let event1 = make_event("m.room.message", "$1");
@@ -1954,6 +1969,7 @@ mod tests {
assert_eq!(ev_id(chosen), rawev_id(event2));
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_cache_the_latest_relevant_event_and_ignore_irrelevant_ones_even_if_later() {
let event1 = make_event("m.room.message", "$1");
@@ -1965,6 +1981,7 @@ mod tests {
assert_eq!(ev_id(chosen), rawev_id(event2));
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_prefer_to_cache_nothing_rather_than_irrelevant_events() {
let event1 = make_event("m.room.power_levels", "$1");
@@ -1973,6 +1990,7 @@ mod tests {
assert!(chosen.is_none());
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_cache_encrypted_events_that_are_after_latest_message() {
// Given two message events followed by two encrypted
@@ -2003,6 +2021,7 @@ mod tests {
assert_eq!(rawevs_ids(&room.latest_encrypted_events), evs_ids(&[event3, event4]));
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_dont_cache_encrypted_events_that_are_before_latest_message() {
// Given an encrypted event before and after the message
@@ -2027,6 +2046,7 @@ mod tests {
assert_eq!(rawevs_ids(&room.latest_encrypted_events), evs_ids(&[event3]));
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_skip_irrelevant_events_eg_receipts_even_if_after_message() {
// Given two message events followed by two encrypted, with a receipt in the
@@ -2054,6 +2074,7 @@ mod tests {
assert_eq!(rawevs_ids(&room.latest_encrypted_events), evs_ids(&[event3, event5]));
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_only_store_the_max_number_of_encrypted_events() {
// Given two message events followed by lots of encrypted and other irrelevant
@@ -2112,6 +2133,7 @@ mod tests {
);
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_dont_overflow_capacity_if_previous_encrypted_events_exist() {
// Given a RoomInfo with lots of encrypted events already inside it
@@ -2154,6 +2176,7 @@ mod tests {
assert_eq!(rawevs_ids(&room.latest_encrypted_events)[9], "$a");
}
#[cfg(feature = "e2e-encryption")]
#[async_test]
async fn test_existing_encrypted_events_are_deleted_if_we_receive_unencrypted() {
// Given a RoomInfo with some encrypted events already inside it
@@ -2558,9 +2581,10 @@ mod tests {
let mut room_response = http::response::Room::new();
set_room_joined(&mut room_response, user_a_id);
let mut response = response_with_room(room_id_1, room_response);
let mut direct_content = BTreeMap::new();
direct_content.insert(user_a_id.to_owned(), vec![room_id_1.to_owned()]);
direct_content.insert(user_b_id.to_owned(), vec![room_id_2.to_owned()]);
let mut direct_content: BTreeMap<OwnedDirectUserIdentifier, Vec<OwnedRoomId>> =
BTreeMap::new();
direct_content.insert(user_a_id.into(), vec![room_id_1.to_owned()]);
direct_content.insert(user_b_id.into(), vec![room_id_2.to_owned()]);
response
.extensions
.account_data
@@ -2581,7 +2605,8 @@ mod tests {
assert!(room_2.is_direct().await.unwrap());
}
async fn choose_event_to_cache(events: &[SyncTimelineEvent]) -> Option<SyncTimelineEvent> {
#[cfg(feature = "e2e-encryption")]
async fn choose_event_to_cache(events: &[TimelineEvent]) -> Option<TimelineEvent> {
let room = make_room();
let mut room_info = room.clone_info();
cache_latest_events(&room, &mut room_info, events, None, None).await;
@@ -2589,22 +2614,26 @@ mod tests {
room.latest_event().map(|latest_event| latest_event.event().clone())
}
fn rawev_id(event: SyncTimelineEvent) -> String {
#[cfg(feature = "e2e-encryption")]
fn rawev_id(event: TimelineEvent) -> String {
event.event_id().unwrap().to_string()
}
fn ev_id(event: Option<SyncTimelineEvent>) -> String {
fn ev_id(event: Option<TimelineEvent>) -> String {
event.unwrap().event_id().unwrap().to_string()
}
#[cfg(feature = "e2e-encryption")]
fn rawevs_ids(events: &Arc<SyncRwLock<RingBuffer<Raw<AnySyncTimelineEvent>>>>) -> Vec<String> {
events.read().unwrap().iter().map(|e| e.get_field("event_id").unwrap().unwrap()).collect()
}
fn evs_ids(events: &[SyncTimelineEvent]) -> Vec<String> {
#[cfg(feature = "e2e-encryption")]
fn evs_ids(events: &[TimelineEvent]) -> Vec<String> {
events.iter().map(|e| e.event_id().unwrap().to_string()).collect()
}
#[cfg(feature = "e2e-encryption")]
fn make_room() -> Room {
let (sender, _receiver) = tokio::sync::broadcast::channel(1);
@@ -2631,12 +2660,14 @@ mod tests {
.unwrap()
}
fn make_event(typ: &str, id: &str) -> SyncTimelineEvent {
SyncTimelineEvent::new(make_raw_event(typ, id))
#[cfg(feature = "e2e-encryption")]
fn make_event(typ: &str, id: &str) -> TimelineEvent {
TimelineEvent::new(make_raw_event(typ, id))
}
fn make_encrypted_event(id: &str) -> SyncTimelineEvent {
SyncTimelineEvent::new_utd_event(
#[cfg(feature = "e2e-encryption")]
fn make_encrypted_event(id: &str) -> TimelineEvent {
TimelineEvent::new_utd_event(
Raw::from_json_string(
json!({
"type": "m.room.encrypted",
@@ -2656,7 +2687,7 @@ mod tests {
.unwrap(),
UnableToDecryptInfo {
session_id: Some("".to_owned()),
reason: UnableToDecryptReason::MissingMegolmSession,
reason: UnableToDecryptReason::MissingMegolmSession { withheld_code: None },
},
)
}
@@ -2671,7 +2702,7 @@ mod tests {
member.membership().clone()
}
fn direct_targets(client: &BaseClient, room_id: &RoomId) -> HashSet<OwnedUserId> {
fn direct_targets(client: &BaseClient, room_id: &RoomId) -> HashSet<OwnedDirectUserIdentifier> {
let room = client.get_room(room_id).expect("Room not found!");
room.direct_targets()
}
@@ -2730,8 +2761,9 @@ mod tests {
user_id: OwnedUserId,
room_ids: Vec<OwnedRoomId>,
) {
let mut direct_content = BTreeMap::new();
direct_content.insert(user_id, room_ids);
let mut direct_content: BTreeMap<OwnedDirectUserIdentifier, Vec<OwnedRoomId>> =
BTreeMap::new();
direct_content.insert(user_id.into(), room_ids);
response
.extensions
.account_data
@@ -430,7 +430,7 @@ mod test {
assert_ambiguity!(
[("@alice:localhost", "alice"), ("@bob:localhost", "аlice")],
[("alice", true)],
"Bob tries to impersonate Alice using a cyrilic а"
"Bob tries to impersonate Alice using a cyrillic а"
);
assert_ambiguity!(
@@ -29,7 +29,8 @@ use ruma::{
},
owned_event_id, owned_mxc_uri, room_id,
serde::Raw,
uint, user_id, EventId, OwnedEventId, OwnedUserId, RoomId, TransactionId, UserId,
uint, user_id, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, RoomId,
TransactionId, UserId,
};
use serde_json::{json, value::Value as JsonValue};
@@ -90,6 +91,8 @@ pub trait StateStoreIntegrationTests {
async fn test_send_queue_priority(&self);
/// Test operations related to send queue dependents.
async fn test_send_queue_dependents(&self);
/// Test an update to a send queue dependent request.
async fn test_update_send_queue_dependent(&self);
/// Test saving/restoring server capabilities.
async fn test_server_capabilities_saving(&self);
}
@@ -972,6 +975,32 @@ impl StateStoreIntegrationTests for DynStateStore {
self.populate().await?;
{
// Add a send queue request in that room.
let txn = TransactionId::new();
let ev =
SerializableEventContent::new(&RoomMessageEventContent::text_plain("sup").into())
.unwrap();
self.save_send_queue_request(
room_id,
txn.clone(),
MilliSecondsSinceUnixEpoch::now(),
ev.into(),
0,
)
.await?;
// Add a single dependent queue request.
self.save_dependent_queued_request(
room_id,
&txn,
ChildTransactionId::new(),
MilliSecondsSinceUnixEpoch::now(),
DependentQueuedRequestKind::RedactEvent,
)
.await?;
}
self.remove_room(room_id).await?;
assert_eq!(self.get_room_infos().await?.len(), 1, "room is still there");
@@ -1023,6 +1052,8 @@ impl StateStoreIntegrationTests for DynStateStore {
.is_empty(),
"still event recepts in the store"
);
assert!(self.load_send_queue_requests(room_id).await?.is_empty());
assert!(self.load_dependent_queued_requests(room_id).await?.is_empty());
self.remove_room(stripped_room_id).await?;
@@ -1220,7 +1251,15 @@ impl StateStoreIntegrationTests for DynStateStore {
let event0 =
SerializableEventContent::new(&RoomMessageEventContent::text_plain("msg0").into())
.unwrap();
self.save_send_queue_request(room_id, txn0.clone(), event0.into(), 0).await.unwrap();
self.save_send_queue_request(
room_id,
txn0.clone(),
MilliSecondsSinceUnixEpoch::now(),
event0.into(),
0,
)
.await
.unwrap();
// Reading it will work.
let pending = self.load_send_queue_requests(room_id).await.unwrap();
@@ -1244,7 +1283,15 @@ impl StateStoreIntegrationTests for DynStateStore {
)
.unwrap();
self.save_send_queue_request(room_id, txn, event.into(), 0).await.unwrap();
self.save_send_queue_request(
room_id,
txn,
MilliSecondsSinceUnixEpoch::now(),
event.into(),
0,
)
.await
.unwrap();
}
// Reading all the events should work.
@@ -1342,7 +1389,15 @@ impl StateStoreIntegrationTests for DynStateStore {
let event =
SerializableEventContent::new(&RoomMessageEventContent::text_plain("room2").into())
.unwrap();
self.save_send_queue_request(room_id2, txn.clone(), event.into(), 0).await.unwrap();
self.save_send_queue_request(
room_id2,
txn.clone(),
MilliSecondsSinceUnixEpoch::now(),
event.into(),
0,
)
.await
.unwrap();
}
// Add and remove one event for room3.
@@ -1352,7 +1407,15 @@ impl StateStoreIntegrationTests for DynStateStore {
let event =
SerializableEventContent::new(&RoomMessageEventContent::text_plain("room3").into())
.unwrap();
self.save_send_queue_request(room_id3, txn.clone(), event.into(), 0).await.unwrap();
self.save_send_queue_request(
room_id3,
txn.clone(),
MilliSecondsSinceUnixEpoch::now(),
event.into(),
0,
)
.await
.unwrap();
self.remove_send_queue_request(room_id3, &txn).await.unwrap();
}
@@ -1377,21 +1440,45 @@ impl StateStoreIntegrationTests for DynStateStore {
let ev0 =
SerializableEventContent::new(&RoomMessageEventContent::text_plain("low0").into())
.unwrap();
self.save_send_queue_request(room_id, low0_txn.clone(), ev0.into(), 2).await.unwrap();
self.save_send_queue_request(
room_id,
low0_txn.clone(),
MilliSecondsSinceUnixEpoch::now(),
ev0.into(),
2,
)
.await
.unwrap();
// Saving one request with higher priority should work.
let high_txn = TransactionId::new();
let ev1 =
SerializableEventContent::new(&RoomMessageEventContent::text_plain("high").into())
.unwrap();
self.save_send_queue_request(room_id, high_txn.clone(), ev1.into(), 10).await.unwrap();
self.save_send_queue_request(
room_id,
high_txn.clone(),
MilliSecondsSinceUnixEpoch::now(),
ev1.into(),
10,
)
.await
.unwrap();
// Saving another request with the low priority should work.
let low1_txn = TransactionId::new();
let ev2 =
SerializableEventContent::new(&RoomMessageEventContent::text_plain("low1").into())
.unwrap();
self.save_send_queue_request(room_id, low1_txn.clone(), ev2.into(), 2).await.unwrap();
self.save_send_queue_request(
room_id,
low1_txn.clone(),
MilliSecondsSinceUnixEpoch::now(),
ev2.into(),
2,
)
.await
.unwrap();
// The requests should be ordered from higher priority to lower, and when equal,
// should use the insertion order instead.
@@ -1431,7 +1518,15 @@ impl StateStoreIntegrationTests for DynStateStore {
let event0 =
SerializableEventContent::new(&RoomMessageEventContent::text_plain("hey").into())
.unwrap();
self.save_send_queue_request(room_id, txn0.clone(), event0.into(), 0).await.unwrap();
self.save_send_queue_request(
room_id,
txn0.clone(),
MilliSecondsSinceUnixEpoch::now(),
event0.into(),
0,
)
.await
.unwrap();
// No dependents, to start with.
assert!(self.load_dependent_queued_requests(room_id).await.unwrap().is_empty());
@@ -1442,6 +1537,7 @@ impl StateStoreIntegrationTests for DynStateStore {
room_id,
&txn0,
child_txn.clone(),
MilliSecondsSinceUnixEpoch::now(),
DependentQueuedRequestKind::RedactEvent,
)
.await
@@ -1458,7 +1554,7 @@ impl StateStoreIntegrationTests for DynStateStore {
// Update the event id.
let event_id = owned_event_id!("$1");
let num_updated = self
.update_dependent_queued_request(
.mark_dependent_queued_requests_as_ready(
room_id,
&txn0,
SentRequestKey::Event(event_id.clone()),
@@ -1493,12 +1589,21 @@ impl StateStoreIntegrationTests for DynStateStore {
let event1 =
SerializableEventContent::new(&RoomMessageEventContent::text_plain("hey2").into())
.unwrap();
self.save_send_queue_request(room_id, txn1.clone(), event1.into(), 0).await.unwrap();
self.save_send_queue_request(
room_id,
txn1.clone(),
MilliSecondsSinceUnixEpoch::now(),
event1.into(),
0,
)
.await
.unwrap();
self.save_dependent_queued_request(
room_id,
&txn0,
ChildTransactionId::new(),
MilliSecondsSinceUnixEpoch::now(),
DependentQueuedRequestKind::RedactEvent,
)
.await
@@ -1509,6 +1614,7 @@ impl StateStoreIntegrationTests for DynStateStore {
room_id,
&txn1,
ChildTransactionId::new(),
MilliSecondsSinceUnixEpoch::now(),
DependentQueuedRequestKind::EditEvent {
new_content: SerializableEventContent::new(
&RoomMessageEventContent::text_plain("edit").into(),
@@ -1528,6 +1634,55 @@ impl StateStoreIntegrationTests for DynStateStore {
let dependents = self.load_dependent_queued_requests(room_id).await.unwrap();
assert_eq!(dependents.len(), 2);
}
async fn test_update_send_queue_dependent(&self) {
let room_id = room_id!("!test_send_queue_dependents:localhost");
let txn = TransactionId::new();
// Save a dependent redaction for an event.
let child_txn = ChildTransactionId::new();
self.save_dependent_queued_request(
room_id,
&txn,
child_txn.clone(),
MilliSecondsSinceUnixEpoch::now(),
DependentQueuedRequestKind::RedactEvent,
)
.await
.unwrap();
// It worked.
let dependents = self.load_dependent_queued_requests(room_id).await.unwrap();
assert_eq!(dependents.len(), 1);
assert_eq!(dependents[0].parent_transaction_id, txn);
assert_eq!(dependents[0].own_transaction_id, child_txn);
assert!(dependents[0].parent_key.is_none());
assert_matches!(dependents[0].kind, DependentQueuedRequestKind::RedactEvent);
// Make it a reaction, instead of a redaction.
self.update_dependent_queued_request(
room_id,
&child_txn,
DependentQueuedRequestKind::ReactEvent { key: "👍".to_owned() },
)
.await
.unwrap();
// It worked.
let dependents = self.load_dependent_queued_requests(room_id).await.unwrap();
assert_eq!(dependents.len(), 1);
assert_eq!(dependents[0].parent_transaction_id, txn);
assert_eq!(dependents[0].own_transaction_id, child_txn);
assert!(dependents[0].parent_key.is_none());
assert_matches!(
&dependents[0].kind,
DependentQueuedRequestKind::ReactEvent { key } => {
assert_eq!(key, "👍");
}
);
}
}
/// Macro building to allow your StateStore implementation to run the entire
@@ -1686,6 +1841,12 @@ macro_rules! statestore_integration_tests {
let store = get_store().await.expect("creating store failed").into_state_store();
store.test_send_queue_dependents().await;
}
#[async_test]
async fn test_update_send_queue_dependent() {
let store = get_store().await.expect("creating store failed").into_state_store();
store.test_update_send_queue_dependent().await;
}
}
};
}
File diff suppressed because it is too large Load Diff
@@ -19,10 +19,10 @@ use std::{
sync::Arc,
};
#[cfg(feature = "experimental-sliding-sync")]
use matrix_sdk_common::deserialized_responses::SyncTimelineEvent;
use matrix_sdk_common::deserialized_responses::TimelineEvent;
use ruma::{
events::{
direct::OwnedDirectUserIdentifier,
room::{
avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent,
@@ -41,10 +41,9 @@ use ruma::{
};
use serde::{Deserialize, Serialize};
#[cfg(feature = "experimental-sliding-sync")]
use crate::latest_event::LatestEvent;
use crate::{
deserialized_responses::SyncOrStrippedState,
latest_event::LatestEvent,
rooms::{
normal::{RoomSummary, SyncInfo},
BaseRoomInfo, RoomNotableTags,
@@ -77,8 +76,7 @@ pub struct RoomInfoV1 {
sync_info: SyncInfo,
#[serde(default = "encryption_state_default")] // see fn docs for why we use this default
encryption_state_synced: bool,
#[cfg(feature = "experimental-sliding-sync")]
latest_event: Option<SyncTimelineEvent>,
latest_event: Option<TimelineEvent>,
base_info: BaseRoomInfoV1,
}
@@ -105,7 +103,6 @@ impl RoomInfoV1 {
last_prev_batch,
sync_info,
encryption_state_synced,
#[cfg(feature = "experimental-sliding-sync")]
latest_event,
base_info,
} = self;
@@ -121,14 +118,12 @@ impl RoomInfoV1 {
last_prev_batch,
sync_info,
encryption_state_synced,
#[cfg(feature = "experimental-sliding-sync")]
latest_event: latest_event.map(|ev| Box::new(LatestEvent::new(ev))),
read_receipts: Default::default(),
base_info: base_info.migrate(create),
warned_about_unknown_room_version: Arc::new(false.into()),
cached_display_name: None,
cached_user_defined_notification_mode: None,
#[cfg(feature = "experimental-sliding-sync")]
recency_stamp: None,
}
}
@@ -200,12 +195,17 @@ impl BaseRoomInfoV1 {
MinimalStateEvent::Redacted(ev) => MinimalStateEvent::Redacted(ev),
});
let mut converted_dm_targets = HashSet::new();
for dm_target in dm_targets {
converted_dm_targets.insert(OwnedDirectUserIdentifier::from(dm_target));
}
Box::new(BaseRoomInfo {
avatar,
beacons: BTreeMap::new(),
canonical_alias,
create,
dm_targets,
dm_targets: converted_dm_targets,
encryption,
guest_access,
history_visibility,
-1
View File
@@ -276,7 +276,6 @@ impl Store {
}
/// Check if a room exists.
#[cfg(feature = "experimental-sliding-sync")]
pub(crate) fn room_exists(&self, room_id: &RoomId) -> bool {
self.rooms.read().unwrap().get(room_id).is_some()
}
@@ -138,6 +138,13 @@ where
L: Hash + Eq + ?Sized,
{
let position = self.mapping.remove(key)?;
// Reindex every mapped entry that is after the position we're looking to
// remove.
for mapped_pos in self.mapping.values_mut().filter(|pos| **pos > position) {
*mapped_pos = mapped_pos.saturating_sub(1);
}
Some(self.values.remove(position))
}
}
@@ -195,6 +202,12 @@ mod tests {
assert_eq!(map.get(&'a'), Some(&'E'));
assert_eq!(map.get(&'b'), Some(&'f'));
assert_eq!(map.get(&'c'), Some(&'G'));
// remove non-last item
assert_eq!(map.remove(&'b'), Some('f'));
// get_or_create item after the removed one
assert_eq!(map.get_or_create(&'c', || 'G'), &'G');
}
#[test]
@@ -208,20 +221,26 @@ mod tests {
// new items
map.insert('a', 'e');
map.insert('b', 'f');
map.insert('c', 'g');
assert_eq!(map.get(&'a'), Some(&'e'));
assert_eq!(map.get(&'b'), Some(&'f'));
assert!(map.get(&'c').is_none());
assert_eq!(map.get(&'c'), Some(&'g'));
assert!(map.get(&'d').is_none());
// remove one item
assert_eq!(map.remove(&'b'), Some('f'));
// remove last item
assert_eq!(map.remove(&'c'), Some('g'));
assert_eq!(map.get(&'a'), Some(&'e'));
assert_eq!(map.get(&'b'), None);
assert_eq!(map.get(&'b'), Some(&'f'));
assert_eq!(map.get(&'c'), None);
// remove a non-existent item
assert_eq!(map.remove(&'c'), None);
// remove a non-last item
assert_eq!(map.remove(&'a'), Some('e'));
assert_eq!(map.get(&'b'), Some(&'f'));
}
#[test]
+37 -3
View File
@@ -23,7 +23,8 @@ use ruma::{
AnyMessageLikeEventContent, EventContent as _, RawExt as _,
},
serde::Raw,
OwnedDeviceId, OwnedEventId, OwnedTransactionId, OwnedUserId, TransactionId, UInt,
MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId, OwnedTransactionId, OwnedUserId,
TransactionId, UInt,
};
use serde::{Deserialize, Serialize};
@@ -131,6 +132,9 @@ pub struct QueuedRequest {
/// The bigger the value, the higher the priority at which this request
/// should be handled.
pub priority: usize,
/// The time that the request was originally attempted.
pub created_at: MilliSecondsSinceUnixEpoch,
}
impl QueuedRequest {
@@ -248,9 +252,15 @@ pub struct FinishUploadThumbnailInfo {
/// Transaction id for the thumbnail upload.
pub txn: OwnedTransactionId,
/// Thumbnail's width.
pub width: UInt,
///
/// Used previously, kept for backwards compatibility.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub width: Option<UInt>,
/// Thumbnail's height.
pub height: UInt,
///
/// Used previously, kept for backwards compatibility.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub height: Option<UInt>,
}
/// A transaction id identifying a [`DependentQueuedRequest`] rather than its
@@ -365,6 +375,30 @@ pub struct DependentQueuedRequest {
/// If the parent request has been sent, the parent's request identifier
/// returned by the server once the local echo has been sent out.
pub parent_key: Option<SentRequestKey>,
/// The time that the request was originally attempted.
pub created_at: MilliSecondsSinceUnixEpoch,
}
impl DependentQueuedRequest {
/// Does the dependent request represent a new event that is *not*
/// aggregated, aka it is going to be its own item in a timeline?
pub fn is_own_event(&self) -> bool {
match self.kind {
DependentQueuedRequestKind::EditEvent { .. }
| DependentQueuedRequestKind::RedactEvent
| DependentQueuedRequestKind::ReactEvent { .. }
| DependentQueuedRequestKind::UploadFileWithThumbnail { .. } => {
// These are all aggregated events, or non-visible items (file upload producing
// a new MXC ID).
false
}
DependentQueuedRequestKind::FinishUpload { .. } => {
// This one graduates into a new media event.
true
}
}
}
}
#[cfg(not(tarpaulin_include))]
+50 -9
View File
@@ -35,8 +35,8 @@ use ruma::{
},
serde::Raw,
time::SystemTime,
EventId, OwnedEventId, OwnedMxcUri, OwnedRoomId, OwnedTransactionId, OwnedUserId, RoomId,
TransactionId, UserId,
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedMxcUri, OwnedRoomId,
OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UserId,
};
use serde::{Deserialize, Serialize};
@@ -359,6 +359,7 @@ pub trait StateStore: AsyncTraitDeps {
&self,
room_id: &RoomId,
transaction_id: OwnedTransactionId,
created_at: MilliSecondsSinceUnixEpoch,
request: QueuedRequestKind,
priority: usize,
) -> Result<(), Self::Error>;
@@ -421,24 +422,35 @@ pub trait StateStore: AsyncTraitDeps {
room_id: &RoomId,
parent_txn_id: &TransactionId,
own_txn_id: ChildTransactionId,
created_at: MilliSecondsSinceUnixEpoch,
content: DependentQueuedRequestKind,
) -> Result<(), Self::Error>;
/// Update a set of dependent send queue requests with a key identifying the
/// homeserver's response, effectively marking them as ready.
/// Mark a set of dependent send queue requests as ready, using a key
/// identifying the homeserver's response.
///
/// ⚠ Beware! There's no verification applied that the parent key type is
/// compatible with the dependent event type. The invalid state may be
/// lazily filtered out in `load_dependent_queued_requests`.
///
/// Returns the number of updated requests.
async fn update_dependent_queued_request(
async fn mark_dependent_queued_requests_as_ready(
&self,
room_id: &RoomId,
parent_txn_id: &TransactionId,
sent_parent_key: SentRequestKey,
) -> Result<usize, Self::Error>;
/// Update a dependent send queue request with the new content.
///
/// Returns true if the request was found and could be updated.
async fn update_dependent_queued_request(
&self,
room_id: &RoomId,
own_transaction_id: &ChildTransactionId,
new_content: DependentQueuedRequestKind,
) -> Result<bool, Self::Error>;
/// Remove a specific dependent send queue request by id.
///
/// Returns true if the dependent send queue request has been indeed
@@ -647,11 +659,12 @@ impl<T: StateStore> StateStore for EraseStateStoreError<T> {
&self,
room_id: &RoomId,
transaction_id: OwnedTransactionId,
created_at: MilliSecondsSinceUnixEpoch,
content: QueuedRequestKind,
priority: usize,
) -> Result<(), Self::Error> {
self.0
.save_send_queue_request(room_id, transaction_id, content, priority)
.save_send_queue_request(room_id, transaction_id, created_at, content, priority)
.await
.map_err(Into::into)
}
@@ -701,22 +714,23 @@ impl<T: StateStore> StateStore for EraseStateStoreError<T> {
room_id: &RoomId,
parent_txn_id: &TransactionId,
own_txn_id: ChildTransactionId,
created_at: MilliSecondsSinceUnixEpoch,
content: DependentQueuedRequestKind,
) -> Result<(), Self::Error> {
self.0
.save_dependent_queued_request(room_id, parent_txn_id, own_txn_id, content)
.save_dependent_queued_request(room_id, parent_txn_id, own_txn_id, created_at, content)
.await
.map_err(Into::into)
}
async fn update_dependent_queued_request(
async fn mark_dependent_queued_requests_as_ready(
&self,
room_id: &RoomId,
parent_txn_id: &TransactionId,
sent_parent_key: SentRequestKey,
) -> Result<usize, Self::Error> {
self.0
.update_dependent_queued_request(room_id, parent_txn_id, sent_parent_key)
.mark_dependent_queued_requests_as_ready(room_id, parent_txn_id, sent_parent_key)
.await
.map_err(Into::into)
}
@@ -735,6 +749,18 @@ impl<T: StateStore> StateStore for EraseStateStoreError<T> {
) -> Result<Vec<DependentQueuedRequest>, Self::Error> {
self.0.load_dependent_queued_requests(room_id).await.map_err(Into::into)
}
async fn update_dependent_queued_request(
&self,
room_id: &RoomId,
own_transaction_id: &ChildTransactionId,
new_content: DependentQueuedRequestKind,
) -> Result<bool, Self::Error> {
self.0
.update_dependent_queued_request(room_id, own_transaction_id, new_content)
.await
.map_err(Into::into)
}
}
/// Convenience functionality for state stores.
@@ -1000,6 +1026,9 @@ pub enum StateStoreDataValue {
///
/// [`ComposerDraft`]: Self::ComposerDraft
ComposerDraft(ComposerDraft),
/// A list of knock request ids marked as seen in a room.
SeenKnockRequests(BTreeMap<OwnedEventId, OwnedUserId>),
}
/// Current draft of the composer for the room.
@@ -1066,6 +1095,11 @@ impl StateStoreDataValue {
pub fn into_server_capabilities(self) -> Option<ServerCapabilities> {
as_variant!(self, Self::ServerCapabilities)
}
/// Get this value if it is the data for the ignored join requests.
pub fn into_seen_knock_requests(self) -> Option<BTreeMap<OwnedEventId, OwnedUserId>> {
as_variant!(self, Self::SeenKnockRequests)
}
}
/// A key for key-value data.
@@ -1095,6 +1129,9 @@ pub enum StateStoreDataKey<'a> {
///
/// [`ComposerDraft`]: Self::ComposerDraft
ComposerDraft(&'a RoomId),
/// A list of knock request ids marked as seen in a room.
SeenKnockRequests(&'a RoomId),
}
impl StateStoreDataKey<'_> {
@@ -1120,6 +1157,10 @@ impl StateStoreDataKey<'_> {
/// Key prefix to use for the [`ComposerDraft`][Self::ComposerDraft]
/// variant.
pub const COMPOSER_DRAFT: &'static str = "composer_draft";
/// Key prefix to use for the
/// [`SeenKnockRequests`][Self::SeenKnockRequests] variant.
pub const SEEN_KNOCK_REQUESTS: &'static str = "seen_knock_requests";
}
#[cfg(test)]
+7 -7
View File
@@ -16,7 +16,7 @@
use std::{collections::BTreeMap, fmt};
use matrix_sdk_common::{debug::DebugRawEvent, deserialized_responses::SyncTimelineEvent};
use matrix_sdk_common::{debug::DebugRawEvent, deserialized_responses::TimelineEvent};
use ruma::{
api::client::sync::sync_events::{
v3::{InvitedRoom as InvitedRoomUpdate, KnockedRoom as KnockedRoomUpdate},
@@ -102,7 +102,7 @@ impl RoomUpdates {
#[cfg(not(tarpaulin_include))]
impl fmt::Debug for RoomUpdates {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Rooms")
f.debug_struct("RoomUpdates")
.field("leave", &self.leave)
.field("join", &self.join)
.field("invite", &DebugInvitedRoomUpdates(&self.invite))
@@ -138,7 +138,7 @@ pub struct JoinedRoomUpdate {
#[cfg(not(tarpaulin_include))]
impl fmt::Debug for JoinedRoomUpdate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("JoinedRoom")
f.debug_struct("JoinedRoomUpdate")
.field("unread_notifications", &self.unread_notifications)
.field("timeline", &self.timeline)
.field("state", &DebugListOfRawEvents(&self.state))
@@ -215,7 +215,7 @@ impl LeftRoomUpdate {
#[cfg(not(tarpaulin_include))]
impl fmt::Debug for LeftRoomUpdate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("JoinedRoom")
f.debug_struct("LeftRoomUpdate")
.field("timeline", &self.timeline)
.field("state", &DebugListOfRawEvents(&self.state))
.field("account_data", &DebugListOfRawEventsNoId(&self.account_data))
@@ -236,7 +236,7 @@ pub struct Timeline {
pub prev_batch: Option<String>,
/// A list of events.
pub events: Vec<SyncTimelineEvent>,
pub events: Vec<TimelineEvent>,
}
impl Timeline {
@@ -248,7 +248,7 @@ impl Timeline {
struct DebugInvitedRoomUpdates<'a>(&'a BTreeMap<OwnedRoomId, InvitedRoomUpdate>);
#[cfg(not(tarpaulin_include))]
impl<'a> fmt::Debug for DebugInvitedRoomUpdates<'a> {
impl fmt::Debug for DebugInvitedRoomUpdates<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.0.iter().map(|(k, v)| (k, DebugInvitedRoom(v)))).finish()
}
@@ -257,7 +257,7 @@ impl<'a> fmt::Debug for DebugInvitedRoomUpdates<'a> {
struct DebugKnockedRoomUpdates<'a>(&'a BTreeMap<OwnedRoomId, KnockedRoomUpdate>);
#[cfg(not(tarpaulin_include))]
impl<'a> fmt::Debug for DebugKnockedRoomUpdates<'a> {
impl fmt::Debug for DebugKnockedRoomUpdates<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.0.iter().map(|(k, v)| (k, DebugKnockedRoom(v)))).finish()
}
+33
View File
@@ -2,6 +2,39 @@
All notable changes to this project will be documented in this file.
<!-- next-header -->
## [Unreleased] - ReleaseDate
## [0.10.0] - 2025-02-04
- [**breaking**]: `SyncTimelineEvent` and `TimelineEvent` have been fused into a single type
`TimelineEvent`, and its field `push_actions` has been made `Option`al (it is set to `None` when
we couldn't compute the push actions, because we lacked some information).
([#4568](https://github.com/matrix-org/matrix-rust-sdk/pull/4568))
## [0.9.0] - 2024-12-18
### Bug Fixes
- Change the behavior of `LinkedChunk::new_with_update_history()` to emit an
`Update::NewItemsChunk` when a new, initial empty, chunk is created.
([#4327](https://github.com/matrix-org/matrix-rust-sdk/pull/4321))
- [**breaking**] Make `Room::history_visibility()` return an Option, and
introduce `Room::history_visibility_or_default()` to return a better
sensible default, according to the spec.
([#4325](https://github.com/matrix-org/matrix-rust-sdk/pull/4325))
- Clear the internal state of the `AsVector` struct if an `Update::Clear`
state has been received.
([#4321](https://github.com/matrix-org/matrix-rust-sdk/pull/4321))
### Documentation
- Document that a decrypted raw event always has a room id.
([#728e1fd](https://github.com/matrix-org/matrix-rust-sdk/commit/728e1fda2ae9f1bfa87df162aa553040be705223))
## [0.8.0] - 2024-11-19
### Refactor
+19 -8
View File
@@ -9,7 +9,7 @@ name = "matrix-sdk-common"
readme = "README.md"
repository = "https://github.com/matrix-org/matrix-rust-sdk"
rust-version = { workspace = true }
version = "0.8.0"
version = "0.10.0"
[package.metadata.docs.rs]
default-target = "x86_64-unknown-linux-gnu"
@@ -18,6 +18,10 @@ targets = ["x86_64-unknown-linux-gnu", "wasm32-unknown-unknown"]
[features]
js = ["wasm-bindgen-futures"]
uniffi = ["dep:uniffi"]
# Private feature, see
# https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823 for the gory
# details.
test-send-sync = []
[dependencies]
async-trait = { workspace = true }
@@ -36,19 +40,26 @@ uniffi = { workspace = true, optional = true }
[target.'cfg(target_arch = "wasm32")'.dependencies]
futures-util = { workspace = true, features = ["channel"] }
wasm-bindgen-futures = { version = "0.4.33", optional = true }
gloo-timers = { version = "0.3.0", features = ["futures"] }
web-sys = { version = "0.3.60", features = ["console"] }
gloo-timers = { workspace = true, features = ["futures"] }
web-sys = { workspace = true, features = ["console"] }
tracing-subscriber = { workspace = true, features = ["fmt", "ansi"] }
wasm-bindgen = "0.2.84"
wasm-bindgen = { workspace = true }
[dev-dependencies]
assert_matches = { workspace = true }
proptest = { version = "1.4.0", default-features = false, features = ["std"] }
matrix-sdk-test = { workspace = true }
wasm-bindgen-test = "0.3.33"
proptest = { workspace = true }
matrix-sdk-test-macros = { path = "../../testing/matrix-sdk-test-macros" }
wasm-bindgen-test = { workspace = true }
insta = { workspace = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# Enable the test macro.
tokio = { workspace = true, features = ["rt", "macros"] }
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
js-sys = "0.3.64"
# Enable the JS feature for getrandom.
getrandom = { workspace = true, default-features = false, features = ["js"] }
js-sys = { workspace = true }
[lints]
workspace = true
@@ -14,10 +14,15 @@
use std::{collections::BTreeMap, fmt};
#[cfg(doc)]
use ruma::events::AnyTimelineEvent;
use ruma::{
events::{AnyMessageLikeEvent, AnySyncTimelineEvent, AnyTimelineEvent},
events::{AnyMessageLikeEvent, AnySyncTimelineEvent},
push::Action,
serde::{JsonObject, Raw},
serde::{
AsRefStr, AsStrAsRefStr, DebugAsRefStr, DeserializeFromCowStr, FromString, JsonObject, Raw,
SerializeAsRefStr,
},
DeviceKeyAlgorithm, OwnedDeviceId, OwnedEventId, OwnedUserId,
};
use serde::{Deserialize, Serialize};
@@ -176,6 +181,7 @@ pub enum VerificationLevel {
/// The message was sent by a user identity we have not verified, but the
/// user was previously verified.
#[serde(alias = "PreviouslyVerified")]
VerificationViolation,
/// The message was sent by a device not linked to (signed by) any user
@@ -259,6 +265,7 @@ pub enum ShieldStateCode {
/// An unencrypted event in an encrypted room.
SentInClear,
/// The sender was previously verified but changed their identity.
#[serde(alias = "PreviouslyVerified")]
VerificationViolation,
}
@@ -303,26 +310,61 @@ pub struct EncryptionInfo {
/// Previously, this differed from [`TimelineEvent`] by wrapping an
/// [`AnySyncTimelineEvent`] instead of an [`AnyTimelineEvent`], but nowadays
/// they are essentially identical, and one of them should probably be removed.
//
// 🚨 Note about this type, please read! 🚨
//
// `TimelineEvent` is heavily used across the SDK crates. In some cases, we
// are reaching a [`recursion_limit`] when the compiler is trying to figure out
// if `TimelineEvent` implements `Sync` when it's embedded in other types.
//
// We want to help the compiler so that one doesn't need to increase the
// `recursion_limit`. We stop the recursive check by (un)safely implement `Sync`
// and `Send` on `TimelineEvent` directly.
//
// See
// https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823
// which has addressed this issue first
//
// [`recursion_limit`]: https://doc.rust-lang.org/reference/attributes/limits.html#the-recursion_limit-attribute
#[derive(Clone, Debug, Serialize)]
pub struct SyncTimelineEvent {
pub struct TimelineEvent {
/// The event itself, together with any information on decryption.
pub kind: TimelineEventKind,
/// The push actions associated with this event.
#[serde(skip_serializing_if = "Vec::is_empty")]
pub push_actions: Vec<Action>,
///
/// If it's set to `None`, then it means we couldn't compute those actions.
#[serde(skip_serializing_if = "Option::is_none")]
pub push_actions: Option<Vec<Action>>,
}
impl SyncTimelineEvent {
/// Create a new `SyncTimelineEvent` from the given raw event.
// See https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823.
#[cfg(not(feature = "test-send-sync"))]
unsafe impl Send for TimelineEvent {}
// See https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823.
#[cfg(not(feature = "test-send-sync"))]
unsafe impl Sync for TimelineEvent {}
#[cfg(feature = "test-send-sync")]
#[test]
// See https://github.com/matrix-org/matrix-rust-sdk/pull/3749#issuecomment-2312939823.
fn test_send_sync_for_sync_timeline_event() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<TimelineEvent>();
}
impl TimelineEvent {
/// Create a new [`TimelineEvent`] from the given raw event.
///
/// This is a convenience constructor for a plaintext event when you don't
/// need to set `push_action`, for example inside a test.
pub fn new(event: Raw<AnySyncTimelineEvent>) -> Self {
Self { kind: TimelineEventKind::PlainText { event }, push_actions: vec![] }
Self { kind: TimelineEventKind::PlainText { event }, push_actions: None }
}
/// Create a new `SyncTimelineEvent` from the given raw event and push
/// Create a new [`TimelineEvent`] from the given raw event and push
/// actions.
///
/// This is a convenience constructor for a plaintext event, for example
@@ -331,140 +373,38 @@ impl SyncTimelineEvent {
event: Raw<AnySyncTimelineEvent>,
push_actions: Vec<Action>,
) -> Self {
Self { kind: TimelineEventKind::PlainText { event }, push_actions }
Self { kind: TimelineEventKind::PlainText { event }, push_actions: Some(push_actions) }
}
/// Create a new `SyncTimelineEvent` to represent the given decryption
/// Create a new [`TimelineEvent`] to represent the given decryption
/// failure.
pub fn new_utd_event(event: Raw<AnySyncTimelineEvent>, utd_info: UnableToDecryptInfo) -> Self {
Self { kind: TimelineEventKind::UnableToDecrypt { event, utd_info }, push_actions: vec![] }
Self { kind: TimelineEventKind::UnableToDecrypt { event, utd_info }, push_actions: None }
}
/// Get the event id of this `SyncTimelineEvent` if the event has any valid
/// Get the event id of this [`TimelineEvent`] if the event has any valid
/// id.
pub fn event_id(&self) -> Option<OwnedEventId> {
self.kind.event_id()
}
/// Returns a reference to the (potentially decrypted) Matrix event inside
/// this `TimelineEvent`.
/// this [`TimelineEvent`].
pub fn raw(&self) -> &Raw<AnySyncTimelineEvent> {
self.kind.raw()
}
/// If the event was a decrypted event that was successfully decrypted, get
/// its encryption info. Otherwise, `None`.
pub fn encryption_info(&self) -> Option<&EncryptionInfo> {
self.kind.encryption_info()
}
/// Takes ownership of this `TimelineEvent`, returning the (potentially
/// decrypted) Matrix event within.
pub fn into_raw(self) -> Raw<AnySyncTimelineEvent> {
self.kind.into_raw()
}
}
impl From<TimelineEvent> for SyncTimelineEvent {
fn from(o: TimelineEvent) -> Self {
Self { kind: o.kind, push_actions: o.push_actions.unwrap_or_default() }
}
}
impl From<DecryptedRoomEvent> for SyncTimelineEvent {
fn from(decrypted: DecryptedRoomEvent) -> Self {
let timeline_event: TimelineEvent = decrypted.into();
timeline_event.into()
}
}
impl<'de> Deserialize<'de> for SyncTimelineEvent {
/// Custom deserializer for [`SyncTimelineEvent`], to support older formats.
///
/// Ideally we might use an untagged enum and then convert from that;
/// however, that doesn't work due to a [serde bug](https://github.com/serde-rs/json/issues/497).
///
/// Instead, we first deserialize into an unstructured JSON map, and then
/// inspect the json to figure out which format we have.
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde_json::{Map, Value};
// First, deserialize to an unstructured JSON map
let value = Map::<String, Value>::deserialize(deserializer)?;
// If we have a top-level `event`, it's V0
if value.contains_key("event") {
let v0: SyncTimelineEventDeserializationHelperV0 =
serde_json::from_value(Value::Object(value)).map_err(|e| {
serde::de::Error::custom(format!(
"Unable to deserialize V0-format SyncTimelineEvent: {}",
e
))
})?;
Ok(v0.into())
/// Replace the raw event included in this item by another one.
pub fn replace_raw(&mut self, replacement: Raw<AnyMessageLikeEvent>) {
match &mut self.kind {
TimelineEventKind::Decrypted(decrypted) => decrypted.event = replacement,
TimelineEventKind::UnableToDecrypt { event, .. }
| TimelineEventKind::PlainText { event } => {
// It's safe to cast `AnyMessageLikeEvent` into `AnySyncMessageLikeEvent`,
// because the former contains a superset of the fields included in the latter.
*event = replacement.cast();
}
}
// Otherwise, it's V1
else {
let v1: SyncTimelineEventDeserializationHelperV1 =
serde_json::from_value(Value::Object(value)).map_err(|e| {
serde::de::Error::custom(format!(
"Unable to deserialize V1-format SyncTimelineEvent: {}",
e
))
})?;
Ok(v1.into())
}
}
}
/// Represents a matrix room event that has been returned from a Matrix
/// client-server API endpoint such as `/messages`, after initial processing.
///
/// The "initial processing" includes an attempt to decrypt encrypted events, so
/// the main thing this adds over [`AnyTimelineEvent`] is information on
/// encryption.
///
/// Previously, this differed from [`SyncTimelineEvent`] by wrapping an
/// [`AnyTimelineEvent`] instead of an [`AnySyncTimelineEvent`], but nowadays
/// they are essentially identical, and one of them should probably be removed.
#[derive(Clone, Debug)]
pub struct TimelineEvent {
/// The event itself, together with any information on decryption.
pub kind: TimelineEventKind,
/// The push actions associated with this event, if we had sufficient
/// context to compute them.
pub push_actions: Option<Vec<Action>>,
}
impl TimelineEvent {
/// Create a new `TimelineEvent` from the given raw event.
///
/// This is a convenience constructor for a plaintext event when you don't
/// need to set `push_action`, for example inside a test.
pub fn new(event: Raw<AnyTimelineEvent>) -> Self {
Self {
// This conversion is unproblematic since a `SyncTimelineEvent` is just a
// `TimelineEvent` without the `room_id`. By converting the raw value in
// this way, we simply cause the `room_id` field in the json to be
// ignored by a subsequent deserialization.
kind: TimelineEventKind::PlainText { event: event.cast() },
push_actions: None,
}
}
/// Create a new `TimelineEvent` to represent the given decryption failure.
pub fn new_utd_event(event: Raw<AnySyncTimelineEvent>, utd_info: UnableToDecryptInfo) -> Self {
Self { kind: TimelineEventKind::UnableToDecrypt { event, utd_info }, push_actions: None }
}
/// Returns a reference to the (potentially decrypted) Matrix event inside
/// this `TimelineEvent`.
pub fn raw(&self) -> &Raw<AnySyncTimelineEvent> {
self.kind.raw()
}
/// If the event was a decrypted event that was successfully decrypted, get
@@ -486,8 +426,49 @@ impl From<DecryptedRoomEvent> for TimelineEvent {
}
}
/// The event within a [`TimelineEvent`] or [`SyncTimelineEvent`], together with
/// encryption data.
impl<'de> Deserialize<'de> for TimelineEvent {
/// Custom deserializer for [`TimelineEvent`], to support older formats.
///
/// Ideally we might use an untagged enum and then convert from that;
/// however, that doesn't work due to a [serde bug](https://github.com/serde-rs/json/issues/497).
///
/// Instead, we first deserialize into an unstructured JSON map, and then
/// inspect the json to figure out which format we have.
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde_json::{Map, Value};
// First, deserialize to an unstructured JSON map
let value = Map::<String, Value>::deserialize(deserializer)?;
// If we have a top-level `event`, it's V0
if value.contains_key("event") {
let v0: SyncTimelineEventDeserializationHelperV0 =
serde_json::from_value(Value::Object(value)).map_err(|e| {
serde::de::Error::custom(format!(
"Unable to deserialize V0-format TimelineEvent: {}",
e
))
})?;
Ok(v0.into())
}
// Otherwise, it's V1
else {
let v1: SyncTimelineEventDeserializationHelperV1 =
serde_json::from_value(Value::Object(value)).map_err(|e| {
serde::de::Error::custom(format!(
"Unable to deserialize V1-format TimelineEvent: {}",
e
))
})?;
Ok(v1.into())
}
}
}
/// The event within a [`TimelineEvent`], together with encryption data.
#[derive(Clone, Serialize, Deserialize)]
pub enum TimelineEventKind {
/// A successfully-decrypted encrypted event.
@@ -587,6 +568,11 @@ impl fmt::Debug for TimelineEventKind {
/// A successfully-decrypted encrypted event.
pub struct DecryptedRoomEvent {
/// The decrypted event.
///
/// Note: it's not an error that this contains an `AnyMessageLikeEvent`: an
/// encrypted payload *always contains* a room id, by the [spec].
///
/// [spec]: https://spec.matrix.org/v1.12/client-server-api/#mmegolmv1aes-sha2
pub event: Raw<AnyMessageLikeEvent>,
/// The encryption info about the event.
@@ -661,7 +647,7 @@ pub struct UnableToDecryptInfo {
pub session_id: Option<String>,
/// Reason code for the decryption failure
#[serde(default = "unknown_utd_reason")]
#[serde(default = "unknown_utd_reason", deserialize_with = "deserialize_utd_reason")]
pub reason: UnableToDecryptReason,
}
@@ -669,6 +655,24 @@ fn unknown_utd_reason() -> UnableToDecryptReason {
UnableToDecryptReason::Unknown
}
/// Provides basic backward compatibility for deserializing older serialized
/// `UnableToDecryptReason` values.
pub fn deserialize_utd_reason<'de, D>(d: D) -> Result<UnableToDecryptReason, D::Error>
where
D: serde::Deserializer<'de>,
{
// Start by deserializing as to an untyped JSON value.
let v: serde_json::Value = Deserialize::deserialize(d)?;
// Backwards compatibility: `MissingMegolmSession` used to be stored without the
// withheld code.
if v.as_str().is_some_and(|s| s == "MissingMegolmSession") {
return Ok(UnableToDecryptReason::MissingMegolmSession { withheld_code: None });
}
// Otherwise, use the derived deserialize impl to turn the JSON into a
// UnableToDecryptReason
serde_json::from_value::<UnableToDecryptReason>(v).map_err(serde::de::Error::custom)
}
/// Reason code for a decryption failure
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum UnableToDecryptReason {
@@ -684,9 +688,11 @@ pub enum UnableToDecryptReason {
/// Decryption failed because we're missing the megolm session that was used
/// to encrypt the event.
///
/// TODO: support withheld codes?
MissingMegolmSession,
MissingMegolmSession {
/// If the key was withheld on purpose, the associated code. `None`
/// means no withheld code was received.
withheld_code: Option<WithheldCode>,
},
/// Decryption failed because, while we have the megolm session that was
/// used to encrypt the message, it is ratcheted too far forward.
@@ -718,13 +724,92 @@ impl UnableToDecryptReason {
/// Returns true if this UTD is due to a missing room key (and hence might
/// resolve itself if we wait a bit.)
pub fn is_missing_room_key(&self) -> bool {
matches!(self, Self::MissingMegolmSession | Self::UnknownMegolmMessageIndex)
// In case of MissingMegolmSession with a withheld code we return false here
// given that this API is used to decide if waiting a bit will help.
matches!(
self,
Self::MissingMegolmSession { withheld_code: None } | Self::UnknownMegolmMessageIndex
)
}
}
/// Deserialization helper for [`SyncTimelineEvent`], for the modern format.
/// A machine-readable code for why a Megolm key was not sent.
///
/// This has the exact same fields as [`SyncTimelineEvent`] itself, but has a
/// Normally sent as the payload of an [`m.room_key.withheld`](https://spec.matrix.org/v1.12/client-server-api/#mroom_keywithheld) to-device message.
#[derive(
Clone,
PartialEq,
Eq,
Hash,
AsStrAsRefStr,
AsRefStr,
FromString,
DebugAsRefStr,
SerializeAsRefStr,
DeserializeFromCowStr,
)]
pub enum WithheldCode {
/// the user/device was blacklisted.
#[ruma_enum(rename = "m.blacklisted")]
Blacklisted,
/// the user/devices is unverified.
#[ruma_enum(rename = "m.unverified")]
Unverified,
/// The user/device is not allowed have the key. For example, this would
/// usually be sent in response to a key request if the user was not in
/// the room when the message was sent.
#[ruma_enum(rename = "m.unauthorised")]
Unauthorised,
/// Sent in reply to a key request if the device that the key is requested
/// from does not have the requested key.
#[ruma_enum(rename = "m.unavailable")]
Unavailable,
/// An olm session could not be established.
/// This may happen, for example, if the sender was unable to obtain a
/// one-time key from the recipient.
#[ruma_enum(rename = "m.no_olm")]
NoOlm,
#[doc(hidden)]
_Custom(PrivOwnedStr),
}
impl fmt::Display for WithheldCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
let string = match self {
WithheldCode::Blacklisted => "The sender has blocked you.",
WithheldCode::Unverified => "The sender has disabled encrypting to unverified devices.",
WithheldCode::Unauthorised => "You are not authorised to read the message.",
WithheldCode::Unavailable => "The requested key was not found.",
WithheldCode::NoOlm => "Unable to establish a secure channel.",
_ => self.as_str(),
};
f.write_str(string)
}
}
// The Ruma macro expects the type to have this name.
// The payload is counter intuitively made public in order to avoid having
// multiple copies of this struct.
#[doc(hidden)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PrivOwnedStr(pub Box<str>);
#[cfg(not(tarpaulin_include))]
impl fmt::Debug for PrivOwnedStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
/// Deserialization helper for [`TimelineEvent`], for the modern format.
///
/// This has the exact same fields as [`TimelineEvent`] itself, but has a
/// regular `Deserialize` implementation.
#[derive(Debug, Deserialize)]
struct SyncTimelineEventDeserializationHelperV1 {
@@ -736,14 +821,14 @@ struct SyncTimelineEventDeserializationHelperV1 {
push_actions: Vec<Action>,
}
impl From<SyncTimelineEventDeserializationHelperV1> for SyncTimelineEvent {
impl From<SyncTimelineEventDeserializationHelperV1> for TimelineEvent {
fn from(value: SyncTimelineEventDeserializationHelperV1) -> Self {
let SyncTimelineEventDeserializationHelperV1 { kind, push_actions } = value;
SyncTimelineEvent { kind, push_actions }
TimelineEvent { kind, push_actions: Some(push_actions) }
}
}
/// Deserialization helper for [`SyncTimelineEvent`], for an older format.
/// Deserialization helper for [`TimelineEvent`], for an older format.
#[derive(Deserialize)]
struct SyncTimelineEventDeserializationHelperV0 {
/// The actual event.
@@ -764,7 +849,7 @@ struct SyncTimelineEventDeserializationHelperV0 {
unsigned_encryption_info: Option<BTreeMap<UnsignedEventLocation, UnsignedDecryptionResult>>,
}
impl From<SyncTimelineEventDeserializationHelperV0> for SyncTimelineEvent {
impl From<SyncTimelineEventDeserializationHelperV0> for TimelineEvent {
fn from(value: SyncTimelineEventDeserializationHelperV0) -> Self {
let SyncTimelineEventDeserializationHelperV0 {
event,
@@ -791,7 +876,7 @@ impl From<SyncTimelineEventDeserializationHelperV0> for SyncTimelineEvent {
None => TimelineEventKind::PlainText { event },
};
SyncTimelineEvent { kind, push_actions }
TimelineEvent { kind, push_actions: Some(push_actions) }
}
}
@@ -800,21 +885,20 @@ mod tests {
use std::collections::BTreeMap;
use assert_matches::assert_matches;
use insta::{assert_json_snapshot, with_settings};
use ruma::{
event_id,
events::{room::message::RoomMessageEventContent, AnySyncTimelineEvent},
serde::Raw,
user_id,
device_id, event_id, events::room::message::RoomMessageEventContent, serde::Raw, user_id,
DeviceKeyAlgorithm,
};
use serde::Deserialize;
use serde_json::json;
use super::{
AlgorithmInfo, DecryptedRoomEvent, EncryptionInfo, SyncTimelineEvent, TimelineEvent,
TimelineEventKind, UnableToDecryptInfo, UnableToDecryptReason, UnsignedDecryptionResult,
UnsignedEventLocation, VerificationState,
AlgorithmInfo, DecryptedRoomEvent, DeviceLinkProblem, EncryptionInfo, ShieldState,
ShieldStateCode, TimelineEvent, TimelineEventKind, UnableToDecryptInfo,
UnableToDecryptReason, UnsignedDecryptionResult, UnsignedEventLocation, VerificationLevel,
VerificationState, WithheldCode,
};
use crate::deserialized_responses::{DeviceLinkProblem, VerificationLevel};
fn example_event() -> serde_json::Value {
json!({
@@ -829,7 +913,7 @@ mod tests {
#[test]
fn sync_timeline_debug_content() {
let room_event = SyncTimelineEvent::new(Raw::new(&example_event()).unwrap().cast());
let room_event = TimelineEvent::new(Raw::new(&example_event()).unwrap().cast());
let debug_s = format!("{room_event:?}");
assert!(
!debug_s.contains("secret"),
@@ -837,18 +921,6 @@ mod tests {
);
}
#[test]
fn room_event_to_sync_room_event() {
let room_event = TimelineEvent::new(Raw::new(&example_event()).unwrap().cast());
let converted_room_event: SyncTimelineEvent = room_event.into();
let converted_event: AnySyncTimelineEvent =
converted_room_event.raw().deserialize().unwrap();
assert_eq!(converted_event.event_id(), "$xxxxx:example.org");
assert_eq!(converted_event.sender(), "@carl:example.com");
}
#[test]
fn old_verification_state_to_new_migration() {
#[derive(Deserialize)]
@@ -889,9 +961,77 @@ mod tests {
);
}
#[test]
fn test_verification_level_deserializes() {
// Given a JSON VerificationLevel
#[derive(Deserialize)]
struct Container {
verification_level: VerificationLevel,
}
let container = json!({ "verification_level": "VerificationViolation" });
// When we deserialize it
let deserialized: Container = serde_json::from_value(container)
.expect("We can deserialize the old PreviouslyVerified value");
// Then it is populated correctly
assert_eq!(deserialized.verification_level, VerificationLevel::VerificationViolation);
}
#[test]
fn test_verification_level_deserializes_from_old_previously_verified_value() {
// Given a JSON VerificationLevel with the old value PreviouslyVerified
#[derive(Deserialize)]
struct Container {
verification_level: VerificationLevel,
}
let container = json!({ "verification_level": "PreviouslyVerified" });
// When we deserialize it
let deserialized: Container = serde_json::from_value(container)
.expect("We can deserialize the old PreviouslyVerified value");
// Then it is migrated to the new value
assert_eq!(deserialized.verification_level, VerificationLevel::VerificationViolation);
}
#[test]
fn test_shield_state_code_deserializes() {
// Given a JSON ShieldStateCode with value VerificationViolation
#[derive(Deserialize)]
struct Container {
shield_state_code: ShieldStateCode,
}
let container = json!({ "shield_state_code": "VerificationViolation" });
// When we deserialize it
let deserialized: Container = serde_json::from_value(container)
.expect("We can deserialize the old PreviouslyVerified value");
// Then it is populated correctly
assert_eq!(deserialized.shield_state_code, ShieldStateCode::VerificationViolation);
}
#[test]
fn test_shield_state_code_deserializes_from_old_previously_verified_value() {
// Given a JSON ShieldStateCode with the old value PreviouslyVerified
#[derive(Deserialize)]
struct Container {
shield_state_code: ShieldStateCode,
}
let container = json!({ "shield_state_code": "PreviouslyVerified" });
// When we deserialize it
let deserialized: Container = serde_json::from_value(container)
.expect("We can deserialize the old PreviouslyVerified value");
// Then it is migrated to the new value
assert_eq!(deserialized.shield_state_code, ShieldStateCode::VerificationViolation);
}
#[test]
fn sync_timeline_event_serialisation() {
let room_event = SyncTimelineEvent {
let room_event = TimelineEvent {
kind: TimelineEventKind::Decrypted(DecryptedRoomEvent {
event: Raw::new(&example_event()).unwrap().cast(),
encryption_info: EncryptionInfo {
@@ -953,7 +1093,7 @@ mod tests {
);
// And it can be properly deserialized from the new format.
let event: SyncTimelineEvent = serde_json::from_value(serialized).unwrap();
let event: TimelineEvent = serde_json::from_value(serialized).unwrap();
assert_eq!(event.event_id(), Some(event_id!("$xxxxx:example.org").to_owned()));
assert_matches!(
event.encryption_info().unwrap().algorithm_info,
@@ -982,7 +1122,7 @@ mod tests {
"verification_state": "Verified",
},
});
let event: SyncTimelineEvent = serde_json::from_value(serialized).unwrap();
let event: TimelineEvent = serde_json::from_value(serialized).unwrap();
assert_eq!(event.event_id(), Some(event_id!("$xxxxx:example.org").to_owned()));
assert_matches!(
event.encryption_info().unwrap().algorithm_info,
@@ -1015,7 +1155,7 @@ mod tests {
"RelationsReplace": {"UnableToDecrypt": {"session_id": "xyz"}}
}
});
let event: SyncTimelineEvent = serde_json::from_value(serialized).unwrap();
let event: TimelineEvent = serde_json::from_value(serialized).unwrap();
assert_eq!(event.event_id(), Some(event_id!("$xxxxx:example.org").to_owned()));
assert_matches!(
event.encryption_info().unwrap().algorithm_info,
@@ -1033,4 +1173,236 @@ mod tests {
});
});
}
#[test]
fn sync_timeline_event_deserialisation_migration_for_withheld() {
// Old serialized version was
// "utd_info": {
// "reason": "MissingMegolmSession",
// "session_id": "session000"
// }
// The new version would be
// "utd_info": {
// "reason": {
// "MissingMegolmSession": {
// "withheld_code": null
// }
// },
// "session_id": "session000"
// }
let serialized = json!({
"kind": {
"UnableToDecrypt": {
"event": {
"content": {
"algorithm": "m.megolm.v1.aes-sha2",
"ciphertext": "AwgAEoABzL1JYhqhjW9jXrlT3M6H8mJ4qffYtOQOnPuAPNxsuG20oiD/Fnpv6jnQGhU6YbV9pNM+1mRnTvxW3CbWOPjLKqCWTJTc7Q0vDEVtYePg38ncXNcwMmfhgnNAoW9S7vNs8C003x3yUl6NeZ8bH+ci870BZL+kWM/lMl10tn6U7snNmSjnE3ckvRdO+11/R4//5VzFQpZdf4j036lNSls/WIiI67Fk9iFpinz9xdRVWJFVdrAiPFwb8L5xRZ8aX+e2JDMlc1eW8gk",
"device_id": "SKCGPNUWAU",
"sender_key": "Gim/c7uQdSXyrrUbmUOrBT6sMC0gO7QSLmOK6B7NOm0",
"session_id": "hgLyeSqXfb8vc5AjQLsg6TSHVu0HJ7HZ4B6jgMvxkrs"
},
"event_id": "$xxxxx:example.org",
"origin_server_ts": 2189,
"room_id": "!someroom:example.com",
"sender": "@carl:example.com",
"type": "m.room.message"
},
"utd_info": {
"reason": "MissingMegolmSession",
"session_id": "session000"
}
}
}
});
let result = serde_json::from_value(serialized);
assert!(result.is_ok());
// should have migrated to the new format
let event: TimelineEvent = result.unwrap();
assert_matches!(
event.kind,
TimelineEventKind::UnableToDecrypt { utd_info, .. }=> {
assert_matches!(
utd_info.reason,
UnableToDecryptReason::MissingMegolmSession { withheld_code: None }
);
}
)
}
#[test]
fn unable_to_decrypt_info_migration_for_withheld() {
let old_format = json!({
"reason": "MissingMegolmSession",
"session_id": "session000"
});
let deserialized = serde_json::from_value::<UnableToDecryptInfo>(old_format).unwrap();
let session_id = Some("session000".to_owned());
assert_eq!(deserialized.session_id, session_id);
assert_eq!(
deserialized.reason,
UnableToDecryptReason::MissingMegolmSession { withheld_code: None },
);
let new_format = json!({
"session_id": "session000",
"reason": {
"MissingMegolmSession": {
"withheld_code": null
}
}
});
let deserialized = serde_json::from_value::<UnableToDecryptInfo>(new_format).unwrap();
assert_eq!(
deserialized.reason,
UnableToDecryptReason::MissingMegolmSession { withheld_code: None },
);
assert_eq!(deserialized.session_id, session_id);
}
#[test]
fn unable_to_decrypt_reason_is_missing_room_key() {
let reason = UnableToDecryptReason::MissingMegolmSession { withheld_code: None };
assert!(reason.is_missing_room_key());
let reason = UnableToDecryptReason::MissingMegolmSession {
withheld_code: Some(WithheldCode::Blacklisted),
};
assert!(!reason.is_missing_room_key());
let reason = UnableToDecryptReason::UnknownMegolmMessageIndex;
assert!(reason.is_missing_room_key());
}
#[test]
fn snapshot_test_verification_level() {
assert_json_snapshot!(VerificationLevel::VerificationViolation);
assert_json_snapshot!(VerificationLevel::UnsignedDevice);
assert_json_snapshot!(VerificationLevel::None(DeviceLinkProblem::InsecureSource));
assert_json_snapshot!(VerificationLevel::None(DeviceLinkProblem::MissingDevice));
assert_json_snapshot!(VerificationLevel::UnverifiedIdentity);
}
#[test]
fn snapshot_test_verification_states() {
assert_json_snapshot!(VerificationState::Unverified(VerificationLevel::UnsignedDevice));
assert_json_snapshot!(VerificationState::Unverified(
VerificationLevel::VerificationViolation
));
assert_json_snapshot!(VerificationState::Unverified(VerificationLevel::None(
DeviceLinkProblem::InsecureSource,
)));
assert_json_snapshot!(VerificationState::Unverified(VerificationLevel::None(
DeviceLinkProblem::MissingDevice,
)));
assert_json_snapshot!(VerificationState::Verified);
}
#[test]
fn snapshot_test_shield_states() {
assert_json_snapshot!(ShieldState::None);
assert_json_snapshot!(ShieldState::Red {
code: ShieldStateCode::UnverifiedIdentity,
message: "a message"
});
assert_json_snapshot!(ShieldState::Grey {
code: ShieldStateCode::AuthenticityNotGuaranteed,
message: "authenticity of this message cannot be guaranteed",
});
}
#[test]
fn snapshot_test_shield_codes() {
assert_json_snapshot!(ShieldStateCode::AuthenticityNotGuaranteed);
assert_json_snapshot!(ShieldStateCode::UnknownDevice);
assert_json_snapshot!(ShieldStateCode::UnsignedDevice);
assert_json_snapshot!(ShieldStateCode::UnverifiedIdentity);
assert_json_snapshot!(ShieldStateCode::SentInClear);
assert_json_snapshot!(ShieldStateCode::VerificationViolation);
}
#[test]
fn snapshot_test_algorithm_info() {
let mut map = BTreeMap::new();
map.insert(DeviceKeyAlgorithm::Curve25519, "claimedclaimedcurve25519".to_owned());
map.insert(DeviceKeyAlgorithm::Ed25519, "claimedclaimeded25519".to_owned());
let info = AlgorithmInfo::MegolmV1AesSha2 {
curve25519_key: "curvecurvecurve".into(),
sender_claimed_keys: BTreeMap::from([
(DeviceKeyAlgorithm::Curve25519, "claimedclaimedcurve25519".to_owned()),
(DeviceKeyAlgorithm::Ed25519, "claimedclaimeded25519".to_owned()),
]),
};
assert_json_snapshot!(info)
}
#[test]
fn snapshot_test_encryption_info() {
let info = EncryptionInfo {
sender: user_id!("@alice:localhost").to_owned(),
sender_device: Some(device_id!("ABCDEFGH").to_owned()),
algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
curve25519_key: "curvecurvecurve".into(),
sender_claimed_keys: Default::default(),
},
verification_state: VerificationState::Verified,
};
with_settings!({sort_maps =>true}, {
assert_json_snapshot!(info)
})
}
#[test]
fn snapshot_test_sync_timeline_event() {
let room_event = TimelineEvent {
kind: TimelineEventKind::Decrypted(DecryptedRoomEvent {
event: Raw::new(&example_event()).unwrap().cast(),
encryption_info: EncryptionInfo {
sender: user_id!("@sender:example.com").to_owned(),
sender_device: Some(device_id!("ABCDEFGHIJ").to_owned()),
algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
curve25519_key: "xxx".to_owned(),
sender_claimed_keys: BTreeMap::from([
(
DeviceKeyAlgorithm::Ed25519,
"I3YsPwqMZQXHkSQbjFNEs7b529uac2xBpI83eN3LUXo".to_owned(),
),
(
DeviceKeyAlgorithm::Curve25519,
"qzdW3F5IMPFl0HQgz5w/L5Oi/npKUFn8Um84acIHfPY".to_owned(),
),
]),
},
verification_state: VerificationState::Verified,
},
unsigned_encryption_info: Some(BTreeMap::from([(
UnsignedEventLocation::RelationsThreadLatestEvent,
UnsignedDecryptionResult::UnableToDecrypt(UnableToDecryptInfo {
session_id: Some("xyz".to_owned()),
reason: UnableToDecryptReason::MissingMegolmSession {
withheld_code: Some(WithheldCode::Unverified),
},
}),
)])),
}),
push_actions: Default::default(),
};
with_settings!({sort_maps =>true}, {
// We use directly the serde_json formatter here, because of a bug in insta
// not serializing custom BTreeMap key enum https://github.com/mitsuhiko/insta/issues/689
assert_json_snapshot! {
serde_json::to_value(&room_event).unwrap(),
}
});
}
}
+1 -1
View File
@@ -81,7 +81,7 @@ impl<T: 'static> Future for JoinHandle<T> {
#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
use matrix_sdk_test::async_test;
use matrix_sdk_test_macros::async_test;
use super::spawn;
+10 -14
View File
@@ -15,16 +15,12 @@
//! A TTL cache which can be used to time out repeated operations that might
//! experience intermittent failures.
use std::{
borrow::Borrow,
collections::HashMap,
hash::Hash,
sync::{Arc, RwLock},
time::Duration,
};
use std::{borrow::Borrow, collections::HashMap, hash::Hash, sync::Arc, time::Duration};
use ruma::time::Instant;
use super::locks::RwLock;
const MAX_DELAY: u64 = 15 * 60;
const MULTIPLIER: u64 = 15;
@@ -105,7 +101,7 @@ where
T: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let lock = self.inner.items.read().unwrap();
let lock = self.inner.items.read();
let contains = if let Some(item) = lock.get(key) { !item.expired() } else { false };
@@ -127,7 +123,7 @@ where
T: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let lock = self.inner.items.read().unwrap();
let lock = self.inner.items.read();
lock.get(key).map(|i| i.failure_count)
}
@@ -155,7 +151,7 @@ where
/// not, will have their TTL extended using an exponential backoff
/// algorithm.
pub fn extend(&self, iterator: impl IntoIterator<Item = T>) {
let mut lock = self.inner.items.write().unwrap();
let mut lock = self.inner.items.write();
let now = Instant::now();
@@ -181,7 +177,7 @@ where
T: Borrow<Q>,
Q: Hash + Eq + 'a + ?Sized,
{
let mut lock = self.inner.items.write().unwrap();
let mut lock = self.inner.items.write();
for item in iterator {
lock.remove(item);
@@ -194,7 +190,7 @@ where
/// for immediate retry.
#[doc(hidden)]
pub fn expire(&self, item: &T) {
let mut lock = self.inner.items.write().unwrap();
let mut lock = self.inner.items.write();
lock.get_mut(item).map(FailuresItem::expire);
}
}
@@ -221,11 +217,11 @@ mod tests {
cache.extend([1u8].iter());
assert!(cache.contains(&1));
cache.inner.items.write().unwrap().get_mut(&1).unwrap().duration = Duration::from_secs(0);
cache.inner.items.write().get_mut(&1).unwrap().duration = Duration::from_secs(0);
assert!(!cache.contains(&1));
cache.remove([1u8].iter());
assert!(cache.inner.items.read().unwrap().get(&1).is_none())
assert!(cache.inner.items.read().get(&1).is_none())
}
#[test]
+2 -2
View File
@@ -306,7 +306,7 @@ impl<'a> JsFieldVisitor<'a> {
}
}
impl<'a> tracing::field::Visit for JsFieldVisitor<'a> {
impl tracing::field::Visit for JsFieldVisitor<'_> {
fn record_debug(&mut self, field: &Field, value: &dyn Debug) {
if self.result.is_err() {
return;
@@ -351,7 +351,7 @@ pub fn make_tracing_subscriber(logger: Option<JsLogger>) -> JsLoggingSubscriber
#[cfg(test)]
pub(crate) mod tests {
use matrix_sdk_test::async_test;
use matrix_sdk_test_macros::async_test;
use tracing::{debug, subscriber::with_default};
use wasm_bindgen::{JsCast, JsValue};
+2
View File
@@ -26,7 +26,9 @@ pub mod deserialized_responses;
pub mod executor;
pub mod failures_cache;
pub mod linked_chunk;
pub mod locks;
pub mod ring_buffer;
pub mod sleep;
pub mod store_locks;
pub mod timeout;
pub mod tracing_timer;
@@ -99,7 +99,7 @@ impl UpdateToVectorDiff {
let mut initial_chunk_lengths = VecDeque::new();
for chunk in chunk_iterator {
initial_chunk_lengths.push_front((
initial_chunk_lengths.push_back((
chunk.identifier(),
match chunk.content() {
ChunkContent::Gap(_) => 0,
@@ -142,17 +142,19 @@ impl UpdateToVectorDiff {
/// [`VectorDiff`] is emitted,
/// * [`Update::StartReattachItems`] and [`Update::EndReattachItems`] are
/// respectively muting or unmuting the emission of [`VectorDiff`] by
/// [`Update::PushItems`].
/// [`Update::PushItems`],
/// * [`Update::Clear`] reinitialises the state.
///
/// The only `VectorDiff` that are emitted are [`VectorDiff::Insert`] or
/// [`VectorDiff::Append`] because a [`LinkedChunk`] is append-only.
/// The only `VectorDiff` that are emitted are [`VectorDiff::Insert`],
/// [`VectorDiff::Append`], [`VectorDiff::Remove`] and
/// [`VectorDiff::Clear`].
///
/// `VectorDiff::Append` is an optimisation when numerous
/// `VectorDiff::Insert`s have to be emitted at the last position.
///
/// `VectorDiff::Insert` need an index. To compute this index, the algorithm
/// will iterate over all pairs to accumulate each chunk length until it
/// finds the appropriate pair (given by
/// `VectorDiff::Insert` needs an index. To compute this index, the
/// algorithm will iterate over all pairs to accumulate each chunk length
/// until it finds the appropriate pair (given by
/// [`Update::PushItems::at`]). This is _the offset_. To this offset, the
/// algorithm adds the position's index of the new items (still given by
/// [`Update::PushItems::at`]). This is _the index_. This logic works
@@ -302,6 +304,12 @@ impl UpdateToVectorDiff {
self.chunks.insert(next_chunk_index, (*new, 0));
}
// First chunk!
(None, None) if self.chunks.is_empty() => {
self.chunks.push_back((*new, 0));
}
// Impossible state.
(None, None) => {
unreachable!(
"Inserting new chunk with no previous nor next chunk identifiers \
@@ -356,6 +364,14 @@ impl UpdateToVectorDiff {
}
}
Update::ReplaceItem { at: position, item } => {
let (offset, (_chunk_index, _chunk_length)) = self.map_to_offset(position);
// The chunk length doesn't change.
diffs.push(VectorDiff::Set { index: offset, value: item.clone() });
}
Update::RemoveItem { at: position } => {
let (offset, (_chunk_index, chunk_length)) = self.map_to_offset(position);
@@ -405,6 +421,14 @@ impl UpdateToVectorDiff {
// Exiting the _detaching_ mode.
detaching = false;
}
Update::Clear => {
// Clean `self.chunks`.
self.chunks.clear();
// Let's straightforwardly emit a `VectorDiff::Clear`.
diffs.push(VectorDiff::Clear);
}
}
}
@@ -450,10 +474,11 @@ impl UpdateToVectorDiff {
mod tests {
use std::fmt::Debug;
use assert_matches::assert_matches;
use imbl::{vector, Vector};
use super::{
super::{EmptyChunk, LinkedChunk},
super::{ChunkIdentifierGenerator, EmptyChunk, LinkedChunk},
VectorDiff,
};
@@ -467,14 +492,7 @@ mod tests {
assert_eq!(diffs, expected_diffs);
for diff in diffs {
match diff {
VectorDiff::Insert { index, value } => accumulator.insert(index, value),
VectorDiff::Append { values } => accumulator.append(values),
VectorDiff::Remove { index } => {
accumulator.remove(index);
}
diff => unimplemented!("{diff:?}"),
}
diff.apply(accumulator);
}
}
@@ -686,18 +704,101 @@ mod tests {
&[VectorDiff::Insert { index: 14, value: 'z' }],
);
drop(linked_chunk);
assert!(as_vector.take().is_empty());
// Finally, ensure the “reconstitued” vector is the one expected.
// Ensure the “reconstitued” vector is the one expected.
assert_eq!(
accumulator,
vector!['m', 'a', 'w', 'x', 'y', 'b', 'd', 'i', 'j', 'k', 'l', 'e', 'f', 'g', 'z', 'h']
);
// Replace element 8 by an uppercase J.
linked_chunk
.replace_item_at(linked_chunk.item_position(|item| *item == 'j').unwrap(), 'J')
.unwrap();
assert_items_eq!(
linked_chunk,
['m', 'a', 'w'] ['x'] ['y', 'b'] ['d'] ['i', 'J', 'k'] ['l'] ['e', 'f', 'g'] ['z', 'h']
);
// From an `ObservableVector` point of view, it would look like:
//
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
// | m | a | w | x | y | b | d | i | J | k | l | e | f | g | z | h |
// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
// ^^^^
// |
// new!
apply_and_assert_eq(
&mut accumulator,
as_vector.take(),
&[VectorDiff::Set { index: 8, value: 'J' }],
);
// Let's try to clear the linked chunk now.
linked_chunk.clear();
apply_and_assert_eq(&mut accumulator, as_vector.take(), &[VectorDiff::Clear]);
assert!(accumulator.is_empty());
drop(linked_chunk);
assert!(as_vector.take().is_empty());
}
#[test]
fn updates_are_drained_when_constructing_as_vector() {
fn test_as_vector_with_update_clear() {
let mut linked_chunk = LinkedChunk::<3, char, ()>::new_with_update_history();
let mut as_vector = linked_chunk.as_vector().unwrap();
{
// 1 initial chunk in the `UpdateToVectorDiff` mapper.
let chunks = &as_vector.mapper.chunks;
assert_eq!(chunks.len(), 1);
assert_eq!(chunks[0].0, ChunkIdentifierGenerator::FIRST_IDENTIFIER);
assert_eq!(chunks[0].1, 0);
assert!(as_vector.take().is_empty());
}
linked_chunk.push_items_back(['a', 'b', 'c', 'd']);
{
let diffs = as_vector.take();
assert_eq!(diffs.len(), 2);
assert_matches!(&diffs[0], VectorDiff::Append { .. });
assert_matches!(&diffs[1], VectorDiff::Append { .. });
// 2 chunks in the `UpdateToVectorDiff` mapper.
assert_eq!(as_vector.mapper.chunks.len(), 2);
}
linked_chunk.clear();
{
let diffs = as_vector.take();
assert_eq!(diffs.len(), 1);
assert_matches!(&diffs[0], VectorDiff::Clear);
// 1 chunk in the `UpdateToVectorDiff` mapper.
let chunks = &as_vector.mapper.chunks;
assert_eq!(chunks.len(), 1);
assert_eq!(chunks[0].0, ChunkIdentifierGenerator::FIRST_IDENTIFIER);
assert_eq!(chunks[0].1, 0);
}
// And we can push again.
linked_chunk.push_items_back(['a', 'b', 'c', 'd']);
{
let diffs = as_vector.take();
assert_eq!(diffs.len(), 2);
assert_matches!(&diffs[0], VectorDiff::Append { .. });
assert_matches!(&diffs[1], VectorDiff::Append { .. });
}
}
#[test]
fn test_updates_are_drained_when_constructing_as_vector() {
let mut linked_chunk = LinkedChunk::<10, char, ()>::new_with_update_history();
linked_chunk.push_items_back(['a']);
@@ -717,6 +818,40 @@ mod tests {
assert_eq!(diffs.len(), 1);
}
#[test]
fn test_as_vector_with_initial_content() {
// Fill the linked chunk with some initial items.
let mut linked_chunk = LinkedChunk::<3, char, ()>::new_with_update_history();
linked_chunk.push_items_back(['a', 'b', 'c', 'd']);
#[rustfmt::skip]
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] ['d']);
// Empty updates first.
let _ = linked_chunk.updates().unwrap().take();
// Start observing future updates.
let mut as_vector = linked_chunk.as_vector().unwrap();
assert!(as_vector.take().is_empty());
// It's important to cause a change that will create new chunks, like pushing
// enough items.
linked_chunk.push_items_back(['e', 'f', 'g']);
#[rustfmt::skip]
assert_items_eq!(linked_chunk, ['a', 'b', 'c'] ['d', 'e', 'f'] ['g']);
// And the vector diffs can be computed without crashing.
let diffs = as_vector.take();
assert_eq!(diffs.len(), 2);
assert_matches!(&diffs[0], VectorDiff::Append { values } => {
assert_eq!(*values, ['e', 'f'].into());
});
assert_matches!(&diffs[1], VectorDiff::Append { values } => {
assert_eq!(*values, ['g'].into());
});
}
#[cfg(not(target_arch = "wasm32"))]
mod proptests {
use proptest::prelude::*;
@@ -748,7 +883,7 @@ mod tests {
proptest! {
#[test]
fn as_vector_is_correct(
fn test_as_vector_is_correct(
operations in prop::collection::vec(as_vector_operation_strategy(), 50..=200)
) {
let mut linked_chunk = LinkedChunk::<10, char, ()>::new_with_update_history();
@@ -0,0 +1,479 @@
// Copyright 2024 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::{
collections::{BTreeMap, HashSet},
marker::PhantomData,
};
use tracing::error;
use super::{
Chunk, ChunkContent, ChunkIdentifier, ChunkIdentifierGenerator, Ends, LinkedChunk,
ObservableUpdates, RawChunk,
};
/// A temporary chunk representation in the [`LinkedChunkBuilder`].
///
/// Instead of using linking the chunks with pointers, this uses
/// [`ChunkIdentifier`] as the temporary links to the previous and next chunks,
/// which will get resolved later when re-building the full data structure. This
/// allows using chunks that references other chunks that aren't known yet.
struct TemporaryChunk<Item, Gap> {
previous: Option<ChunkIdentifier>,
next: Option<ChunkIdentifier>,
content: ChunkContent<Item, Gap>,
}
/// A data structure to rebuild a linked chunk from its raw representation.
///
/// A linked chunk can be rebuilt incrementally from its internal
/// representation, with the chunks being added *in any order*, as long as they
/// form a single connected component eventually (viz., there's no
/// subgraphs/sublists isolated from the one final linked list). If they don't,
/// then the final call to [`LinkedChunkBuilder::build()`] will result in an
/// error).
#[allow(missing_debug_implementations)]
pub struct LinkedChunkBuilder<const CAP: usize, Item, Gap> {
/// Work-in-progress chunks.
chunks: BTreeMap<ChunkIdentifier, TemporaryChunk<Item, Gap>>,
/// Is the final `LinkedChunk` expected to include an update history, as if
/// it were created with [`LinkedChunk::new_with_update_history`]?
build_with_update_history: bool,
}
impl<const CAP: usize, Item, Gap> Default for LinkedChunkBuilder<CAP, Item, Gap> {
fn default() -> Self {
Self::new()
}
}
impl<const CAP: usize, Item, Gap> LinkedChunkBuilder<CAP, Item, Gap> {
/// Create an empty [`LinkedChunkBuilder`] with no update history.
pub fn new() -> Self {
Self { chunks: Default::default(), build_with_update_history: false }
}
/// Stash a gap chunk with its content.
///
/// This can be called even if the previous and next chunks have not been
/// added yet. Resolving these chunks will happen at the time of calling
/// [`LinkedChunkBuilder::build()`].
pub fn push_gap(
&mut self,
previous: Option<ChunkIdentifier>,
id: ChunkIdentifier,
next: Option<ChunkIdentifier>,
content: Gap,
) {
let chunk = TemporaryChunk { previous, next, content: ChunkContent::Gap(content) };
self.chunks.insert(id, chunk);
}
/// Stash an item chunk with its contents.
///
/// This can be called even if the previous and next chunks have not been
/// added yet. Resolving these chunks will happen at the time of calling
/// [`LinkedChunkBuilder::build()`].
pub fn push_items(
&mut self,
previous: Option<ChunkIdentifier>,
id: ChunkIdentifier,
next: Option<ChunkIdentifier>,
items: impl IntoIterator<Item = Item>,
) {
let chunk = TemporaryChunk {
previous,
next,
content: ChunkContent::Items(items.into_iter().collect()),
};
self.chunks.insert(id, chunk);
}
/// Request that the resulting linked chunk will have an update history, as
/// if it were created with [`LinkedChunk::new_with_update_history`].
pub fn with_update_history(&mut self) {
self.build_with_update_history = true;
}
/// Run all error checks before reconstructing the full linked chunk.
///
/// Must be called after checking `self.chunks` isn't empty in
/// [`Self::build`].
///
/// Returns the identifier of the first chunk.
fn check_consistency(&mut self) -> Result<ChunkIdentifier, LinkedChunkBuilderError> {
// Look for the first id.
let first_id =
self.chunks.iter().find_map(|(id, chunk)| chunk.previous.is_none().then_some(*id));
// There's no first chunk, but we've checked that `self.chunks` isn't empty:
// it's a malformed list.
let Some(first_id) = first_id else {
return Err(LinkedChunkBuilderError::MissingFirstChunk);
};
// We're going to iterate from the first to the last chunk.
// Keep track of chunks we've already visited.
let mut visited = HashSet::new();
// Start from the first chunk.
let mut maybe_cur = Some(first_id);
while let Some(cur) = maybe_cur {
// The chunk must be referenced in `self.chunks`.
let Some(chunk) = self.chunks.get(&cur) else {
return Err(LinkedChunkBuilderError::MissingChunk { id: cur });
};
if let ChunkContent::Items(items) = &chunk.content {
if items.len() > CAP {
return Err(LinkedChunkBuilderError::ChunkTooLarge { id: cur });
}
}
// If it's not the first chunk,
if cur != first_id {
// It must have a previous link.
let Some(prev) = chunk.previous else {
return Err(LinkedChunkBuilderError::MultipleFirstChunks {
first_candidate: first_id,
second_candidate: cur,
});
};
// And we must have visited its predecessor at this point, since we've
// iterated from the first chunk.
if !visited.contains(&prev) {
return Err(LinkedChunkBuilderError::MissingChunk { id: prev });
}
}
// Add the current chunk to the list of seen chunks.
if !visited.insert(cur) {
// If we didn't insert, then it was already visited: there's a cycle!
return Err(LinkedChunkBuilderError::Cycle { repeated: cur });
}
// Move on to the next chunk. If it's none, we'll quit the loop.
maybe_cur = chunk.next;
}
// If there are more chunks than those we've visited: some of them were not
// linked to the "main" branch of the linked list, so we had multiple connected
// components.
if visited.len() != self.chunks.len() {
return Err(LinkedChunkBuilderError::MultipleConnectedComponents);
}
Ok(first_id)
}
pub fn build(mut self) -> Result<Option<LinkedChunk<CAP, Item, Gap>>, LinkedChunkBuilderError> {
if self.chunks.is_empty() {
return Ok(None);
}
// Run checks.
let first_id = self.check_consistency()?;
// We're now going to iterate from the first to the last chunk. As we're doing
// this, we're also doing a few other things:
//
// - rebuilding the final `Chunk`s one by one, that will be linked using
// pointers,
// - counting items from the item chunks we'll encounter,
// - finding the max `ChunkIdentifier` (`max_chunk_id`).
let mut max_chunk_id = first_id.index();
// Small helper to graduate a temporary chunk into a final one. As we're doing
// this, we're also updating the maximum chunk id (that will be used to
// set up the id generator), and the number of items in this chunk.
let mut graduate_chunk = |id: ChunkIdentifier| {
let temp = self.chunks.remove(&id)?;
// Update the maximum chunk identifier, while we're around.
max_chunk_id = max_chunk_id.max(id.index());
// Graduate the current temporary chunk into a final chunk.
let chunk_ptr = Chunk::new_leaked(id, temp.content);
Some((temp.next, chunk_ptr))
};
let Some((mut next_chunk_id, first_chunk_ptr)) = graduate_chunk(first_id) else {
// Can't really happen, but oh well.
return Err(LinkedChunkBuilderError::MissingFirstChunk);
};
let mut prev_chunk_ptr = first_chunk_ptr;
while let Some(id) = next_chunk_id {
let Some((new_next, mut chunk_ptr)) = graduate_chunk(id) else {
// Can't really happen, but oh well.
return Err(LinkedChunkBuilderError::MissingChunk { id });
};
let chunk = unsafe { chunk_ptr.as_mut() };
// Link the current chunk to its previous one.
let prev_chunk = unsafe { prev_chunk_ptr.as_mut() };
prev_chunk.next = Some(chunk_ptr);
chunk.previous = Some(prev_chunk_ptr);
// Prepare for the next iteration.
prev_chunk_ptr = chunk_ptr;
next_chunk_id = new_next;
}
debug_assert!(self.chunks.is_empty());
// Maintain the convention that `Ends::last` may be unset.
let last_chunk_ptr = prev_chunk_ptr;
let last_chunk_ptr =
if first_chunk_ptr == last_chunk_ptr { None } else { Some(last_chunk_ptr) };
let links = Ends { first: first_chunk_ptr, last: last_chunk_ptr };
let chunk_identifier_generator =
ChunkIdentifierGenerator::new_from_previous_chunk_identifier(ChunkIdentifier::new(
max_chunk_id,
));
let updates =
if self.build_with_update_history { Some(ObservableUpdates::new()) } else { None };
Ok(Some(LinkedChunk { links, chunk_identifier_generator, updates, marker: PhantomData }))
}
/// Fills a linked chunk builder from all the given raw parts.
pub fn from_raw_parts(raws: Vec<RawChunk<Item, Gap>>) -> Self {
let mut this = Self::new();
for raw in raws {
match raw.content {
ChunkContent::Gap(gap) => {
this.push_gap(raw.previous, raw.identifier, raw.next, gap);
}
ChunkContent::Items(vec) => {
this.push_items(raw.previous, raw.identifier, raw.next, vec);
}
}
}
this
}
}
#[derive(thiserror::Error, Debug)]
pub enum LinkedChunkBuilderError {
#[error("chunk with id {} is too large", id.index())]
ChunkTooLarge { id: ChunkIdentifier },
#[error("there's no first chunk")]
MissingFirstChunk,
#[error("there are multiple first chunks")]
MultipleFirstChunks { first_candidate: ChunkIdentifier, second_candidate: ChunkIdentifier },
#[error("unable to resolve chunk with id {}", id.index())]
MissingChunk { id: ChunkIdentifier },
#[error("rebuilt chunks form a cycle: repeated identifier: {}", repeated.index())]
Cycle { repeated: ChunkIdentifier },
#[error("multiple connected components")]
MultipleConnectedComponents,
}
#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
use super::LinkedChunkBuilder;
use crate::linked_chunk::{ChunkIdentifier, LinkedChunkBuilderError};
#[test]
fn test_empty() {
let lcb = LinkedChunkBuilder::<3, char, char>::new();
// Building an empty linked chunk works, and returns `None`.
let lc = lcb.build().unwrap();
assert!(lc.is_none());
}
#[test]
fn test_success() {
let mut lcb = LinkedChunkBuilder::<3, char, char>::new();
let cid0 = ChunkIdentifier::new(0);
let cid1 = ChunkIdentifier::new(1);
// Note: cid2 is missing on purpose, to confirm that it's fine to have holes in
// the chunk id space.
let cid3 = ChunkIdentifier::new(3);
// Check that we can successfully create a linked chunk, independently of the
// order in which chunks are added.
//
// The final chunk will contain [cid0 <-> cid1 <-> cid3], in this order.
// Adding chunk cid0.
lcb.push_items(None, cid0, Some(cid1), vec!['a', 'b', 'c']);
// Adding chunk cid3.
lcb.push_items(Some(cid1), cid3, None, vec!['d', 'e']);
// Adding chunk cid1.
lcb.push_gap(Some(cid0), cid1, Some(cid3), 'g');
let mut lc =
lcb.build().expect("building works").expect("returns a non-empty linked chunk");
// Check the entire content first.
assert_items_eq!(lc, ['a', 'b', 'c'] [-] ['d', 'e']);
// Run checks on the first chunk.
let mut chunks = lc.chunks();
let first_chunk = chunks.next().unwrap();
{
assert!(first_chunk.previous().is_none());
assert_eq!(first_chunk.identifier(), cid0);
}
// Run checks on the second chunk.
let second_chunk = chunks.next().unwrap();
{
assert_eq!(second_chunk.identifier(), first_chunk.next().unwrap().identifier());
assert_eq!(second_chunk.previous().unwrap().identifier(), first_chunk.identifier());
assert_eq!(second_chunk.identifier(), cid1);
}
// Run checks on the third chunk.
let third_chunk = chunks.next().unwrap();
{
assert_eq!(third_chunk.identifier(), second_chunk.next().unwrap().identifier());
assert_eq!(third_chunk.previous().unwrap().identifier(), second_chunk.identifier());
assert!(third_chunk.next().is_none());
assert_eq!(third_chunk.identifier(), cid3);
}
// There's no more chunk.
assert!(chunks.next().is_none());
// The linked chunk had 5 items.
assert_eq!(lc.num_items(), 5);
// Now, if we add a new chunk, its identifier should be the previous one we used
// + 1.
lc.push_gap_back('h');
let last_chunk = lc.chunks().last().unwrap();
assert_eq!(last_chunk.identifier(), ChunkIdentifier::new(cid3.index() + 1));
}
#[test]
fn test_chunk_too_large() {
let mut lcb = LinkedChunkBuilder::<3, char, char>::new();
let cid0 = ChunkIdentifier::new(0);
// Adding a chunk with 4 items will fail, because the max capacity specified in
// the builder generics is 3.
lcb.push_items(None, cid0, None, vec!['a', 'b', 'c', 'd']);
let res = lcb.build();
assert_matches!(res, Err(LinkedChunkBuilderError::ChunkTooLarge { id }) => {
assert_eq!(id, cid0);
});
}
#[test]
fn test_missing_first_chunk() {
let mut lcb = LinkedChunkBuilder::<3, char, char>::new();
let cid0 = ChunkIdentifier::new(0);
let cid1 = ChunkIdentifier::new(1);
let cid2 = ChunkIdentifier::new(2);
lcb.push_gap(Some(cid2), cid0, Some(cid1), 'g');
lcb.push_items(Some(cid0), cid1, Some(cid2), ['a', 'b', 'c']);
lcb.push_items(Some(cid1), cid2, Some(cid0), ['d', 'e', 'f']);
let res = lcb.build();
assert_matches!(res, Err(LinkedChunkBuilderError::MissingFirstChunk));
}
#[test]
fn test_multiple_first_chunks() {
let mut lcb = LinkedChunkBuilder::<3, char, char>::new();
let cid0 = ChunkIdentifier::new(0);
let cid1 = ChunkIdentifier::new(1);
lcb.push_gap(None, cid0, Some(cid1), 'g');
// Second chunk lies and pretends to be the first too.
lcb.push_items(None, cid1, Some(cid0), ['a', 'b', 'c']);
let res = lcb.build();
assert_matches!(res, Err(LinkedChunkBuilderError::MultipleFirstChunks { first_candidate, second_candidate }) => {
assert_eq!(first_candidate, cid0);
assert_eq!(second_candidate, cid1);
});
}
#[test]
fn test_missing_chunk() {
let mut lcb = LinkedChunkBuilder::<3, char, char>::new();
let cid0 = ChunkIdentifier::new(0);
let cid1 = ChunkIdentifier::new(1);
lcb.push_gap(None, cid0, Some(cid1), 'g');
let res = lcb.build();
assert_matches!(res, Err(LinkedChunkBuilderError::MissingChunk { id }) => {
assert_eq!(id, cid1);
});
}
#[test]
fn test_cycle() {
let mut lcb = LinkedChunkBuilder::<3, char, char>::new();
let cid0 = ChunkIdentifier::new(0);
let cid1 = ChunkIdentifier::new(1);
lcb.push_gap(None, cid0, Some(cid1), 'g');
lcb.push_gap(Some(cid0), cid1, Some(cid0), 'g');
let res = lcb.build();
assert_matches!(res, Err(LinkedChunkBuilderError::Cycle { repeated }) => {
assert_eq!(repeated, cid0);
});
}
#[test]
fn test_multiple_connected_components() {
let mut lcb = LinkedChunkBuilder::<3, char, char>::new();
let cid0 = ChunkIdentifier::new(0);
let cid1 = ChunkIdentifier::new(1);
let cid2 = ChunkIdentifier::new(2);
// cid0 and cid1 are linked to each other.
lcb.push_gap(None, cid0, Some(cid1), 'g');
lcb.push_items(Some(cid0), cid1, None, ['a', 'b', 'c']);
// cid2 stands on its own.
lcb.push_items(None, cid2, None, ['d', 'e', 'f']);
let res = lcb.build();
assert_matches!(res, Err(LinkedChunkBuilderError::MultipleConnectedComponents));
}
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -29,6 +29,9 @@ use super::{ChunkIdentifier, Position};
///
/// These updates are useful to store a `LinkedChunk` in another form of
/// storage, like a database or something similar.
///
/// [`LinkedChunk`]: super::LinkedChunk
/// [`LinkedChunk::updates`]: super::LinkedChunk::updates
#[derive(Debug, Clone, PartialEq)]
pub enum Update<Item, Gap> {
/// A new chunk of kind Items has been created.
@@ -76,6 +79,18 @@ pub enum Update<Item, Gap> {
items: Vec<Item>,
},
/// An item has been replaced in the linked chunk.
///
/// The `at` position MUST resolve to the actual position an existing *item*
/// (not a gap).
ReplaceItem {
/// The position of the item that's being replaced.
at: Position,
/// The new value for the item.
item: Item,
},
/// An item has been removed inside a chunk of kind Items.
RemoveItem {
/// The [`Position`] of the item.
@@ -96,11 +111,17 @@ pub enum Update<Item, Gap> {
/// Reattaching items (see [`Self::StartReattachItems`]) is finished.
EndReattachItems,
/// All chunks have been cleared, i.e. all items and all gaps have been
/// dropped.
Clear,
}
/// A collection of [`Update`]s that can be observed.
///
/// Get a value for this type with [`LinkedChunk::updates`].
///
/// [`LinkedChunk::updates`]: super::LinkedChunk::updates
#[derive(Debug)]
pub struct ObservableUpdates<Item, Gap> {
pub(super) inner: Arc<RwLock<UpdatesInner<Item, Gap>>>,
@@ -120,7 +141,7 @@ impl<Item, Gap> ObservableUpdates<Item, Gap> {
/// Take new updates.
///
/// Updates that have been taken will not be read again.
pub(super) fn take(&mut self) -> Vec<Update<Item, Gap>>
pub fn take(&mut self) -> Vec<Update<Item, Gap>>
where
Item: Clone,
Gap: Clone,
@@ -129,6 +150,7 @@ impl<Item, Gap> ObservableUpdates<Item, Gap> {
}
/// Subscribe to updates by using a [`Stream`].
#[cfg(test)]
pub(super) fn subscribe(&mut self) -> UpdatesSubscriber<Item, Gap> {
// A subscriber is a new update reader, it needs its own token.
let token = self.new_reader_token();
@@ -255,6 +277,7 @@ impl<Item, Gap> UpdatesInner<Item, Gap> {
}
/// Return the number of updates in the buffer.
#[cfg(test)]
fn len(&self) -> usize {
self.updates.len()
}
@@ -293,6 +316,7 @@ pub(super) struct UpdatesSubscriber<Item, Gap> {
impl<Item, Gap> UpdatesSubscriber<Item, Gap> {
/// Create a new [`Self`].
#[cfg(test)]
fn new(updates: Weak<RwLock<UpdatesInner<Item, Gap>>>, token: ReaderToken) -> Self {
Self { updates, token }
}
@@ -375,7 +399,21 @@ mod tests {
other_token
};
// There is no new update yet.
// There is an initial update.
{
let updates = linked_chunk.updates().unwrap();
assert_eq!(
updates.take(),
&[NewItemsChunk { previous: None, new: ChunkIdentifier(0), next: None }],
);
assert_eq!(
updates.inner.write().unwrap().take_with_token(other_token),
&[NewItemsChunk { previous: None, new: ChunkIdentifier(0), next: None }],
);
}
// No new update.
{
let updates = linked_chunk.updates().unwrap();
@@ -608,7 +646,16 @@ mod tests {
let updates_subscriber = linked_chunk.updates().unwrap().subscribe();
pin_mut!(updates_subscriber);
// No update, stream is pending.
// Initial update, stream is ready.
assert_matches!(
updates_subscriber.as_mut().poll_next(&mut context),
Poll::Ready(Some(items)) => {
assert_eq!(
items,
&[NewItemsChunk { previous: None, new: ChunkIdentifier(0), next: None }]
);
}
);
assert_matches!(updates_subscriber.as_mut().poll_next(&mut context), Poll::Pending);
assert_eq!(*counter_waker.number_of_wakeup.lock().unwrap(), 0);
@@ -642,6 +689,7 @@ mod tests {
assert_eq!(
linked_chunk.updates().unwrap().take(),
&[
NewItemsChunk { previous: None, new: ChunkIdentifier(0), next: None },
PushItems { at: Position(ChunkIdentifier(0), 0), items: vec!['a'] },
PushItems { at: Position(ChunkIdentifier(0), 1), items: vec!['b'] },
PushItems { at: Position(ChunkIdentifier(0), 2), items: vec!['c'] },
@@ -692,9 +740,28 @@ mod tests {
let updates_subscriber2 = linked_chunk.updates().unwrap().subscribe();
pin_mut!(updates_subscriber2);
// No update, streams are pending.
// Initial updates, streams are ready.
assert_matches!(
updates_subscriber1.as_mut().poll_next(&mut context1),
Poll::Ready(Some(items)) => {
assert_eq!(
items,
&[NewItemsChunk { previous: None, new: ChunkIdentifier(0), next: None }]
);
}
);
assert_matches!(updates_subscriber1.as_mut().poll_next(&mut context1), Poll::Pending);
assert_eq!(*counter_waker1.number_of_wakeup.lock().unwrap(), 0);
assert_matches!(
updates_subscriber2.as_mut().poll_next(&mut context2),
Poll::Ready(Some(items)) => {
assert_eq!(
items,
&[NewItemsChunk { previous: None, new: ChunkIdentifier(0), next: None }]
);
}
);
assert_matches!(updates_subscriber2.as_mut().poll_next(&mut context2), Poll::Pending);
assert_eq!(*counter_waker2.number_of_wakeup.lock().unwrap(), 0);
+130
View File
@@ -0,0 +1,130 @@
// Copyright 2025 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.
//! Simplified locks hat panic instead of returning a `Result` when the lock is
//! poisoned.
use std::{
fmt,
sync::{Mutex as StdMutex, MutexGuard, RwLock as StdRwLock, RwLockReadGuard, RwLockWriteGuard},
};
use serde::{Deserialize, Serialize};
/// A wrapper around `std::sync::Mutex` that panics on poison.
///
/// This `Mutex` works similarly to the standard library's `Mutex`, except its
/// `lock` method does not return a `Result`. Instead, if the mutex is poisoned,
/// it will panic.
///
/// # Examples
///
/// ```
/// use matrix_sdk_common::locks::Mutex;
///
/// let mutex = Mutex::new(42);
///
/// {
/// let mut guard = mutex.lock();
/// *guard = 100;
/// }
///
/// assert_eq!(*mutex.lock(), 100);
/// ```
#[derive(Default)]
pub struct Mutex<T: ?Sized>(StdMutex<T>);
impl<T> Mutex<T> {
/// Creates a new `Mutex` wrapping the given value.
pub const fn new(t: T) -> Self {
Self(StdMutex::new(t))
}
}
impl<T: ?Sized> Mutex<T> {
/// Acquires the lock, panicking if the lock is poisoned.
///
/// This method blocks the current thread until the lock is acquired.
pub fn lock(&self) -> MutexGuard<'_, T> {
self.0.lock().expect("The Mutex should never be poisoned")
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
/// A wrapper around [`std::sync::RwLock`] that panics on poison.
///
/// This `RwLock` works similarly to the standard library's `RwLock`, except its
/// `read` and `write` methods do not return a `Result`. Instead, if the lock is
/// poisoned, it will panic.
///
/// # Examples
///
/// ```
/// use matrix_sdk_common::locks::RwLock;
///
/// let lock = RwLock::new(42);
///
/// {
/// let read_guard = lock.read();
/// assert_eq!(*read_guard, 42);
/// }
/// {
/// let mut write_guard = lock.write();
/// *write_guard = 100;
/// }
/// assert_eq!(*lock.read(), 100);
/// ```
#[derive(Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct RwLock<T: ?Sized>(StdRwLock<T>);
impl<T> RwLock<T> {
/// Creates a new `RwLock` wrapping the given value.
pub const fn new(t: T) -> Self {
Self(StdRwLock::new(t))
}
}
impl<T: ?Sized> RwLock<T> {
/// Acquires a mutable write lock, panicking if the lock is poisoned.
///
/// This method blocks the current thread until the lock is acquired.
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
self.0.write().expect("The RwLock should never be poisoned")
}
/// Acquires a shared read lock, panicking if the lock is poisoned.
///
/// This method blocks the current thread until the lock is acquired.
pub fn read(&self) -> RwLockReadGuard<'_, T> {
self.0.read().expect("The RwLock should never be poisoned")
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<T> From<T> for RwLock<T> {
fn from(value: T) -> Self {
Self::new(value)
}
}
+49
View File
@@ -0,0 +1,49 @@
// Copyright 2024 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::time::Duration;
/// Sleep for the specified duration.
///
/// This is a cross-platform sleep implementation that works on both wasm32 and
/// non-wasm32 targets.
pub async fn sleep(duration: Duration) {
#[cfg(not(target_arch = "wasm32"))]
tokio::time::sleep(duration).await;
#[cfg(target_arch = "wasm32")]
gloo_timers::future::TimeoutFuture::new(u32::try_from(duration.as_millis()).unwrap_or_else(
|_| {
tracing::error!("Sleep duration too long, sleeping for u32::MAX ms");
u32::MAX
},
))
.await;
}
#[cfg(test)]
mod tests {
use matrix_sdk_test_macros::async_test;
use super::*;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
#[async_test]
async fn test_sleep() {
// Just test that it doesn't panic
sleep(Duration::from_millis(1)).await;
}
}
@@ -0,0 +1,13 @@
---
source: crates/matrix-sdk-common/src/deserialized_responses.rs
expression: info
---
{
"MegolmV1AesSha2": {
"curve25519_key": "curvecurvecurve",
"sender_claimed_keys": {
"ed25519": "claimedclaimeded25519",
"curve25519": "claimedclaimedcurve25519"
}
}
}
@@ -0,0 +1,15 @@
---
source: crates/matrix-sdk-common/src/deserialized_responses.rs
expression: info
---
{
"sender": "@alice:localhost",
"sender_device": "ABCDEFGH",
"algorithm_info": {
"MegolmV1AesSha2": {
"curve25519_key": "curvecurvecurve",
"sender_claimed_keys": {}
}
},
"verification_state": "Verified"
}

Some files were not shown because too many files have changed in this diff Show More