From 62f1da053f59d1b7ca98c22b548dcdaee27de31d Mon Sep 17 00:00:00 2001 From: Matthias Ahouansou Date: Sat, 22 Jun 2024 11:50:39 +0100 Subject: [PATCH] feat(appservice): support timestamped messaging --- src/api/client_server/membership.rs | 7 +++++++ src/api/client_server/message.rs | 5 +++++ src/api/client_server/profile.rs | 2 ++ src/api/client_server/redact.rs | 1 + src/api/client_server/room.rs | 14 ++++++++++++++ src/api/client_server/state.rs | 14 +++++++++++++- src/api/server_server.rs | 1 + src/service/admin/mod.rs | 14 ++++++++++++++ src/service/pdu.rs | 4 ++++ src/service/rooms/state_accessor/mod.rs | 1 + src/service/rooms/timeline/mod.rs | 11 ++++++----- 11 files changed, 68 insertions(+), 6 deletions(-) diff --git a/src/api/client_server/membership.rs b/src/api/client_server/membership.rs index 1ca711e2..5c0169d2 100644 --- a/src/api/client_server/membership.rs +++ b/src/api/client_server/membership.rs @@ -241,6 +241,7 @@ pub async fn kick_user_route( unsigned: None, state_key: Some(body.user_id.to_string()), redacts: None, + timestamp: None, }, sender_user, &body.room_id, @@ -313,6 +314,7 @@ pub async fn ban_user_route(body: Ruma) -> Result( unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, sender_user, room_id, @@ -1379,6 +1384,7 @@ pub(crate) async fn invite_helper<'a>( unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, sender_user, room_id, @@ -1506,6 +1512,7 @@ pub async fn leave_room(user_id: &UserId, room_id: &RoomId, reason: Option, state_key: String, + timestamp: Option, ) -> Result> { let sender_user = sender; @@ -243,6 +254,7 @@ async fn send_state_event_for_key_helper( unsigned: None, state_key: Some(state_key), redacts: None, + timestamp, }, sender_user, room_id, diff --git a/src/api/server_server.rs b/src/api/server_server.rs index 605a4672..8d205f56 100644 --- a/src/api/server_server.rs +++ b/src/api/server_server.rs @@ -1445,6 +1445,7 @@ pub 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/service/admin/mod.rs b/src/service/admin/mod.rs index 70c63381..583bfcd1 100644 --- a/src/service/admin/mod.rs +++ b/src/service/admin/mod.rs @@ -246,6 +246,7 @@ impl Service { unsigned: None, state_key: None, redacts: None, + timestamp: None, }, conduit_user, &conduit_room, @@ -1105,6 +1106,7 @@ impl Service { unsigned: None, state_key: Some("".to_owned()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1133,6 +1135,7 @@ impl Service { unsigned: None, state_key: Some(conduit_user.to_string()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1158,6 +1161,7 @@ impl Service { unsigned: None, state_key: Some("".to_owned()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1177,6 +1181,7 @@ impl Service { unsigned: None, state_key: Some("".to_owned()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1198,6 +1203,7 @@ impl Service { unsigned: None, state_key: Some("".to_owned()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1219,6 +1225,7 @@ impl Service { unsigned: None, state_key: Some("".to_owned()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1239,6 +1246,7 @@ impl Service { unsigned: None, state_key: Some("".to_owned()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1259,6 +1267,7 @@ impl Service { unsigned: None, state_key: Some("".to_owned()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1283,6 +1292,7 @@ impl Service { unsigned: None, state_key: Some("".to_owned()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1352,6 +1362,7 @@ impl Service { unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1378,6 +1389,7 @@ impl Service { unsigned: None, state_key: Some(user_id.to_string()), redacts: None, + timestamp: None, }, user_id, &room_id, @@ -1404,6 +1416,7 @@ impl Service { unsigned: None, state_key: Some("".to_owned()), redacts: None, + timestamp: None, }, conduit_user, &room_id, @@ -1423,6 +1436,7 @@ impl Service { unsigned: None, state_key: None, redacts: None, + timestamp: None, }, conduit_user, &room_id, diff --git a/src/service/pdu.rs b/src/service/pdu.rs index 6991a083..dab7b6e0 100644 --- a/src/service/pdu.rs +++ b/src/service/pdu.rs @@ -460,4 +460,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/rooms/state_accessor/mod.rs b/src/service/rooms/state_accessor/mod.rs index 53e3176f..aa654aee 100644 --- a/src/service/rooms/state_accessor/mod.rs +++ b/src/service/rooms/state_accessor/mod.rs @@ -321,6 +321,7 @@ impl Service { unsigned: None, state_key: Some(target_user.into()), redacts: None, + timestamp: None, }; Ok(services() diff --git a/src/service/rooms/timeline/mod.rs b/src/service/rooms/timeline/mod.rs index 29d8339d..6603ea65 100644 --- a/src/service/rooms/timeline/mod.rs +++ b/src/service/rooms/timeline/mod.rs @@ -22,8 +22,8 @@ use ruma::{ }, push::{Action, Ruleset, Tweak}, state_res::{self, Event, RoomVersion}, - uint, user_id, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId, OwnedRoomId, - OwnedServerName, RoomId, RoomVersionId, ServerName, UserId, + uint, user_id, CanonicalJsonObject, CanonicalJsonValue, EventId, MilliSecondsSinceUnixEpoch, + OwnedEventId, OwnedRoomId, OwnedServerName, RoomId, RoomVersionId, ServerName, UserId, }; use serde::Deserialize; use serde_json::value::{to_raw_value, RawValue as RawJsonValue}; @@ -665,6 +665,7 @@ impl Service { unsigned, state_key, redacts, + timestamp, } = pdu_builder; let prev_events: Vec<_> = services() @@ -734,9 +735,9 @@ impl Service { event_id: ruma::event_id!("$thiswillbefilledinlater").into(), room_id: room_id.to_owned(), sender: sender.to_owned(), - origin_server_ts: utils::millis_since_unix_epoch() - .try_into() - .expect("time is valid"), + origin_server_ts: timestamp + .map(|ts| ts.get()) + .unwrap_or_else(|| MilliSecondsSinceUnixEpoch::now().get()), kind: event_type, content, state_key,