appservice-api: Remove sync conversion helper
This commit is contained in:
parent
a8e7c47bbe
commit
d37a9316bc
@ -16,7 +16,6 @@ all-features = true
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
unstable-exhaustive-types = []
|
unstable-exhaustive-types = []
|
||||||
helper = ["ruma-client-api", "tracing"]
|
|
||||||
client = []
|
client = []
|
||||||
server = []
|
server = []
|
||||||
|
|
||||||
|
@ -49,53 +49,6 @@ pub mod v1 {
|
|||||||
pub fn new(txn_id: OwnedTransactionId, events: Vec<Raw<AnyRoomEvent>>) -> IncomingRequest {
|
pub fn new(txn_id: OwnedTransactionId, events: Vec<Raw<AnyRoomEvent>>) -> IncomingRequest {
|
||||||
IncomingRequest { txn_id, events }
|
IncomingRequest { txn_id, events }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Consumes the `IncomingRequest` and tries to convert it to a `sync_events::Response`
|
|
||||||
///
|
|
||||||
/// This is a helper conversion in cases where it's easier to work with
|
|
||||||
/// `sync_events::Response` instead of the original `push_events::IncomingRequest`.
|
|
||||||
/// It puts all events with a `room_id` into the `JoinedRoom`'s `timeline`. The
|
|
||||||
/// rationale behind that is that incoming Appservice transactions from the
|
|
||||||
/// homeserver are not necessarily bound to a specific user but can cover
|
|
||||||
/// a multitude of namespaces, and as such the Appservice basically only "observes joined
|
|
||||||
/// rooms".
|
|
||||||
///
|
|
||||||
/// Note: Currently homeservers only push PDUs to appservices, no EDUs. There's the open
|
|
||||||
/// [MSC2409] regarding supporting EDUs in the future, though it seems to be planned to put
|
|
||||||
/// EDUs into a different JSON key than `events` to stay backwards compatible.
|
|
||||||
///
|
|
||||||
/// [MSC2409]: https://github.com/matrix-org/matrix-spec-proposals/pull/2409
|
|
||||||
#[cfg(feature = "helper")]
|
|
||||||
pub fn try_into_sync_response(
|
|
||||||
self,
|
|
||||||
next_batch: impl Into<String>,
|
|
||||||
) -> serde_json::Result<ruma_client_api::sync::sync_events::v3::Response> {
|
|
||||||
use ruma_client_api::sync::sync_events;
|
|
||||||
use ruma_common::OwnedRoomId;
|
|
||||||
use serde::Deserialize;
|
|
||||||
use tracing::warn;
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
struct EventDeHelper {
|
|
||||||
room_id: Option<OwnedRoomId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut response = sync_events::v3::Response::new(next_batch.into());
|
|
||||||
|
|
||||||
for raw_event in self.events {
|
|
||||||
let helper = raw_event.deserialize_as::<EventDeHelper>()?;
|
|
||||||
let event_json = Raw::into_json(raw_event);
|
|
||||||
|
|
||||||
if let Some(room_id) = helper.room_id {
|
|
||||||
let join = response.rooms.join.entry(room_id).or_default();
|
|
||||||
join.timeline.events.push(Raw::from_json(event_json));
|
|
||||||
} else {
|
|
||||||
warn!("Event without room_id: {event_json}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(response)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Response {
|
impl Response {
|
||||||
@ -105,62 +58,6 @@ pub mod v1 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "helper")]
|
|
||||||
#[cfg(test)]
|
|
||||||
mod helper_tests {
|
|
||||||
use ruma_client_api::sync::sync_events;
|
|
||||||
use ruma_common::{room_id, TransactionId};
|
|
||||||
use serde_json::{json, value::to_raw_value as to_raw_json_value};
|
|
||||||
|
|
||||||
use super::{IncomingRequest, Raw};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn convert_incoming_request_to_sync_response() {
|
|
||||||
let txn_id = <&TransactionId>::from("any_txn_id");
|
|
||||||
let state_event = Raw::from_json(
|
|
||||||
to_raw_json_value(&json!({
|
|
||||||
"content": {},
|
|
||||||
"event_id": "$h29iv0s8:example.com",
|
|
||||||
"origin_server_ts": 1,
|
|
||||||
"room_id": "!roomid:room.com",
|
|
||||||
"sender": "@carl:example.com",
|
|
||||||
"state_key": "",
|
|
||||||
"type": "m.room.name"
|
|
||||||
}))
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
let message_event = Raw::from_json(
|
|
||||||
to_raw_json_value(&json!({
|
|
||||||
"type": "m.room.message",
|
|
||||||
"event_id": "$143273582443PhrSn:example.com",
|
|
||||||
"origin_server_ts": 1,
|
|
||||||
"room_id": "!roomid:room.com",
|
|
||||||
"sender": "@user:example.com",
|
|
||||||
"content": {
|
|
||||||
"body": "test",
|
|
||||||
"msgtype": "m.audio",
|
|
||||||
"url": "mxc://example.com/AuDi0",
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
.unwrap(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let events = vec![state_event, message_event];
|
|
||||||
let incoming_request = IncomingRequest { txn_id: txn_id.into(), events };
|
|
||||||
|
|
||||||
let response: sync_events::v3::Response =
|
|
||||||
incoming_request.try_into_sync_response("token").unwrap();
|
|
||||||
|
|
||||||
let response_rooms_join = response
|
|
||||||
.rooms
|
|
||||||
.join
|
|
||||||
.get(room_id!("!roomid:room.com"))
|
|
||||||
.expect("joined room response");
|
|
||||||
|
|
||||||
assert_eq!(response_rooms_join.timeline.events.len(), 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "server")]
|
#[cfg(feature = "server")]
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -94,10 +94,6 @@ compat = [
|
|||||||
# Specific compatibility for past ring public/private key documents.
|
# Specific compatibility for past ring public/private key documents.
|
||||||
ring-compat = ["ruma-signatures/ring-compat"]
|
ring-compat = ["ruma-signatures/ring-compat"]
|
||||||
|
|
||||||
# Helper features that aren't exactly part of the spec but could be helpful
|
|
||||||
# for crate consumers
|
|
||||||
appservice-api-helper = ["ruma-appservice-api/helper"]
|
|
||||||
|
|
||||||
# unstable: by using any of these, you opt out of all semver guarantees Ruma
|
# unstable: by using any of these, you opt out of all semver guarantees Ruma
|
||||||
# otherwise provides!
|
# otherwise provides!
|
||||||
unstable-exhaustive-types = [
|
unstable-exhaustive-types = [
|
||||||
|
Loading…
x
Reference in New Issue
Block a user