From 2d9aab2e79ee3426a3b447f1f300dd6e70e8edb9 Mon Sep 17 00:00:00 2001 From: Matthias Ahouansou Date: Wed, 14 Aug 2024 20:01:34 -0400 Subject: [PATCH] support timestamped appservice messaging Co-authored-by: strawberry Signed-off-by: strawberry --- src/api/client/membership.rs | 7 +++++++ src/api/client/message.rs | 5 +++++ src/api/client/profile.rs | 2 ++ src/api/client/redact.rs | 1 + src/api/client/room.rs | 14 ++++++++++++++ src/api/client/state.rs | 8 +++++++- src/api/server/make_join.rs | 1 + src/api/server/make_leave.rs | 1 + src/core/pdu/builder.rs | 6 +++++- src/service/admin/create.rs | 10 ++++++++++ src/service/admin/grant.rs | 4 ++++ src/service/admin/mod.rs | 2 ++ src/service/rooms/state_accessor/mod.rs | 1 + src/service/rooms/timeline/mod.rs | 12 +++++++++--- 14 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/api/client/membership.rs b/src/api/client/membership.rs index 9eb40b4a..8a2ab8b4 100644 --- a/src/api/client/membership.rs +++ b/src/api/client/membership.rs @@ -403,6 +403,7 @@ pub(crate) async fn kick_user_route( unsigned: None, state_key: Some(body.user_id.to_string()), redacts: None, + timestamp: None, }, sender_user, &body.room_id, @@ -465,6 +466,7 @@ pub(crate) async fn ban_user_route( unsigned: None, state_key: Some(body.user_id.to_string()), redacts: None, + timestamp: None, }, sender_user, &body.room_id, @@ -512,6 +514,7 @@ pub(crate) async fn unban_user_route( unsigned: None, state_key: Some(body.user_id.to_string()), redacts: None, + timestamp: None, }, sender_user, &body.room_id, @@ -1100,6 +1103,7 @@ async fn join_room_by_id_helper_local( unsigned: None, state_key: Some(sender_user.to_string()), redacts: None, + timestamp: None, }, sender_user, room_id, @@ -1412,6 +1416,7 @@ pub(crate) async fn invite_helper( unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, sender_user, room_id, @@ -1523,6 +1528,7 @@ pub(crate) async fn invite_helper( unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, sender_user, room_id, @@ -1640,6 +1646,7 @@ pub async fn leave_room(services: &Services, user_id: &UserId, room_id: &RoomId, unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, user_id, room_id, diff --git a/src/api/client/message.rs b/src/api/client/message.rs index 9aae4aaf..51aee8c1 100644 --- a/src/api/client/message.rs +++ b/src/api/client/message.rs @@ -84,6 +84,11 @@ pub(crate) async fn send_message_event_route( unsigned: Some(unsigned), state_key: None, redacts: None, + timestamp: if body.appservice_info.is_some() { + body.timestamp + } else { + None + }, }, sender_user, &body.room_id, diff --git a/src/api/client/profile.rs b/src/api/client/profile.rs index a72938b5..71d49cd8 100644 --- a/src/api/client/profile.rs +++ b/src/api/client/profile.rs @@ -295,6 +295,7 @@ pub async fn update_displayname( unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, room_id, )) @@ -355,6 +356,7 @@ pub async fn update_avatar_url( unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, room_id, )) diff --git a/src/api/client/redact.rs b/src/api/client/redact.rs index 89446754..2102f6cd 100644 --- a/src/api/client/redact.rs +++ b/src/api/client/redact.rs @@ -34,6 +34,7 @@ pub(crate) async fn redact_event_route( unsigned: None, state_key: None, redacts: Some(body.event_id.into()), + timestamp: None, }, sender_user, &body.room_id, diff --git a/src/api/client/room.rs b/src/api/client/room.rs index e894b878..a18ae206 100644 --- a/src/api/client/room.rs +++ b/src/api/client/room.rs @@ -197,6 +197,7 @@ pub(crate) async fn create_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &room_id, @@ -225,6 +226,7 @@ pub(crate) async fn create_room_route( unsigned: None, state_key: Some(sender_user.to_string()), redacts: None, + timestamp: None, }, sender_user, &room_id, @@ -262,6 +264,7 @@ pub(crate) async fn create_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &room_id, @@ -285,6 +288,7 @@ pub(crate) async fn create_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &room_id, @@ -311,6 +315,7 @@ pub(crate) async fn create_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &room_id, @@ -330,6 +335,7 @@ pub(crate) async fn create_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &room_id, @@ -352,6 +358,7 @@ pub(crate) async fn create_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &room_id, @@ -405,6 +412,7 @@ pub(crate) async fn create_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &room_id, @@ -427,6 +435,7 @@ pub(crate) async fn create_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &room_id, @@ -581,6 +590,7 @@ pub(crate) async fn upgrade_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &body.room_id, @@ -672,6 +682,7 @@ pub(crate) async fn upgrade_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &replacement_room, @@ -700,6 +711,7 @@ pub(crate) async fn upgrade_room_route( unsigned: None, state_key: Some(sender_user.to_string()), redacts: None, + timestamp: None, }, sender_user, &replacement_room, @@ -728,6 +740,7 @@ pub(crate) async fn upgrade_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &replacement_room, @@ -786,6 +799,7 @@ pub(crate) async fn upgrade_room_route( unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, sender_user, &body.room_id, diff --git a/src/api/client/state.rs b/src/api/client/state.rs index d0fb83d1..fd049663 100644 --- a/src/api/client/state.rs +++ b/src/api/client/state.rs @@ -43,6 +43,11 @@ pub(crate) async fn send_state_event_for_key_route( &body.event_type, &body.body.body, body.state_key.clone(), + if body.appservice_info.is_some() { + body.timestamp + } else { + None + }, ) .await? .into(), @@ -172,7 +177,7 @@ pub(crate) async fn get_state_events_for_empty_key_route( async fn send_state_event_for_key_helper( services: &Services, sender: &UserId, room_id: &RoomId, event_type: &StateEventType, - json: &Raw, state_key: String, + json: &Raw, state_key: String, timestamp: Option, ) -> Result> { allowed_to_send_state_event(services, room_id, event_type, json).await?; let state_lock = services.rooms.state.mutex.lock(room_id).await; @@ -186,6 +191,7 @@ async fn send_state_event_for_key_helper( unsigned: None, state_key: Some(state_key), redacts: None, + timestamp, }, sender, room_id, diff --git a/src/api/server/make_join.rs b/src/api/server/make_join.rs index e9b1a6c7..86e821d1 100644 --- a/src/api/server/make_join.rs +++ b/src/api/server/make_join.rs @@ -139,6 +139,7 @@ pub(crate) async fn create_join_event_template_route( unsigned: None, state_key: Some(body.user_id.to_string()), redacts: None, + timestamp: None, }, &body.user_id, &body.room_id, diff --git a/src/api/server/make_leave.rs b/src/api/server/make_leave.rs index ae7237ad..3eb0d77a 100644 --- a/src/api/server/make_leave.rs +++ b/src/api/server/make_leave.rs @@ -57,6 +57,7 @@ pub(crate) async fn create_leave_event_template_route( unsigned: None, state_key: Some(body.user_id.to_string()), redacts: None, + timestamp: None, }, &body.user_id, &body.room_id, diff --git a/src/core/pdu/builder.rs b/src/core/pdu/builder.rs index a8bad677..ba4c19e5 100644 --- a/src/core/pdu/builder.rs +++ b/src/core/pdu/builder.rs @@ -1,6 +1,6 @@ use std::{collections::BTreeMap, sync::Arc}; -use ruma::{events::TimelineEventType, EventId}; +use ruma::{events::TimelineEventType, EventId, MilliSecondsSinceUnixEpoch}; use serde::Deserialize; use serde_json::value::RawValue as RawJsonValue; @@ -13,4 +13,8 @@ pub struct PduBuilder { pub unsigned: Option>, pub state_key: Option, pub redacts: Option>, + /// For timestamped messaging, should only be used for appservices + /// + /// Will be set to current time if None + pub timestamp: Option, } diff --git a/src/service/admin/create.rs b/src/service/admin/create.rs index 18cbe039..5cc85d70 100644 --- a/src/service/admin/create.rs +++ b/src/service/admin/create.rs @@ -71,6 +71,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> { unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -99,6 +100,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> { unsigned: None, state_key: Some(server_user.to_string()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -124,6 +126,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> { unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -143,6 +146,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> { unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -162,6 +166,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> { unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -181,6 +186,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> { unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -201,6 +207,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> { unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -221,6 +228,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> { unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -245,6 +253,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> { unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -271,6 +280,7 @@ pub async fn create_admin_room(services: &Services) -> Result<()> { unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, server_user, &room_id, diff --git a/src/service/admin/grant.rs b/src/service/admin/grant.rs index c35f8c42..44e11a3e 100644 --- a/src/service/admin/grant.rs +++ b/src/service/admin/grant.rs @@ -50,6 +50,7 @@ impl super::Service { unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -75,6 +76,7 @@ impl super::Service { unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, user_id, &room_id, @@ -100,6 +102,7 @@ impl super::Service { unsigned: None, state_key: Some(String::new()), redacts: None, + timestamp: None, }, server_user, &room_id, @@ -119,6 +122,7 @@ impl super::Service { unsigned: None, state_key: None, redacts: None, + timestamp: None, }, server_user, &room_id, diff --git a/src/service/admin/mod.rs b/src/service/admin/mod.rs index 0e8a65f1..962d31dd 100644 --- a/src/service/admin/mod.rs +++ b/src/service/admin/mod.rs @@ -260,6 +260,7 @@ impl Service { unsigned: None, state_key: None, redacts: None, + timestamp: None, }; if let Err(e) = self @@ -289,6 +290,7 @@ impl Service { unsigned: None, state_key: None, redacts: None, + timestamp: None, }; self.services diff --git a/src/service/rooms/state_accessor/mod.rs b/src/service/rooms/state_accessor/mod.rs index 2526f1bd..58fa31b3 100644 --- a/src/service/rooms/state_accessor/mod.rs +++ b/src/service/rooms/state_accessor/mod.rs @@ -350,6 +350,7 @@ impl Service { unsigned: None, state_key: Some(target_user.into()), redacts: None, + timestamp: None, }; Ok(self diff --git a/src/service/rooms/timeline/mod.rs b/src/service/rooms/timeline/mod.rs index ac25b2a8..22c27498 100644 --- a/src/service/rooms/timeline/mod.rs +++ b/src/service/rooms/timeline/mod.rs @@ -638,6 +638,7 @@ impl Service { unsigned, state_key, redacts, + timestamp, } = pdu_builder; let prev_events: Vec<_> = self @@ -705,9 +706,14 @@ impl Service { room_id: room_id.to_owned(), sender: sender.to_owned(), origin: None, - origin_server_ts: utils::millis_since_unix_epoch() - .try_into() - .expect("time is valid"), + origin_server_ts: timestamp.map_or_else( + || { + utils::millis_since_unix_epoch() + .try_into() + .expect("u64 fits into UInt") + }, + |ts| ts.get(), + ), kind: event_type, content, state_key,