diff --git a/crates/ruma-client-api/src/r0/contact/get_contacts.rs b/crates/ruma-client-api/src/r0/contact/get_contacts.rs index 569e29d7..b567557d 100644 --- a/crates/ruma-client-api/src/r0/contact/get_contacts.rs +++ b/crates/ruma-client-api/src/r0/contact/get_contacts.rs @@ -1,9 +1,7 @@ //! [GET /_matrix/client/r0/account/3pid](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-account-3pid) -use std::time::SystemTime; - use ruma_api::ruma_api; -use ruma_common::thirdparty::Medium; +use ruma_common::{thirdparty::Medium, MilliSecondsSinceUnixEpoch}; use serde::{Deserialize, Serialize}; ruma_api! { @@ -58,12 +56,10 @@ pub struct ThirdPartyIdentifier { pub medium: Medium, /// The time when the identifier was validated by the identity server. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub validated_at: SystemTime, + pub validated_at: MilliSecondsSinceUnixEpoch, /// The time when the homeserver associated the third party identifier with the user. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub added_at: SystemTime, + pub added_at: MilliSecondsSinceUnixEpoch, } /// Initial set of fields of `ThirdPartyIdentifier`. @@ -79,10 +75,10 @@ pub struct ThirdPartyIdentifierInit { pub medium: Medium, /// The time when the identifier was validated by the identity server. - pub validated_at: SystemTime, + pub validated_at: MilliSecondsSinceUnixEpoch, /// The time when the homeserver associated the third party identifier with the user. - pub added_at: SystemTime, + pub added_at: MilliSecondsSinceUnixEpoch, } impl From for ThirdPartyIdentifier { @@ -94,8 +90,9 @@ impl From for ThirdPartyIdentifier { #[cfg(test)] mod tests { - use std::time::{Duration, UNIX_EPOCH}; + use std::convert::TryInto; + use ruma_common::MilliSecondsSinceUnixEpoch; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; use super::{Medium, ThirdPartyIdentifier}; @@ -105,8 +102,8 @@ mod tests { let third_party_id = ThirdPartyIdentifier { address: "monkey@banana.island".into(), medium: Medium::Email, - validated_at: UNIX_EPOCH + Duration::from_millis(1_535_176_800_000), - added_at: UNIX_EPOCH + Duration::from_millis(1_535_336_848_756), + validated_at: MilliSecondsSinceUnixEpoch(1_535_176_800_000_u64.try_into().unwrap()), + added_at: MilliSecondsSinceUnixEpoch(1_535_336_848_756_u64.try_into().unwrap()), }; let third_party_id_serialized = json!({ diff --git a/crates/ruma-client-api/src/r0/device.rs b/crates/ruma-client-api/src/r0/device.rs index a0740ef4..19ba1b00 100644 --- a/crates/ruma-client-api/src/r0/device.rs +++ b/crates/ruma-client-api/src/r0/device.rs @@ -1,7 +1,6 @@ //! Endpoints for managing devices. -use std::time::SystemTime; - +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::DeviceIdBox; use serde::{Deserialize, Serialize}; @@ -25,12 +24,8 @@ pub struct Device { pub last_seen_ip: Option, /// Unix timestamp that the session was last active. - #[serde( - with = "ruma_serde::time::opt_ms_since_unix_epoch", - default, - skip_serializing_if = "Option::is_none" - )] - pub last_seen_ts: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub last_seen_ts: Option, } impl Device { diff --git a/crates/ruma-client-api/src/r0/media/get_media_preview.rs b/crates/ruma-client-api/src/r0/media/get_media_preview.rs index 9d95b370..819ec015 100644 --- a/crates/ruma-client-api/src/r0/media/get_media_preview.rs +++ b/crates/ruma-client-api/src/r0/media/get_media_preview.rs @@ -1,8 +1,7 @@ //! [GET /_matrix/media/r0/preview_url](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-media-r0-preview-url) -use std::time::SystemTime; - use ruma_api::ruma_api; +use ruma_common::MilliSecondsSinceUnixEpoch; use serde::Serialize; use serde_json::value::{to_raw_value as to_raw_json_value, RawValue as RawJsonValue}; @@ -23,8 +22,7 @@ ruma_api! { /// Preferred point in time (in milliseconds) to return a preview for. #[ruma_api(query)] - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub ts: SystemTime, + pub ts: MilliSecondsSinceUnixEpoch, } #[derive(Default)] @@ -42,7 +40,7 @@ ruma_api! { impl<'a> Request<'a> { /// Creates a new `Request` with the given url and timestamp. - pub fn new(url: &'a str, ts: SystemTime) -> Self { + pub fn new(url: &'a str, ts: MilliSecondsSinceUnixEpoch) -> Self { Self { url, ts } } } diff --git a/crates/ruma-client-api/src/r0/push/get_notifications.rs b/crates/ruma-client-api/src/r0/push/get_notifications.rs index 82ab0608..6508e86a 100644 --- a/crates/ruma-client-api/src/r0/push/get_notifications.rs +++ b/crates/ruma-client-api/src/r0/push/get_notifications.rs @@ -1,10 +1,8 @@ //! [GET /_matrix/client/r0/notifications](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-notifications) -use std::time::SystemTime; - use js_int::UInt; use ruma_api::ruma_api; -use ruma_common::push::Action; +use ruma_common::{push::Action, MilliSecondsSinceUnixEpoch}; use ruma_events::AnySyncRoomEvent; use ruma_identifiers::RoomId; use ruma_serde::Raw; @@ -89,8 +87,7 @@ pub struct Notification { pub room_id: RoomId, /// The time at which the event notification was sent. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub ts: SystemTime, + pub ts: MilliSecondsSinceUnixEpoch, } impl Notification { @@ -101,7 +98,7 @@ impl Notification { event: Raw, read: bool, room_id: RoomId, - ts: SystemTime, + ts: MilliSecondsSinceUnixEpoch, ) -> Self { Self { actions, event, profile_tag: None, read, room_id, ts } } diff --git a/crates/ruma-client-api/src/r0/server/get_user_info.rs b/crates/ruma-client-api/src/r0/server/get_user_info.rs index 573e071c..a4e181d0 100644 --- a/crates/ruma-client-api/src/r0/server/get_user_info.rs +++ b/crates/ruma-client-api/src/r0/server/get_user_info.rs @@ -1,8 +1,9 @@ //! [GET /_matrix/client/r0/admin/whois/{userId}](https://matrix.org/docs/spec/client_server/r0.6.0#get-matrix-client-r0-admin-whois-userid) -use std::{collections::BTreeMap, time::SystemTime}; +use std::collections::BTreeMap; use ruma_api::ruma_api; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::UserId; use serde::{Deserialize, Serialize}; @@ -73,8 +74,7 @@ pub struct ConnectionInfo { pub ip: Option, /// Time when that the session was last active. - #[serde(with = "ruma_serde::time::opt_ms_since_unix_epoch")] - pub last_seen: Option, + pub last_seen: Option, /// User agent string last seen in the session. pub user_agent: Option, diff --git a/crates/ruma-events-macros/src/event.rs b/crates/ruma-events-macros/src/event.rs index 1111ed35..df3d5ea8 100644 --- a/crates/ruma-events-macros/src/event.rs +++ b/crates/ruma-events-macros/src/event.rs @@ -63,7 +63,6 @@ fn expand_serialize_event( fields: &[Field], ruma_events: &TokenStream, ) -> TokenStream { - let js_int = quote! { #ruma_events::exports::js_int }; let serde = quote! { #ruma_events::exports::serde }; let ident = &input.ident; @@ -84,17 +83,6 @@ fn expand_serialize_event( state.serialize_field("prev_content", content)?; } } - } else if name == "origin_server_ts" { - quote! { - let time_since_epoch = - self.origin_server_ts.duration_since(::std::time::UNIX_EPOCH).unwrap(); - - let timestamp = <#js_int::UInt as ::std::convert::TryFrom<_>>::try_from( - time_since_epoch.as_millis(), - ).map_err(S::Error::custom)?; - - state.serialize_field("origin_server_ts", ×tamp)?; - } } else if name == "unsigned" { quote! { if !self.unsigned.is_empty() { @@ -136,7 +124,6 @@ fn expand_deserialize_event( fields: &[Field], ruma_events: &TokenStream, ) -> syn::Result { - let js_int = quote! { #ruma_events::exports::js_int }; let serde = quote! { #ruma_events::exports::serde }; let serde_json = quote! { #ruma_events::exports::serde_json }; @@ -171,8 +158,6 @@ fn expand_deserialize_event( } else { quote! { #content_type } } - } else if name == "origin_server_ts" { - quote! { #js_int::UInt } } else { quote! { #ty } } @@ -236,15 +221,6 @@ fn expand_deserialize_event( }; } } - } else if name == "origin_server_ts" { - quote! { - let origin_server_ts = origin_server_ts - .map(|time| { - let t = time.into(); - ::std::time::UNIX_EPOCH + ::std::time::Duration::from_millis(t) - }) - .ok_or_else(|| #serde::de::Error::missing_field("origin_server_ts"))?; - } } else if name == "unsigned" { quote! { let unsigned = unsigned.unwrap_or_default(); } } else { diff --git a/crates/ruma-events-macros/src/event_enum.rs b/crates/ruma-events-macros/src/event_enum.rs index f5d2e27f..8ad580de 100644 --- a/crates/ruma-events-macros/src/event_enum.rs +++ b/crates/ruma-events-macros/src/event_enum.rs @@ -937,10 +937,11 @@ fn field_return_type( var: &EventKindVariation, ruma_events: &TokenStream, ) -> TokenStream { + let ruma_common = quote! { #ruma_events::exports::ruma_common }; let ruma_identifiers = quote! { #ruma_events::exports::ruma_identifiers }; match name { - "origin_server_ts" => quote! { ::std::time::SystemTime }, + "origin_server_ts" => quote! { #ruma_common::MilliSecondsSinceUnixEpoch }, "room_id" => quote! { #ruma_identifiers::RoomId }, "event_id" => quote! { #ruma_identifiers::EventId }, "sender" => quote! { #ruma_identifiers::UserId }, diff --git a/crates/ruma-events/src/event_kinds.rs b/crates/ruma-events/src/event_kinds.rs index 8801c4f5..34cb890e 100644 --- a/crates/ruma-events/src/event_kinds.rs +++ b/crates/ruma-events/src/event_kinds.rs @@ -1,5 +1,4 @@ -use std::time::SystemTime; - +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events_macros::Event; use ruma_identifiers::{EventId, RoomId, UserId}; @@ -56,7 +55,7 @@ pub struct MessageEvent { pub sender: UserId, /// Timestamp in milliseconds on originating homeserver when this event was sent. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// The ID of the room associated with this event. pub room_id: RoomId, @@ -82,7 +81,7 @@ pub struct SyncMessageEvent { pub sender: UserId, /// Timestamp in milliseconds on originating homeserver when this event was sent. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// Additional key-value pairs not signed by the homeserver. pub unsigned: Unsigned, @@ -105,7 +104,7 @@ pub struct RedactedMessageEvent { pub sender: UserId, /// Timestamp in milliseconds on originating homeserver when this event was sent. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// The ID of the room associated with this event. pub room_id: RoomId, @@ -132,7 +131,7 @@ pub struct RedactedSyncMessageEvent { pub sender: UserId, /// Timestamp in milliseconds on originating homeserver when this event was sent. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// Additional key-value pairs not signed by the homeserver. pub unsigned: RedactedSyncUnsigned, @@ -155,7 +154,7 @@ pub struct StateEvent { pub sender: UserId, /// Timestamp in milliseconds on originating homeserver when this event was sent. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// The ID of the room associated with this event. pub room_id: RoomId, @@ -190,7 +189,7 @@ pub struct SyncStateEvent { pub sender: UserId, /// Timestamp in milliseconds on originating homeserver when this event was sent. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// A unique key which defines the overwriting semantics for this piece of room state. /// @@ -255,7 +254,7 @@ pub struct RedactedStateEvent { pub sender: UserId, /// Timestamp in milliseconds on originating homeserver when this event was sent. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// The ID of the room associated with this event. pub room_id: RoomId, @@ -288,7 +287,7 @@ pub struct RedactedSyncStateEvent { pub sender: UserId, /// Timestamp in milliseconds on originating homeserver when this event was sent. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// A unique key which defines the overwriting semantics for this piece of room state. /// diff --git a/crates/ruma-events/src/key/verification/request.rs b/crates/ruma-events/src/key/verification/request.rs index 2d375e8d..9f1659d1 100644 --- a/crates/ruma-events/src/key/verification/request.rs +++ b/crates/ruma-events/src/key/verification/request.rs @@ -1,7 +1,6 @@ //! Types for the *m.key.verification.request* event. -use std::time::SystemTime; - +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events_macros::EventContent; use ruma_identifiers::DeviceIdBox; use serde::{Deserialize, Serialize}; @@ -27,6 +26,5 @@ pub struct RequestToDeviceEventContent { /// /// If the request is in the future by more than 5 minutes or more than 10 minutes in /// the past, the message should be ignored by the receiver. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub timestamp: SystemTime, + pub timestamp: MilliSecondsSinceUnixEpoch, } diff --git a/crates/ruma-events/src/lib.rs b/crates/ruma-events/src/lib.rs index 21232b7b..93224cac 100644 --- a/crates/ruma-events/src/lib.rs +++ b/crates/ruma-events/src/lib.rs @@ -143,7 +143,7 @@ extern crate self as ruma_events; /// It is not considered part of ruma-events' public API. #[doc(hidden)] pub mod exports { - pub use js_int; + pub use ruma_common; pub use ruma_identifiers; pub use serde; pub use serde_json; diff --git a/crates/ruma-events/src/pdu.rs b/crates/ruma-events/src/pdu.rs index d24e7c91..9673f965 100644 --- a/crates/ruma-events/src/pdu.rs +++ b/crates/ruma-events/src/pdu.rs @@ -8,9 +8,10 @@ //! The stubbed versions of each PDU type remove the `event_id` field (if any) //! and the `room_id` field for use in PDU templates. -use std::{collections::BTreeMap, time::SystemTime}; +use std::collections::BTreeMap; use js_int::UInt; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::EventType; use ruma_identifiers::{EventId, RoomId, ServerNameBox, ServerSigningKeyId, UserId}; use serde::{Deserialize, Serialize}; @@ -46,8 +47,7 @@ pub struct RoomV1Pdu { /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver /// of when this event was created. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, // TODO: Encode event type as content enum variant, like event enums do /// The event's type. @@ -106,8 +106,7 @@ pub struct RoomV3Pdu { /// Timestamp (milliseconds since the UNIX epoch) on originating homeserver /// of when this event was created. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, // TODO: Encode event type as content enum variant, like event enums do /// The event's type. diff --git a/crates/ruma-events/src/policy/rule/room.rs b/crates/ruma-events/src/policy/rule/room.rs index 78863cb3..0f000ffa 100644 --- a/crates/ruma-events/src/policy/rule/room.rs +++ b/crates/ruma-events/src/policy/rule/room.rs @@ -15,8 +15,10 @@ pub struct RoomEventContent(pub PolicyRuleEventContent); #[cfg(test)] mod tests { - use std::time::{Duration, UNIX_EPOCH}; + use std::convert::TryInto; + use js_int::int; + use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::{event_id, room_id, user_id}; use ruma_serde::Raw; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -32,12 +34,12 @@ mod tests { let room_event = RoomEvent { event_id: event_id!("$143273582443PhrSn:example.org"), sender: user_id!("@example:example.org"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1_432_735_824_653), + origin_server_ts: MilliSecondsSinceUnixEpoch(1_432_735_824_653_u64.try_into().unwrap()), room_id: room_id!("!jEsUZKDJdhlrceRyVU:example.org"), state_key: "rule:#*:example.org".into(), prev_content: None, unsigned: Unsigned { - age: Some(1234.into()), + age: Some(int!(1234)), transaction_id: None, #[cfg(feature = "unstable-pre-spec")] relations: None, diff --git a/crates/ruma-events/src/receipt.rs b/crates/ruma-events/src/receipt.rs index 82faf80d..4bd77fa2 100644 --- a/crates/ruma-events/src/receipt.rs +++ b/crates/ruma-events/src/receipt.rs @@ -3,10 +3,9 @@ use std::{ collections::BTreeMap, ops::{Deref, DerefMut}, - time::SystemTime, }; -use ruma_common::receipt::ReceiptType; +use ruma_common::{receipt::ReceiptType, MilliSecondsSinceUnixEpoch}; use ruma_events_macros::EphemeralRoomEventContent; use ruma_identifiers::{EventId, UserId}; use serde::{Deserialize, Serialize}; @@ -50,10 +49,6 @@ pub type UserReceipts = BTreeMap; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Receipt { /// The time when the receipt was sent. - #[serde( - with = "ruma_serde::time::opt_ms_since_unix_epoch", - default, - skip_serializing_if = "Option::is_none" - )] - pub ts: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub ts: Option, } diff --git a/crates/ruma-events/src/relation.rs b/crates/ruma-events/src/relation.rs index f405da56..170bcfd2 100644 --- a/crates/ruma-events/src/relation.rs +++ b/crates/ruma-events/src/relation.rs @@ -1,8 +1,9 @@ //! Types describing event relations after MSC 2674, 2675, 2676, 2677. -use std::{fmt::Debug, time::SystemTime}; +use std::fmt::Debug; use js_int::UInt; +use ruma_common::MilliSecondsSinceUnixEpoch; use serde::{Deserialize, Serialize}; /// Summary of all reactions with the given key to an event. @@ -13,12 +14,8 @@ pub struct BundledReaction { pub key: String, /// Time of the bundled reaction being compiled on the server. - #[serde( - with = "ruma_serde::time::opt_ms_since_unix_epoch", - default, - skip_serializing_if = "Option::is_none" - )] - pub origin_server_ts: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub origin_server_ts: Option, /// Number of reactions. pub count: UInt, @@ -26,7 +23,11 @@ pub struct BundledReaction { impl BundledReaction { /// Creates a new `BundledReaction`. - pub fn new(key: String, origin_server_ts: Option, count: UInt) -> Self { + pub fn new( + key: String, + origin_server_ts: Option, + count: UInt, + ) -> Self { Self { key, origin_server_ts, count } } } diff --git a/crates/ruma-events/src/room/canonical_alias.rs b/crates/ruma-events/src/room/canonical_alias.rs index 0e54af6e..fc6e84cb 100644 --- a/crates/ruma-events/src/room/canonical_alias.rs +++ b/crates/ruma-events/src/room/canonical_alias.rs @@ -39,8 +39,8 @@ impl CanonicalAliasEventContent { #[cfg(test)] mod tests { - use std::time::{Duration, UNIX_EPOCH}; - + use js_int::uint; + use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::{event_id, room_alias_id, room_id, user_id}; use ruma_serde::Raw; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -56,7 +56,7 @@ mod tests { alt_aliases: Vec::new(), }, event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), prev_content: None, room_id: room_id!("!dummy:example.com"), sender: user_id!("@carl:example.com"), diff --git a/crates/ruma-events/src/room/member.rs b/crates/ruma-events/src/room/member.rs index 213123df..2145a0c1 100644 --- a/crates/ruma-events/src/room/member.rs +++ b/crates/ruma-events/src/room/member.rs @@ -254,10 +254,10 @@ impl StrippedStateEvent { #[cfg(test)] mod tests { - use std::time::{Duration, UNIX_EPOCH}; - + use js_int::uint; use maplit::btreemap; use matches::assert_matches; + use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::{server_name, server_signing_key_id}; use ruma_serde::Raw; use serde_json::{from_value as from_json_value, json}; @@ -300,7 +300,7 @@ mod tests { unsigned, prev_content: None, } if event_id == "$h29iv0s8:example.com" - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && room_id == "!n8f893n9:example.com" && sender == "@carl:example.com" && state_key == "example.com" @@ -352,7 +352,7 @@ mod tests { third_party_invite: None, }), } if event_id == "$h29iv0s8:example.com" - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && room_id == "!n8f893n9:example.com" && sender == "@carl:example.com" && state_key == "example.com" @@ -423,7 +423,7 @@ mod tests { } && token == "abc123" && event_id == "$143273582443PhrSn:example.org" - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(233) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(233)) && room_id == "!jEsUZKDJdhlrceRyVU:example.org" && sender == "@alice:example.org" && state_key == "@alice:example.org" @@ -493,7 +493,7 @@ mod tests { }), }), } if event_id == "$143273582443PhrSn:example.org" - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(233) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(233)) && room_id == "!jEsUZKDJdhlrceRyVU:example.org" && sender == "@alice:example.org" && state_key == "@alice:example.org" @@ -569,7 +569,7 @@ mod tests { }), }), } if event_id == "$143273582443PhrSn:example.org" - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(233) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(233)) && room_id == "!jEsUZKDJdhlrceRyVU:example.org" && sender == "@alice:example.org" && state_key == "@alice:example.org" diff --git a/crates/ruma-events/src/room/name.rs b/crates/ruma-events/src/room/name.rs index c73f3ec7..2eb1c15e 100644 --- a/crates/ruma-events/src/room/name.rs +++ b/crates/ruma-events/src/room/name.rs @@ -56,10 +56,9 @@ where #[cfg(test)] mod tests { - use std::time::{Duration, UNIX_EPOCH}; - - use js_int::int; + use js_int::{int, uint}; use matches::assert_matches; + use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::{event_id, room_id, user_id}; use ruma_serde::Raw; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -72,7 +71,7 @@ mod tests { let name_event = StateEvent { content: NameEventContent { name: Some("The room name".into()) }, event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), prev_content: None, room_id: room_id!("!n8f893n9:example.com"), sender: user_id!("@carl:example.com"), @@ -101,7 +100,7 @@ mod tests { let name_event = StateEvent { content: NameEventContent { name: Some("The room name".into()) }, event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), prev_content: Some(NameEventContent { name: Some("The old name".into()) }), room_id: room_id!("!n8f893n9:example.com"), sender: user_id!("@carl:example.com"), diff --git a/crates/ruma-events/src/room/pinned_events.rs b/crates/ruma-events/src/room/pinned_events.rs index cbb32e54..42160461 100644 --- a/crates/ruma-events/src/room/pinned_events.rs +++ b/crates/ruma-events/src/room/pinned_events.rs @@ -27,11 +27,9 @@ impl PinnedEventsEventContent { #[cfg(test)] mod tests { - use std::{ - convert::TryFrom, - time::{Duration, UNIX_EPOCH}, - }; + use std::convert::{TryFrom, TryInto}; + use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::{EventId, RoomId, ServerName, UserId}; use ruma_serde::Raw; @@ -49,7 +47,7 @@ mod tests { let event = StateEvent { content: content.clone(), event_id: EventId::new(server_name), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1_432_804_485_886_u64), + origin_server_ts: MilliSecondsSinceUnixEpoch(1_432_804_485_886_u64.try_into().unwrap()), prev_content: None, room_id: RoomId::new(server_name), sender: UserId::new(server_name), diff --git a/crates/ruma-events/src/room/power_levels.rs b/crates/ruma-events/src/room/power_levels.rs index edd93c19..8d501878 100644 --- a/crates/ruma-events/src/room/power_levels.rs +++ b/crates/ruma-events/src/room/power_levels.rs @@ -143,14 +143,12 @@ fn is_default_power_level(l: &Int) -> bool { #[cfg(test)] mod tests { - use std::{ - collections::BTreeMap, - time::{Duration, UNIX_EPOCH}, - }; + use std::collections::BTreeMap; use assign::assign; - use js_int::int; + use js_int::{int, uint}; use maplit::btreemap; + use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::{event_id, room_id, user_id}; use serde_json::{json, to_value as to_json_value}; @@ -175,7 +173,7 @@ mod tests { notifications: NotificationPowerLevels::default(), }, event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), prev_content: None, room_id: room_id!("!n8f893n9:example.com"), unsigned: Unsigned::default(), @@ -218,7 +216,7 @@ mod tests { notifications: assign!(NotificationPowerLevels::new(), { room: int!(23) }), }, event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), prev_content: Some(PowerLevelsEventContent { // Make just one field different so we at least know they're two different objects. ban: int!(42), diff --git a/crates/ruma-events/src/room/redaction.rs b/crates/ruma-events/src/room/redaction.rs index 92aad5cf..45485e9d 100644 --- a/crates/ruma-events/src/room/redaction.rs +++ b/crates/ruma-events/src/room/redaction.rs @@ -1,7 +1,6 @@ //! Types for the *m.room.redaction* event. -use std::time::SystemTime; - +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events_macros::{Event, EventContent}; use ruma_identifiers::{EventId, RoomId, UserId}; use serde::{Deserialize, Serialize}; @@ -27,7 +26,7 @@ pub struct RedactionEvent { pub sender: UserId, /// Timestamp in milliseconds on originating homeserver when this event was sent. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// The ID of the room associated with this event. pub room_id: RoomId, @@ -52,7 +51,7 @@ pub struct SyncRedactionEvent { pub sender: UserId, /// Timestamp in milliseconds on originating homeserver when this event was sent. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// Additional key-value pairs not signed by the homeserver. pub unsigned: Unsigned, diff --git a/crates/ruma-events/tests/custom.rs b/crates/ruma-events/tests/custom.rs index d8f04adf..714bed62 100644 --- a/crates/ruma-events/tests/custom.rs +++ b/crates/ruma-events/tests/custom.rs @@ -1,7 +1,7 @@ -use std::time::{Duration, UNIX_EPOCH}; - +use js_int::uint; use maplit::btreemap; use matches::assert_matches; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ custom::CustomEventContent, AnyMessageEvent, AnyStateEvent, AnyStateEventContent, AnySyncMessageEvent, AnySyncRoomEvent, MessageEvent, StateEvent, SyncMessageEvent, @@ -53,7 +53,7 @@ fn serialize_custom_message_event() { event_type: "m.room.message".into(), }, event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(10), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(10)), room_id: room_id!("!room:room.com"), sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), @@ -93,7 +93,7 @@ fn serialize_custom_state_event() { event_type: "m.made.up".into(), }, event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(10), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(10)), prev_content: None, room_id: room_id!("!roomid:room.com"), sender: user_id!("@carl:example.com"), @@ -147,7 +147,7 @@ fn deserialize_custom_state_event() { unsigned, }) if data == expected_content && event_type == "m.reaction" && event_id == event_id!("$h29iv0s8:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(10) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(10)) && sender == user_id!("@carl:example.com") && room_id == room_id!("!room:room.com") && state_key.is_empty() @@ -183,7 +183,7 @@ fn deserialize_custom_state_sync_event() { unsigned, } if data == expected_content && event_type == "m.reaction" && event_id == event_id!("$h29iv0s8:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(10) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(10)) && sender == user_id!("@carl:example.com") && state_key.is_empty() && !unsigned.is_empty() @@ -221,7 +221,7 @@ fn deserialize_custom_message_sync_event() { unsigned, })) if data == expected_content && event_type == "m.ruma_custom" && event_id == event_id!("$h29iv0s8:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(10) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(10)) && sender == user_id!("@carl:example.com") && !unsigned.is_empty() ); diff --git a/crates/ruma-events/tests/enums.rs b/crates/ruma-events/tests/enums.rs index a3d42fb7..494ccb03 100644 --- a/crates/ruma-events/tests/enums.rs +++ b/crates/ruma-events/tests/enums.rs @@ -1,6 +1,6 @@ -use std::time::UNIX_EPOCH; - +use js_int::uint; use matches::assert_matches; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::{event_id, room_alias_id, room_id, user_id}; use serde_json::{from_value as from_json_value, json, Value as JsonValue}; @@ -201,7 +201,7 @@ fn message_event_serialization() { let event = MessageEvent { content: MessageEventContent::text_plain("test"), event_id: event_id!("$1234:example.com"), - origin_server_ts: UNIX_EPOCH, + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(0)), room_id: room_id!("!roomid:example.com"), sender: user_id!("@test:example.com"), unsigned: Unsigned::default(), diff --git a/crates/ruma-events/tests/ephemeral_event.rs b/crates/ruma-events/tests/ephemeral_event.rs index ff2e9235..7961cdf7 100644 --- a/crates/ruma-events/tests/ephemeral_event.rs +++ b/crates/ruma-events/tests/ephemeral_event.rs @@ -1,8 +1,7 @@ -use std::time::{Duration, UNIX_EPOCH}; - +use js_int::uint; use maplit::btreemap; use matches::assert_matches; -use ruma_common::receipt::ReceiptType; +use ruma_common::{receipt::ReceiptType, MilliSecondsSinceUnixEpoch}; use ruma_identifiers::{event_id, room_id, user_id}; use ruma_serde::Raw; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -66,7 +65,7 @@ fn ephemeral_serialize_receipt() { content: AnyEphemeralRoomEventContent::Receipt(ReceiptEventContent(btreemap! { event_id => btreemap! { ReceiptType::Read => btreemap! { - user_id => Receipt { ts: Some(UNIX_EPOCH + Duration::from_millis(1)) }, + user_id => Receipt { ts: Some(MilliSecondsSinceUnixEpoch(uint!(1))) }, }, }, })), @@ -121,6 +120,6 @@ fn deserialize_ephemeral_receipt() { .map(|r| r.get(&ReceiptType::Read).unwrap().get(&user_id).unwrap()) .map(|r| r.ts) .unwrap() - == Some(UNIX_EPOCH + Duration::from_millis(1)) + == Some(MilliSecondsSinceUnixEpoch(uint!(1))) ); } diff --git a/crates/ruma-events/tests/event_enums.rs b/crates/ruma-events/tests/event_enums.rs index b4f6fa12..0c434421 100644 --- a/crates/ruma-events/tests/event_enums.rs +++ b/crates/ruma-events/tests/event_enums.rs @@ -1,8 +1,7 @@ -use std::time::{Duration, UNIX_EPOCH}; - use assign::assign; -use js_int::UInt; +use js_int::{uint, UInt}; use matches::assert_matches; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::{event_id, mxc_uri, room_id, user_id}; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -60,7 +59,7 @@ fn deserialize_message_event() { unsigned, }) if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap() && event_id == event_id!("$h29iv0s8:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && room_id == room_id!("!roomid:room.com") && sender == user_id!("@carl:example.com") && unsigned.is_empty() @@ -88,7 +87,7 @@ fn serialize_message_event() { mxc_uri!("mxc://matrix.org/arsrns98rsRSR"), ), event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), room_id: room_id!("!roomid:room.com"), sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), diff --git a/crates/ruma-events/tests/message_event.rs b/crates/ruma-events/tests/message_event.rs index 3e581bcd..1dc35940 100644 --- a/crates/ruma-events/tests/message_event.rs +++ b/crates/ruma-events/tests/message_event.rs @@ -1,8 +1,7 @@ -use std::time::{Duration, UNIX_EPOCH}; - use assign::assign; use js_int::{uint, UInt}; use matches::assert_matches; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ call::{answer::AnswerEventContent, SessionDescription, SessionDescriptionType}, room::{ImageInfo, ThumbnailInfo}, @@ -34,7 +33,7 @@ fn message_serialize_sticker() { mxc_uri!("mxc://matrix.org/rnsldl8srs98IRrs"), )), event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), room_id: room_id!("!roomid:room.com"), sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), @@ -139,7 +138,7 @@ fn deserialize_message_call_answer() { unsigned, } if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap() && event_id == event_id!("$h29iv0s8:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && room_id == room_id!("!roomid:room.com") && sender == user_id!("@carl:example.com") && unsigned.is_empty() @@ -203,7 +202,7 @@ fn deserialize_message_sticker() { unsigned } if event_id == event_id!("$h29iv0s8:example.com") && body == "Hello" - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && room_id == room_id!("!roomid:room.com") && sender == user_id!("@carl:example.com") && height == UInt::new(423) @@ -279,7 +278,7 @@ fn deserialize_message_then_convert_to_full() { && call_id == "foofoo" && version == uint!(1) && event_id == "$h29iv0s8:example.com" - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && room_id == "!roomid:room.com" && sender == "@carl:example.com" && unsigned.is_empty() diff --git a/crates/ruma-events/tests/pdu.rs b/crates/ruma-events/tests/pdu.rs index 0caac3f3..9ed57b3f 100644 --- a/crates/ruma-events/tests/pdu.rs +++ b/crates/ruma-events/tests/pdu.rs @@ -1,10 +1,8 @@ #![cfg(not(feature = "unstable-pre-spec"))] -use std::{ - collections::BTreeMap, - time::{Duration, SystemTime}, -}; +use std::{collections::BTreeMap, convert::TryInto}; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ pdu::{EventHash, Pdu, RoomV1Pdu, RoomV3Pdu}, EventType, @@ -30,7 +28,7 @@ fn serialize_pdu_as_v1() { event_id: event_id!("$somejoinevent:matrix.org"), sender: user_id!("@sender:example.com"), origin: "matrix.org".into(), - origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), + origin_server_ts: MilliSecondsSinceUnixEpoch(1_592_050_773_658_u64.try_into().unwrap()), kind: EventType::RoomPowerLevels, content: json!({"testing": 123}), state_key: Some("state".into()), @@ -96,7 +94,7 @@ fn serialize_pdu_as_v3() { room_id: room_id!("!n8f893n9:example.com"), sender: user_id!("@sender:example.com"), origin: "matrix.org".into(), - origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658), + origin_server_ts: MilliSecondsSinceUnixEpoch(1_592_050_773_658_u64.try_into().unwrap()), kind: EventType::RoomPowerLevels, content: json!({"testing": 123}), state_key: Some("state".into()), diff --git a/crates/ruma-events/tests/redacted.rs b/crates/ruma-events/tests/redacted.rs index 33fbd590..5931e07e 100644 --- a/crates/ruma-events/tests/redacted.rs +++ b/crates/ruma-events/tests/redacted.rs @@ -1,6 +1,6 @@ -use std::time::{Duration, UNIX_EPOCH}; - +use js_int::uint; use matches::assert_matches; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ custom::RedactedCustomEventContent, room::{ @@ -27,7 +27,7 @@ fn sync_unsigned() -> RedactedSyncUnsigned { content: RedactionEventContent { reason: Some("redacted because".into()) }, redacts: event_id!("$h29iv0s8:example.com"), event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), })); @@ -42,7 +42,7 @@ fn full_unsigned() -> RedactedUnsigned { room_id: room_id!("!roomid:room.com"), redacts: event_id!("$h29iv0s8:example.com"), event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), })); @@ -55,7 +55,7 @@ fn redacted_message_event_serialize() { let redacted = RedactedSyncMessageEvent { content: RedactedMessageEventContent, event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), sender: user_id!("@carl:example.com"), unsigned: RedactedSyncUnsigned::default(), }; @@ -77,7 +77,7 @@ fn redacted_aliases_event_serialize_no_content() { content: RedactedAliasesEventContent { aliases: None }, event_id: event_id!("$h29iv0s8:example.com"), state_key: "".into(), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), sender: user_id!("@carl:example.com"), unsigned: RedactedSyncUnsigned::default(), }; @@ -100,7 +100,7 @@ fn redacted_aliases_event_serialize_with_content() { content: RedactedAliasesEventContent { aliases: Some(vec![]) }, event_id: event_id!("$h29iv0s8:example.com"), state_key: "".to_owned(), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), sender: user_id!("@carl:example.com"), unsigned: RedactedSyncUnsigned::default(), }; @@ -189,7 +189,7 @@ fn redacted_deserialize_any_room_sync() { content: RedactionEventContent { reason: Some("redacted because".into()) }, redacts: event_id!("$h29iv0s8:example.com"), event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), room_id: room_id!("!roomid:room.com"), sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), @@ -301,7 +301,7 @@ fn redacted_custom_event_deserialize() { event_id: event_id!("$h29iv0s8:example.com"), sender: user_id!("@carl:example.com"), state_key: "hello there".into(), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), unsigned: unsigned.clone(), }; @@ -337,7 +337,7 @@ fn redact_method_properly_redacts() { content: RedactionEventContent { reason: Some("redacted because".into()) }, redacts: event_id!("$143273582443PhrSn:example.com"), event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), room_id: room_id!("!roomid:room.com"), sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), @@ -358,6 +358,6 @@ fn redact_method_properly_redacts() { && unsigned.redacted_because.is_some() && room_id == room_id!("!roomid:room.com") && sender == user_id!("@user:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) ); } diff --git a/crates/ruma-events/tests/redaction.rs b/crates/ruma-events/tests/redaction.rs index bd3ac415..9ad13aa0 100644 --- a/crates/ruma-events/tests/redaction.rs +++ b/crates/ruma-events/tests/redaction.rs @@ -1,6 +1,6 @@ -use std::time::{Duration, UNIX_EPOCH}; - +use js_int::uint; use matches::assert_matches; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ room::redaction::{RedactionEvent, RedactionEventContent}, AnyMessageEvent, Unsigned, @@ -31,7 +31,7 @@ fn serialize_redaction() { content: RedactionEventContent { reason: Some("being a turd".into()) }, redacts: event_id!("$nomore:example.com"), event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), room_id: room_id!("!roomid:room.com"), sender: user_id!("@carl:example.com"), unsigned: Unsigned::default(), @@ -63,7 +63,7 @@ fn deserialize_redaction() { }) if reas == "being a turd" && event_id == event_id!("$h29iv0s8:example.com") && redacts == event_id!("$nomore:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && room_id == room_id!("!roomid:room.com") && sender == user_id!("@carl:example.com") && unsigned.is_empty() diff --git a/crates/ruma-events/tests/room_message.rs b/crates/ruma-events/tests/room_message.rs index 38f43f74..b6e716e3 100644 --- a/crates/ruma-events/tests/room_message.rs +++ b/crates/ruma-events/tests/room_message.rs @@ -1,7 +1,7 @@ -use std::time::{Duration, UNIX_EPOCH}; - use assign::assign; +use js_int::uint; use matches::assert_matches; +use ruma_common::MilliSecondsSinceUnixEpoch; #[cfg(feature = "unstable-pre-spec")] use ruma_events::{ key::verification::VerificationMethod, room::message::KeyVerificationRequestEventContent, @@ -44,7 +44,7 @@ fn serialization() { file: None, })), event_id: event_id!("$143273582443PhrSn:example.org"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(10_000), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(10_000)), room_id: room_id!("!testroomid:example.org"), sender: user_id!("@user:example.org"), unsigned: Unsigned::default(), diff --git a/crates/ruma-events/tests/state_event.rs b/crates/ruma-events/tests/state_event.rs index 68f42900..bf2387ec 100644 --- a/crates/ruma-events/tests/state_event.rs +++ b/crates/ruma-events/tests/state_event.rs @@ -1,7 +1,6 @@ -use std::time::{Duration, UNIX_EPOCH}; - -use js_int::UInt; +use js_int::{uint, UInt}; use matches::assert_matches; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ room::{ aliases::AliasesEventContent, @@ -41,7 +40,7 @@ fn serialize_aliases_with_prev_content() { "#somewhere:localhost" )])), event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), prev_content: Some(AnyStateEventContent::RoomAliases(AliasesEventContent::new(vec![ room_alias_id!("#inner:localhost"), ]))), @@ -64,7 +63,7 @@ fn serialize_aliases_without_prev_content() { "#somewhere:localhost" )])), event_id: event_id!("$h29iv0s8:example.com"), - origin_server_ts: UNIX_EPOCH + Duration::from_millis(1), + origin_server_ts: MilliSecondsSinceUnixEpoch(uint!(1)), prev_content: None, room_id: room_id!("!roomid:room.com"), sender: user_id!("@carl:example.com"), @@ -124,7 +123,7 @@ fn deserialize_aliases_with_prev_content() { unsigned, } if content.aliases == vec![room_alias_id!("#somewhere:localhost")] && event_id == event_id!("$h29iv0s8:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && prev_content.aliases == vec![room_alias_id!("#inner:localhost")] && room_id == room_id!("!roomid:room.com") && sender == user_id!("@carl:example.com") @@ -151,7 +150,7 @@ fn deserialize_aliases_sync_with_room_id() { unsigned, } if content.aliases == vec![room_alias_id!("#somewhere:localhost")] && event_id == event_id!("$h29iv0s8:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && prev_content.aliases == vec![room_alias_id!("#inner:localhost")] && sender == user_id!("@carl:example.com") && state_key.is_empty() @@ -210,7 +209,7 @@ fn deserialize_avatar_without_prev_content() { state_key, unsigned } if event_id == event_id!("$h29iv0s8:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && room_id == room_id!("!roomid:room.com") && sender == user_id!("@carl:example.com") && state_key.is_empty() @@ -287,7 +286,7 @@ fn deserialize_member_event_with_top_level_membership_field() { .. } )) if event_id == event_id!("$h29iv0s8:example.com") - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && sender == user_id!("@example:localhost") && content.displayname == Some("example".into()) ); @@ -318,7 +317,7 @@ fn deserialize_full_event_convert_to_sync() { unsigned, }) if content.aliases == vec![room_alias_id!("#somewhere:localhost")] && event_id == "$h29iv0s8:example.com" - && origin_server_ts == UNIX_EPOCH + Duration::from_millis(1) + && origin_server_ts == MilliSecondsSinceUnixEpoch(uint!(1)) && prev_content.aliases == vec![room_alias_id!("#inner:localhost")] && sender == "@carl:example.com" && state_key.is_empty() diff --git a/crates/ruma-federation-api/src/backfill/get_backfill/v1.rs b/crates/ruma-federation-api/src/backfill/get_backfill/v1.rs index 51f5306a..b3ac682e 100644 --- a/crates/ruma-federation-api/src/backfill/get_backfill/v1.rs +++ b/crates/ruma-federation-api/src/backfill/get_backfill/v1.rs @@ -1,9 +1,8 @@ //! [GET /_matrix/federation/v1/backfill/{roomId}](https://matrix.org/docs/spec/server_server/r0.1.4#get-matrix-federation-v1-backfill-roomid) -use std::time::SystemTime; - use js_int::UInt; use ruma_api::ruma_api; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::pdu::Pdu; use ruma_identifiers::{EventId, RoomId, ServerNameBox}; use ruma_serde::Raw; @@ -37,8 +36,7 @@ ruma_api! { pub origin: ServerNameBox, /// POSIX timestamp in milliseconds on originating homeserver when this transaction started. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// List of persistent updates to rooms. pub pdus: Vec>, @@ -60,7 +58,11 @@ impl Response { /// * the `server_name` of the homeserver. /// * the timestamp in milliseconds of when this transaction started. /// * the list of persistent updates to rooms. - pub fn new(origin: ServerNameBox, origin_server_ts: SystemTime, pdus: Vec>) -> Self { + pub fn new( + origin: ServerNameBox, + origin_server_ts: MilliSecondsSinceUnixEpoch, + pdus: Vec>, + ) -> Self { Self { origin, origin_server_ts, pdus } } } diff --git a/crates/ruma-federation-api/src/discovery.rs b/crates/ruma-federation-api/src/discovery.rs index 40b36f42..f8929361 100644 --- a/crates/ruma-federation-api/src/discovery.rs +++ b/crates/ruma-federation-api/src/discovery.rs @@ -1,7 +1,8 @@ //! Server discovery endpoints. -use std::{collections::BTreeMap, time::SystemTime}; +use std::collections::BTreeMap; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::{ServerNameBox, ServerSigningKeyId}; use serde::{Deserialize, Serialize}; @@ -31,8 +32,7 @@ impl VerifyKey { #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] pub struct OldVerifyKey { /// Timestamp when this key expired. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub expired_ts: SystemTime, + pub expired_ts: MilliSecondsSinceUnixEpoch, /// The Unpadded Base64 encoded key. pub key: String, @@ -40,7 +40,7 @@ pub struct OldVerifyKey { impl OldVerifyKey { /// Creates a new `OldVerifyKey` with the given expiry time and key. - pub fn new(expired_ts: SystemTime, key: String) -> Self { + pub fn new(expired_ts: MilliSecondsSinceUnixEpoch, key: String) -> Self { Self { expired_ts, key } } } @@ -65,15 +65,14 @@ pub struct ServerSigningKeys { /// Timestamp when the keys should be refreshed. This field MUST be ignored in room /// versions 1, 2, 3, and 4. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub valid_until_ts: SystemTime, + pub valid_until_ts: MilliSecondsSinceUnixEpoch, } impl ServerSigningKeys { /// Creates a new `ServerSigningKeys` with the given server name and validity timestamp. /// /// All other fields will be empty. - pub fn new(server_name: ServerNameBox, valid_until_ts: SystemTime) -> Self { + pub fn new(server_name: ServerNameBox, valid_until_ts: MilliSecondsSinceUnixEpoch) -> Self { Self { server_name, verify_keys: BTreeMap::new(), diff --git a/crates/ruma-federation-api/src/discovery/get_remote_server_keys/v2.rs b/crates/ruma-federation-api/src/discovery/get_remote_server_keys/v2.rs index 13e3188a..27405762 100644 --- a/crates/ruma-federation-api/src/discovery/get_remote_server_keys/v2.rs +++ b/crates/ruma-federation-api/src/discovery/get_remote_server_keys/v2.rs @@ -1,8 +1,7 @@ //! [GET /_matrix/key/v2/query/{serverName}/{keyId}](https://matrix.org/docs/spec/server_server/r0.1.4#get-matrix-key-v2-query-servername-keyid) -use std::time::SystemTime; - use ruma_api::ruma_api; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::ServerName; use crate::discovery::ServerSigningKeys; @@ -29,8 +28,8 @@ ruma_api! { /// /// If not supplied, the current time as determined by the receiving server is used. #[ruma_api(query)] - #[serde(default = "SystemTime::now", with = "ruma_serde::time::ms_since_unix_epoch")] - pub minimum_valid_until_ts: SystemTime, + #[serde(default = "MilliSecondsSinceUnixEpoch::now")] + pub minimum_valid_until_ts: MilliSecondsSinceUnixEpoch, } response: { @@ -41,7 +40,10 @@ ruma_api! { impl<'a> Request<'a> { /// Creates a new `Request` with the given server name and `minimum_valid_until` timestamp. - pub fn new(server_name: &'a ServerName, minimum_valid_until_ts: SystemTime) -> Self { + pub fn new( + server_name: &'a ServerName, + minimum_valid_until_ts: MilliSecondsSinceUnixEpoch, + ) -> Self { Self { server_name, minimum_valid_until_ts } } } diff --git a/crates/ruma-federation-api/src/discovery/get_remote_server_keys_batch/v2.rs b/crates/ruma-federation-api/src/discovery/get_remote_server_keys_batch/v2.rs index 67b2ede8..6111501f 100644 --- a/crates/ruma-federation-api/src/discovery/get_remote_server_keys_batch/v2.rs +++ b/crates/ruma-federation-api/src/discovery/get_remote_server_keys_batch/v2.rs @@ -1,8 +1,9 @@ //! [POST /_matrix/key/v2/query](https://matrix.org/docs/spec/server_server/r0.1.4#post-matrix-key-v2-query) -use std::{collections::BTreeMap, time::SystemTime}; +use std::collections::BTreeMap; use ruma_api::ruma_api; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_identifiers::{ServerNameBox, ServerSigningKeyId}; use serde::{Deserialize, Serialize}; @@ -38,8 +39,7 @@ ruma_api! { /// If not supplied, the current time as determined by the notary server /// is used. #[ruma_api(query)] - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub minimum_valid_until_ts: SystemTime, + pub minimum_valid_until_ts: MilliSecondsSinceUnixEpoch, } response: { @@ -52,7 +52,7 @@ impl Request { /// Creates a new `Request` with the given query criteria and `minimum_valid_until` timestamp. pub fn new( server_keys: BTreeMap>, - minimum_valid_until_ts: SystemTime, + minimum_valid_until_ts: MilliSecondsSinceUnixEpoch, ) -> Self { Self { server_keys, minimum_valid_until_ts } } @@ -75,12 +75,8 @@ pub struct QueryCriteria { /// /// If not supplied, the current time as determined by the notary server is /// used. - #[serde( - default, - skip_serializing_if = "Option::is_none", - with = "ruma_serde::time::opt_ms_since_unix_epoch" - )] - pub minimum_valid_until_ts: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub minimum_valid_until_ts: Option, } impl QueryCriteria { diff --git a/crates/ruma-federation-api/src/event/get_event/v1.rs b/crates/ruma-federation-api/src/event/get_event/v1.rs index ab4a7e32..c505e57d 100644 --- a/crates/ruma-federation-api/src/event/get_event/v1.rs +++ b/crates/ruma-federation-api/src/event/get_event/v1.rs @@ -1,8 +1,7 @@ //! [GET /_matrix/federation/v1/event/{eventId}](https://matrix.org/docs/spec/server_server/r0.1.4#get-matrix-federation-v1-event-eventid) -use std::time::SystemTime; - use ruma_api::ruma_api; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::pdu::Pdu; use ruma_identifiers::{EventId, ServerNameBox}; use ruma_serde::Raw; @@ -28,8 +27,7 @@ ruma_api! { pub origin: ServerNameBox, /// Time on originating homeserver when this transaction started. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// The event. #[serde(rename = "pdus", with = "ruma_serde::single_element_seq")] @@ -46,7 +44,11 @@ impl<'a> Request<'a> { impl Response { /// Creates a new `Response` with the given server name, timestamp, and event. - pub fn new(origin: ServerNameBox, origin_server_ts: SystemTime, pdu: Raw) -> Self { + pub fn new( + origin: ServerNameBox, + origin_server_ts: MilliSecondsSinceUnixEpoch, + pdu: Raw, + ) -> Self { Self { origin, origin_server_ts, pdu } } } diff --git a/crates/ruma-federation-api/src/membership/create_invite/v1.rs b/crates/ruma-federation-api/src/membership/create_invite/v1.rs index 7a677040..9b7900aa 100644 --- a/crates/ruma-federation-api/src/membership/create_invite/v1.rs +++ b/crates/ruma-federation-api/src/membership/create_invite/v1.rs @@ -1,8 +1,7 @@ //! [PUT /_matrix/federation/v1/invite/{roomId}/{eventId}](https://matrix.org/docs/spec/server_server/r0.1.4#put-matrix-federation-v1-invite-roomid-eventid) -use std::time::SystemTime; - use ruma_api::ruma_api; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{pdu::Pdu, room::member::MemberEventContent, AnyStrippedStateEvent, EventType}; use ruma_identifiers::{EventId, RoomId, ServerName, UserId}; use ruma_serde::Raw; @@ -34,8 +33,7 @@ ruma_api! { pub origin: &'a ServerName, /// A timestamp added by the inviting homeserver. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// The value `m.room.member`. #[serde(rename = "type")] @@ -98,7 +96,7 @@ pub struct RequestInit<'a> { pub origin: &'a ServerName, /// A timestamp added by the inviting homeserver. - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// The user ID of the invited member. pub state_key: &'a UserId, diff --git a/crates/ruma-federation-api/src/membership/create_leave_event/v1.rs b/crates/ruma-federation-api/src/membership/create_leave_event/v1.rs index dc6c1f95..4ea56608 100644 --- a/crates/ruma-federation-api/src/membership/create_leave_event/v1.rs +++ b/crates/ruma-federation-api/src/membership/create_leave_event/v1.rs @@ -1,9 +1,8 @@ //! [PUT /_matrix/federation/v1/send_leave/{roomId}/{eventId}](https://matrix.org/docs/spec/server_server/r0.1.4#put-matrix-federation-v1-send-leave-roomid-eventid) -use std::time::SystemTime; - use js_int::UInt; use ruma_api::ruma_api; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{room::member::MemberEventContent, EventType}; use ruma_identifiers::{EventId, RoomId, ServerName, UserId}; use ruma_serde::Raw; @@ -38,8 +37,7 @@ ruma_api! { /// A timestamp added by the leaving homeserver. #[ruma_api(query)] - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// The value `m.room.member`. #[ruma_api(query)] @@ -87,7 +85,7 @@ impl<'a> Request<'a> { event_id: &'a EventId, sender: &'a UserId, origin: &'a ServerName, - origin_server_ts: SystemTime, + origin_server_ts: MilliSecondsSinceUnixEpoch, event_type: EventType, state_key: &'a str, content: Raw, diff --git a/crates/ruma-federation-api/src/transactions/send_transaction_message/v1.rs b/crates/ruma-federation-api/src/transactions/send_transaction_message/v1.rs index fdd95efa..bc8b1405 100644 --- a/crates/ruma-federation-api/src/transactions/send_transaction_message/v1.rs +++ b/crates/ruma-federation-api/src/transactions/send_transaction_message/v1.rs @@ -1,8 +1,9 @@ //! [PUT /_matrix/federation/v1/send/{txnId}](https://matrix.org/docs/spec/server_server/r0.1.3#put-matrix-federation-v1-send-txnid) -use std::{collections::BTreeMap, time::SystemTime}; +use std::collections::BTreeMap; use ruma_api::ruma_api; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::pdu::Pdu; use ruma_identifiers::{EventId, ServerName}; use ruma_serde::Raw; @@ -29,8 +30,7 @@ ruma_api! { /// POSIX timestamp in milliseconds on the originating homeserver when this transaction /// started. - #[serde(with = "ruma_serde::time::ms_since_unix_epoch")] - pub origin_server_ts: SystemTime, + pub origin_server_ts: MilliSecondsSinceUnixEpoch, /// List of persistent updates to rooms. /// @@ -60,7 +60,7 @@ impl<'a> Request<'a> { pub fn new( transaction_id: &'a str, origin: &'a ServerName, - origin_server_ts: SystemTime, + origin_server_ts: MilliSecondsSinceUnixEpoch, ) -> Self { Self { transaction_id, origin, origin_server_ts, pdus: &[], edus: &[] } } diff --git a/crates/ruma-push-gateway-api/src/send_event_notification/v1.rs b/crates/ruma-push-gateway-api/src/send_event_notification/v1.rs index 608931e9..ae1eba32 100644 --- a/crates/ruma-push-gateway-api/src/send_event_notification/v1.rs +++ b/crates/ruma-push-gateway-api/src/send_event_notification/v1.rs @@ -1,10 +1,11 @@ //! [POST /_matrix/push/v1/notify](https://matrix.org/docs/spec/push_gateway/r0.1.1#post-matrix-push-v1-notify) -use std::time::SystemTime; - use js_int::UInt; use ruma_api::ruma_api; -use ruma_common::push::{PusherData, Tweak}; +use ruma_common::{ + push::{PusherData, Tweak}, + SecondsSinceUnixEpoch, +}; use ruma_events::EventType; use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId}; use ruma_serde::{Outgoing, StringEnum}; @@ -192,11 +193,8 @@ pub struct Device { pub pushkey: String, /// The unix timestamp (in seconds) when the pushkey was last updated. - #[serde( - with = "ruma_serde::time::opt_s_since_unix_epoch", - skip_serializing_if = "Option::is_none" - )] - pub pushkey_ts: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub pushkey_ts: Option, /// A dictionary of additional pusher-specific data. For 'http' pushers, /// this is the data dictionary passed in at pusher creation minus the `url` @@ -292,9 +290,8 @@ mod tweak_serde { #[cfg(test)] mod tests { - use std::time::{Duration, SystemTime}; - use js_int::uint; + use ruma_common::SecondsSinceUnixEpoch; use ruma_events::EventType; use ruma_identifiers::{event_id, room_alias_id, room_id, user_id}; use serde_json::{ @@ -339,7 +336,7 @@ mod tests { let count = NotificationCounts { unread: uint!(2), ..NotificationCounts::default() }; let device = Device { - pushkey_ts: Some(SystemTime::UNIX_EPOCH + Duration::from_secs(123)), + pushkey_ts: Some(SecondsSinceUnixEpoch(uint!(123))), tweaks: vec![ Tweak::Highlight(true), Tweak::Sound("silence".into()), diff --git a/crates/ruma-state-res/Cargo.toml b/crates/ruma-state-res/Cargo.toml index be963537..a9c12c8b 100644 --- a/crates/ruma-state-res/Cargo.toml +++ b/crates/ruma-state-res/Cargo.toml @@ -18,6 +18,7 @@ unstable-pre-spec = ["ruma-events/unstable-pre-spec"] itertools = "0.10.0" js_int = "0.2.0" maplit = "1.0.2" +ruma-common = { version = "0.5.0", path = "../ruma-common" } ruma-events = { version = "=0.22.0-alpha.3", path = "../ruma-events" } ruma-identifiers = { version = "0.19.0", path = "../ruma-identifiers" } ruma-serde = { version = "0.3.1", path = "../ruma-serde" } diff --git a/crates/ruma-state-res/benches/state_res_bench.rs b/crates/ruma-state-res/benches/state_res_bench.rs index 8ea8c93e..68355925 100644 --- a/crates/ruma-state-res/benches/state_res_bench.rs +++ b/crates/ruma-state-res/benches/state_res_bench.rs @@ -6,15 +6,15 @@ // `cargo bench --bench -- --save-baseline `. use std::{ collections::{BTreeMap, BTreeSet}, - convert::TryFrom, + convert::{TryFrom, TryInto}, sync::Arc, - time::{Duration, UNIX_EPOCH}, }; use criterion::{criterion_group, criterion_main, Criterion}; use event::StateEvent; use js_int::uint; use maplit::btreemap; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ pdu::{EventHash, Pdu, RoomV3Pdu}, room::{ @@ -40,7 +40,7 @@ fn lexico_topo_sort(c: &mut Criterion) { }; b.iter(|| { let _ = StateResolution::lexicographical_topological_sort(&graph, |id| { - (0, UNIX_EPOCH, id.clone()) + (0, MilliSecondsSinceUnixEpoch(uint!(0)), id.clone()) }); }) }); @@ -376,7 +376,7 @@ where rest: Pdu::RoomV3Pdu(RoomV3Pdu { room_id: room_id(), sender, - origin_server_ts: UNIX_EPOCH + Duration::from_secs(ts), + origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()), state_key, kind: ev_type, content, @@ -522,9 +522,10 @@ fn BAN_STATE_SET() -> BTreeMap> { } pub mod event { - use std::{collections::BTreeMap, time::SystemTime}; + use std::collections::BTreeMap; use js_int::UInt; + use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ pdu::{EventHash, Pdu}, room::member::MembershipState, @@ -559,7 +560,7 @@ pub mod event { self.content() } - fn origin_server_ts(&self) -> SystemTime { + fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch { *self.origin_server_ts() } @@ -656,7 +657,7 @@ pub mod event { } } - pub fn origin_server_ts(&self) -> &SystemTime { + pub fn origin_server_ts(&self) -> &MilliSecondsSinceUnixEpoch { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts, Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts, diff --git a/crates/ruma-state-res/src/lib.rs b/crates/ruma-state-res/src/lib.rs index 9e5d4956..0e5d13f2 100644 --- a/crates/ruma-state-res/src/lib.rs +++ b/crates/ruma-state-res/src/lib.rs @@ -2,10 +2,10 @@ use std::{ cmp::Reverse, collections::{BTreeMap, BTreeSet, BinaryHeap}, sync::Arc, - time::SystemTime, }; use maplit::btreeset; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ room::{ member::{MemberEventContent, MembershipState}, @@ -288,7 +288,7 @@ impl StateResolution { key_fn: F, ) -> Vec where - F: Fn(&EventId) -> (i64, SystemTime, EventId), + F: Fn(&EventId) -> (i64, MilliSecondsSinceUnixEpoch, EventId), { info!("starting lexicographical topological sort"); // NOTE: an event that has no incoming edges happened most recently, diff --git a/crates/ruma-state-res/src/state_event.rs b/crates/ruma-state-res/src/state_event.rs index 765a55cd..be229d72 100644 --- a/crates/ruma-state-res/src/state_event.rs +++ b/crates/ruma-state-res/src/state_event.rs @@ -1,6 +1,7 @@ -use std::{collections::BTreeMap, time::SystemTime}; +use std::collections::BTreeMap; use js_int::UInt; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{pdu::EventHash, EventType}; use ruma_identifiers::{EventId, RoomId, ServerName, ServerSigningKeyId, UserId}; use serde_json::value::Value as JsonValue; @@ -17,7 +18,7 @@ pub trait Event { fn sender(&self) -> &UserId; /// The time of creation on the originating server. - fn origin_server_ts(&self) -> SystemTime; + fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch; /// The kind of event. fn kind(&self) -> EventType; diff --git a/crates/ruma-state-res/tests/state_res.rs b/crates/ruma-state-res/tests/state_res.rs index 89332422..afe20df0 100644 --- a/crates/ruma-state-res/tests/state_res.rs +++ b/crates/ruma-state-res/tests/state_res.rs @@ -1,6 +1,8 @@ -use std::{sync::Arc, time::UNIX_EPOCH}; +use std::sync::Arc; +use js_int::uint; use maplit::btreemap; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{room::join_rules::JoinRule, EventType}; use ruma_identifiers::{EventId, RoomVersionId}; use ruma_state_res::{EventMap, StateMap, StateResolution}; @@ -276,8 +278,9 @@ fn test_lexicographical_sort() { event_id("p") => vec![event_id("o")], }; - let res = - StateResolution::lexicographical_topological_sort(&graph, |id| (0, UNIX_EPOCH, id.clone())); + let res = StateResolution::lexicographical_topological_sort(&graph, |id| { + (0, MilliSecondsSinceUnixEpoch(uint!(0)), id.clone()) + }); assert_eq!( vec!["o", "l", "n", "m", "p"], diff --git a/crates/ruma-state-res/tests/utils.rs b/crates/ruma-state-res/tests/utils.rs index e7868ea3..fb863280 100644 --- a/crates/ruma-state-res/tests/utils.rs +++ b/crates/ruma-state-res/tests/utils.rs @@ -2,13 +2,13 @@ use std::{ collections::{BTreeMap, BTreeSet}, - convert::TryFrom, + convert::{TryFrom, TryInto}, sync::{Arc, Once}, - time::{Duration, UNIX_EPOCH}, }; use js_int::uint; use maplit::btreemap; +use ruma_common::MilliSecondsSinceUnixEpoch; use ruma_events::{ pdu::{EventHash, Pdu, RoomV3Pdu}, room::{ @@ -77,9 +77,9 @@ pub fn do_check( // resolve the current state and add it to the state_at_event map then continue // on in "time" - for node in - StateResolution::lexicographical_topological_sort(&graph, |id| (0, UNIX_EPOCH, id.clone())) - { + for node in StateResolution::lexicographical_topological_sort(&graph, |id| { + (0, MilliSecondsSinceUnixEpoch(uint!(0)), id.clone()) + }) { let fake_event = fake_event_map.get(&node).unwrap(); let event_id = fake_event.event_id().clone(); @@ -340,7 +340,7 @@ pub fn to_init_pdu_event( rest: Pdu::RoomV3Pdu(RoomV3Pdu { room_id: room_id(), sender, - origin_server_ts: UNIX_EPOCH + Duration::from_secs(ts), + origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()), state_key, kind: ev_type, content, @@ -385,7 +385,7 @@ where rest: Pdu::RoomV3Pdu(RoomV3Pdu { room_id: room_id(), sender, - origin_server_ts: UNIX_EPOCH + Duration::from_secs(ts), + origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()), state_key, kind: ev_type, content, @@ -497,10 +497,11 @@ pub fn INITIAL_EDGES() -> Vec { } pub mod event { - use std::{collections::BTreeMap, time::SystemTime}; + use std::collections::BTreeMap; use js_int::UInt; use ruma_events::{ + exports::ruma_common::MilliSecondsSinceUnixEpoch, pdu::{EventHash, Pdu}, room::member::{MemberEventContent, MembershipState}, EventType, @@ -532,7 +533,7 @@ pub mod event { fn content(&self) -> serde_json::Value { self.content() } - fn origin_server_ts(&self) -> SystemTime { + fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch { *self.origin_server_ts() } @@ -623,7 +624,7 @@ pub mod event { Pdu::RoomV3Pdu(ev) => serde_json::from_value(ev.content.clone()), } } - pub fn origin_server_ts(&self) -> &SystemTime { + pub fn origin_server_ts(&self) -> &MilliSecondsSinceUnixEpoch { match &self.rest { Pdu::RoomV1Pdu(ev) => &ev.origin_server_ts, Pdu::RoomV3Pdu(ev) => &ev.origin_server_ts,