mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-19 06:57:36 +00:00
Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
634002060b | ||
|
|
2db4b3a007 | ||
|
|
78b42b2c87 | ||
|
|
8808342331 | ||
|
|
53c3d0484f | ||
|
|
eb3155eac3 | ||
|
|
a319421b98 | ||
|
|
d108849f1a | ||
|
|
db837e3709 |
No files matched your search
Generated
-2
@@ -1122,7 +1122,6 @@ dependencies = [
|
||||
"lru-cache",
|
||||
"nonzero_ext",
|
||||
"openidconnect",
|
||||
"opentelemetry",
|
||||
"p256 0.14.0",
|
||||
"rand 0.10.3",
|
||||
"regex",
|
||||
@@ -1131,7 +1130,6 @@ dependencies = [
|
||||
"resolvematrix",
|
||||
"ruma",
|
||||
"ruminuwuity",
|
||||
"rustls",
|
||||
"rustyline-async",
|
||||
"sd-notify",
|
||||
"serde",
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
Fix incoming federation events losing state changes when predecessors share the same state-before snapshot. Contributed by @eleboucher.
|
||||
@@ -0,0 +1 @@
|
||||
URL previews no longer completely fail if downloading auxiliary content (images/videos/audio) fails. Contributed by @nex.
|
||||
@@ -2,7 +2,7 @@ use std::time::Duration;
|
||||
|
||||
use axum::extract::State;
|
||||
use conduwuit::{
|
||||
Err, Result, err,
|
||||
Err, Result, err, info,
|
||||
utils::{self, content_disposition::make_content_disposition, math::ruma_from_usize},
|
||||
};
|
||||
use conduwuit_core::error;
|
||||
@@ -219,10 +219,15 @@ pub(crate) async fn get_media_preview_route(
|
||||
)));
|
||||
}
|
||||
|
||||
info!("Fetching a URL preview for {url} on behalf of {sender_user}");
|
||||
let preview = services
|
||||
.media
|
||||
.get_url_preview(&url)
|
||||
.await
|
||||
.inspect(|_| info!(user=%sender_user, %url, "Finished fetching a URL preview"))
|
||||
.inspect_err(
|
||||
|e| info!(user=%sender_user, %url, error=?e, "Failed to fetch a URL preview"),
|
||||
)
|
||||
.map_err(|error| {
|
||||
err!(Request(Unknown(
|
||||
debug_error!(%sender_user, %url, "Failed to fetch URL preview: {error}")
|
||||
|
||||
@@ -145,7 +145,3 @@ workspace = true
|
||||
|
||||
[package.metadata.cargo-shear]
|
||||
ignored = ["reqwest_0_12"]
|
||||
|
||||
[dev-dependencies]
|
||||
opentelemetry.workspace = true
|
||||
rustls = { workspace = true, features = ["ring"] }
|
||||
@@ -111,7 +111,7 @@ impl crate::Service for Service {
|
||||
.local_address(url_preview_bind_addr)
|
||||
.timeout(Duration::from_secs(config.url_preview_timeout))
|
||||
.redirect(redirect::Policy::limited(3))
|
||||
.user_agent(url_preview_user_agent)
|
||||
.user_agent(url_preview_user_agent.replace("<version>", conduwuit::version_ua()))
|
||||
.build()?,
|
||||
|
||||
external_resource: base(config, reqwest_dns_resolver.clone())?
|
||||
|
||||
@@ -248,6 +248,8 @@ impl Data {
|
||||
x => x,
|
||||
};*/
|
||||
|
||||
// TODO(nex): Oh my god this is awful when can we kill it
|
||||
|
||||
let title = match values
|
||||
.next()
|
||||
.and_then(|b| String::from_utf8(b.to_vec()).ok())
|
||||
@@ -346,6 +348,11 @@ impl Data {
|
||||
video_height,
|
||||
audio,
|
||||
audio_size,
|
||||
..Default::default() /* TODO(nex,silversys): This weird manual field
|
||||
* de/serialisation just needs replacing with JSON storage to
|
||||
* avoid this mess. This is consequently intentionally
|
||||
* dropping information added later (like image alt text).
|
||||
* I'll do it later. */
|
||||
})
|
||||
}
|
||||
}
|
||||
+231
-46
@@ -7,8 +7,9 @@
|
||||
|
||||
use std::time::SystemTime;
|
||||
|
||||
use conduwuit::{Err, Result, debug, err, utils::response::LimitReadExt};
|
||||
use conduwuit::{Err, Result, debug, err, utils::response::LimitReadExt, info};
|
||||
use ipaddress::IPAddress;
|
||||
use lettre::message::header::ContentType;
|
||||
use ruma::OwnedMxcUri;
|
||||
use serde::Serialize;
|
||||
use url::Url;
|
||||
@@ -16,20 +17,54 @@ use url::Url;
|
||||
use super::Service;
|
||||
use crate::media::mxc::Mxc;
|
||||
|
||||
#[derive(Serialize, Default)]
|
||||
#[derive(Serialize, Default, Debug)]
|
||||
pub struct UrlPreviewData {
|
||||
// From https://ogp.me/
|
||||
/// The title of your object as it should appear within the graph
|
||||
/// "The Rock".
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:title"))]
|
||||
pub title: Option<String>,
|
||||
|
||||
/// The type of your object, e.g., "video.movie".
|
||||
#[serde(skip_serializing_if = "String::is_empty", rename(serialize = "og:type"))]
|
||||
pub site_type: String,
|
||||
/// The canonical URL of your object that will be used as its permanent ID
|
||||
/// in the graph.
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:url"))]
|
||||
pub url: Option<String>,
|
||||
/// A one to two sentence description of your object.
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:description"))]
|
||||
pub description: Option<String>,
|
||||
/// The word that appears before this object's title in a sentence.
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:determiner"))]
|
||||
pub determiner: Option<String>,
|
||||
/// The locale these tags are marked up in
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:locale"))]
|
||||
pub locale: Option<String>,
|
||||
/// If your object is part of a larger web site, the name which should be
|
||||
/// displayed for the overall site.
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:site_name"))]
|
||||
pub site_name: Option<String>,
|
||||
|
||||
/// An image URL which should represent your object within the graph.
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image"))]
|
||||
pub image: Option<String>,
|
||||
/// A MIME type for this image.
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image:type"))]
|
||||
pub image_type: Option<ContentType>,
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "matrix:image:size"))]
|
||||
pub image_size: Option<usize>,
|
||||
/// The number of pixels wide.
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image:width"))]
|
||||
pub image_width: Option<u32>,
|
||||
/// The number of pixels high.
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image:height"))]
|
||||
pub image_height: Option<u32>,
|
||||
/// A description of what is in the image (not a caption).
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:image:alt"))]
|
||||
pub image_alt: Option<String>,
|
||||
|
||||
/// A URL to a video file that complements this object.
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video"))]
|
||||
pub video: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "matrix:video:size"))]
|
||||
@@ -38,6 +73,11 @@ pub struct UrlPreviewData {
|
||||
pub video_width: Option<u32>,
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video:height"))]
|
||||
pub video_height: Option<u32>,
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video:type"))]
|
||||
pub video_type: Option<ContentType>,
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:video:alt"))]
|
||||
pub video_alt: Option<String>,
|
||||
/// A URL to an audio file to accompany this object.
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:audio"))]
|
||||
pub audio: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none", rename(serialize = "matrix:audio:size"))]
|
||||
@@ -100,7 +140,9 @@ impl Service {
|
||||
}
|
||||
|
||||
let Some(content_type) = response.headers().get(reqwest::header::CONTENT_TYPE) else {
|
||||
return Err!(Request(Unknown("Unknown or invalid Content-Type header")));
|
||||
return Err!(Request(Unknown(
|
||||
"Response contains unknown, invalid, or missing Content-Type header"
|
||||
)));
|
||||
};
|
||||
|
||||
let content_type = content_type
|
||||
@@ -109,12 +151,10 @@ impl Service {
|
||||
|
||||
let data = match content_type {
|
||||
| html if html.starts_with("text/html") => self.download_html(url.as_str()).await?,
|
||||
| img if img.starts_with("image/") => self.download_image(url.as_str(), None).await?,
|
||||
| video if video.starts_with("video/") =>
|
||||
self.download_video(url.as_str(), None).await?,
|
||||
| audio if audio.starts_with("audio/") =>
|
||||
self.download_audio(url.as_str(), None).await?,
|
||||
| _ => return Err!(Request(Unknown("Unsupported Content-Type"))),
|
||||
| img if img.starts_with("image/") => self.download_image(url.as_str()).await?,
|
||||
| video if video.starts_with("video/") => self.download_video(url.as_str()).await?,
|
||||
| audio if audio.starts_with("audio/") => self.download_audio(url.as_str()).await?,
|
||||
| other => return Err!(Request(Unknown("Unsupported Content-Type: {other}"))),
|
||||
};
|
||||
|
||||
self.set_url_preview(url.as_str(), &data).await?;
|
||||
@@ -123,17 +163,13 @@ impl Service {
|
||||
}
|
||||
|
||||
#[cfg(feature = "url_preview")]
|
||||
pub async fn download_image(
|
||||
&self,
|
||||
url: &str,
|
||||
preview_data: Option<UrlPreviewData>,
|
||||
) -> Result<UrlPreviewData> {
|
||||
pub async fn download_image(&self, url: &str) -> Result<UrlPreviewData> {
|
||||
use conduwuit::utils::random_string;
|
||||
use image::ImageReader;
|
||||
|
||||
use crate::media::mxc::Mxc;
|
||||
|
||||
let mut preview_data = preview_data.unwrap_or_default();
|
||||
let mut preview_data = UrlPreviewData::default();
|
||||
|
||||
let response = self.services.client.url_preview.get(url).send().await?;
|
||||
if !response.status().is_success() {
|
||||
@@ -157,13 +193,12 @@ impl Service {
|
||||
.ok_or_else(|| err!(Request(Unknown("Downloaded file is not a supported image"))))?
|
||||
.to_mime_type();
|
||||
|
||||
if preview_data.image_height.is_none() || preview_data.image_width.is_none() {
|
||||
let (width, height) = reader.into_dimensions().map_err(|error| {
|
||||
err!(Request(Unknown("Failed to read preview image dimensions: {error}")))
|
||||
})?;
|
||||
preview_data.image_width.get_or_insert(width);
|
||||
preview_data.image_height.get_or_insert(height);
|
||||
}
|
||||
let (width, height) = reader.into_dimensions().map_err(|error| {
|
||||
err!(Request(Unknown("Failed to read preview image dimensions: {error}")))
|
||||
})?;
|
||||
preview_data.image_width.get_or_insert(width);
|
||||
preview_data.image_height.get_or_insert(height);
|
||||
preview_data.image_type = content_type.parse().ok();
|
||||
|
||||
let mxc = Mxc {
|
||||
server_name: self.services.globals.server_name(),
|
||||
@@ -179,12 +214,9 @@ impl Service {
|
||||
}
|
||||
|
||||
#[cfg(feature = "url_preview")]
|
||||
pub async fn download_video(
|
||||
&self,
|
||||
url: &str,
|
||||
preview_data: Option<UrlPreviewData>,
|
||||
) -> Result<UrlPreviewData> {
|
||||
let mut preview_data = preview_data.unwrap_or_default();
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub async fn download_video(&self, url: &str) -> Result<UrlPreviewData> {
|
||||
let mut preview_data = UrlPreviewData::default();
|
||||
|
||||
if self.services.globals.url_preview_allow_audio_video() {
|
||||
let (url, size) = self.download_media(url).await?;
|
||||
@@ -196,12 +228,9 @@ impl Service {
|
||||
}
|
||||
|
||||
#[cfg(feature = "url_preview")]
|
||||
pub async fn download_audio(
|
||||
&self,
|
||||
url: &str,
|
||||
preview_data: Option<UrlPreviewData>,
|
||||
) -> Result<UrlPreviewData> {
|
||||
let mut preview_data = preview_data.unwrap_or_default();
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub async fn download_audio(&self, url: &str) -> Result<UrlPreviewData> {
|
||||
let mut preview_data = UrlPreviewData::default();
|
||||
|
||||
if self.services.globals.url_preview_allow_audio_video() {
|
||||
let (url, size) = self.download_media(url).await?;
|
||||
@@ -212,6 +241,7 @@ impl Service {
|
||||
Ok(preview_data)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub async fn download_media(&self, url: &str) -> Result<(OwnedMxcUri, usize)> {
|
||||
use conduwuit::utils::random_string;
|
||||
use http::header::CONTENT_TYPE;
|
||||
@@ -269,6 +299,7 @@ impl Service {
|
||||
}
|
||||
|
||||
#[cfg(feature = "url_preview")]
|
||||
#[tracing::instrument(skip(self))]
|
||||
async fn download_html(&self, url: &str) -> Result<UrlPreviewData> {
|
||||
use webpage::HTML;
|
||||
|
||||
@@ -286,32 +317,75 @@ impl Service {
|
||||
.expect("u64 should fit in usize"),
|
||||
)
|
||||
.await?;
|
||||
let Ok(html) = HTML::from_string(body.clone(), Some(url.to_owned())) else {
|
||||
let Ok(html) = HTML::from_string(body.clone(), Some(url.to_owned()))
|
||||
.inspect_err(|e| debug!(error=?e, "Failed to parse HTML data"))
|
||||
else {
|
||||
return Err!(Request(Unknown("Failed to parse HTML")));
|
||||
};
|
||||
|
||||
let mut preview_data = UrlPreviewData::default();
|
||||
let mut preview_data = UrlPreviewData::from(&html.opengraph);
|
||||
debug!(parsed=?preview_data, deserialised=?html.opengraph, "Parsed opengraph data");
|
||||
|
||||
if let Some(obj) = html.opengraph.images.first() {
|
||||
preview_data = self.download_image(&obj.url, Some(preview_data)).await?;
|
||||
info!(asset_url=%obj.url, "Downloading preview image");
|
||||
if let Ok(image_data) = self.download_image(&obj.url).await.inspect_err(
|
||||
|e| info!(error=?e, asset_url=%obj.url, "Could not download URL preview image"),
|
||||
) {
|
||||
preview_data.image = image_data.image;
|
||||
preview_data.image_size = image_data.image_size;
|
||||
// Prioritise what the top-level site specified, falling back to
|
||||
// detected params.
|
||||
preview_data.image_width = preview_data.image_width.or(image_data.image_width);
|
||||
preview_data.image_height = preview_data.image_height.or(image_data.image_height);
|
||||
preview_data.image_type = preview_data.image_type.or(image_data.image_type);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(obj) = html.opengraph.videos.first() {
|
||||
preview_data = self.download_video(&obj.url, Some(preview_data)).await?;
|
||||
preview_data.video_width = obj.properties.get("width").and_then(|v| v.parse().ok());
|
||||
preview_data.video_height = obj.properties.get("height").and_then(|v| v.parse().ok());
|
||||
info!(asset_url=%obj.url, "Downloading preview video");
|
||||
if let Ok(video_data) = self.download_video(&obj.url).await.inspect_err(
|
||||
|e| info!(error=?e, asset_url=%obj.url, "Could not download URL preview video"),
|
||||
) {
|
||||
preview_data.video = video_data.video;
|
||||
preview_data.video_size = video_data.video_size;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(obj) = html.opengraph.audios.first() {
|
||||
preview_data = self.download_audio(&obj.url, Some(preview_data)).await?;
|
||||
info!(asset_url=%obj.url, "Downloading URL preview audio");
|
||||
if let Ok(audio_data) = self.download_audio(&obj.url).await.inspect_err(
|
||||
|e| info!(error=?e, asset_url=%obj.url, "Could not download URL preview audio"),
|
||||
) {
|
||||
preview_data.audio = audio_data.audio;
|
||||
preview_data.audio_size = audio_data.audio_size;
|
||||
}
|
||||
}
|
||||
|
||||
let props = html.opengraph.properties;
|
||||
// Provide defaults for some params
|
||||
preview_data.title = preview_data.title.or(html.title);
|
||||
preview_data.description = preview_data.description.or(html.description);
|
||||
preview_data.url = preview_data.url.or_else(|| Some(url.to_owned()));
|
||||
|
||||
/* use OpenGraph title/description, but fall back to HTML if not
|
||||
* available */
|
||||
preview_data.title = props.get("title").cloned().or(html.title);
|
||||
preview_data.description = props.get("description").cloned().or(html.description);
|
||||
// We remove og:image:*, og:video:*, and og:audio:* if their respective
|
||||
// prior-level properties are not present.
|
||||
// We do this here instead of in the From<> impl because we can't know
|
||||
// if we'll be able to fetch the media to convert it to an mxc that
|
||||
// point.
|
||||
if preview_data.image.is_none() {
|
||||
preview_data.image_type = None;
|
||||
preview_data.image_alt = None;
|
||||
preview_data.image_height = None;
|
||||
preview_data.image_width = None;
|
||||
}
|
||||
if preview_data.video.is_none() {
|
||||
preview_data.video_type = None;
|
||||
preview_data.video_alt = None;
|
||||
preview_data.video_height = None;
|
||||
preview_data.video_width = None;
|
||||
}
|
||||
if preview_data.audio.is_none() {
|
||||
preview_data.audio_size = None;
|
||||
}
|
||||
|
||||
Ok(preview_data)
|
||||
}
|
||||
@@ -443,3 +517,114 @@ impl Service {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&webpage::Opengraph> for UrlPreviewData {
|
||||
fn from(og: &webpage::Opengraph) -> Self {
|
||||
Self {
|
||||
title: og.properties.get("title").cloned(),
|
||||
description: og.properties.get("description").cloned(),
|
||||
site_type: og.og_type.clone(),
|
||||
url: og.properties.get("url").cloned(),
|
||||
determiner: og.properties.get("determiner").cloned(),
|
||||
locale: og.properties.get("locale").cloned(),
|
||||
site_name: og.properties.get("site_name").cloned(),
|
||||
image_type: og
|
||||
.images
|
||||
.first()
|
||||
.and_then(|img| img.properties.get("type").and_then(|t| t.parse().ok())),
|
||||
image_width: og
|
||||
.images
|
||||
.first()
|
||||
.and_then(|img| img.properties.get("width").and_then(|w| w.parse().ok())),
|
||||
image_height: og
|
||||
.images
|
||||
.first()
|
||||
.and_then(|img| img.properties.get("height").and_then(|h| h.parse().ok())),
|
||||
image_alt: og
|
||||
.images
|
||||
.first()
|
||||
.and_then(|img| img.properties.get("alt").cloned()),
|
||||
video_type: og
|
||||
.videos
|
||||
.first()
|
||||
.and_then(|vid| vid.properties.get("type"))
|
||||
.and_then(|t| t.parse().ok()),
|
||||
video_width: og
|
||||
.videos
|
||||
.first()
|
||||
.and_then(|vid| vid.properties.get("width"))
|
||||
.and_then(|w| w.parse().ok()),
|
||||
video_height: og
|
||||
.videos
|
||||
.first()
|
||||
.and_then(|vid| vid.properties.get("height"))
|
||||
.and_then(|h| h.parse().ok()),
|
||||
video_alt: og
|
||||
.videos
|
||||
.first()
|
||||
.and_then(|vid| vid.properties.get("alt").cloned()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use webpage::HTML;
|
||||
|
||||
use super::UrlPreviewData;
|
||||
|
||||
#[test]
|
||||
fn test_urlpreviewdata_parses_correctly() {
|
||||
let html = r#"
|
||||
<html>
|
||||
<head>
|
||||
<meta property="og:title" content="Test Title" />
|
||||
<meta property="og:description" content="Test Description" />
|
||||
<meta property="og:image" content="https://example.com/image.png" />
|
||||
<meta property="og:url" content="https://example.com/index.html" />
|
||||
<meta property="og:type" content="org.continuwuity.other" />
|
||||
<meta property="og:site_name" content="Example Site" />
|
||||
<meta property="og:image:alt" content="An example image" />
|
||||
<meta property="og:image:type" content="image/png" />
|
||||
<meta property="og:image:height" content="69" />
|
||||
<meta property="og:image:width" content="420" />
|
||||
<meta property="og:video" content="https://example.com/video.mp4" />
|
||||
<meta property="og:audio" content="https://example.com/audio.ogg" />
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
||||
"#;
|
||||
|
||||
let parsed_html =
|
||||
HTML::from_string(html.to_owned(), Some("https://example.com".to_owned()))
|
||||
.expect("Failed to parse HTML");
|
||||
|
||||
let data = UrlPreviewData::from(&parsed_html.opengraph);
|
||||
|
||||
assert_eq!(data.title, Some("Test Title".to_owned()));
|
||||
assert_eq!(data.description, Some("Test Description".to_owned()));
|
||||
assert_eq!(
|
||||
data.image, None,
|
||||
"UrlPreviewData should not inherit HTTP/S URL from URL preview"
|
||||
);
|
||||
assert_eq!(
|
||||
data.video, None,
|
||||
"UrlPreviewData should not inherit HTTP/S URL from URL preview"
|
||||
);
|
||||
assert_eq!(
|
||||
data.audio, None,
|
||||
"UrlPreviewData should not inherit HTTP/S URL from URL preview"
|
||||
);
|
||||
assert_eq!(data.url, Some("https://example.com/index.html".to_owned()));
|
||||
assert_eq!(data.site_type, "org.continuwuity.other".to_owned());
|
||||
assert_eq!(data.site_name, Some("Example Site".to_owned()));
|
||||
assert_eq!(data.image_alt, Some("An example image".to_owned()));
|
||||
assert_eq!(data.image_type, Some("image/png".parse().unwrap()));
|
||||
assert_eq!(data.image_height, Some(69));
|
||||
assert_eq!(data.image_width, Some(420));
|
||||
assert_eq!(data.image_size, None, "*_size must be None when pulling from opengraph");
|
||||
assert_eq!(data.video_size, None, "*_size must be None when pulling from opengraph");
|
||||
assert_eq!(data.audio_size, None, "*_size must be None when pulling from opengraph");
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,6 @@ use ruma::{OwnedEventId, RoomId, room_version_rules::RoomVersionRules};
|
||||
|
||||
use crate::rooms::short::ShortStateHash;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
impl super::Service {
|
||||
/// Resolves the state before the incoming event.
|
||||
///
|
||||
@@ -127,7 +124,7 @@ impl super::Service {
|
||||
.pdu_shortstatehash(prev_eventid)
|
||||
.map_ok(move |sstatehash| (sstatehash, prev_event))
|
||||
})
|
||||
.try_collect::<Vec<_>>()
|
||||
.try_collect::<HashMap<_, _>>()
|
||||
.inspect_err(|e| debug_error!("failed to calculate N-degree short state hashes: {e}"))
|
||||
.await
|
||||
else {
|
||||
|
||||
@@ -1,271 +0,0 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use conduwuit::{Config, PduEvent, Server, log::Log, metrics::Metrics};
|
||||
use futures::StreamExt;
|
||||
use ruma::{RoomId, RoomVersionId, events::StateEventType};
|
||||
use serde_json::json;
|
||||
|
||||
#[tokio::test]
|
||||
async fn admitted_joins_do_not_disappear_after_a_merge() {
|
||||
use conduwuit::matrix::PartialPdu;
|
||||
use ruma::{
|
||||
UserId,
|
||||
events::room::{
|
||||
join_rules::{JoinRule, RoomJoinRulesEventContent},
|
||||
member::{MembershipState, RoomMemberEventContent},
|
||||
},
|
||||
};
|
||||
|
||||
let (services, path) = test_services().await;
|
||||
let alice = UserId::parse("@alice:test.example").unwrap();
|
||||
let bob = UserId::parse("@bob:bob.example").unwrap();
|
||||
let charlie = UserId::parse("@charlie:charlie.example").unwrap();
|
||||
let dave = UserId::parse("@dave:dave.example").unwrap();
|
||||
let mut remote_keys = Vec::new();
|
||||
for user in [&bob, &charlie, &dave] {
|
||||
let key = ruma::signatures::Ed25519KeyPair::from_der(
|
||||
&ruma::signatures::Ed25519KeyPair::generate(),
|
||||
"test".into(),
|
||||
)
|
||||
.unwrap();
|
||||
cache_key(&services, user.server_name(), &key);
|
||||
remote_keys.push(key);
|
||||
}
|
||||
let bootstrap = services
|
||||
.rooms
|
||||
.state
|
||||
.mutex
|
||||
.lock("!bootstrap:test.example")
|
||||
.await;
|
||||
let create_id = services
|
||||
.rooms
|
||||
.timeline
|
||||
.build_and_append_pdu(
|
||||
PartialPdu {
|
||||
event_type: "m.room.create".into(),
|
||||
content: serde_json::value::to_raw_value(&json!({ "room_version": "12" }))
|
||||
.unwrap(),
|
||||
state_key: Some("".into()),
|
||||
..Default::default()
|
||||
},
|
||||
&alice,
|
||||
None,
|
||||
&bootstrap,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
drop(bootstrap);
|
||||
let room = RoomId::parse(create_id.as_str().replacen('$', "!", 1)).unwrap();
|
||||
let lock = services.rooms.state.mutex.lock(&room).await;
|
||||
services
|
||||
.rooms
|
||||
.timeline
|
||||
.build_and_append_pdu(
|
||||
PartialPdu::state(
|
||||
alice.to_string(),
|
||||
&RoomMemberEventContent::new(MembershipState::Join),
|
||||
),
|
||||
&alice,
|
||||
Some(&room),
|
||||
&lock,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let rules_id = services
|
||||
.rooms
|
||||
.timeline
|
||||
.build_and_append_pdu(
|
||||
PartialPdu::state("", &RoomJoinRulesEventContent::new(JoinRule::Public)),
|
||||
&alice,
|
||||
Some(&room),
|
||||
&lock,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
drop(lock);
|
||||
let rules = services.rooms.timeline.get_pdu(&rules_id).await.unwrap();
|
||||
let (dave_join, json) = signed_remote_event(
|
||||
json!({
|
||||
"room_id": room, "sender": dave, "type": "m.room.member", "state_key": dave,
|
||||
"content": { "membership": "join" }, "auth_events": [rules_id], "prev_events": [rules_id],
|
||||
"depth": rules.depth + ruma::uint!(1), "origin_server_ts": conduwuit::utils::millis_since_unix_epoch(),
|
||||
}),
|
||||
dave.server_name(),
|
||||
&remote_keys[2],
|
||||
);
|
||||
assert!(
|
||||
services
|
||||
.rooms
|
||||
.event_handler
|
||||
.handle_incoming_pdu(dave.server_name(), &room, &dave_join.event_id, json, false)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_some()
|
||||
);
|
||||
let mut joins = Vec::new();
|
||||
for (user, key) in [&bob, &charlie].into_iter().zip(&remote_keys) {
|
||||
let signed = signed_remote_event(
|
||||
json!({
|
||||
"room_id": room, "sender": user, "type": "m.room.member", "state_key": user,
|
||||
"content": { "membership": "join" }, "auth_events": [rules_id], "prev_events": [dave_join.event_id],
|
||||
"depth": dave_join.depth + ruma::uint!(1), "origin_server_ts": conduwuit::utils::millis_since_unix_epoch(),
|
||||
}),
|
||||
user.server_name(),
|
||||
key,
|
||||
);
|
||||
joins.push(signed);
|
||||
}
|
||||
for ((pdu, json), user) in joins.iter().zip([&bob, &charlie]) {
|
||||
let accepted = services
|
||||
.rooms
|
||||
.event_handler
|
||||
.handle_incoming_pdu(user.server_name(), &room, &pdu.event_id, json.clone(), false)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(accepted.is_some());
|
||||
}
|
||||
let bob_hash = services
|
||||
.rooms
|
||||
.state_accessor
|
||||
.pdu_shortstatehash(&joins[0].0.event_id)
|
||||
.await
|
||||
.unwrap();
|
||||
let charlie_hash = services
|
||||
.rooms
|
||||
.state_accessor
|
||||
.pdu_shortstatehash(&joins[1].0.event_id)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(bob_hash, charlie_hash);
|
||||
let (merge, json) = signed_remote_event(
|
||||
json!({
|
||||
"room_id": room, "sender": dave, "type": "m.room.message",
|
||||
"content": { "msgtype": "m.text", "body": "merge" },
|
||||
"auth_events": [dave_join.event_id],
|
||||
"prev_events": [joins[0].0.event_id, joins[1].0.event_id],
|
||||
"depth": joins[0].0.depth + ruma::uint!(1),
|
||||
"origin_server_ts": conduwuit::utils::millis_since_unix_epoch(),
|
||||
}),
|
||||
dave.server_name(),
|
||||
&remote_keys[2],
|
||||
);
|
||||
assert!(
|
||||
services
|
||||
.rooms
|
||||
.event_handler
|
||||
.handle_incoming_pdu(dave.server_name(), &room, &merge.event_id, json, false)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_some()
|
||||
);
|
||||
let mut results = Vec::new();
|
||||
for ((user, key), (join, _)) in [&bob, &charlie].into_iter().zip(&remote_keys).zip(&joins) {
|
||||
let membership = services
|
||||
.rooms
|
||||
.state_accessor
|
||||
.room_state_get_content::<RoomMemberEventContent>(
|
||||
&room,
|
||||
&StateEventType::RoomMember,
|
||||
user.as_str(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(membership.membership, MembershipState::Join);
|
||||
let prev_events = services
|
||||
.rooms
|
||||
.state
|
||||
.get_forward_extremities(&room)
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
let depth = services
|
||||
.rooms
|
||||
.timeline
|
||||
.get_pdu(&prev_events[0])
|
||||
.await
|
||||
.unwrap()
|
||||
.depth
|
||||
+ ruma::uint!(1);
|
||||
let (message, json) = signed_remote_event(
|
||||
json!({
|
||||
"room_id": room, "sender": user, "type": "m.room.message",
|
||||
"content": { "msgtype": "m.text", "body": format!("message from {user}") },
|
||||
"auth_events": [join.event_id], "prev_events": prev_events,
|
||||
"depth": depth, "origin_server_ts": conduwuit::utils::millis_since_unix_epoch(),
|
||||
}),
|
||||
user.server_name(),
|
||||
key,
|
||||
);
|
||||
let result = services
|
||||
.rooms
|
||||
.event_handler
|
||||
.handle_incoming_pdu(user.server_name(), &room, &message.event_id, json, false)
|
||||
.await;
|
||||
results.push(result.is_ok_and(|pdu| pdu.is_some()));
|
||||
}
|
||||
services.stop().await;
|
||||
drop(services);
|
||||
std::fs::remove_dir_all(path).unwrap();
|
||||
assert_eq!(results, [true, true], "both joined users must be able to send messages");
|
||||
}
|
||||
|
||||
async fn test_services() -> (Arc<crate::Services>, std::path::PathBuf) {
|
||||
let _ = rustls::crypto::ring::default_provider().install_default();
|
||||
let path = std::env::temp_dir()
|
||||
.join(format!("continuwuity-shared-state-{:016x}", rand::random::<u64>()));
|
||||
std::fs::create_dir(&path).unwrap();
|
||||
let config: Config = serde_json::from_value(json!({
|
||||
"server_name": "test.example",
|
||||
"database_path": path,
|
||||
"db_cache_capacity_mb": 16.0,
|
||||
"rocksdb_write_buffer_capacity_mb": 4.0,
|
||||
"allow_local_presence": false
|
||||
}))
|
||||
.unwrap();
|
||||
let runtime = tokio::runtime::Handle::current();
|
||||
let server = Arc::new(Server::new(
|
||||
config,
|
||||
Some(runtime.clone()),
|
||||
Log {
|
||||
reload: Default::default(),
|
||||
capture: Default::default(),
|
||||
},
|
||||
Metrics::new(Some(runtime), opentelemetry::global::meter("test"), None),
|
||||
));
|
||||
let services = crate::Services::build(server).await.unwrap();
|
||||
(services, path)
|
||||
}
|
||||
|
||||
fn cache_key(
|
||||
services: &crate::Services,
|
||||
server: &ruma::ServerName,
|
||||
key: &ruma::signatures::Ed25519KeyPair,
|
||||
) {
|
||||
let mut response = ruma::api::federation::discovery::ServerSigningKeys::new(
|
||||
server.to_owned(),
|
||||
crate::server_keys::in_one_week(),
|
||||
);
|
||||
let id: ruma::OwnedServerSigningKeyId =
|
||||
format!("ed25519:{}", key.version()).try_into().unwrap();
|
||||
response.verify_keys.insert(
|
||||
id.clone(),
|
||||
ruma::api::federation::discovery::VerifyKey::new(ruma::serde::Base64::new(
|
||||
key.public_key().to_vec(),
|
||||
)),
|
||||
);
|
||||
services.db["servernamekeyid_response"]
|
||||
.put((server.as_str(), id.as_str()), database::Json(&response));
|
||||
}
|
||||
|
||||
fn signed_remote_event(
|
||||
value: serde_json::Value,
|
||||
server: &ruma::ServerName,
|
||||
key: &ruma::signatures::Ed25519KeyPair,
|
||||
) -> (PduEvent, ruma::CanonicalJsonObject) {
|
||||
let rules = RoomVersionId::V12.rules().unwrap();
|
||||
let mut value = serde_json::from_value(value).unwrap();
|
||||
ruma::signatures::hash_and_sign_event(server.as_str(), key, &mut value, &rules.redaction)
|
||||
.unwrap();
|
||||
let id = conduwuit::matrix::event::gen_event_id(&value, &rules).unwrap();
|
||||
let pdu = PduEvent::from_id_val(&id, value.clone()).unwrap();
|
||||
(pdu, value)
|
||||
}
|
||||
Reference in new issue
Block a user