Add constructions and Default for PduBuilder

simplify various RoomMemberEventContent constructions

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-04 20:25:32 +00:00 committed by strawberry
parent f503ed918c
commit e482c0646f
16 changed files with 279 additions and 564 deletions

View File

@ -12,11 +12,10 @@ use ruma::{
redaction::RoomRedactionEventContent, redaction::RoomRedactionEventContent,
}, },
tag::{TagEvent, TagEventContent, TagInfo}, tag::{TagEvent, TagEventContent, TagInfo},
RoomAccountDataEventType, StateEventType, TimelineEventType, RoomAccountDataEventType, StateEventType,
}, },
EventId, OwnedRoomId, OwnedRoomOrAliasId, OwnedUserId, RoomId, EventId, OwnedRoomId, OwnedRoomOrAliasId, OwnedUserId, RoomId,
}; };
use serde_json::value::to_raw_value;
use crate::{ use crate::{
admin_command, get_room_info, admin_command, get_room_info,
@ -461,14 +460,7 @@ pub(super) async fn force_demote(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(String::new(), &power_levels_content),
event_type: TimelineEventType::RoomPowerLevels,
content: to_raw_value(&power_levels_content).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
&user_id, &user_id,
&room_id, &room_id,
&state_lock, &state_lock,
@ -623,16 +615,11 @@ pub(super) async fn redact_event(&self, event_id: Box<EventId>) -> Result<RoomMe
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: TimelineEventType::RoomRedaction, redacts: Some(event.event_id.clone()),
content: to_raw_value(&RoomRedactionEventContent { ..PduBuilder::timeline(&RoomRedactionEventContent {
redacts: Some(event.event_id.clone().into()), redacts: Some(event.event_id.clone().into()),
reason: Some(reason), reason: Some(reason),
}) })
.expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: Some(event.event_id),
timestamp: None,
}, },
&sender_user, &sender_user,
&room_id, &room_id,

View File

@ -21,11 +21,10 @@ use ruma::{
message::RoomMessageEventContent, message::RoomMessageEventContent,
power_levels::{RoomPowerLevels, RoomPowerLevelsEventContent}, power_levels::{RoomPowerLevels, RoomPowerLevelsEventContent},
}, },
GlobalAccountDataEventType, StateEventType, TimelineEventType, GlobalAccountDataEventType, StateEventType,
}, },
push, OwnedRoomId, UserId, push, OwnedRoomId, UserId,
}; };
use serde_json::value::to_raw_value;
use service::Services; use service::Services;
use super::{join_room_by_id_helper, DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH}; use super::{join_room_by_id_helper, DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH};
@ -747,14 +746,7 @@ pub async fn full_user_deactivate(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(String::new(), &power_levels_content),
event_type: TimelineEventType::RoomPowerLevels,
content: to_raw_value(&power_levels_content).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
user_id, user_id,
room_id, room_id,
&state_lock, &state_lock,

View File

@ -34,13 +34,13 @@ use ruma::{
member::{MembershipState, RoomMemberEventContent}, member::{MembershipState, RoomMemberEventContent},
message::RoomMessageEventContent, message::RoomMessageEventContent,
}, },
StateEventType, TimelineEventType, StateEventType,
}, },
serde::Base64, serde::Base64,
state_res, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId, OwnedRoomId, OwnedServerName, state_res, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId, OwnedRoomId, OwnedServerName,
OwnedUserId, RoomId, RoomVersionId, ServerName, UserId, OwnedUserId, RoomId, RoomVersionId, ServerName, UserId,
}; };
use serde_json::value::{to_raw_value, RawValue as RawJsonValue}; use serde_json::value::RawValue as RawJsonValue;
use service::{appservice::RegistrationInfo, rooms::state::RoomMutexGuard, Services}; use service::{appservice::RegistrationInfo, rooms::state::RoomMutexGuard, Services};
use tokio::sync::RwLock; use tokio::sync::RwLock;
@ -383,28 +383,25 @@ pub(crate) async fn kick_user_route(
let state_lock = services.rooms.state.mutex.lock(&body.room_id).await; let state_lock = services.rooms.state.mutex.lock(&body.room_id).await;
let mut event: RoomMemberEventContent = services let event: RoomMemberEventContent = services
.rooms .rooms
.state_accessor .state_accessor
.room_state_get_content(&body.room_id, &StateEventType::RoomMember, body.user_id.as_ref()) .room_state_get_content(&body.room_id, &StateEventType::RoomMember, body.user_id.as_ref())
.await .await
.map_err(|_| err!(Request(BadState("Cannot kick member that's not in the room."))))?; .map_err(|_| err!(Request(BadState("Cannot kick member that's not in the room."))))?;
event.membership = MembershipState::Leave;
event.reason.clone_from(&body.reason);
services services
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomMember, body.user_id.to_string(),
content: to_raw_value(&event).expect("event is valid, we just created it"), &RoomMemberEventContent {
unsigned: None, membership: MembershipState::Leave,
state_key: Some(body.user_id.to_string()), reason: body.reason.clone(),
redacts: None, ..event
timestamp: None, },
}, ),
sender_user, sender_user,
&body.room_id, &body.room_id,
&state_lock, &state_lock,
@ -435,14 +432,9 @@ pub(crate) async fn ban_user_route(
.await .await
.map_or_else( .map_or_else(
|_| RoomMemberEventContent { |_| RoomMemberEventContent {
membership: MembershipState::Ban,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: blurhash.clone(), blurhash: blurhash.clone(),
reason: body.reason.clone(), reason: body.reason.clone(),
join_authorized_via_users_server: None, ..RoomMemberEventContent::new(MembershipState::Ban)
}, },
|event| RoomMemberEventContent { |event| RoomMemberEventContent {
membership: MembershipState::Ban, membership: MembershipState::Ban,
@ -459,14 +451,7 @@ pub(crate) async fn ban_user_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(body.user_id.to_string(), &event),
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&event).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(body.user_id.to_string()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
&body.room_id, &body.room_id,
&state_lock, &state_lock,
@ -488,29 +473,26 @@ pub(crate) async fn unban_user_route(
let state_lock = services.rooms.state.mutex.lock(&body.room_id).await; let state_lock = services.rooms.state.mutex.lock(&body.room_id).await;
let mut event: RoomMemberEventContent = services let event: RoomMemberEventContent = services
.rooms .rooms
.state_accessor .state_accessor
.room_state_get_content(&body.room_id, &StateEventType::RoomMember, body.user_id.as_ref()) .room_state_get_content(&body.room_id, &StateEventType::RoomMember, body.user_id.as_ref())
.await .await
.map_err(|_| err!(Request(BadState("Cannot unban a user who is not banned."))))?; .map_err(|_| err!(Request(BadState("Cannot unban a user who is not banned."))))?;
event.membership = MembershipState::Leave;
event.reason.clone_from(&body.reason);
event.join_authorized_via_users_server = None;
services services
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomMember, body.user_id.to_string(),
content: to_raw_value(&event).expect("event is valid, we just created it"), &RoomMemberEventContent {
unsigned: None, membership: MembershipState::Leave,
state_key: Some(body.user_id.to_string()), reason: body.reason.clone(),
redacts: None, join_authorized_via_users_server: None,
timestamp: None, ..event
}, },
),
sender_user, sender_user,
&body.room_id, &body.room_id,
&state_lock, &state_lock,
@ -745,14 +727,12 @@ async fn join_room_by_id_helper_remote(
join_event_stub.insert( join_event_stub.insert(
"content".to_owned(), "content".to_owned(),
to_canonical_value(RoomMemberEventContent { to_canonical_value(RoomMemberEventContent {
membership: MembershipState::Join,
displayname: services.users.displayname(sender_user).await.ok(), displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(), avatar_url: services.users.avatar_url(sender_user).await.ok(),
is_direct: None,
third_party_invite: None,
blurhash: services.users.blurhash(sender_user).await.ok(), blurhash: services.users.blurhash(sender_user).await.ok(),
reason, reason,
join_authorized_via_users_server: join_authorized_via_users_server.clone(), join_authorized_via_users_server: join_authorized_via_users_server.clone(),
..RoomMemberEventContent::new(MembershipState::Join)
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
); );
@ -1085,15 +1065,13 @@ async fn join_room_by_id_helper_local(
} }
} }
let event = RoomMemberEventContent { let content = RoomMemberEventContent {
membership: MembershipState::Join,
displayname: services.users.displayname(sender_user).await.ok(), displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(), avatar_url: services.users.avatar_url(sender_user).await.ok(),
is_direct: None,
third_party_invite: None,
blurhash: services.users.blurhash(sender_user).await.ok(), blurhash: services.users.blurhash(sender_user).await.ok(),
reason: reason.clone(), reason: reason.clone(),
join_authorized_via_users_server, join_authorized_via_users_server,
..RoomMemberEventContent::new(MembershipState::Join)
}; };
// Try normal join first // Try normal join first
@ -1101,14 +1079,7 @@ async fn join_room_by_id_helper_local(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(sender_user.to_string(), &content),
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&event).expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(sender_user.to_string()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
room_id, room_id,
&state_lock, &state_lock,
@ -1164,14 +1135,12 @@ async fn join_room_by_id_helper_local(
join_event_stub.insert( join_event_stub.insert(
"content".to_owned(), "content".to_owned(),
to_canonical_value(RoomMemberEventContent { to_canonical_value(RoomMemberEventContent {
membership: MembershipState::Join,
displayname: services.users.displayname(sender_user).await.ok(), displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(), avatar_url: services.users.avatar_url(sender_user).await.ok(),
is_direct: None,
third_party_invite: None,
blurhash: services.users.blurhash(sender_user).await.ok(), blurhash: services.users.blurhash(sender_user).await.ok(),
reason, reason,
join_authorized_via_users_server, join_authorized_via_users_server,
..RoomMemberEventContent::new(MembershipState::Join)
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
); );
@ -1400,30 +1369,19 @@ pub(crate) async fn invite_helper(
if !services.globals.user_is_local(user_id) { if !services.globals.user_is_local(user_id) {
let (pdu, pdu_json, invite_room_state) = { let (pdu, pdu_json, invite_room_state) = {
let state_lock = services.rooms.state.mutex.lock(room_id).await; let state_lock = services.rooms.state.mutex.lock(room_id).await;
let content = to_raw_value(&RoomMemberEventContent {
let content = RoomMemberEventContent {
avatar_url: services.users.avatar_url(user_id).await.ok(), avatar_url: services.users.avatar_url(user_id).await.ok(),
displayname: None,
is_direct: Some(is_direct), is_direct: Some(is_direct),
membership: MembershipState::Invite,
third_party_invite: None,
blurhash: None,
reason, reason,
join_authorized_via_users_server: None, ..RoomMemberEventContent::new(MembershipState::Invite)
}) };
.expect("member event is valid value");
let (pdu, pdu_json) = services let (pdu, pdu_json) = services
.rooms .rooms
.timeline .timeline
.create_hash_and_sign_event( .create_hash_and_sign_event(
PduBuilder { PduBuilder::state(user_id.to_string(), &content),
event_type: TimelineEventType::RoomMember,
content,
unsigned: None,
state_key: Some(user_id.to_string()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
room_id, room_id,
&state_lock, &state_lock,
@ -1524,28 +1482,20 @@ pub(crate) async fn invite_helper(
let state_lock = services.rooms.state.mutex.lock(room_id).await; let state_lock = services.rooms.state.mutex.lock(room_id).await;
let content = RoomMemberEventContent {
displayname: services.users.displayname(user_id).await.ok(),
avatar_url: services.users.avatar_url(user_id).await.ok(),
blurhash: services.users.blurhash(user_id).await.ok(),
is_direct: Some(is_direct),
reason,
..RoomMemberEventContent::new(MembershipState::Invite)
};
services services
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(user_id.to_string(), &content),
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Invite,
displayname: services.users.displayname(user_id).await.ok(),
avatar_url: services.users.avatar_url(user_id).await.ok(),
is_direct: Some(is_direct),
third_party_invite: None,
blurhash: services.users.blurhash(user_id).await.ok(),
reason,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(user_id.to_string()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
room_id, room_id,
&state_lock, &state_lock,
@ -1625,7 +1575,7 @@ pub async fn leave_room(services: &Services, user_id: &UserId, room_id: &RoomId,
} else { } else {
let state_lock = services.rooms.state.mutex.lock(room_id).await; let state_lock = services.rooms.state.mutex.lock(room_id).await;
let Ok(mut event) = services let Ok(event) = services
.rooms .rooms
.state_accessor .state_accessor
.room_state_get_content::<RoomMemberEventContent>(room_id, &StateEventType::RoomMember, user_id.as_str()) .room_state_get_content::<RoomMemberEventContent>(room_id, &StateEventType::RoomMember, user_id.as_str())
@ -1651,21 +1601,18 @@ pub async fn leave_room(services: &Services, user_id: &UserId, room_id: &RoomId,
return Ok(()); return Ok(());
}; };
event.membership = MembershipState::Leave;
event.reason = reason;
services services
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomMember, user_id.to_string(),
content: to_raw_value(&event).expect("event is valid, we just created it"), &RoomMemberEventContent {
unsigned: None, membership: MembershipState::Leave,
state_key: Some(user_id.to_string()), reason,
redacts: None, ..event
timestamp: None, },
}, ),
user_id, user_id,
room_id, room_id,
&state_lock, &state_lock,

View File

@ -9,7 +9,6 @@ use conduit::{
use futures::{FutureExt, StreamExt}; use futures::{FutureExt, StreamExt};
use ruma::{ use ruma::{
api::client::{ api::client::{
error::ErrorKind,
filter::{RoomEventFilter, UrlFilter}, filter::{RoomEventFilter, UrlFilter},
message::{get_message_events, send_message_event}, message::{get_message_events, send_message_event},
}, },
@ -21,7 +20,7 @@ use service::rooms::timeline::PdusIterItem;
use crate::{ use crate::{
service::{pdu::PduBuilder, Services}, service::{pdu::PduBuilder, Services},
utils, Error, Result, Ruma, utils, Result, Ruma,
}; };
/// # `PUT /_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId}` /// # `PUT /_matrix/client/v3/rooms/{roomId}/send/{eventType}/{txnId}`
@ -77,27 +76,25 @@ pub(crate) async fn send_message_event_route(
let mut unsigned = BTreeMap::new(); let mut unsigned = BTreeMap::new();
unsigned.insert("transaction_id".to_owned(), body.txn_id.to_string().into()); unsigned.insert("transaction_id".to_owned(), body.txn_id.to_string().into());
let content = from_str(body.body.body.json().get()) let content =
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?; from_str(body.body.body.json().get()).map_err(|e| err!(Request(BadJson("Invalid JSON body: {e}"))))?;
let event_id = services let event_id = services
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: body.event_type.to_string().into(), event_type: body.event_type.clone().into(),
content, content,
unsigned: Some(unsigned), unsigned: Some(unsigned),
state_key: None,
redacts: None,
timestamp: appservice_info.and(body.timestamp), timestamp: appservice_info.and(body.timestamp),
..Default::default()
}, },
sender_user, sender_user,
&body.room_id, &body.room_id,
&state_lock, &state_lock,
) )
.await .await?;
.map(|event_id| (*event_id).to_owned())?;
services services
.transaction_ids .transaction_ids
@ -106,7 +103,7 @@ pub(crate) async fn send_message_event_route(
drop(state_lock); drop(state_lock);
Ok(send_message_event::v3::Response { Ok(send_message_event::v3::Response {
event_id, event_id: event_id.into(),
}) })
} }

View File

@ -13,11 +13,10 @@ use ruma::{
}, },
federation, federation,
}, },
events::{room::member::RoomMemberEventContent, StateEventType, TimelineEventType}, events::{room::member::RoomMemberEventContent, StateEventType},
presence::PresenceState, presence::PresenceState,
OwnedMxcUri, OwnedRoomId, UserId, OwnedMxcUri, OwnedRoomId, UserId,
}; };
use serde_json::value::to_raw_value;
use service::Services; use service::Services;
use crate::Ruma; use crate::Ruma;
@ -310,19 +309,14 @@ pub async fn update_displayname(
continue; continue;
}; };
let pdu = PduBuilder { let pdu = PduBuilder::state(
event_type: TimelineEventType::RoomMember, user_id.to_string(),
content: to_raw_value(&RoomMemberEventContent { &RoomMemberEventContent {
displayname: displayname.clone(), displayname: displayname.clone(),
join_authorized_via_users_server: None, join_authorized_via_users_server: None,
..content ..content
}) },
.expect("event is valid, we just created it"), );
unsigned: None,
state_key: Some(user_id.to_string()),
redacts: None,
timestamp: None,
};
joined_rooms.push((pdu, room_id)); joined_rooms.push((pdu, room_id));
} }
@ -360,20 +354,15 @@ pub async fn update_avatar_url(
.room_state_get_content(room_id, &StateEventType::RoomMember, user_id.as_str()) .room_state_get_content(room_id, &StateEventType::RoomMember, user_id.as_str())
.await?; .await?;
let pdu = PduBuilder { let pdu = PduBuilder::state(
event_type: TimelineEventType::RoomMember, user_id.to_string(),
content: to_raw_value(&RoomMemberEventContent { &RoomMemberEventContent {
avatar_url: avatar_url.clone(), avatar_url: avatar_url.clone(),
blurhash: blurhash.clone(), blurhash: blurhash.clone(),
join_authorized_via_users_server: None, join_authorized_via_users_server: None,
..content ..content
}) },
.expect("event is valid, we just created it"), );
unsigned: None,
state_key: Some(user_id.to_string()),
redacts: None,
timestamp: None,
};
Ok((pdu, room_id)) Ok((pdu, room_id))
}) })

View File

@ -1,9 +1,5 @@
use axum::extract::State; use axum::extract::State;
use ruma::{ use ruma::{api::client::redact::redact_event, events::room::redaction::RoomRedactionEventContent};
api::client::redact::redact_event,
events::{room::redaction::RoomRedactionEventContent, TimelineEventType},
};
use serde_json::value::to_raw_value;
use crate::{service::pdu::PduBuilder, Result, Ruma}; use crate::{service::pdu::PduBuilder, Result, Ruma};
@ -25,16 +21,11 @@ pub(crate) async fn redact_event_route(
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: TimelineEventType::RoomRedaction, redacts: Some(body.event_id.clone().into()),
content: to_raw_value(&RoomRedactionEventContent { ..PduBuilder::timeline(&RoomRedactionEventContent {
redacts: Some(body.event_id.clone()), redacts: Some(body.event_id.clone()),
reason: body.reason.clone(), reason: body.reason.clone(),
}) })
.expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: Some(body.event_id.into()),
timestamp: None,
}, },
sender_user, sender_user,
&body.room_id, &body.room_id,
@ -44,8 +35,7 @@ pub(crate) async fn redact_event_route(
drop(state_lock); drop(state_lock);
let event_id = (*event_id).to_owned();
Ok(redact_event::v3::Response { Ok(redact_event::v3::Response {
event_id, event_id: event_id.into(),
}) })
} }

View File

@ -150,8 +150,7 @@ pub(crate) async fn create_room_route(
None => services.globals.default_room_version(), None => services.globals.default_room_version(),
}; };
#[allow(clippy::single_match_else)] let create_content = match &body.creation_content {
let content = match &body.creation_content {
Some(content) => { Some(content) => {
use RoomVersionId::*; use RoomVersionId::*;
@ -213,11 +212,9 @@ pub(crate) async fn create_room_route(
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: TimelineEventType::RoomCreate, event_type: TimelineEventType::RoomCreate,
content: to_raw_value(&content).expect("event is valid, we just created it"), content: to_raw_value(&create_content).expect("create event content serialization"),
unsigned: None,
state_key: Some(String::new()), state_key: Some(String::new()),
redacts: None, ..Default::default()
timestamp: None,
}, },
sender_user, sender_user,
&room_id, &room_id,
@ -231,24 +228,16 @@ pub(crate) async fn create_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomMember, sender_user.to_string(),
content: to_raw_value(&RoomMemberEventContent { &RoomMemberEventContent {
membership: MembershipState::Join,
displayname: services.users.displayname(sender_user).await.ok(), displayname: services.users.displayname(sender_user).await.ok(),
avatar_url: services.users.avatar_url(sender_user).await.ok(), avatar_url: services.users.avatar_url(sender_user).await.ok(),
is_direct: Some(body.is_direct),
third_party_invite: None,
blurhash: services.users.blurhash(sender_user).await.ok(), blurhash: services.users.blurhash(sender_user).await.ok(),
reason: None, is_direct: Some(body.is_direct),
join_authorized_via_users_server: None, ..RoomMemberEventContent::new(MembershipState::Join)
}) },
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(sender_user.to_string()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -289,11 +278,9 @@ pub(crate) async fn create_room_route(
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: TimelineEventType::RoomPowerLevels, event_type: TimelineEventType::RoomPowerLevels,
content: to_raw_value(&power_levels_content).expect("to_raw_value always works on serde_json::Value"), content: to_raw_value(&power_levels_content).expect("serialized power_levels event content"),
unsigned: None,
state_key: Some(String::new()), state_key: Some(String::new()),
redacts: None, ..Default::default()
timestamp: None,
}, },
sender_user, sender_user,
&room_id, &room_id,
@ -308,18 +295,13 @@ pub(crate) async fn create_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomCanonicalAlias, String::new(),
content: to_raw_value(&RoomCanonicalAliasEventContent { &RoomCanonicalAliasEventContent {
alias: Some(room_alias_id.to_owned()), alias: Some(room_alias_id.to_owned()),
alt_aliases: vec![], alt_aliases: vec![],
}) },
.expect("We checked that alias earlier, it must be fine"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -335,19 +317,14 @@ pub(crate) async fn create_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomJoinRules, String::new(),
content: to_raw_value(&RoomJoinRulesEventContent::new(match preset { &RoomJoinRulesEventContent::new(match preset {
RoomPreset::PublicChat => JoinRule::Public, RoomPreset::PublicChat => JoinRule::Public,
// according to spec "invite" is the default // according to spec "invite" is the default
_ => JoinRule::Invite, _ => JoinRule::Invite,
})) }),
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -360,15 +337,10 @@ pub(crate) async fn create_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomHistoryVisibility, String::new(),
content: to_raw_value(&RoomHistoryVisibilityEventContent::new(HistoryVisibility::Shared)) &RoomHistoryVisibilityEventContent::new(HistoryVisibility::Shared),
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -381,18 +353,13 @@ pub(crate) async fn create_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomGuestAccess, String::new(),
content: to_raw_value(&RoomGuestAccessEventContent::new(match preset { &RoomGuestAccessEventContent::new(match preset {
RoomPreset::PublicChat => GuestAccess::Forbidden, RoomPreset::PublicChat => GuestAccess::Forbidden,
_ => GuestAccess::CanJoin, _ => GuestAccess::CanJoin,
})) }),
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -440,15 +407,7 @@ pub(crate) async fn create_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(String::new(), &RoomNameEventContent::new(name.clone())),
event_type: TimelineEventType::RoomName,
content: to_raw_value(&RoomNameEventContent::new(name.clone()))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -462,17 +421,12 @@ pub(crate) async fn create_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomTopic, String::new(),
content: to_raw_value(&RoomTopicEventContent { &RoomTopicEventContent {
topic: topic.clone(), topic: topic.clone(),
}) },
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -641,18 +595,13 @@ pub(crate) async fn upgrade_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomTombstone, String::new(),
content: to_raw_value(&RoomTombstoneEventContent { &RoomTombstoneEventContent {
body: "This room has been replaced".to_owned(), body: "This room has been replaced".to_owned(),
replacement_room: replacement_room.clone(), replacement_room: replacement_room.clone(),
}) },
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
sender_user, sender_user,
&body.room_id, &body.room_id,
&state_lock, &state_lock,
@ -788,10 +737,8 @@ pub(crate) async fn upgrade_room_route(
PduBuilder { PduBuilder {
event_type: event_type.to_string().into(), event_type: event_type.to_string().into(),
content: event_content, content: event_content,
unsigned: None,
state_key: Some(String::new()), state_key: Some(String::new()),
redacts: None, ..Default::default()
timestamp: None,
}, },
sender_user, sender_user,
&replacement_room, &replacement_room,
@ -821,7 +768,7 @@ pub(crate) async fn upgrade_room_route(
} }
// Get the old room power levels // Get the old room power levels
let mut power_levels_event_content: RoomPowerLevelsEventContent = services let power_levels_event_content: RoomPowerLevelsEventContent = services
.rooms .rooms
.state_accessor .state_accessor
.room_state_get_content(&body.room_id, &StateEventType::RoomPowerLevels, "") .room_state_get_content(&body.room_id, &StateEventType::RoomPowerLevels, "")
@ -836,8 +783,6 @@ pub(crate) async fn upgrade_room_route(
.checked_add(int!(1)) .checked_add(int!(1))
.ok_or_else(|| err!(Request(BadJson("users_default power levels event content is not valid"))))?, .ok_or_else(|| err!(Request(BadJson("users_default power levels event content is not valid"))))?,
); );
power_levels_event_content.events_default = new_level;
power_levels_event_content.invite = new_level;
// Modify the power levels in the old room to prevent sending of events and // Modify the power levels in the old room to prevent sending of events and
// inviting new users // inviting new users
@ -845,14 +790,14 @@ pub(crate) async fn upgrade_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomPowerLevels, String::new(),
content: to_raw_value(&power_levels_event_content).expect("event is valid, we just created it"), &RoomPowerLevelsEventContent {
unsigned: None, events_default: new_level,
state_key: Some(String::new()), invite: new_level,
redacts: None, ..power_levels_event_content
timestamp: None, },
}, ),
sender_user, sender_user,
&body.room_id, &body.room_id,
&state_lock, &state_lock,

View File

@ -177,10 +177,9 @@ async fn send_state_event_for_key_helper(
PduBuilder { PduBuilder {
event_type: event_type.to_string().into(), event_type: event_type.to_string().into(),
content: serde_json::from_str(json.json().get())?, content: serde_json::from_str(json.json().get())?,
unsigned: None,
state_key: Some(state_key), state_key: Some(state_key),
redacts: None,
timestamp, timestamp,
..Default::default()
}, },
sender, sender,
room_id, room_id,

View File

@ -8,7 +8,7 @@ use ruma::{
join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent}, join_rules::{AllowRule, JoinRule, RoomJoinRulesEventContent},
member::{MembershipState, RoomMemberEventContent}, member::{MembershipState, RoomMemberEventContent},
}, },
StateEventType, TimelineEventType, StateEventType,
}, },
CanonicalJsonObject, RoomId, RoomVersionId, UserId, CanonicalJsonObject, RoomId, RoomVersionId, UserId,
}; };
@ -125,30 +125,17 @@ pub(crate) async fn create_join_event_template_route(
)); ));
} }
let content = to_raw_value(&RoomMemberEventContent {
avatar_url: None,
blurhash: None,
displayname: None,
is_direct: None,
membership: MembershipState::Join,
third_party_invite: None,
reason: None,
join_authorized_via_users_server,
})
.expect("member event is valid value");
let (_pdu, mut pdu_json) = services let (_pdu, mut pdu_json) = services
.rooms .rooms
.timeline .timeline
.create_hash_and_sign_event( .create_hash_and_sign_event(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomMember, body.user_id.to_string(),
content, &RoomMemberEventContent {
unsigned: None, join_authorized_via_users_server,
state_key: Some(body.user_id.to_string()), ..RoomMemberEventContent::new(MembershipState::Join)
redacts: None, },
timestamp: None, ),
},
&body.user_id, &body.user_id,
&body.room_id, &body.room_id,
&state_lock, &state_lock,

