aboutsummaryrefslogtreecommitdiff
path: root/src/matrix.rs
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2021-10-07 21:57:52 +0200
committerDaniel Schadt <kingdread@gmx.de>2021-10-07 21:57:52 +0200
commit85ecbb03e299e99dbb2bc7864136a40add71df72 (patch)
tree4aa76ad71f8854d6fb6d6b8e1baefa584ab6a993 /src/matrix.rs
parent4918d362600da542131b6e7cf8df4c2015f0cb6e (diff)
downloadezau-85ecbb03e299e99dbb2bc7864136a40add71df72.tar.gz
ezau-85ecbb03e299e99dbb2bc7864136a40add71df72.tar.bz2
ezau-85ecbb03e299e99dbb2bc7864136a40add71df72.zip
Update dependencies
The biggest adaption was the update to matrix-sdk 0.4, as some types got shuffled around and the API is now imported from ruma. The relates_to.is_none() check seems to no longer work, as Relation::_Custom is used instead, so instead we now explicitely check if the message is neither a reply nor a replacement (on a related note, it is unknown to me whether we need to find the first original message or if we could also set a replaces on the last message in the chain). serenity, tokio and reqwest needed no API updates on the other hand.
Diffstat (limited to 'src/matrix.rs')
-rw-r--r--src/matrix.rs52
1 files changed, 27 insertions, 25 deletions
diff --git a/src/matrix.rs b/src/matrix.rs
index 5243db4..ce31fe3 100644
--- a/src/matrix.rs
+++ b/src/matrix.rs
@@ -10,18 +10,21 @@ use evtclib::Log;
use log::{debug, info};
use tokio::runtime::Runtime;
-use matrix_sdk::{
- api::r0::message::get_message_events,
- events::room::message::{MessageEventContent, Relation, TextMessageEventContent},
- events::room::relationships::Replacement,
+use reqwest::Url;
+
+use ruma::{
+ api::client::r0::message::get_message_events,
+ events::room::message::{MessageEventContent, MessageType, Relation, Replacement},
events::{AnyMessageEvent, AnyMessageEventContent, AnyRoomEvent},
identifiers::{EventId, RoomId, UserId},
- Client, UInt,
+ MilliSecondsSinceUnixEpoch, UInt,
};
+use matrix_sdk::Client;
+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MatrixUser {
- pub homeserver: String,
+ pub homeserver: Url,
pub username: String,
pub password: String,
pub device_id: Option<String>,
@@ -52,11 +55,11 @@ const MESSAGE_CHUNK_COUNT: u16 = 3;
/// This function blocks until all API calls have been made, that is until the message has reached
/// the homeserver.
pub fn post_link(user: MatrixUser, room_id: &str, log: &Log, link: &str) -> Result<()> {
- let mut rt = Runtime::new()?;
+ let rt = Runtime::new()?;
let room_id = RoomId::try_from(room_id)?;
rt.block_on(async {
- let client = Client::new(&user.homeserver as &str)?;
+ let client = Client::new(user.homeserver)?;
let my_data = client
.login(
&user.username,
@@ -96,7 +99,10 @@ async fn find_message(
room_id: &RoomId,
) -> Result<Option<(EventId, String)>> {
let limit = UInt::try_from(MESSAGE_CHUNK_SIZE).unwrap();
- let time_limit = SystemTime::now() - Duration::from_secs(MAX_HOURS * 60 * 60);
+ let time_limit = MilliSecondsSinceUnixEpoch::from_system_time(
+ SystemTime::now() - Duration::from_secs(MAX_HOURS * 60 * 60),
+ )
+ .expect("Our time limit is before the epoch");
let mut continue_from = String::new();
for chunk_nr in 0..MESSAGE_CHUNK_COUNT {
@@ -109,14 +115,17 @@ async fn find_message(
let mut request = get_message_events::Request::backward(room_id, &continue_from);
request.limit = limit;
- let response = client.room_messages(request).await?;
+ let response = client.send(request, None).await?;
for raw_message in response.chunk {
if let Ok(AnyRoomEvent::Message(AnyMessageEvent::RoomMessage(msg))) =
raw_message.deserialize()
{
if &msg.sender == my_id && msg.origin_server_ts >= time_limit {
- if let MessageEventContent::Text(text) = msg.content {
- if text.relates_to.is_none() {
+ if let MessageType::Text(text) = msg.content.msgtype {
+ if !matches!(
+ msg.content.relates_to,
+ Some(Relation::Reply { .. } | Relation::Replacement(..))
+ ) {
return Ok(Some((msg.event_id, text.body)));
}
}
@@ -139,11 +148,10 @@ async fn post_new(client: &Client, room_id: &RoomId, log: &Log, link: &str) -> R
let body = logbag.render_plain();
let html = logbag.render_html();
- let text_message = TextMessageEventContent::html(body, html);
client
.room_send(
room_id,
- AnyMessageEventContent::RoomMessage(MessageEventContent::Text(text_message)),
+ AnyMessageEventContent::RoomMessage(MessageEventContent::text_html(body, html)),
None,
)
.await?;
@@ -160,19 +168,13 @@ async fn update_message(
new_text: &str,
new_html: &str,
) -> Result<()> {
- let mut message = TextMessageEventContent::html(new_text, new_html);
- message.new_content = Some(Box::new(MessageEventContent::Text(
- TextMessageEventContent::html(new_text, new_html),
+ let mut message = MessageEventContent::text_html(new_text, new_html);
+ message.relates_to = Some(Relation::Replacement(Replacement::new(
+ old_id.clone(),
+ Box::new(MessageEventContent::text_html(new_text, new_html)),
)));
- message.relates_to = Some(Relation::Replacement(Replacement {
- event_id: old_id.clone(),
- }));
client
- .room_send(
- room_id,
- AnyMessageEventContent::RoomMessage(MessageEventContent::Text(message)),
- None,
- )
+ .room_send(room_id, AnyMessageEventContent::RoomMessage(message), None)
.await?;
Ok(())
}