This commit is contained in:
Nikita Vilunov 2024-05-25 23:16:45 +02:00
parent ca908dea12
commit 8a8cb0da5c
6 changed files with 27 additions and 54 deletions

View File

@ -112,7 +112,7 @@ impl PlayerConnection {
}
#[tracing::instrument(skip(self), name = "PlayerConnection::get_room_message_history")]
pub async fn get_room_message_history(&self, room_id: RoomId) -> Result<(Vec<StoredMessage>)> {
pub async fn get_room_message_history(&self, room_id: RoomId) -> Result<Vec<StoredMessage>> {
let (promise, deferred) = oneshot();
let cmd = ClientCommand::GetRoomHistory { room_id, promise };
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
@ -525,7 +525,6 @@ impl Player {
let result = self.get_room_history(room_id).await;
let _ = promise.send(result);
}
_ => {}
}
}

View File

@ -3,7 +3,7 @@ use chrono::{DateTime, Utc};
use sqlx::FromRow;
use crate::repo::Storage;
use crate::room::{RoomId, StoredMessage, StoredUser};
use crate::room::{RoomId, StoredMessage};
#[derive(FromRow)]
pub struct StoredRoom {
@ -38,7 +38,6 @@ impl Storage {
messages.id as id,
content,
created_at,
users.id as author_id,
users.name as author_name
from
messages

View File

@ -286,30 +286,10 @@ pub struct RoomInfo {
pub topic: Str,
}
#[derive(Debug)]
pub struct StoredUser {
pub id: u32,
pub name: String,
}
#[derive(Debug)]
#[derive(Debug, FromRow)]
pub struct StoredMessage {
pub id: u32,
pub author: StoredUser,
pub author_name: String,
pub content: String,
pub created_at: DateTime<Utc>,
}
impl FromRow<'_, SqliteRow> for StoredMessage {
fn from_row(row: &SqliteRow) -> sqlx::Result<Self> {
Ok(Self {
id: row.try_get("id")?,
author: StoredUser {
id: row.try_get("author_id")?,
name: row.try_get("author_name")?,
},
content: row.try_get("content")?,
created_at: row.try_get("created_at")?,
})
}
}

View File

@ -2,7 +2,6 @@
use anyhow::Result;
use quick_xml::events::Event;
use serde::Serialize;
use lavina_core::room::RoomId;
use proto_xmpp::bind::{Jid, Name, Resource, Server};
@ -45,12 +44,13 @@ impl<'a> XmppConnection<'a> {
custom: vec![],
};
muc_presence.serialize(output);
subject.serialize(output);
let messages = self.retrieve_message_history(&name).await?;
for message in messages {
message.serialize(output)
}
// The subject is the last stanza sent during a MUC join process.
subject.serialize(output);
}
_ => {
// TODO other presence cases
@ -88,7 +88,7 @@ impl<'a> XmppConnection<'a> {
}
}
async fn retrieve_muc_presence(&mut self, name: &Name) -> Result<(Presence<XUser>)> {
async fn retrieve_muc_presence(&mut self, name: &Name) -> Result<Presence<XUser>> {
let a = self.user_handle.join_room(RoomId::try_from(name.0.clone())?).await?;
// TODO handle bans
let response = Presence {
@ -120,18 +120,11 @@ impl<'a> XmppConnection<'a> {
Ok(response)
}
/*
Retrieve a room's message history. The output can be serialized into a stream of XML stanzas.
Example of such a stanza:
<message from="duqedadi@conference.oflor.me/misha" xml:lang="en" to="misha@oflor.me/tux" type="groupchat" id="7ca7cb14-b2af-49c9-bd90-05dabb1113a5">
<delay xmlns="urn:xmpp:delay" stamp="2024-05-17T16:05:28.440337Z" from="duqedadi@conference.oflor.me"/>
<body></body>
</message>
*/
/// Retrieve a room's message history. The output can be serialized into a stream of XML stanzas.
///
/// Example in [XmppHistoryMessage]'s docs.
#[tracing::instrument(skip(self), name = "XmppConnection::retrieve_message_history")]
async fn retrieve_message_history(&self, room_name: &Name) -> Result<(Vec<XmppHistoryMessage>)> {
async fn retrieve_message_history(&self, room_name: &Name) -> Result<Vec<XmppHistoryMessage>> {
let room_id = RoomId::try_from(room_name.0.clone())?;
let history_messages = self.user_handle.get_room_message_history(room_id).await?;
let mut response = vec![];
@ -147,11 +140,11 @@ impl<'a> XmppConnection<'a> {
from: Jid {
name: Option::from(room_name.clone()),
server: Server(self.hostname_rooms.clone()),
resource: Option::from(Resource(history_message.author.name.clone().into())),
resource: Option::from(Resource(history_message.author_name.clone().into())),
},
delay: Delay {
from: Jid {
name: Option::from(Name(history_message.author.name.clone().into())),
name: Option::from(Name(history_message.author_name.clone().into())),
server: Server(self.hostname_rooms.clone()),
resource: None,
},
@ -161,7 +154,7 @@ impl<'a> XmppConnection<'a> {
});
tracing::info!(
"Retrieved message: {:?} {:?}",
history_message.author,
history_message.author_name,
history_message.content.clone()
);
}

View File

@ -15,7 +15,8 @@ pub mod streamerror;
pub mod tls;
pub mod xml;
pub mod testkit;
#[cfg(test)]
mod testkit;
// Implemented as a macro instead of a fn due to borrowck limitations
macro_rules! skip_text {

View File

@ -280,14 +280,15 @@ impl ToXml for Delay {
}
}
/*
Example of an XMPP message with a history stanza:
<message from="duqedadi@conference.oflor.me/misha" xml:lang="en" to="misha@oflor.me/tux" type="groupchat" id="7ca7cb14-b2af-49c9-bd90-05dabb1113a5">
<delay xmlns="urn:xmpp:delay" stamp="2024-05-17T16:05:28.440337Z" from="duqedadi@conference.oflor.me"/>
<body></body>
</message>
*/
/// Message-stanza of a historic message.
///
/// Example:
/// ```xml
/// <message from="duqedadi@conference.example.com/misha" xml:lang="en" to="misha@example.com/tux" type="groupchat" id="7ca7cb14-b2af-49c9-bd90-05dabb1113a5">
/// <delay xmlns="urn:xmpp:delay" stamp="2024-05-17T16:05:28.440337Z" from="duqedadi@conference.example.com"/>
/// <body></body>
/// </message>
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct XmppHistoryMessage {
pub id: String,
@ -420,7 +421,7 @@ mod test {
let history_message = XmppHistoryMessage {
id: "id".to_string(),
to: Jid {
name: Some(Name("sauer@oflor.me".into())),
name: Some(Name("sauer@example.com".into())),
server: Server("localhost".into()),
resource: Some(Resource("tester".into())),
},
@ -440,7 +441,7 @@ mod test {
body: "Hello World.".to_string(),
};
let mut events = vec![];
let expected = r#"<message id="id" to="sauer@oflor.me@localhost/tester" from="pepe@rooms.localhost/sauer" type="groupchat"><delay xmlns="urn:xmpp:delay" from="pepe@rooms.localhost/tester" stamp="2021-10-10T10:10:10Z"/><body>Hello World.</body></message>"#;
let expected = r#"<message id="id" to="sauer@example.com@localhost/tester" from="pepe@rooms.localhost/sauer" type="groupchat"><delay xmlns="urn:xmpp:delay" from="pepe@rooms.localhost/tester" stamp="2021-10-10T10:10:10Z"/><body>Hello World.</body></message>"#;
// Act
history_message.serialize(&mut events);