mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-07 23:44:53 -04:00
sdk: Add private method to get the current push rules
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
committed by
Jonas Platte
parent
f5ba7ddc4a
commit
fae10ae7f3
@@ -35,15 +35,18 @@ use ruma::{
|
||||
assign,
|
||||
events::{
|
||||
ignored_user_list::{IgnoredUser, IgnoredUserListEventContent},
|
||||
push_rules::PushRulesEventContent,
|
||||
room::MediaSource,
|
||||
AnyGlobalAccountDataEventContent, GlobalAccountDataEventContent,
|
||||
GlobalAccountDataEventType, StaticEventContent,
|
||||
},
|
||||
push::Ruleset,
|
||||
serde::Raw,
|
||||
thirdparty::Medium,
|
||||
ClientSecret, MxcUri, OwnedMxcUri, OwnedUserId, RoomId, SessionId, UInt, UserId,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use tracing::error;
|
||||
|
||||
use crate::{config::RequestConfig, Client, Error, HttpError, Result};
|
||||
|
||||
@@ -822,6 +825,30 @@ impl Account {
|
||||
.unwrap_or_default();
|
||||
Ok(ignored_user_list)
|
||||
}
|
||||
|
||||
/// Get the current push rules.
|
||||
///
|
||||
/// If no push rules event was found, or it fails to deserialize, a ruleset
|
||||
/// with the server-default push rules is returned.
|
||||
///
|
||||
/// Panics if called when the client is not logged in.
|
||||
pub(crate) async fn push_rules(&self) -> Result<Ruleset> {
|
||||
Ok(self
|
||||
.account_data::<PushRulesEventContent>()
|
||||
.await?
|
||||
.and_then(|r| match r.deserialize() {
|
||||
Ok(r) => Some(r.global),
|
||||
Err(e) => {
|
||||
error!("Push rules event failed to deserialize: {e}");
|
||||
None
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
Ruleset::server_default(
|
||||
self.client.user_id().expect("The client should be logged in"),
|
||||
)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
fn get_raw_content<Ev, C>(raw: Option<Raw<Ev>>) -> Result<Option<Raw<C>>> {
|
||||
|
||||
@@ -28,7 +28,6 @@ use ruma::{
|
||||
assign,
|
||||
events::{
|
||||
direct::DirectEventContent,
|
||||
push_rules::PushRulesEventContent,
|
||||
receipt::{Receipt, ReceiptThread, ReceiptType},
|
||||
room::{
|
||||
encryption::RoomEncryptionEventContent, history_visibility::HistoryVisibility,
|
||||
@@ -41,7 +40,7 @@ use ruma::{
|
||||
RoomAccountDataEventType, StateEventType, StaticEventContent, StaticStateEventContent,
|
||||
SyncStateEvent,
|
||||
},
|
||||
push::{PushConditionRoomCtx, Ruleset},
|
||||
push::PushConditionRoomCtx,
|
||||
serde::Raw,
|
||||
uint, EventId, MatrixToUri, MatrixUri, OwnedEventId, OwnedServerName, OwnedUserId, RoomId,
|
||||
UInt, UserId,
|
||||
@@ -240,13 +239,7 @@ impl Common {
|
||||
}
|
||||
|
||||
if let Some(push_context) = self.push_context().await? {
|
||||
let push_rules = self
|
||||
.client()
|
||||
.account()
|
||||
.account_data::<PushRulesEventContent>()
|
||||
.await?
|
||||
.and_then(|r| r.deserialize().ok().map(|r| r.global))
|
||||
.unwrap_or_else(|| Ruleset::server_default(self.own_user_id()));
|
||||
let push_rules = self.client().account().push_rules().await?;
|
||||
|
||||
for event in &mut response.chunk {
|
||||
event.push_actions = push_rules.get_actions(&event.event, &push_context).to_owned();
|
||||
|
||||
Reference in New Issue
Block a user