View File

@ -2,10 +2,7 @@ use axum::extract::State;
use conduit::{Error, Result}; use conduit::{Error, Result};
use ruma::{ use ruma::{
api::{client::error::ErrorKind, federation::membership::prepare_leave_event}, api::{client::error::ErrorKind, federation::membership::prepare_leave_event},
events::{ events::room::member::{MembershipState, RoomMemberEventContent},
room::member::{MembershipState, RoomMemberEventContent},
TimelineEventType,
},
}; };
use serde_json::value::to_raw_value; use serde_json::value::to_raw_value;
@ -39,30 +36,12 @@ pub(crate) async fn create_leave_event_template_route(
let room_version_id = services.rooms.state.get_room_version(&body.room_id).await?; let room_version_id = services.rooms.state.get_room_version(&body.room_id).await?;
let state_lock = services.rooms.state.mutex.lock(&body.room_id).await; let state_lock = services.rooms.state.mutex.lock(&body.room_id).await;
let content = to_raw_value(&RoomMemberEventContent {
avatar_url: None,
blurhash: None,
displayname: None,
is_direct: None,
membership: MembershipState::Leave,
third_party_invite: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("member event is valid value");
let (_pdu, mut pdu_json) = services let (_pdu, mut pdu_json) = services
.rooms .rooms
.timeline .timeline
.create_hash_and_sign_event( .create_hash_and_sign_event(
PduBuilder { PduBuilder::state(body.user_id.to_string(), &RoomMemberEventContent::new(MembershipState::Leave)),
event_type: TimelineEventType::RoomMember,
content,
unsigned: None,
state_key: Some(body.user_id.to_string()),
redacts: None,
timestamp: None,
},
&body.user_id, &body.user_id,
&body.room_id, &body.room_id,
&state_lock, &state_lock,

View File

@ -1,20 +1,67 @@
use std::{collections::BTreeMap, sync::Arc}; use std::{collections::BTreeMap, sync::Arc};
use ruma::{events::TimelineEventType, EventId, MilliSecondsSinceUnixEpoch}; use ruma::{
events::{EventContent, MessageLikeEventType, StateEventType, TimelineEventType},
EventId, MilliSecondsSinceUnixEpoch,
};
use serde::Deserialize; use serde::Deserialize;
use serde_json::value::RawValue as RawJsonValue; use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
/// Build the start of a PDU in order to add it to the Database. /// Build the start of a PDU in order to add it to the Database.
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct PduBuilder { pub struct Builder {
#[serde(rename = "type")] #[serde(rename = "type")]
pub event_type: TimelineEventType, pub event_type: TimelineEventType,
pub content: Box<RawJsonValue>, pub content: Box<RawJsonValue>,
pub unsigned: Option<BTreeMap<String, serde_json::Value>>,
pub unsigned: Option<Unsigned>,
pub state_key: Option<String>, pub state_key: Option<String>,
pub redacts: Option<Arc<EventId>>, pub redacts: Option<Arc<EventId>>,
/// For timestamped messaging, should only be used for appservices
/// /// For timestamped messaging, should only be used for appservices.
/// Will be set to current time if None /// Will be set to current time if None
pub timestamp: Option<MilliSecondsSinceUnixEpoch>, pub timestamp: Option<MilliSecondsSinceUnixEpoch>,
} }
type Unsigned = BTreeMap<String, serde_json::Value>;
impl Builder {
pub fn state<T>(state_key: String, content: &T) -> Self
where
T: EventContent<EventType = StateEventType>,
{
Self {
event_type: content.event_type().into(),
content: to_raw_value(content).expect("Builder failed to serialize state event content to RawValue"),
state_key: Some(state_key),
..Self::default()
}
}
pub fn timeline<T>(content: &T) -> Self
where
T: EventContent<EventType = MessageLikeEventType>,
{
Self {
event_type: content.event_type().into(),
content: to_raw_value(content).expect("Builder failed to serialize timeline event content to RawValue"),
..Self::default()
}
}
}
impl Default for Builder {
fn default() -> Self {
Self {
event_type: "m.room.message".into(),
content: Box::<RawJsonValue>::default(),
unsigned: None,
state_key: None,
redacts: None,
timestamp: None,
}
}
}

View File

@ -21,7 +21,10 @@ use serde_json::{
value::{to_raw_value, RawValue as RawJsonValue, Value as JsonValue}, value::{to_raw_value, RawValue as RawJsonValue, Value as JsonValue},
}; };
pub use self::{builder::PduBuilder, count::PduCount}; pub use self::{
builder::{Builder, Builder as PduBuilder},
count::PduCount,
};
use crate::{err, is_true, warn, Error, Result}; use crate::{err, is_true, warn, Error, Result};
#[derive(Deserialize)] #[derive(Deserialize)]

View File

@ -2,24 +2,20 @@ use std::collections::BTreeMap;
use conduit::{pdu::PduBuilder, Result}; use conduit::{pdu::PduBuilder, Result};
use ruma::{ use ruma::{
events::{ events::room::{
room::{ canonical_alias::RoomCanonicalAliasEventContent,
canonical_alias::RoomCanonicalAliasEventContent, create::RoomCreateEventContent,
create::RoomCreateEventContent, guest_access::{GuestAccess, RoomGuestAccessEventContent},
guest_access::{GuestAccess, RoomGuestAccessEventContent}, history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent}, join_rules::{JoinRule, RoomJoinRulesEventContent},
join_rules::{JoinRule, RoomJoinRulesEventContent}, member::{MembershipState, RoomMemberEventContent},
member::{MembershipState, RoomMemberEventContent}, name::RoomNameEventContent,
name::RoomNameEventContent, power_levels::RoomPowerLevelsEventContent,
power_levels::RoomPowerLevelsEventContent, preview_url::RoomPreviewUrlsEventContent,
preview_url::RoomPreviewUrlsEventContent, topic::RoomTopicEventContent,
topic::RoomTopicEventContent,
},
TimelineEventType,
}, },
RoomId, RoomVersionId, RoomId, RoomVersionId,
}; };
use serde_json::value::to_raw_value;
use crate::Services; use crate::Services;
@ -44,7 +40,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
let room_version = services.globals.default_room_version(); let room_version = services.globals.default_room_version();
let mut content = { let create_content = {
use RoomVersionId::*; use RoomVersionId::*;
match room_version { match room_version {
V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 => RoomCreateEventContent::new_v1(server_user.clone()), V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 => RoomCreateEventContent::new_v1(server_user.clone()),
@ -52,23 +48,20 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
} }
}; };
content.federate = true;
content.predecessor = None;
content.room_version = room_version;
// 1. The room create event // 1. The room create event
services services
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomCreate, String::new(),
content: to_raw_value(&content).expect("event is valid, we just created it"), &RoomCreateEventContent {
unsigned: None, federate: true,
state_key: Some(String::new()), predecessor: None,
redacts: None, room_version,
timestamp: None, ..create_content
}, },
),
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -80,24 +73,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(server_user.to_string(), &RoomMemberEventContent::new(MembershipState::Join)),
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(server_user.to_string()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -111,18 +87,13 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomPowerLevels, String::new(),
content: to_raw_value(&RoomPowerLevelsEventContent { &RoomPowerLevelsEventContent {
users, users,
..Default::default() ..Default::default()
}) },
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -134,15 +105,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(String::new(), &RoomJoinRulesEventContent::new(JoinRule::Invite)),
event_type: TimelineEventType::RoomJoinRules,
content: to_raw_value(&RoomJoinRulesEventContent::new(JoinRule::Invite))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -154,15 +117,10 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomHistoryVisibility, String::new(),
content: to_raw_value(&RoomHistoryVisibilityEventContent::new(HistoryVisibility::Shared)) &RoomHistoryVisibilityEventContent::new(HistoryVisibility::Shared),
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -174,15 +132,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(String::new(), &RoomGuestAccessEventContent::new(GuestAccess::Forbidden)),
event_type: TimelineEventType::RoomGuestAccess,
content: to_raw_value(&RoomGuestAccessEventContent::new(GuestAccess::Forbidden))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -195,15 +145,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(String::new(), &RoomNameEventContent::new(room_name)),
event_type: TimelineEventType::RoomName,
content: to_raw_value(&RoomNameEventContent::new(room_name))
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -214,17 +156,12 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomTopic, String::new(),
content: to_raw_value(&RoomTopicEventContent { &RoomTopicEventContent {
topic: format!("Manage {}", services.globals.server_name()), topic: format!("Manage {}", services.globals.server_name()),
}) },
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -238,18 +175,13 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomCanonicalAlias, String::new(),
content: to_raw_value(&RoomCanonicalAliasEventContent { &RoomCanonicalAliasEventContent {
alias: Some(alias.clone()), alias: Some(alias.clone()),
alt_aliases: Vec::new(), alt_aliases: Vec::new(),
}) },
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -266,17 +198,12 @@ pub async fn create_admin_room(services: &Services) -> Result<()> {
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomPreviewUrls, String::new(),
content: to_raw_value(&RoomPreviewUrlsEventContent { &RoomPreviewUrlsEventContent {
disabled: true, disabled: true,
}) },
.expect("event is valid we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,

View File

@ -9,11 +9,10 @@ use ruma::{
power_levels::RoomPowerLevelsEventContent, power_levels::RoomPowerLevelsEventContent,
}, },
tag::{TagEvent, TagEventContent, TagInfo}, tag::{TagEvent, TagEventContent, TagInfo},
RoomAccountDataEventType, TimelineEventType, RoomAccountDataEventType,
}, },
RoomId, UserId, RoomId, UserId,
}; };
use serde_json::value::to_raw_value;
use crate::pdu::PduBuilder; use crate::pdu::PduBuilder;
@ -35,24 +34,7 @@ pub async fn make_user_admin(&self, user_id: &UserId) -> Result<()> {
self.services self.services
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(user_id.to_string(), &RoomMemberEventContent::new(MembershipState::Invite)),
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Invite,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(user_id.to_string()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -61,24 +43,7 @@ pub async fn make_user_admin(&self, user_id: &UserId) -> Result<()> {
self.services self.services
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(user_id.to_string(), &RoomMemberEventContent::new(MembershipState::Join)),
event_type: TimelineEventType::RoomMember,
content: to_raw_value(&RoomMemberEventContent {
membership: MembershipState::Join,
displayname: None,
avatar_url: None,
is_direct: None,
third_party_invite: None,
blurhash: None,
reason: None,
join_authorized_via_users_server: None,
})
.expect("event is valid, we just created it"),
unsigned: None,
state_key: Some(user_id.to_string()),
redacts: None,
timestamp: None,
},
user_id, user_id,
&room_id, &room_id,
&state_lock, &state_lock,
@ -91,18 +56,13 @@ pub async fn make_user_admin(&self, user_id: &UserId) -> Result<()> {
self.services self.services
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder::state(
event_type: TimelineEventType::RoomPowerLevels, String::new(),
content: to_raw_value(&RoomPowerLevelsEventContent { &RoomPowerLevelsEventContent {
users, users,
..Default::default() ..Default::default()
}) },
.expect("event is valid, we just created it"), ),
unsigned: None,
state_key: Some(String::new()),
redacts: None,
timestamp: None,
},
server_user, server_user,
&room_id, &room_id,
&state_lock, &state_lock,
@ -117,23 +77,18 @@ pub async fn make_user_admin(&self, user_id: &UserId) -> Result<()> {
} }
} }
let welcome_message = String::from("## Thank you for trying out conduwuit!\n\nconduwuit is a fork of upstream Conduit which is in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.\n\nHelpful links:\n> Git and Documentation: https://github.com/girlbossceo/conduwuit\n> Report issues: https://github.com/girlbossceo/conduwuit/issues\n\nFor a list of available commands, send the following message in this room: `!admin --help`\n\nHere are some rooms you can join (by typing the command):\n\nconduwuit room (Ask questions and get notified on updates):\n`/join #conduwuit:puppygock.gay`");
// Send welcome message // Send welcome message
self.services.timeline.build_and_append_pdu( self.services
PduBuilder { .timeline
event_type: TimelineEventType::RoomMessage, .build_and_append_pdu(
content: to_raw_value(&RoomMessageEventContent::text_markdown( PduBuilder::timeline(&RoomMessageEventContent::text_markdown(welcome_message)),
String::from("## Thank you for trying out conduwuit!\n\nconduwuit is a fork of upstream Conduit which is in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.\n\nHelpful links:\n> Git and Documentation: https://github.com/girlbossceo/conduwuit\n> Report issues: https://github.com/girlbossceo/conduwuit/issues\n\nFor a list of available commands, send the following message in this room: `!admin --help`\n\nHere are some rooms you can join (by typing the command):\n\nconduwuit room (Ask questions and get notified on updates):\n`/join #conduwuit:puppygock.gay`"), server_user,
)) &room_id,
.expect("event is valid, we just created it"), &state_lock,
unsigned: None, )
state_key: None, .await?;
redacts: None,
timestamp: None,
},
server_user,
&room_id,
&state_lock,
).await?;
Ok(()) Ok(())
} }

