Refactor Event.redacts_id to look at room version rules, use it
This commit is contained in:
@@ -11,8 +11,8 @@ mod unsigned;
|
||||
use std::fmt::Debug;
|
||||
|
||||
use ruma::{
|
||||
CanonicalJsonObject, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId,
|
||||
RoomVersionId, UserId, events::TimelineEventType,
|
||||
CanonicalJsonObject, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, UserId,
|
||||
events::TimelineEventType, room_version_rules::RoomVersionRules,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value as JsonValue, value::RawValue as RawJsonValue};
|
||||
@@ -107,11 +107,11 @@ pub trait Event: Clone + Debug + Send + Sync {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn redacts_id(&self, room_version: &RoomVersionId) -> Option<OwnedEventId>
|
||||
fn redacts_id(&self, room_rules: &RoomVersionRules) -> Option<OwnedEventId>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
redact::redacts_id(self, room_version)
|
||||
redact::redacts_id(self, room_rules)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use ruma::{
|
||||
OwnedEventId, RoomVersionId,
|
||||
OwnedEventId,
|
||||
events::{TimelineEventType, room::redaction::RoomRedactionEventContent},
|
||||
room_version_rules::RoomVersionRules,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde_json::value::{RawValue as RawJsonValue, to_raw_value};
|
||||
@@ -61,22 +62,19 @@ pub(super) fn is_redacted<E: Event>(event: &E) -> bool {
|
||||
#[must_use]
|
||||
pub(super) fn redacts_id<E: Event>(
|
||||
event: &E,
|
||||
room_version: &RoomVersionId,
|
||||
room_rules: &RoomVersionRules,
|
||||
) -> Option<OwnedEventId> {
|
||||
use RoomVersionId::*;
|
||||
|
||||
if *event.kind() != TimelineEventType::RoomRedaction {
|
||||
return None;
|
||||
}
|
||||
|
||||
match *room_version {
|
||||
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 =>
|
||||
event.redacts().map(ToOwned::to_owned),
|
||||
| _ =>
|
||||
event
|
||||
.get_content::<RoomRedactionEventContent>()
|
||||
.ok()?
|
||||
.redacts,
|
||||
if room_rules.redaction.content_field_redacts {
|
||||
event
|
||||
.get_content::<RoomRedactionEventContent>()
|
||||
.ok()?
|
||||
.redacts
|
||||
} else {
|
||||
event.redacts().map(ToOwned::to_owned)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -139,7 +139,7 @@ pub(super) async fn upgrade_outlier_to_timeline_pdu(
|
||||
|
||||
// Soft fail check before doing state res
|
||||
trace!("Performing soft-fail check");
|
||||
let soft_fail = match incoming_pdu.redacts_id(room_version) {
|
||||
let soft_fail = match incoming_pdu.redacts_id(&room_rules) {
|
||||
| None => false,
|
||||
| Some(redact_id) =>
|
||||
!self
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
use ruma::{
|
||||
CanonicalJsonObject, CanonicalJsonValue, EventId, RoomVersionId, UserId,
|
||||
CanonicalJsonObject, CanonicalJsonValue, EventId, UserId,
|
||||
events::{
|
||||
TimelineEventType,
|
||||
room::{
|
||||
encrypted::Relation,
|
||||
member::{MembershipState, RoomMemberEventContent},
|
||||
redaction::RoomRedactionEventContent,
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -16,6 +15,7 @@ use tuwunel_core::{
|
||||
matrix::{
|
||||
event::Event,
|
||||
pdu::{PduCount, PduEvent, PduId, RawPduId},
|
||||
room_version,
|
||||
},
|
||||
utils::{self, result::LogErr},
|
||||
};
|
||||
@@ -211,30 +211,24 @@ async fn append_pdu_effects(
|
||||
) -> Result {
|
||||
match *pdu.kind() {
|
||||
| TimelineEventType::RoomRedaction => {
|
||||
use RoomVersionId::*;
|
||||
|
||||
let room_version_id = self
|
||||
let room_version = self
|
||||
.services
|
||||
.state
|
||||
.get_room_version(pdu.room_id())
|
||||
.await?;
|
||||
|
||||
let content: RoomRedactionEventContent;
|
||||
let event_id = match room_version_id {
|
||||
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 => pdu.redacts(),
|
||||
| _ => {
|
||||
content = pdu.get_content()?;
|
||||
content.redacts.as_deref()
|
||||
},
|
||||
};
|
||||
if let Some(redact_id) = event_id
|
||||
let room_rules = room_version::rules(&room_version)?;
|
||||
|
||||
let redacts_id = pdu.redacts_id(&room_rules);
|
||||
|
||||
if let Some(redacts_id) = &redacts_id
|
||||
&& self
|
||||
.services
|
||||
.state_accessor
|
||||
.user_can_redact(redact_id, pdu.sender(), pdu.room_id(), false)
|
||||
.user_can_redact(redacts_id, pdu.sender(), pdu.room_id(), false)
|
||||
.await?
|
||||
{
|
||||
self.redact_pdu(redact_id, pdu, shortroomid, state_lock)
|
||||
self.redact_pdu(redacts_id, pdu, shortroomid, state_lock)
|
||||
.await?;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -2,18 +2,15 @@ use std::{collections::HashSet, iter::once};
|
||||
|
||||
use futures::{FutureExt, StreamExt};
|
||||
use ruma::{
|
||||
OwnedEventId, OwnedServerName, RoomId, RoomVersionId, UserId,
|
||||
OwnedEventId, OwnedServerName, RoomId, UserId,
|
||||
events::{
|
||||
TimelineEventType,
|
||||
room::{
|
||||
member::{MembershipState, RoomMemberEventContent},
|
||||
redaction::RoomRedactionEventContent,
|
||||
},
|
||||
room::member::{MembershipState, RoomMemberEventContent},
|
||||
},
|
||||
};
|
||||
use tuwunel_core::{
|
||||
Err, Result, implement,
|
||||
matrix::{event::Event, pdu::PduBuilder},
|
||||
matrix::{event::Event, pdu::PduBuilder, room_version},
|
||||
utils::{IterStream, ReadyExt},
|
||||
};
|
||||
|
||||
@@ -62,36 +59,24 @@ pub async fn build_and_append_pdu(
|
||||
|
||||
// If redaction event is not authorized, do not append it to the timeline
|
||||
if *pdu.kind() == TimelineEventType::RoomRedaction {
|
||||
use RoomVersionId::*;
|
||||
match self
|
||||
let room_version = self
|
||||
.services
|
||||
.state
|
||||
.get_room_version(pdu.room_id())
|
||||
.await?
|
||||
.await?;
|
||||
|
||||
let room_rules = room_version::rules(&room_version)?;
|
||||
|
||||
let redacts_id = pdu.redacts_id(&room_rules);
|
||||
|
||||
if let Some(redacts_id) = &redacts_id
|
||||
&& !self
|
||||
.services
|
||||
.state_accessor
|
||||
.user_can_redact(redacts_id, pdu.sender(), pdu.room_id(), false)
|
||||
.await?
|
||||
{
|
||||
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 => {
|
||||
if let Some(redact_id) = pdu.redacts()
|
||||
&& !self
|
||||
.services
|
||||
.state_accessor
|
||||
.user_can_redact(redact_id, pdu.sender(), pdu.room_id(), false)
|
||||
.await?
|
||||
{
|
||||
return Err!(Request(Forbidden("User cannot redact this event.")));
|
||||
}
|
||||
},
|
||||
| _ => {
|
||||
let content: RoomRedactionEventContent = pdu.get_content()?;
|
||||
if let Some(redact_id) = &content.redacts
|
||||
&& !self
|
||||
.services
|
||||
.state_accessor
|
||||
.user_can_redact(redact_id, pdu.sender(), pdu.room_id(), false)
|
||||
.await?
|
||||
{
|
||||
return Err!(Request(Forbidden("User cannot redact this event.")));
|
||||
}
|
||||
},
|
||||
return Err!(Request(Forbidden("User cannot redact this event.")));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user