Upgrade ruma

… and refactor push rule code along the way.
This commit is contained in:
Jonas Platte 2021-04-05 21:25:10 +02:00
parent db7044a950
commit fe744c856f
No known key found for this signature in database
GPG key ID: CC154DE0E30B7C67
13 changed files with 261 additions and 862 deletions

View file

@ -21,7 +21,7 @@ use ruma::{
},
EventType,
},
RoomAliasId, RoomId, RoomVersionId, UserId,
push, RoomAliasId, RoomId, RoomVersionId, UserId,
};
use register::RegistrationKind;
@ -181,7 +181,7 @@ pub async fn register_route(
EventType::PushRules,
&ruma::events::push_rules::PushRulesEvent {
content: ruma::events::push_rules::PushRulesEventContent {
global: crate::push_rules::default_pushrules(&user_id),
global: push::Ruleset::server_default(&user_id),
},
},
&db.globals,

View file

@ -46,7 +46,7 @@ pub async fn create_content_route(
db.flush().await?;
Ok(create_content::Response {
content_uri: mxc,
content_uri: mxc.try_into().expect("Invalid mxc:// URI"),
blurhash: None,
}
.into())

View file

@ -10,10 +10,7 @@ use ruma::{
},
},
events::{push_rules, EventType},
push::{
ConditionalPushRuleInit, ContentPushRule, OverridePushRule, PatternedPushRuleInit,
RoomPushRule, SenderPushRule, SimplePushRuleInit, UnderridePushRule,
},
push::{ConditionalPushRuleInit, PatternedPushRuleInit, SimplePushRuleInit},
};
#[cfg(feature = "conduit_bin")]
@ -67,29 +64,24 @@ pub async fn get_pushrule_route(
let rule = match body.kind {
RuleKind::Override => global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.clone().into()),
.get(body.rule_id.as_str())
.map(|rule| rule.clone().into()),
RuleKind::Underride => global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.clone().into()),
.get(body.rule_id.as_str())
.map(|rule| rule.clone().into()),
RuleKind::Sender => global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.clone().into()),
.get(body.rule_id.as_str())
.map(|rule| rule.clone().into()),
RuleKind::Room => global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.clone().into()),
.get(body.rule_id.as_str())
.map(|rule| rule.clone().into()),
RuleKind::Content => global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.clone().into()),
.get(body.rule_id.as_str())
.map(|rule| rule.clone().into()),
RuleKind::_Custom(_) => None,
};
@ -105,14 +97,15 @@ pub async fn get_pushrule_route(
#[cfg_attr(
feature = "conduit_bin",
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<body>")
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<req>")
)]
#[tracing::instrument(skip(db, body))]
#[tracing::instrument(skip(db, req))]
pub async fn set_pushrule_route(
db: State<'_, Database>,
body: Ruma<set_pushrule::Request<'_>>,
req: Ruma<set_pushrule::Request<'_>>,
) -> ConduitResult<set_pushrule::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_user = req.sender_user.as_ref().expect("user is authenticated");
let body = req.body;
if body.scope != "global" {
return Err(Error::BadRequest(
@ -132,107 +125,62 @@ pub async fn set_pushrule_route(
let global = &mut event.content.global;
match body.kind {
RuleKind::Override => {
if let Some(rule) = global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.override_.remove(&rule);
}
global.override_.insert(OverridePushRule(
global.override_.replace(
ConditionalPushRuleInit {
actions: body.actions.clone(),
actions: body.actions,
default: false,
enabled: true,
rule_id: body.rule_id.clone(),
conditions: body.conditions.clone(),
rule_id: body.rule_id,
conditions: body.conditions,
}
.into(),
));
);
}
RuleKind::Underride => {
if let Some(rule) = global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.underride.remove(&rule);
}
global.underride.insert(UnderridePushRule(
global.underride.replace(
ConditionalPushRuleInit {
actions: body.actions.clone(),
actions: body.actions,
default: false,
enabled: true,
rule_id: body.rule_id.clone(),
conditions: body.conditions.clone(),
rule_id: body.rule_id,
conditions: body.conditions,
}
.into(),
));
);
}
RuleKind::Sender => {
if let Some(rule) = global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.sender.remove(&rule);
}
global.sender.insert(SenderPushRule(
global.sender.replace(
SimplePushRuleInit {
actions: body.actions.clone(),
actions: body.actions,
default: false,
enabled: true,
rule_id: body.rule_id.clone(),
rule_id: body.rule_id,
}
.into(),
));
);
}
RuleKind::Room => {
if let Some(rule) = global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.room.remove(&rule);
}
global.room.insert(RoomPushRule(
global.room.replace(
SimplePushRuleInit {
actions: body.actions.clone(),
actions: body.actions,
default: false,
enabled: true,
rule_id: body.rule_id.clone(),
rule_id: body.rule_id,
}
.into(),
));
);
}
RuleKind::Content => {
if let Some(rule) = global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.content.remove(&rule);
}
global.content.insert(ContentPushRule(
global.content.replace(
PatternedPushRuleInit {
actions: body.actions.clone(),
actions: body.actions,
default: false,
enabled: true,
rule_id: body.rule_id.clone(),
pattern: body.pattern.clone().unwrap_or_default(),
rule_id: body.rule_id,
pattern: body.pattern.unwrap_or_default(),
}
.into(),
));
);
}
RuleKind::_Custom(_) => {}
}
@ -280,29 +228,24 @@ pub async fn get_pushrule_actions_route(
let actions = match body.kind {
RuleKind::Override => global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.actions.clone()),
.get(body.rule_id.as_str())
.map(|rule| rule.actions.clone()),
RuleKind::Underride => global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.actions.clone()),
.get(body.rule_id.as_str())
.map(|rule| rule.actions.clone()),
RuleKind::Sender => global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.actions.clone()),
.get(body.rule_id.as_str())
.map(|rule| rule.actions.clone()),
RuleKind::Room => global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.actions.clone()),
.get(body.rule_id.as_str())
.map(|rule| rule.actions.clone()),
RuleKind::Content => global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.actions.clone()),
.get(body.rule_id.as_str())
.map(|rule| rule.actions.clone()),
RuleKind::_Custom(_) => None,
};
@ -343,63 +286,33 @@ pub async fn set_pushrule_actions_route(
let global = &mut event.content.global;
match body.kind {
RuleKind::Override => {
if let Some(mut rule) = global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.override_.remove(&rule);
rule.0.actions = body.actions.clone();
global.override_.insert(rule);
if let Some(mut rule) = global.override_.get(body.rule_id.as_str()).cloned() {
rule.actions = body.actions.clone();
global.override_.replace(rule);
}
}
RuleKind::Underride => {
if let Some(mut rule) = global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.underride.remove(&rule);
rule.0.actions = body.actions.clone();
global.underride.insert(rule);
if let Some(mut rule) = global.underride.get(body.rule_id.as_str()).cloned() {
rule.actions = body.actions.clone();
global.underride.replace(rule);
}
}
RuleKind::Sender => {
if let Some(mut rule) = global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.sender.remove(&rule);
rule.0.actions = body.actions.clone();
global.sender.insert(rule);
if let Some(mut rule) = global.sender.get(body.rule_id.as_str()).cloned() {
rule.actions = body.actions.clone();
global.sender.replace(rule);
}
}
RuleKind::Room => {
if let Some(mut rule) = global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.room.remove(&rule);
rule.0.actions = body.actions.clone();
global.room.insert(rule);
if let Some(mut rule) = global.room.get(body.rule_id.as_str()).cloned() {
rule.actions = body.actions.clone();
global.room.replace(rule);
}
}
RuleKind::Content => {
if let Some(mut rule) = global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.content.remove(&rule);
rule.0.actions = body.actions.clone();
global.content.insert(rule);
if let Some(mut rule) = global.content.get(body.rule_id.as_str()).cloned() {
rule.actions = body.actions.clone();
global.content.replace(rule);
}
}
RuleKind::_Custom(_) => {}
@ -449,28 +362,28 @@ pub async fn get_pushrule_enabled_route(
RuleKind::Override => global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map_or(false, |rule| rule.0.enabled),
.find(|rule| rule.rule_id == body.rule_id)
.map_or(false, |rule| rule.enabled),
RuleKind::Underride => global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map_or(false, |rule| rule.0.enabled),
.find(|rule| rule.rule_id == body.rule_id)
.map_or(false, |rule| rule.enabled),
RuleKind::Sender => global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map_or(false, |rule| rule.0.enabled),
.find(|rule| rule.rule_id == body.rule_id)
.map_or(false, |rule| rule.enabled),
RuleKind::Room => global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map_or(false, |rule| rule.0.enabled),
.find(|rule| rule.rule_id == body.rule_id)
.map_or(false, |rule| rule.enabled),
RuleKind::Content => global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map_or(false, |rule| rule.0.enabled),
.find(|rule| rule.rule_id == body.rule_id)
.map_or(false, |rule| rule.enabled),
RuleKind::_Custom(_) => false,
};
@ -508,62 +421,37 @@ pub async fn set_pushrule_enabled_route(
let global = &mut event.content.global;
match body.kind {
RuleKind::Override => {
if let Some(mut rule) = global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(mut rule) = global.override_.get(body.rule_id.as_str()).cloned() {
global.override_.remove(&rule);
rule.0.enabled = body.enabled;
rule.enabled = body.enabled;
global.override_.insert(rule);
}
}
RuleKind::Underride => {
if let Some(mut rule) = global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(mut rule) = global.underride.get(body.rule_id.as_str()).cloned() {
global.underride.remove(&rule);
rule.0.enabled = body.enabled;
rule.enabled = body.enabled;
global.underride.insert(rule);
}
}
RuleKind::Sender => {
if let Some(mut rule) = global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(mut rule) = global.sender.get(body.rule_id.as_str()).cloned() {
global.sender.remove(&rule);
rule.0.enabled = body.enabled;
rule.enabled = body.enabled;
global.sender.insert(rule);
}
}
RuleKind::Room => {
if let Some(mut rule) = global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(mut rule) = global.room.get(body.rule_id.as_str()).cloned() {
global.room.remove(&rule);
rule.0.enabled = body.enabled;
rule.enabled = body.enabled;
global.room.insert(rule);
}
}
RuleKind::Content => {
if let Some(mut rule) = global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(mut rule) = global.content.get(body.rule_id.as_str()).cloned() {
global.content.remove(&rule);
rule.0.enabled = body.enabled;
rule.enabled = body.enabled;
global.content.insert(rule);
}
}
@ -612,52 +500,27 @@ pub async fn delete_pushrule_route(
let global = &mut event.content.global;
match body.kind {
RuleKind::Override => {
if let Some(rule) = global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(rule) = global.override_.get(body.rule_id.as_str()).cloned() {
global.override_.remove(&rule);
}
}
RuleKind::Underride => {
if let Some(rule) = global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(rule) = global.underride.get(body.rule_id.as_str()).cloned() {
global.underride.remove(&rule);
}
}
RuleKind::Sender => {
if let Some(rule) = global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(rule) = global.sender.get(body.rule_id.as_str()).cloned() {
global.sender.remove(&rule);
}
}
RuleKind::Room => {
if let Some(rule) = global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(rule) = global.room.get(body.rule_id.as_str()).cloned() {
global.room.remove(&rule);
}
}
RuleKind::Content => {
if let Some(rule) = global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(rule) = global.content.get(body.rule_id.as_str()).cloned() {
global.content.remove(&rule);
}
}

View file

@ -3,10 +3,7 @@ use crate::{pdu::PduBuilder, ConduitResult, Database, Error, Result, Ruma};
use ruma::{
api::client::{
error::ErrorKind,
r0::state::{
get_state_events, get_state_events_for_empty_key, get_state_events_for_key,
send_state_event_for_empty_key, send_state_event_for_key,
},
r0::state::{get_state_events, get_state_events_for_key, send_state_event},
},
events::{
room::history_visibility::{HistoryVisibility, HistoryVisibilityEventContent},
@ -25,8 +22,8 @@ use rocket::{get, put};
#[tracing::instrument(skip(db, body))]
pub async fn send_state_event_for_key_route(
db: State<'_, Database>,
body: Ruma<send_state_event_for_key::Request<'_>>,
) -> ConduitResult<send_state_event_for_key::Response> {
body: Ruma<send_state_event::Request<'_>>,
) -> ConduitResult<send_state_event::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let content = serde_json::from_str::<serde_json::Value>(
@ -49,7 +46,7 @@ pub async fn send_state_event_for_key_route(
db.flush().await?;
Ok(send_state_event_for_key::Response { event_id }.into())
Ok(send_state_event::Response { event_id }.into())
}
#[cfg_attr(
@ -59,8 +56,8 @@ pub async fn send_state_event_for_key_route(
#[tracing::instrument(skip(db, body))]
pub async fn send_state_event_for_empty_key_route(
db: State<'_, Database>,
body: Ruma<send_state_event_for_empty_key::Request<'_>>,
) -> ConduitResult<send_state_event_for_empty_key::Response> {
body: Ruma<send_state_event::Request<'_>>,
) -> ConduitResult<send_state_event::Response> {
// This just calls send_state_event_for_key_route
let Ruma {
body,
@ -81,7 +78,7 @@ pub async fn send_state_event_for_empty_key_route(
&db,
sender_user
.as_ref()
.expect("no user for send state empty key rout"),
.expect("no user for send state empty key route"),
&body.content,
json,
&body.room_id,
@ -91,7 +88,7 @@ pub async fn send_state_event_for_empty_key_route(
db.flush().await?;
Ok(send_state_event_for_empty_key::Response { event_id }.into())
Ok(send_state_event::Response { event_id }.into())
}
#[cfg_attr(
@ -199,8 +196,8 @@ pub async fn get_state_events_for_key_route(
#[tracing::instrument(skip(db, body))]
pub async fn get_state_events_for_empty_key_route(
db: State<'_, Database>,
body: Ruma<get_state_events_for_empty_key::Request<'_>>,
) -> ConduitResult<get_state_events_for_empty_key::Response> {
body: Ruma<get_state_events_for_key::Request<'_>>,
) -> ConduitResult<get_state_events_for_key::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
#[allow(clippy::blocks_in_if_conditions)]
@ -236,7 +233,7 @@ pub async fn get_state_events_for_empty_key_route(
"State event not found.",
))?;
Ok(get_state_events_for_empty_key::Response {
Ok(get_state_events_for_key::Response {
content: serde_json::value::to_raw_value(&event.content)
.map_err(|_| Error::bad_database("Invalid event content in database"))?,
}

View file

@ -9,15 +9,8 @@ use ruma::{
},
OutgoingRequest,
},
events::{
room::{
member::{MemberEventContent, MembershipState},
message::{MessageEventContent, MessageType, TextMessageEventContent},
power_levels::PowerLevelsEventContent,
},
EventType,
},
push::{Action, PushCondition, PushFormat, Ruleset, Tweak},
events::{room::power_levels::PowerLevelsEventContent, EventType},
push::{Action, PushConditionRoomCtx, PushFormat, Ruleset, Tweak},
uint, UInt, UserId,
};
use sled::IVec;
@ -181,276 +174,56 @@ pub async fn send_push_notice(
pdu: &PduEvent,
db: &Database,
) -> Result<()> {
if let Some(msgtype) = pdu.content.get("msgtype").and_then(|b| b.as_str()) {
if msgtype == "m.notice" {
return Ok(());
let power_levels: PowerLevelsEventContent = db
.rooms
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
.map(|ev| {
serde_json::from_value(ev.content)
.map_err(|_| Error::bad_database("invalid m.room.power_levels event"))
})
.transpose()?
.unwrap_or_default();
let ctx = PushConditionRoomCtx {
room_id: pdu.room_id.clone(),
member_count: (db.rooms.room_members(&pdu.room_id).count() as u32).into(),
user_display_name: user.localpart().into(), // TODO: Use actual display name
users_power_levels: power_levels.users,
default_power_level: power_levels.users_default,
notification_power_levels: power_levels.notifications,
};
let mut notify = None;
let mut tweaks = Vec::new();
for action in ruleset.get_actions(&pdu.to_sync_state_event(), &ctx) {
let n = match action {
Action::DontNotify => false,
// TODO: Implement proper support for coalesce
Action::Notify | Action::Coalesce => true,
Action::SetTweak(tweak) => {
tweaks.push(tweak.clone());
continue;
}
};
if notify.is_some() {
return Err(Error::bad_database(
r#"Malformed pushrule contains more than one of these actions: ["dont_notify", "notify", "coalesce"]"#,
));
}
notify = Some(n);
}
for rule in ruleset.into_iter() {
// TODO: can actions contain contradictory Actions
if rule
.actions
.iter()
.any(|act| matches!(act, ruma::push::Action::DontNotify))
|| !rule.enabled
{
continue;
}
let notify = notify.ok_or_else(|| {
Error::bad_database(
r#"Malformed pushrule contains none of these actions: ["dont_notify", "notify", "coalesce"]"#,
)
})?;
match rule.rule_id.as_str() {
".m.rule.master" => {}
".m.rule.suppress_notices" => {
if pdu.kind == EventType::RoomMessage
&& pdu
.content
.get("msgtype")
.map_or(false, |ty| ty == "m.notice")
{
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
".m.rule.invite_for_me" => {
if let EventType::RoomMember = &pdu.kind {
if pdu.state_key.as_deref() == Some(user.as_str())
&& serde_json::from_value::<MemberEventContent>(pdu.content.clone())
.map_err(|_| Error::bad_database("PDU contained bad message content"))?
.membership
== MembershipState::Invite
{
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
}
".m.rule.member_event" => {
if let EventType::RoomMember = &pdu.kind {
// TODO use this?
let _member = serde_json::from_value::<MemberEventContent>(pdu.content.clone())
.map_err(|_| Error::bad_database("PDU contained bad message content"))?;
if let Some(conditions) = rule.conditions {
if conditions.iter().any(|cond| match cond {
PushCondition::EventMatch { key, pattern } => {
let mut json =
serde_json::to_value(pdu).expect("PDU is valid JSON");
for key in key.split('.') {
json = json[key].clone();
}
// TODO: this is baddddd
json.to_string().contains(pattern)
}
_ => false,
}) {
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
}
}
".m.rule.contains_display_name" => {
if let EventType::RoomMessage = &pdu.kind {
let msg_content =
serde_json::from_value::<MessageEventContent>(pdu.content.clone())
.map_err(|_| {
Error::bad_database("PDU contained bad message content")
})?;
if let MessageType::Text(TextMessageEventContent { body, .. }) =
&msg_content.msgtype
{
if body.contains(user.localpart()) {
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
}
}
".m.rule.tombstone" => {
if pdu.kind == EventType::RoomTombstone && pdu.state_key.as_deref() == Some("") {
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
".m.rule.roomnotif" => {
if let EventType::RoomMessage = &pdu.kind {
let msg_content =
serde_json::from_value::<MessageEventContent>(pdu.content.clone())
.map_err(|_| {
Error::bad_database("PDU contained bad message content")
})?;
if let MessageType::Text(TextMessageEventContent { body, .. }) =
&msg_content.msgtype
{
let power_level_cmp = |pl: PowerLevelsEventContent| {
&pl.notifications.room
<= pl.users.get(&pdu.sender).unwrap_or(&ruma::int!(0))
};
let deserialize = |pl: PduEvent| {
serde_json::from_value::<PowerLevelsEventContent>(pl.content).ok()
};
if body.contains("@room")
&& db
.rooms
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
.map(deserialize)
.flatten()
.map_or(false, power_level_cmp)
{
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
}
}
".m.rule.contains_user_name" => {
if let EventType::RoomMessage = &pdu.kind {
let msg_content =
serde_json::from_value::<MessageEventContent>(pdu.content.clone())
.map_err(|_| {
Error::bad_database("PDU contained bad message content")
})?;
if let MessageType::Text(TextMessageEventContent { body, .. }) =
&msg_content.msgtype
{
if body.contains(user.localpart()) {
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
}
}
".m.rule.call" => {
if pdu.kind == EventType::CallInvite {
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
".m.rule.encrypted_room_one_to_one" => {
if db.rooms.room_members(&pdu.room_id).count() == 2
&& pdu.kind == EventType::RoomEncrypted
{
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
".m.rule.room_one_to_one" => {
if db.rooms.room_members(&pdu.room_id).count() == 2
&& pdu.kind == EventType::RoomMessage
{
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
".m.rule.message" => {
if pdu.kind == EventType::RoomMessage {
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
".m.rule.encrypted" => {
if pdu.kind == EventType::RoomEncrypted {
let tweaks = rule
.actions
.iter()
.filter_map(|a| match a {
Action::SetTweak(tweak) => Some(tweak.clone()),
_ => None,
})
.collect::<Vec<_>>();
send_notice(unread, pusher, tweaks, pdu, db).await?;
break;
}
}
_ => {}
}
if notify {
send_notice(unread, pusher, tweaks, pdu, db).await?;
}
Ok(())

View file

@ -16,7 +16,7 @@ use rocket::futures::stream::{FuturesUnordered, StreamExt};
use ruma::{
api::{appservice, federation, OutgoingRequest},
events::{push_rules, EventType},
uint, ServerName, UInt, UserId,
push, uint, ServerName, UInt, UserId,
};
use sled::IVec;
use tokio::{select, sync::Semaphore};
@ -428,7 +428,7 @@ impl Sending {
.get::<push_rules::PushRulesEvent>(None, &userid, EventType::PushRules)
.map_err(|e| (OutgoingKind::Push(user.clone(), pushkey.clone()), e))?
.map(|ev| ev.content.global)
.unwrap_or_else(|| crate::push_rules::default_pushrules(&userid));
.unwrap_or_else(|| push::Ruleset::server_default(&userid));
let unread: UInt = if let Some(last_read) = db
.rooms

View file

@ -9,6 +9,7 @@ use ruma::{
},
encryption::DeviceKeys,
events::{AnyToDeviceEvent, EventType},
identifiers::MxcUri,
serde::Raw,
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, UInt, UserId,
};
@ -150,21 +151,22 @@ impl Users {
}
/// Get a the avatar_url of a user.
pub fn avatar_url(&self, user_id: &UserId) -> Result<Option<String>> {
pub fn avatar_url(&self, user_id: &UserId) -> Result<Option<MxcUri>> {
self.userid_avatarurl
.get(user_id.to_string())?
.map_or(Ok(None), |bytes| {
Ok(Some(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("Avatar URL in db is invalid.")
})?))
.map(|bytes| {
let s = utils::string_from_bytes(&bytes)
.map_err(|_| Error::bad_database("Avatar URL in db is invalid."))?;
MxcUri::try_from(s).map_err(|_| Error::bad_database("Avatar URL in db is invalid."))
})
.transpose()
}
/// Sets a new avatar_url or removes it if avatar_url is None.
pub fn set_avatar_url(&self, user_id: &UserId, avatar_url: Option<String>) -> Result<()> {
pub fn set_avatar_url(&self, user_id: &UserId, avatar_url: Option<MxcUri>) -> Result<()> {
if let Some(avatar_url) = avatar_url {
self.userid_avatarurl
.insert(user_id.to_string(), &*avatar_url)?;
.insert(user_id.to_string(), avatar_url.to_string().as_str())?;
} else {
self.userid_avatarurl.remove(user_id.to_string())?;
}

View file

@ -3,7 +3,6 @@ pub mod client_server;
mod database;
mod error;
mod pdu;
mod push_rules;
mod ruma_wrapper;
pub mod server_server;
mod utils;

View file

@ -7,7 +7,6 @@ pub mod server_server;
mod database;
mod error;
mod pdu;
mod push_rules;
mod ruma_wrapper;
mod utils;

View file

@ -1,256 +0,0 @@
use ruma::{
push::{
Action, ConditionalPushRule, ConditionalPushRuleInit, ContentPushRule, OverridePushRule,
PatternedPushRule, PatternedPushRuleInit, PushCondition, RoomMemberCountIs, Ruleset, Tweak,
UnderridePushRule,
},
UserId,
};
pub fn default_pushrules(user_id: &UserId) -> Ruleset {
let mut rules = Ruleset::default();
rules.add(ContentPushRule(contains_user_name_rule(&user_id)));
for rule in vec![
master_rule(),
suppress_notices_rule(),
invite_for_me_rule(),
member_event_rule(),
contains_display_name_rule(),
tombstone_rule(),
roomnotif_rule(),
] {
rules.add(OverridePushRule(rule));
}
for rule in vec![
call_rule(),
encrypted_room_one_to_one_rule(),
room_one_to_one_rule(),
message_rule(),
encrypted_rule(),
] {
rules.add(UnderridePushRule(rule));
}
rules
}
pub fn master_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::DontNotify],
default: true,
enabled: false,
rule_id: ".m.rule.master".to_owned(),
conditions: vec![],
}
.into()
}
pub fn suppress_notices_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::DontNotify],
default: true,
enabled: true,
rule_id: ".m.rule.suppress_notices".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "content.msgtype".to_owned(),
pattern: "m.notice".to_owned(),
}],
}
.into()
}
pub fn invite_for_me_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
Action::SetTweak(Tweak::Highlight(false)),
],
default: true,
enabled: true,
rule_id: ".m.rule.invite_for_me".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "content.membership".to_owned(),
pattern: "m.invite".to_owned(),
}],
}
.into()
}
pub fn member_event_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::DontNotify],
default: true,
enabled: true,
rule_id: ".m.rule.member_event".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "content.membership".to_owned(),
pattern: "type".to_owned(),
}],
}
.into()
}
pub fn contains_display_name_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
Action::SetTweak(Tweak::Highlight(true)),
],
default: true,
enabled: true,
rule_id: ".m.rule.contains_display_name".to_owned(),
conditions: vec![PushCondition::ContainsDisplayName],
}
.into()
}
pub fn tombstone_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(true))],
default: true,
enabled: true,
rule_id: ".m.rule.tombstone".to_owned(),
conditions: vec![
PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.tombstone".to_owned(),
},
PushCondition::EventMatch {
key: "state_key".to_owned(),
pattern: "".to_owned(),
},
],
}
.into()
}
pub fn roomnotif_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(true))],
default: true,
enabled: true,
rule_id: ".m.rule.roomnotif".to_owned(),
conditions: vec![
PushCondition::EventMatch {
key: "content.body".to_owned(),
pattern: "@room".to_owned(),
},
PushCondition::SenderNotificationPermission {
key: "room".to_owned(),
},
],
}
.into()
}
pub fn contains_user_name_rule(user_id: &UserId) -> PatternedPushRule {
PatternedPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
Action::SetTweak(Tweak::Highlight(true)),
],
default: true,
enabled: true,
rule_id: ".m.rule.contains_user_name".to_owned(),
pattern: user_id.localpart().to_owned(),
}
.into()
}
pub fn call_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("ring".to_owned())),
Action::SetTweak(Tweak::Highlight(false)),
],
default: true,
enabled: true,
rule_id: ".m.rule.call".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.call.invite".to_owned(),
}],
}
.into()
}
pub fn encrypted_room_one_to_one_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
Action::SetTweak(Tweak::Highlight(false)),
],
default: true,
enabled: true,
rule_id: ".m.rule.encrypted_room_one_to_one".to_owned(),
conditions: vec![
PushCondition::RoomMemberCount {
is: RoomMemberCountIs::from(2_u32.into()..),
},
PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.encrypted".to_owned(),
},
],
}
.into()
}
pub fn room_one_to_one_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
Action::SetTweak(Tweak::Highlight(false)),
],
default: true,
enabled: true,
rule_id: ".m.rule.room_one_to_one".to_owned(),
conditions: vec![
PushCondition::RoomMemberCount {
is: RoomMemberCountIs::from(2_u32.into()..),
},
PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.message".to_owned(),
},
],
}
.into()
}
pub fn message_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(false))],
default: true,
enabled: true,
rule_id: ".m.rule.message".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.message".to_owned(),
}],
}
.into()
}
pub fn encrypted_rule() -> ConditionalPushRule {
ConditionalPushRuleInit {
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(false))],
default: true,
enabled: true,
rule_id: ".m.rule.encrypted".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.encrypted".to_owned(),
}],
}
.into()
}