View File

@ -15,13 +15,9 @@ pub use create::create_admin_room;
use futures::{FutureExt, TryFutureExt}; use futures::{FutureExt, TryFutureExt};
use loole::{Receiver, Sender}; use loole::{Receiver, Sender};
use ruma::{ use ruma::{
events::{ events::room::message::{Relation, RoomMessageEventContent},
room::message::{Relation, RoomMessageEventContent},
TimelineEventType,
},
OwnedEventId, OwnedRoomId, RoomId, UserId, OwnedEventId, OwnedRoomId, RoomId, UserId,
}; };
use serde_json::value::to_raw_value;
use tokio::sync::{Mutex, RwLock}; use tokio::sync::{Mutex, RwLock};
use crate::{account_data, globals, rooms, rooms::state::RoomMutexGuard, Dep}; use crate::{account_data, globals, rooms, rooms::state::RoomMutexGuard, Dep};
@ -285,20 +281,12 @@ impl Service {
) -> Result<()> { ) -> Result<()> {
assert!(self.user_is_admin(user_id).await, "sender is not admin"); assert!(self.user_is_admin(user_id).await, "sender is not admin");
let response_pdu = PduBuilder {
event_type: TimelineEventType::RoomMessage,
content: to_raw_value(&content).expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: None,
timestamp: None,
};
let state_lock = self.services.state.mutex.lock(room_id).await; let state_lock = self.services.state.mutex.lock(room_id).await;
if let Err(e) = self if let Err(e) = self
.services .services
.timeline .timeline
.build_and_append_pdu(response_pdu, user_id, room_id, &state_lock) .build_and_append_pdu(PduBuilder::timeline(&content), user_id, room_id, &state_lock)
.await .await
{ {
self.handle_response_error(e, room_id, user_id, &state_lock) self.handle_response_error(e, room_id, user_id, &state_lock)
@ -313,23 +301,14 @@ impl Service {
&self, e: Error, room_id: &RoomId, user_id: &UserId, state_lock: &RoomMutexGuard, &self, e: Error, room_id: &RoomId, user_id: &UserId, state_lock: &RoomMutexGuard,
) -> Result<()> { ) -> Result<()> {
error!("Failed to build and append admin room response PDU: \"{e}\""); error!("Failed to build and append admin room response PDU: \"{e}\"");
let error_room_message = RoomMessageEventContent::text_plain(format!( let content = RoomMessageEventContent::text_plain(format!(
"Failed to build and append admin room PDU: \"{e}\"\n\nThe original admin command may have finished \ "Failed to build and append admin room PDU: \"{e}\"\n\nThe original admin command may have finished \
successfully, but we could not return the output." successfully, but we could not return the output."
)); ));
let response_pdu = PduBuilder {
event_type: TimelineEventType::RoomMessage,
content: to_raw_value(&error_room_message).expect("event is valid, we just created it"),
unsigned: None,
state_key: None,
redacts: None,
timestamp: None,
};
self.services self.services
.timeline .timeline
.build_and_append_pdu(response_pdu, user_id, room_id, state_lock) .build_and_append_pdu(PduBuilder::timeline(&content), user_id, room_id, state_lock)
.await?; .await?;
Ok(()) Ok(())

View File

@ -37,7 +37,6 @@ use ruma::{
ServerName, UserId, ServerName, UserId,
}; };
use serde::Deserialize; use serde::Deserialize;
use serde_json::value::to_raw_value;
use self::data::Data; use self::data::Data;
use crate::{rooms, rooms::state::RoomMutexGuard, Dep}; use crate::{rooms, rooms::state::RoomMutexGuard, Dep};
@ -353,21 +352,14 @@ impl Service {
pub async fn user_can_invite( pub async fn user_can_invite(
&self, room_id: &RoomId, sender: &UserId, target_user: &UserId, state_lock: &RoomMutexGuard, &self, room_id: &RoomId, sender: &UserId, target_user: &UserId, state_lock: &RoomMutexGuard,
) -> bool { ) -> bool {
let content = to_raw_value(&RoomMemberEventContent::new(MembershipState::Invite))
.expect("Event content always serializes");
let new_event = PduBuilder {
event_type: ruma::events::TimelineEventType::RoomMember,
content,
unsigned: None,
state_key: Some(target_user.into()),
redacts: None,
timestamp: None,
};
self.services self.services
.timeline .timeline
.create_hash_and_sign_event(new_event, sender, room_id, state_lock) .create_hash_and_sign_event(
PduBuilder::state(target_user.into(), &RoomMemberEventContent::new(MembershipState::Invite)),
sender,
room_id,
state_lock,
)
.await .await
.is_ok() .is_ok()
} }