Use vectors and commands

This commit is contained in:
Mikhail 2024-05-23 14:35:52 +02:00
parent 6285a31c85
commit 09abdd5346
12 changed files with 174 additions and 46 deletions

View File

@ -18,7 +18,7 @@ use tracing::{Instrument, Span};
use crate::clustering::room::*; use crate::clustering::room::*;
use crate::prelude::*; use crate::prelude::*;
use crate::room::{RoomHandle, RoomId, RoomInfo}; use crate::room::{HistoryMessage, RoomHandle, RoomId, RoomInfo};
use crate::table::{AnonTable, Key as AnonKey}; use crate::table::{AnonTable, Key as AnonKey};
use crate::LavinaCore; use crate::LavinaCore;
@ -111,6 +111,14 @@ impl PlayerConnection {
Ok(deferred.await?) Ok(deferred.await?)
} }
#[tracing::instrument(skip(self), name = "PlayerConnection::get_room_message_history")]
pub async fn get_room_message_history(&self, room_id: RoomId) -> Result<(Vec<HistoryMessage>)> {
let (promise, deferred) = oneshot();
let cmd = ClientCommand::GetRoomHistory { room_id, promise };
self.player_handle.send(ActorCommand::ClientCommand(cmd, self.connection_id.clone())).await;
Ok(deferred.await?)
}
/// Handler in [Player::send_dialog_message]. /// Handler in [Player::send_dialog_message].
#[tracing::instrument(skip(self, body), name = "PlayerConnection::send_dialog_message")] #[tracing::instrument(skip(self, body), name = "PlayerConnection::send_dialog_message")]
pub async fn send_dialog_message(&self, recipient: PlayerId, body: Str) -> Result<()> { pub async fn send_dialog_message(&self, recipient: PlayerId, body: Str) -> Result<()> {
@ -212,6 +220,10 @@ pub enum ClientCommand {
recipient: PlayerId, recipient: PlayerId,
promise: Promise<GetInfoResult>, promise: Promise<GetInfoResult>,
}, },
GetRoomHistory {
room_id: RoomId,
promise: Promise<Vec<HistoryMessage>>,
},
} }
pub enum GetInfoResult { pub enum GetInfoResult {
@ -509,6 +521,11 @@ impl Player {
let result = self.check_user_existence(recipient).await; let result = self.check_user_existence(recipient).await;
let _ = promise.send(result); let _ = promise.send(result);
} }
ClientCommand::GetRoomHistory { room_id, promise } => {
let result = self.get_room_history(room_id).await;
let _ = promise.send(result);
}
_ => {}
} }
} }
@ -557,6 +574,23 @@ impl Player {
} }
} }
#[tracing::instrument(skip(self), name = "Player::retrieve_room_history")]
async fn get_room_history(&mut self, room_id: RoomId) -> Vec<HistoryMessage> {
let room = self.my_rooms.get(&room_id);
if let Some(room) = room {
match room {
RoomRef::Local(room) => room.get_message_history().await,
RoomRef::Remote { node_id } => {
todo!()
}
}
} else {
tracing::info!("Room with ID {room_id:?} not found");
// todo: return error
todo!()
}
}
#[tracing::instrument(skip(self), name = "Player::leave_room")] #[tracing::instrument(skip(self), name = "Player::leave_room")]
async fn leave_room(&mut self, connection_id: ConnectionId, room_id: RoomId) { async fn leave_room(&mut self, connection_id: ConnectionId, room_id: RoomId) {
let room = self.my_rooms.remove(&room_id); let room = self.my_rooms.remove(&room_id);
@ -593,7 +627,7 @@ impl Player {
body: Str, body: Str,
) -> SendMessageResult { ) -> SendMessageResult {
let Some(room) = self.my_rooms.get(&room_id) else { let Some(room) = self.my_rooms.get(&room_id) else {
tracing::info!("no room found"); tracing::info!("Room with ID {room_id:?} not found");
return SendMessageResult::NoSuchRoom; return SendMessageResult::NoSuchRoom;
}; };
let created_at = Utc::now(); let created_at = Utc::now();
@ -632,7 +666,7 @@ impl Player {
#[tracing::instrument(skip(self, new_topic), name = "Player::change_room_topic")] #[tracing::instrument(skip(self, new_topic), name = "Player::change_room_topic")]
async fn change_room_topic(&mut self, connection_id: ConnectionId, room_id: RoomId, new_topic: Str) { async fn change_room_topic(&mut self, connection_id: ConnectionId, room_id: RoomId, new_topic: Str) {
let Some(room) = self.my_rooms.get(&room_id) else { let Some(room) = self.my_rooms.get(&room_id) else {
tracing::info!("no room found"); tracing::info!("Room with ID {room_id:?} not found");
return; return;
}; };
match room { match room {

View File

@ -174,6 +174,6 @@ impl Storage {
.fetch_all(&mut *executor) .fetch_all(&mut *executor)
.await?; .await?;
res.into_iter().map(|(room_id,)| RoomId::from(room_id)).collect() res.into_iter().map(|(room_id,)| RoomId::try_from(room_id)).collect()
} }
} }

View File

@ -96,6 +96,27 @@ impl Storage {
Ok(res.map(|(id,)| id)) Ok(res.map(|(id,)| id))
} }
#[tracing::instrument(skip(self), name = "Storage::retrieve_user_by_id")]
pub async fn retrieve_user_by_id(&self, name: &str) -> Result<Option<u32>> {
let mut executor = self.conn.lock().await;
let res: Option<(u32,)> = sqlx::query_as(
"
select
u.id,
u.name
from
users u
where
u.id = ?;
",
)
.bind(name)
.fetch_optional(&mut *executor)
.await?;
Ok(res.map(|(id,)| id))
}
#[tracing::instrument(skip(self), name = "Storage::create_or_retrieve_user_id_by_name")] #[tracing::instrument(skip(self), name = "Storage::create_or_retrieve_user_id_by_name")]
pub async fn create_or_retrieve_user_id_by_name(&self, name: &str) -> Result<u32> { pub async fn create_or_retrieve_user_id_by_name(&self, name: &str) -> Result<u32> {
let mut executor = self.conn.lock().await; let mut executor = self.conn.lock().await;

View File

@ -16,7 +16,7 @@ use crate::Services;
pub struct RoomId(Str); pub struct RoomId(Str);
impl RoomId { impl RoomId {
pub fn from(str: impl Into<Str>) -> Result<RoomId> { pub fn try_from(str: impl Into<Str>) -> Result<RoomId> {
let bytes = str.into(); let bytes = str.into();
if bytes.len() > 32 { if bytes.len() > 32 {
return Err(anyhow::Error::msg("Room name cannot be longer than 32 symbols")); return Err(anyhow::Error::msg("Room name cannot be longer than 32 symbols"));
@ -158,6 +158,29 @@ impl RoomHandle {
lock.broadcast_update(update, player_id).await; lock.broadcast_update(update, player_id).await;
} }
pub async fn get_message_history(&self) -> Vec<HistoryMessage> {
return vec![
HistoryMessage {
id: "kek0".to_string(),
body: "Willkommen in Brem'".to_string(),
created_at: Utc::now(),
author: User {
id: 0,
name: "sauer".to_string(),
},
},
HistoryMessage {
id: "kek1".to_string(),
body: "Willkommen in Hamburg".to_string(),
created_at: Utc::now(),
author: User {
id: 0,
name: "sauer".to_string(),
},
},
];
}
#[tracing::instrument(skip(self), name = "RoomHandle::unsubscribe")] #[tracing::instrument(skip(self), name = "RoomHandle::unsubscribe")]
pub async fn unsubscribe(&self, player_id: &PlayerId) { pub async fn unsubscribe(&self, player_id: &PlayerId) {
let mut lock = self.0.write().await; let mut lock = self.0.write().await;
@ -279,3 +302,15 @@ pub struct RoomInfo {
pub members: Vec<PlayerId>, pub members: Vec<PlayerId>,
pub topic: Str, pub topic: Str,
} }
pub struct User {
pub id: u32,
pub name: String,
}
pub struct HistoryMessage {
pub id: String,
pub author: User,
pub body: String,
pub created_at: DateTime<Utc>,
}

View File

@ -720,7 +720,7 @@ async fn handle_incoming_message(
} }
ClientMessage::PrivateMessage { recipient, body } => match recipient { ClientMessage::PrivateMessage { recipient, body } => match recipient {
Recipient::Chan(Chan::Global(chan)) => { Recipient::Chan(Chan::Global(chan)) => {
let room_id = RoomId::from(chan)?; let room_id = RoomId::try_from(chan)?;
user_handle.send_message(room_id, body).await?; user_handle.send_message(room_id, body).await?;
} }
Recipient::Nick(nick) => { Recipient::Nick(nick) => {
@ -732,7 +732,7 @@ async fn handle_incoming_message(
ClientMessage::Topic { chan, topic } => { ClientMessage::Topic { chan, topic } => {
match chan { match chan {
Chan::Global(chan) => { Chan::Global(chan) => {
let room_id = RoomId::from(chan)?; let room_id = RoomId::try_from(chan)?;
user_handle.change_topic(room_id.clone(), topic.clone()).await?; user_handle.change_topic(room_id.clone(), topic.clone()).await?;
ServerMessage { ServerMessage {
tags: vec![], tags: vec![],
@ -774,7 +774,7 @@ async fn handle_incoming_message(
writer.flush().await?; writer.flush().await?;
} }
Recipient::Chan(Chan::Global(chan)) => { Recipient::Chan(Chan::Global(chan)) => {
let room = core.get_room(&RoomId::from(chan.clone())?).await; let room = core.get_room(&RoomId::try_from(chan.clone())?).await;
if let Some(room) = room { if let Some(room) = room {
let room_info = room.get_room_info().await; let room_info = room.get_room_info().await;
for member in room_info.members { for member in room_info.members {
@ -893,7 +893,7 @@ async fn handle_join(
) -> Result<()> { ) -> Result<()> {
match chan { match chan {
Chan::Global(chan_name) => { Chan::Global(chan_name) => {
let room_id = RoomId::from(chan_name.clone())?; let room_id = RoomId::try_from(chan_name.clone())?;
if let JoinResult::Success(room_info) = user_handle.join_room(room_id).await? { if let JoinResult::Success(room_info) = user_handle.join_room(room_id).await? {
produce_on_join_cmd_messages(&config, &user, chan, &room_info, writer).await?; produce_on_join_cmd_messages(&config, &user, chan, &room_info, writer).await?;
} else { } else {
@ -924,7 +924,7 @@ async fn handle_part(
writer: &mut (impl AsyncWrite + Unpin), writer: &mut (impl AsyncWrite + Unpin),
) -> Result<()> { ) -> Result<()> {
if let Chan::Global(chan_name) = chan { if let Chan::Global(chan_name) = chan {
let room_id = RoomId::from(chan_name.clone())?; let room_id = RoomId::try_from(chan_name.clone())?;
user_handle.leave_room(room_id).await?; user_handle.leave_room(room_id).await?;
ServerMessage { ServerMessage {
tags: vec![], tags: vec![],

View File

@ -619,14 +619,14 @@ async fn server_time_capability() -> Result<()> {
server.core.create_player(&PlayerId::from("some_guy")?).await?; server.core.create_player(&PlayerId::from("some_guy")?).await?;
let mut conn = server.core.connect_to_player(&PlayerId::from("some_guy").unwrap()).await; let mut conn = server.core.connect_to_player(&PlayerId::from("some_guy").unwrap()).await;
let res = conn.join_room(RoomId::from("test").unwrap()).await?; let res = conn.join_room(RoomId::try_from("test").unwrap()).await?;
let JoinResult::Success(_) = res else { let JoinResult::Success(_) = res else {
panic!("Failed to join room"); panic!("Failed to join room");
}; };
s.expect(":some_guy JOIN #test").await?; s.expect(":some_guy JOIN #test").await?;
let SendMessageResult::Success(res) = conn.send_message(RoomId::from("test").unwrap(), "Hello".into()).await? let SendMessageResult::Success(res) = conn.send_message(RoomId::try_from("test").unwrap(), "Hello".into()).await?
else { else {
panic!("Failed to send message"); panic!("Failed to send message");
}; };

View File

@ -163,7 +163,7 @@ impl<'a> XmppConnection<'a> {
server, server,
resource: None, resource: None,
}) if server.0 == self.hostname_rooms => { }) if server.0 == self.hostname_rooms => {
let room_id = RoomId::from(room_name.0.clone()).unwrap(); let room_id = RoomId::try_from(room_name.0.clone()).unwrap();
let Some(_) = self.core.get_room(&room_id).await else { let Some(_) = self.core.get_room(&room_id).await else {
// TODO should return item-not-found // TODO should return item-not-found
// example: // example:

View File

@ -20,7 +20,7 @@ impl<'a> XmppConnection<'a> {
}) = m.to }) = m.to
{ {
if server.0.as_ref() == &*self.hostname_rooms && m.r#type == MessageType::Groupchat { if server.0.as_ref() == &*self.hostname_rooms && m.r#type == MessageType::Groupchat {
self.user_handle.send_message(RoomId::from(name.0.clone())?, m.body.clone().into()).await?; self.user_handle.send_message(RoomId::try_from(name.0.clone())?, m.body.clone().into()).await?;
Message::<()> { Message::<()> {
to: Some(Jid { to: Some(Jid {
name: Some(self.user.xmpp_name.clone()), name: Some(self.user.xmpp_name.clone()),

View File

@ -2,11 +2,12 @@
use anyhow::Result; use anyhow::Result;
use quick_xml::events::Event; use quick_xml::events::Event;
use serde::Serialize;
use lavina_core::room::RoomId; use lavina_core::room::RoomId;
use proto_xmpp::bind::{Jid, Name, Resource, Server}; use proto_xmpp::bind::{Jid, Name, Resource, Server};
use proto_xmpp::client::Presence; use proto_xmpp::client::Presence;
use proto_xmpp::muc::{Delay, HistoryMessage, XUser}; use proto_xmpp::muc::{Delay, XUser, XmppHistoryMessage};
use proto_xmpp::xml::{Ignore, ToXml}; use proto_xmpp::xml::{Ignore, ToXml};
use crate::XmppConnection; use crate::XmppConnection;
@ -26,8 +27,11 @@ impl<'a> XmppConnection<'a> {
let muc_join_response = self.muc_presence(&name).await?; let muc_join_response = self.muc_presence(&name).await?;
muc_join_response.serialize(output); muc_join_response.serialize(output);
let history_on_join_response = self.send_history_on_join().await?; let history_on_join_response = self.send_history_on_join(&name).await?;
history_on_join_response.serialize(output)
for message in history_on_join_response {
message.serialize(output)
}
} }
_ => { _ => {
// TODO other presence cases // TODO other presence cases
@ -64,7 +68,7 @@ impl<'a> XmppConnection<'a> {
} }
async fn muc_presence(&mut self, name: &Name) -> Result<(Presence<XUser>)> { async fn muc_presence(&mut self, name: &Name) -> Result<(Presence<XUser>)> {
let a = self.user_handle.join_room(RoomId::from(name.0.clone())?).await?; let a = self.user_handle.join_room(RoomId::try_from(name.0.clone())?).await?;
// TODO handle bans // TODO handle bans
let response = Presence { let response = Presence {
to: Some(Jid { to: Some(Jid {
@ -82,29 +86,63 @@ impl<'a> XmppConnection<'a> {
}; };
Ok(response) Ok(response)
} }
async fn send_history_on_join(&self) -> Result<(HistoryMessage)> { async fn send_history_on_join(&self, room_name: &Name) -> Result<(Vec<XmppHistoryMessage>)> {
Ok(HistoryMessage { let room_id = RoomId::try_from(room_name.0.clone())?;
id: "kek".to_string(), let history_messages = self.user_handle.get_room_message_history(room_id).await?;
to: Jid { let mut response = vec![];
name: Some(Name("sauer@oflor.me".into())),
server: Server("localhost".into()), for history_message in history_messages.into_iter() {
resource: Some(Resource("tester".into())), let author_name = Option::from(Name(history_message.author.name.into()));
}, tracing::info!("author_name: {:?}", author_name);
from: Jid {
name: Some(Name("pepe".into())), let author_jid = Jid {
server: Server("rooms.localhost".into()), name: author_name.clone(),
resource: Some(Resource("sauer".into())), server: Server(self.hostname_rooms.clone()),
}, resource: None, // Option::from(Resource(room_name.0.clone())),
delay: Delay::new( };
Jid {
name: Some(Name("pepe".into())), response.push(XmppHistoryMessage {
server: Server("rooms.localhost".into()), id: history_message.id,
resource: Some(Resource("tester".into())), // sauer@localhost/sauer
to: Jid {
name: Option::from(Name(self.user.xmpp_muc_name.0.clone().into())), // Option::from(Name("chelik".into())),
server: Server(self.hostname.clone()),
resource: None, // Option::from(Resource("sauer".into())), // Option::from(Resource(room_name.0.clone())),
}, },
"2021-10-10T10:10:10Z".to_string(), from: Jid {
), name: Option::from(room_name.clone()), // Option::from(Name("chelik".into())),
body: "Vasya Pupkin says hello.".to_string(), server: Server(self.hostname_rooms.clone()),
}) resource: Option::from(Resource("sauer".into())), // Option::from(Resource(room_name.0.clone())),
},
delay: Delay::new(author_jid.clone(), history_message.created_at.to_rfc3339()),
body: history_message.body,
});
}
return Ok(response);
// Ok(HistoryMessage {
// id: "kek".to_string(),
// to: Jid {
// name: Some(Name("sauer@oflor.me".into())),
// server: Server("localhost".into()),
// resource: Some(Resource("tester".into())),
// },
// from: Jid {
// name: Some(Name("pepe".into())),
// server: Server("rooms.localhost".into()),
// resource: Some(Resource("sauer".into())),
// },
// delay: Delay::new(
// Jid {
// name: Some(Name("pepe".into())),
// server: Server("rooms.localhost".into()),
// resource: Some(Resource("tester".into())),
// },
// "2021-10-10T10:10:10Z".to_string(),
// ),
// body: ,
// })
} }
} }

View File

@ -175,7 +175,7 @@ impl Delay {
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub struct HistoryMessage { pub struct XmppHistoryMessage {
pub id: String, pub id: String,
pub to: Jid, pub to: Jid,
pub from: Jid, pub from: Jid,
@ -183,7 +183,7 @@ pub struct HistoryMessage {
pub body: String, pub body: String,
} }
impl ToXml for HistoryMessage { impl ToXml for XmppHistoryMessage {
fn serialize(&self, events: &mut Vec<Event<'static>>) { fn serialize(&self, events: &mut Vec<Event<'static>>) {
let mut tag = BytesStart::new("message"); let mut tag = BytesStart::new("message");
tag.push_attribute(Attribute { tag.push_attribute(Attribute {

View File

@ -164,7 +164,7 @@ async fn endpoint_send_room_message(
let Ok(req) = serde_json::from_slice::<rooms::SendMessageReq>(&str[..]) else { let Ok(req) = serde_json::from_slice::<rooms::SendMessageReq>(&str[..]) else {
return Ok(malformed_request()); return Ok(malformed_request());
}; };
let Ok(room_id) = RoomId::from(req.room_id) else { let Ok(room_id) = RoomId::try_from(req.room_id) else {
return Ok(room_not_found()); return Ok(room_not_found());
}; };
let Ok(player_id) = PlayerId::from(req.author_id) else { let Ok(player_id) = PlayerId::from(req.author_id) else {
@ -187,7 +187,7 @@ async fn endpoint_set_room_topic(
let Ok(req) = serde_json::from_slice::<rooms::SetTopicReq>(&str[..]) else { let Ok(req) = serde_json::from_slice::<rooms::SetTopicReq>(&str[..]) else {
return Ok(malformed_request()); return Ok(malformed_request());
}; };
let Ok(room_id) = RoomId::from(req.room_id) else { let Ok(room_id) = RoomId::try_from(req.room_id) else {
return Ok(room_not_found()); return Ok(room_not_found());
}; };
let Ok(player_id) = PlayerId::from(req.author_id) else { let Ok(player_id) = PlayerId::from(req.author_id) else {

View File

@ -29,7 +29,7 @@ async fn endpoint_cluster_join_room(
return Ok(malformed_request()); return Ok(malformed_request());
}; };
tracing::info!("Incoming request: {:?}", &req); tracing::info!("Incoming request: {:?}", &req);
let Ok(room_id) = RoomId::from(req.room_id) else { let Ok(room_id) = RoomId::try_from(req.room_id) else {
dbg!(&req.room_id); dbg!(&req.room_id);
return Ok(room_not_found()); return Ok(room_not_found());
}; };
@ -55,7 +55,7 @@ async fn endpoint_cluster_add_message(
dbg!(&req.created_at); dbg!(&req.created_at);
return Ok(malformed_request()); return Ok(malformed_request());
}; };
let Ok(room_id) = RoomId::from(req.room_id) else { let Ok(room_id) = RoomId::try_from(req.room_id) else {
dbg!(&req.room_id); dbg!(&req.room_id);
return Ok(room_not_found()); return Ok(room_not_found());
}; };