View file

@ -1,6 +1,5 @@
use crate::Error;
use ruma::{
api::OutgoingRequest,
identifiers::{DeviceId, UserId},
Outgoing,
};
@ -28,7 +27,7 @@ use {
/// This struct converts rocket requests into ruma structs by converting them into http requests
/// first.
pub struct Ruma<T: Outgoing + OutgoingRequest> {
pub struct Ruma<T: Outgoing> {
pub body: T::Incoming,
pub sender_user: Option<UserId>,
pub sender_device: Option<Box<DeviceId>>,
@ -37,7 +36,7 @@ pub struct Ruma<T: Outgoing + OutgoingRequest> {
}
#[cfg(feature = "conduit_bin")]
impl<'a, T: Outgoing + OutgoingRequest> FromTransformedData<'a> for Ruma<T>
impl<'a, T: Outgoing> FromTransformedData<'a> for Ruma<T>
where
T::Incoming: IncomingRequest,
{
@ -56,6 +55,8 @@ where
request: &'a Request<'_>,
outcome: Transformed<'a, Self>,
) -> FromDataFuture<'a, Self, Self::Error> {
let metadata = T::Incoming::METADATA;
Box::pin(async move {
let data = rocket::try_outcome!(outcome.owned());
let db = request
@ -80,7 +81,7 @@ where
.and_then(|as_token| as_token.as_str())
.map_or(false, |as_token| token.as_deref() == Some(as_token))
}) {
match T::METADATA.authentication {
match metadata.authentication {
AuthScheme::AccessToken | AuthScheme::QueryOnlyAccessToken => {
let user_id = request.get_query_value::<String>("user_id").map_or_else(
|| {
@ -112,7 +113,7 @@ where
AuthScheme::None => (None, None, true),
}
} else {
match T::METADATA.authentication {
match metadata.authentication {
AuthScheme::AccessToken | AuthScheme::QueryOnlyAccessToken => {
if let Some(token) = token {
match db.users.find_from_token(&token).unwrap() {
@ -166,7 +167,7 @@ where
}
}
impl<T: Outgoing + OutgoingRequest> Deref for Ruma<T> {
impl<T: Outgoing> Deref for Ruma<T> {
type Target = T::Incoming;
fn deref(&self) -> &Self::Target {