Skip to content

Commit

Permalink
chore: first event handler test in client API
Browse files Browse the repository at this point in the history
  • Loading branch information
avdb13 committed Feb 16, 2024
1 parent 63d8048 commit 7cdcb01
Show file tree
Hide file tree
Showing 16 changed files with 361 additions and 168 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ axum = { version = "0.7.4", features = ["tokio"] }
chrono = { version = "0.4.34", features = ["serde"] }
dotenv = "0.15.0"
http = "0.2.11"
mime = { version = "0.3.17", features = ["serde1"] }
mime = "0.3.17"
openssl = { version = "0.10.63", features = ["vendored"] }
openssl-sys = { version = "0.9.99", features = ["vendored"] }
reqwest = { version = "0.11.22", default-features = false, features = ["blocking", "json", "rustls", "multipart"] }
serde = "1.0.192"
serde_json = "1.0.108"
tokio = "1.34.0"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
tracing-subscriber = { version = "0.3.18", features = ["json"] }
uuid = { version = "1.6.1", features = ["v4"] }
url = "2.4.1"
4 changes: 2 additions & 2 deletions crates/core/src/room/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tracing::instrument;

use matrix::{
client::resources::room::{
CreateRoomBody, Room as MatrixRoom, RoomCreationContent, RoomPreset,
CreateRoomBody, RoomService as MatrixRoomService, RoomCreationContent, RoomPreset,
},
Client as MatrixAdminClient,
};
Expand Down Expand Up @@ -36,7 +36,7 @@ impl RoomService {
access_token: &Secret,
dto: CreateRoomDto,
) -> Result<Room> {
match MatrixRoom::create(
match MatrixRoomService::create(
&self.admin,
access_token.to_string(),
CreateRoomBody {
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ruma-macros = "0.12.0"
# Workspace Dependencies
anyhow = { workspace = true }
chrono = { workspace = true, features = ["serde"] }
mime = { workspace = true, features = ["serde1"] }
mime = { workspace = true }
reqwest = { workspace = true, features = ["json"] }
serde = { workspace = true }
tokio = { workspace = true }
Expand Down
56 changes: 44 additions & 12 deletions crates/matrix/src/client/resources/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,51 @@ use anyhow::Result;
use ruma_common::{serde::Raw, EventId, OwnedEventId, RoomId, TransactionId};

use ruma_events::{
relation::RelationType, AnyMessageLikeEvent, AnyStateEvent, AnyStateEventContent,
relation::RelationType, AnyStateEvent, AnyStateEventContent,
AnyTimelineEvent, MessageLikeEventContent, MessageLikeEventType, StateEventContent,
StateEventType,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tracing::instrument;

use crate::{admin::resources::room::Direction, filter::RoomEventFilter, Client};
use crate::{admin::resources::room::Direction, filter::RoomEventFilter, Client, error::MatrixError};

pub struct EventsService;

#[derive(Debug, Default, Clone, Serialize)]
pub struct GetMessagesQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub from: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub to: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u64>,
pub dir: Option<Direction>,
pub filter: Option<RoomEventFilter>,

pub dir: Direction,

#[serde(skip_serializing_if = "String::is_empty")]
pub filter: String,
}

#[derive(Debug, Default, Serialize)]
#[derive(Debug, Default, Clone, Serialize)]
pub struct GetRelationsQuery {
#[serde(skip_serializing_if = "Option::is_none")]
pub from: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub to: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u64>,
pub dir: Option<Direction>,

pub dir: Direction,
}

#[derive(Debug, Deserialize)]
pub struct GetMessagesResponse {
pub chunk: Vec<Raw<AnyMessageLikeEvent>>,
pub chunk: Vec<Raw<AnyTimelineEvent>>,
pub start: String,
pub end: String,
pub state: Option<Vec<Raw<AnyStateEvent>>>,
Expand Down Expand Up @@ -103,10 +117,10 @@ impl EventsService {
tmp.set_token(access_token)?;

let resp = tmp
.get(format!(
.get_query(format!(
"/_matrix/client/v3/rooms/{room_id}/messages",
room_id = room_id,
))
), &query)
.await?;

Ok(resp.json().await?)
Expand Down Expand Up @@ -217,7 +231,13 @@ impl EventsService {
)
.await?;

Ok(resp.json().await?)
if resp.status().is_success() {
return Ok(resp.json().await?);
}

let error = resp.json::<MatrixError>().await?;

Err(anyhow::anyhow!(error.error))
}

#[instrument(skip(client, access_token, body))]
Expand All @@ -243,7 +263,13 @@ impl EventsService {

let resp = tmp.put_json(path, &body).await?;

Ok(resp.json().await?)
if resp.status().is_success() {
return Ok(resp.json().await?);
}

let error = resp.json::<MatrixError>().await?;

Err(anyhow::anyhow!(error.error))
}

#[instrument(skip(client, access_token, body))]
Expand All @@ -270,6 +296,12 @@ impl EventsService {
)
.await?;

Ok(resp.json().await?)
if resp.status().is_success() {
return Ok(resp.json().await?);
}

let error = resp.json::<MatrixError>().await?;

Err(anyhow::anyhow!(error.error))
}
}
23 changes: 18 additions & 5 deletions crates/matrix/src/client/resources/mxc.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
use std::str::FromStr;

use anyhow::Result;
use mime::Mime;
use ruma_common::{MxcUri, OwnedMxcUri};
use serde::{Deserialize, Serialize};
use serde::{de, Deserialize, Deserializer, Serialize};
use tracing::instrument;

use chrono::{serde::ts_microseconds_option, DateTime, Utc};

use crate::error::MatrixError;

fn parse_mime_opt<'de, D>(d: D) -> Result<Option<Mime>, D::Error>
where
D: Deserializer<'de>,
{
Option::<&str>::deserialize(d)?
.map(<Mime as FromStr>::from_str)
.transpose()
.map_err(de::Error::custom)
}

#[derive(Debug, Serialize)]
pub struct GetPreviewUrlQuery {
pub url: url::Url,
Expand Down Expand Up @@ -38,7 +51,7 @@ pub struct GetPreviewUrlResponse {
#[serde(rename = "og:image:width")]
pub width: Option<u64>,

#[serde(rename = "og:image:type")]
#[serde(rename = "og:image:type", deserialize_with = "parse_mime_opt")]
pub kind: Option<mime::Mime>,

#[serde(rename = "og:title")]
Expand Down Expand Up @@ -112,8 +125,8 @@ impl MxcService {
Err(anyhow::anyhow!(error.inner.error))
}

/// Retrieve a URL to download content from the content repository, optionally replacing the
/// name of the file.
/// Retrieve a URL to download content from the content repository,
/// optionally replacing the name of the file.
///
/// Refer: https://spec.matrix.org/v1.9/client-server-api/#get_matrixmediav3downloadservernamemediaid
#[instrument(skip(client, access_token))]
Expand Down Expand Up @@ -144,7 +157,7 @@ impl MxcService {
Ok(base_url)
}

///
///
///
/// Refer: https://spec.matrix.org/v1.9/client-server-api/#get_matrixmediav3preview_url
#[instrument(skip(client, access_token))]
Expand Down
8 changes: 2 additions & 6 deletions crates/matrix/src/client/resources/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use ruma_events::{room::power_levels::RoomPowerLevelsEventContent, AnyInitialSta
use serde::{Deserialize, Serialize};
use tracing::instrument;

use crate::error::MatrixError;

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RoomPreset {
Expand Down Expand Up @@ -103,12 +105,6 @@ pub struct ForgetRoomResponse {}
#[derive(Debug, Deserialize)]
pub struct RoomKickOrBanResponse {}

#[derive(Debug, Deserialize)]
pub struct MatrixError {
pub errcode: String,
pub error: String,
}

pub struct RoomService;

impl RoomService {
Expand Down
33 changes: 0 additions & 33 deletions crates/matrix/src/event_filter.rs

This file was deleted.

Loading

0 comments on commit 7cdcb01

Please sign in to comment.